GoogleChromeLabs / worker-plugin

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

Help: Integration of WebpackHTMLPlugin #40

Closed matsp closed 4 years ago

matsp commented 5 years ago

I am actually using a worker to outsource work of a component that is loaded at start time of the page. My "index" chunk is loading the worker chunk on page load - the will slow down the app.

From reading the "plugins" section of the docs I don't get how to integrate my worker chunk as asset in the HTMLPlugin.

My simple HTMLPlugin config (for the hole app):

new HtmlWebpackPlugin({
      template: path.resolve(paths.srcPath, 'index.html'),
      minify: {
        collapseWhitespace: true,
        removeComments: true,
        processScripts: ['text/javascript']
      },
      filename: 'index.html'
})

Also using additonal plugins for the production build:

new ScriptExtHtmlWebpackPlugin({
      // inline: ['runtime'],
      async: /\.js$/,
      // defer: /\.wc.js$/,
      prefetch: {
        test: /\.js$/,
        chunks: 'async'
      },
      preload: {
        test: /\.js$/,
        chunks: 'initial'
      }
})

Does anyone have an idea how to get the same behavior for worker chunks generated by the worker-plugin?

Thanks in advance!

developit commented 5 years ago

Hi there - I might be missing something, but since it's not possible to preload workers, your HTML can't do anything with the knowledge of the worker script.

developit commented 4 years ago

Just a note, I did some research on this and the only working solution is to use <link rel=prefetch> to preload the Worker script. It won't preparse and has a lower priority than preload requests, but it does preload the Worker. Also make sure to set crossorigin to the appropriate value.

developit commented 4 years ago

Closing since this is a platform limitation with a solution. Happy to discuss on Twitter though!