GoogleChromeLabs / preload-webpack-plugin

Please use https://github.com/vuejs/preload-webpack-plugin instead.
Apache License 2.0
2.15k stars 144 forks source link

Filtering chunks #6

Open mooyoul opened 7 years ago

mooyoul commented 7 years ago

It would be nice if preload-webpack-plugin supports filtering chunks (only include some chunks/exclude some chunks.) like html-webpack-plugin's one.

It will be useful on some use-cases.

  1. Some chunks (e.g. Admin page) may contain some sensitive information. so user wants to exclude it from preload targets. (blacklisting)
  2. Some chunks don't need to be preloaded. so user wants to include some chunks only (whitelisting)
addyosmani commented 7 years ago

I think that's a valid ask. You're proposing using a similar API to how html-webpack-plugin tackles this I assume? :)

mooyoul commented 7 years ago

Yeah that's correct. I just want to include/exclude some named chunks on preload-webpack-plugin like html-webpack-plugin :)

FYI, previously i was used below snippet (quick & dirty solution) for implementing preload chunks with filtering on my html-webpack-plugin html template file:

    <%
      Object.keys(webpack.assetsByChunkName)
        .filter((k) => k && k.indexOf('CHUNK_NAME_TO_BE_IGNORED') === -1)
        .reduce((collection, k) => collection.concat(webpack.assetsByChunkName[k]), [])
        .filter((filename) => filename && filename.slice(-3) === '.js')
        .forEach((filename) => {
    %>
    <link rel="preload" href="<%= webpackConfig.output.publicPath %><%= filename %>" as="script">
    <%  }); %>
jantimon commented 7 years ago

The best way would be to use the chunks from htmlPluginData which gives you the setting for each generated page as you can use multiple html-webpack-plugin instances https://github.com/GoogleChrome/preload-webpack-plugin/blob/7760ef791605c05cd306c0f63ce9b6d6318a2c7c/index.js#L36

ingro commented 7 years ago

This option would be very useful! I wanted to exclude sourcemap's chunks but it seems not possible at the moment...

jackfranklin commented 7 years ago

This would be useful for avoiding sourcemaps - maybe you could pass in a blacklist of regexes/globs to match against? It could be populated with ['*.map'] for a start to stop the plugin including sourcemaps.

jackfranklin commented 7 years ago

@addyosmani I'm happy to attempt a PR if you're happy with that approach, or to talk through it.

limichange commented 7 years ago

This would be useful for avoiding sourcemaps - maybe you could pass in a blacklist of regexes/globs to match against? It could be populated with ['*.map'] for a start to stop the plugin including sourcemaps.

This is useful. We don't want to preload sourcemap.

jackfranklin commented 7 years ago

I opened a PR with the blacklist feature: https://github.com/GoogleChrome/preload-webpack-plugin/pull/16

If anyone needs it for now you can always depend on my version of this repo.