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

Feature request: support fixed filename #19

Closed rottmann closed 4 years ago

rottmann commented 5 years ago

compilerOptions.output.chunkFilename is [name].[hash].js and i need to provide the generated name.hash.worker.js to sw-precache-webpack-plugin

I cant use the generated .worker.js filename, the [name] could not be used from /sw-precache-webpack-plugin (ok thats their problem) and the hash seems not to match the generated file.

An option to set a fixed target filename in webpack config would be perfect:

      new WorkerPlugin({
        filename: 'worker.js'
      }),
developit commented 5 years ago

@rottmann perhaps just an option to override chunkFilename as used by WorkerPlugin? It could be a template, but in your case you'd just not use any fields.

thijstriemstra commented 5 years ago

I have no idea why I keep getting this error but it seems related to this ticket..

Uncaught DOMException: Failed to construct 'Worker': Script at 'localhost:8080/dist/0.worker.js' cannot be accessed from origin 'http://localhost:8080'.

That worker filename sometimes changes to 1.worker.js, sometimes 0.worker.js, meh.

The webpack config :

entry: {
        wavesurfer: path.join(rootDir, 'src', 'wavesurfer.js')
    },
    output: {
        path: path.join(rootDir, 'dist'),
        filename: '[name].js',
        library: 'WaveSurfer'
    },
plugins: [new WorkerPlugin()]
filipesilva commented 5 years ago

Also saw the name ping pong between 0 and 1 in https://github.com/angular/angular-cli/pull/13700#discussion_r269972055. Was very problematic for our tests.

FTWinston commented 4 years ago

Related to this, I'd like to be able to stop the generated file names when I do an import() statement in my worker having .worker.js appended at the end.

(The import statement is in a function I serialsed and passed to the worker, so the actual generated file doesn't end in .chunk.worker.js, but just in .chunk.js.)

Using templates for generated filenames as suggested would work for me as long as it also applied to dynamic imports. I'd raised another issue relating to htis on #43, but have since worked around it.

jackdbd commented 4 years ago

With no fixed filename for the web worker bundle, is there a workaround to inject a <link> with modulepreload in the template <head>?

If I had a single web worker I guess I could get the filename from the manifest.json generated by webpack-manifest-plugin. But what if I had more than one worker?

developit commented 4 years ago

@jackdbd No browsers support preloading Web Workers. There are some options for preloading Module Workers that are only supported in Chrome, you can read about them here.

developit commented 4 years ago

I just implemented { filename, chunkFilename } options in #82.

developit commented 4 years ago

The filename option is now released in 5.0.0.