andywer / threads-plugin

📦 Makes your threads.js code build with webpack out of the box.
Apache License 2.0
28 stars 6 forks source link

Use threads-plugin with SSR #8

Open carlosvillu opened 5 years ago

carlosvillu commented 5 years ago

Hi,

I am getting an error when I try to use the loader in SSR.

Captura de pantalla 2019-10-11 a las 11 34 29

Basically the loader is creating a new file from my worker called 0.worker.js and putting it in the folder where the server code has been compiled.

Captura de pantalla 2019-10-11 a las 11 39 30

However, when I try to run this code,

  const greeter = await spawn(new Worker('./worker'))
  const {response} = await greeter({name: 'Carlos'})
  console.log(response)
  await Thread.terminate(greeter)

I get the error.

Any suggestions ?!

Thx!

Carlos

andywer commented 5 years ago

Hey Carlos!

Can you share the actual code with the spawn() invocation?

carlosvillu commented 5 years ago

Hi, sure,

worker.js:

import {expose} from 'threads/worker'

expose(async username => {
  return {response: `Hello ${username.name}`}
})

Webpack.server.config.js

const webpackConfig = {
  context: path.resolve(process.cwd(), 'src'),
  mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
  resolve: {
    alias: parseAlias(config.alias),
    extensions: ['*', '.js', '.jsx', '.json']
  },
  entry: './server.js',
  target: 'node',
  output: {
    path: path.resolve(process.cwd(), 'build'),
    chunkFilename: '[name].[chunkhash:8].js',
    filename: '[name].[chunkhash:8].js'
  },
  optimization: {
    nodeEnv: false
  },
  externals: [webpackNodeExternals()]
  plugins: [
    new webpack.DefinePlugin({'global.GENTLY': false}),
    new ThreadsPlugin()
  ],
  resolveLoader: {
    alias: {
      'externals-manifest-loader': require.resolve(
        './loaders/ExternalsManifestLoader'
      )
    }
  },
  module: {
    rules: cleanList([
      babelRules,
      {
        // ignore css/scss require/imports files in the server
        test: /\.s?css$/,
        use: ['null-loader']
      },
      when(config['externals-manifest'], () =>
        manifestLoaderRules(config['externals-manifest'])
      )
    ])
  }
}

Thx,

andywer commented 5 years ago

Thanks for sharing, but you literally sent me everything that could be interesting except the bit that I asked for 🙈😅

Can you share the actual code with the spawn() invocation?

I really need to see the master thread code with the spawn() call to rule out something's going wrong there 😉

carlosvillu commented 5 years ago

Maybe I don't get it, but I think what you are asking it is in the first comment:

  const greeter = await spawn(new Worker('./worker'))
  const {response} = await greeter({name: 'Carlos'})
  console.log(response)
  await Thread.terminate(greeter)

Or do you mean the complete function?

Captura de pantalla 2019-10-14 a las 11 14 16
andywer commented 5 years ago

Ahhh, sorry man – my bad!

Was a bit in a hurry when I responded to the first comment and didn't notice the snippet at the bottom...

So, I don't see any particular reason for this error on first glance. Damn webpack config – it's probably one single option set somewhere.

Will take a deeper look into it later.

carlosvillu commented 5 years ago

Don't worry!!

Thx :)

carlosvillu commented 5 years ago

@andywer could you take a look at this issue ?!

andywer commented 5 years ago

Hey @carlosvillu. Sorry, I've been swamped with work!

This is a tricky one. I am not even sure that anything's wrong with the threads-plugin as there is in fact an integration test in the threads.js repo testing node-targeted webpack builds using the threads-plugin.

It could as well be some weird issue with your webpack config, but I cannot spot anything obviously wrong there either.

The only obvious hint seems to be that it tries to resolve the file created by webpack (0.worker.js) not with the node.js require(), but with webpack's require(). This is of course wrong. The question "why?" remains...

It is as if webpack resolves the module path that it already resolved.

The threads-plugin is a fork of the worker-plugin. There doesn't seem to be a known issue like this in the upstream repository either... 😕