preload-webpack-plugin icon indicating copy to clipboard operation
preload-webpack-plugin copied to clipboard

Filtering chunks

Open mooyoul opened this issue 8 years ago • 8 comments

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)

mooyoul avatar Feb 01 '17 22:02 mooyoul

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

addyosmani avatar Feb 02 '17 01:02 addyosmani

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">
    <%  }); %>

mooyoul avatar Feb 02 '17 11:02 mooyoul

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

jantimon avatar Feb 02 '17 20:02 jantimon

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

ingro avatar Feb 06 '17 11:02 ingro

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 avatar Feb 08 '17 16:02 jackfranklin

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

jackfranklin avatar Feb 08 '17 16:02 jackfranklin

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.

limichange avatar Feb 13 '17 05:02 limichange

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.

jackfranklin avatar Feb 13 '17 09:02 jackfranklin