GoogleChromeLabs / worker-plugin

👩‍🏭 Adds native Web Worker bundling support to Webpack.
https://npm.im/worker-plugin
Apache License 2.0
1.91k stars 79 forks source link

Multiple workers conflict #79

Closed andraaspar closed 4 years ago

andraaspar commented 4 years ago

The following warning is shown by webpack:

WARNING in Conflict: Multiple assets emit different content to the same filename 0.foo.worker.js

When using two workers like this:

new Worker('./w1.js', { type: 'module' })
new Worker('./w2.js', { type: 'module' })

And webpack.config.js like this:

const path = require('path')
const WorkerPlugin = require('worker-plugin')

module.exports = {
  entry: './src/index.js',
  mode: 'production', // This causes it
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'foo.js', // This causes it
    // chunkFilename: 'chunk.[id].[chunkhash].js', // This fixes it
  },
  plugins: [new WorkerPlugin()],
}

Workaround:

Specify output.chunkFilename with [chunkhash]. (See above.)

developit commented 4 years ago

FWIW you can also specify output.filename to use [name]:

const path = require('path')
const WorkerPlugin = require('worker-plugin')

module.exports = {
  entry: {
    foo: './src/index.js',
  },
  mode: 'production',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: '[name].js',
  },
  plugins: [new WorkerPlugin()],
}
developit commented 4 years ago

For those still running into this who don't want to change output.filename, you can use the new filename option in 5.0.0:

new WorkerPlugin({
    filename: '[name].js'
});

This bypasses inference from output.filename and avoids overwriting same-named workers.