From cf895ed7253192611361d9b5cb5bbd881c45ba18 Mon Sep 17 00:00:00 2001 From: toxic-johann Date: Wed, 7 Feb 2018 09:02:52 +0800 Subject: [PATCH] [update] add excludeHtmlNames to solve problem mentioned in https://github.com/GoogleChromeLabs/preload-webpack-plugin/issues/48 --- README.md | 25 +++++++++++ index.js | 7 +++- package-lock.json | 2 +- test/spec.js | 105 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9bb1415..b304c80 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,31 @@ new PreloadWebpackPlugin({ }) ``` +## Filtering Html + +In some case, you may don't want to preload resource on some file. But using `fileBlacklist` is werid, because you may want to inlcude this chunk on another file. So you can use `excludeHtmlNames` to tell preload plugin to ignore this file. + +If you have multiple html like index.html and example.html, you can exclude index.html like this. + +```javascript +plugins: [ + new HtmlWebpackPlugin({ + filename: 'index.html', + template: 'src/index.html', + chunks: ['main'] + }), + new HtmlWebpackPlugin({ + filename: 'example.html', + template: 'src/example.html', + chunks: ['exampleEntry'] + }), + // I want this to affect only index.html + new PreloadWebpackPlugin({ + excludeHtmlNames: ['index.html'], + }) +] +``` + Resource Hints --------------------- diff --git a/index.js b/index.js index 0277c71..dc68193 100644 --- a/index.js +++ b/index.js @@ -49,7 +49,8 @@ const doesChunkBelongToHTML = (chunk, roots, visitedChunks) => { const defaultOptions = { rel: 'preload', include: 'asyncChunks', - fileBlacklist: [/\.map/] + fileBlacklist: [/\.map/], + excludeHtmlNames: [], }; class PreloadPlugin { @@ -61,6 +62,10 @@ class PreloadPlugin { const options = this.options; compiler.plugin('compilation', compilation => { compilation.plugin('html-webpack-plugin-before-html-processing', (htmlPluginData, cb) => { + if (this.options.excludeHtmlNames.indexOf(htmlPluginData.plugin.options.filename) > -1) { + cb(null, htmlPluginData); + return; + } let filesToInclude = ''; let extractedChunks = []; // 'asyncChunks' are chunks intended for lazy/async loading usually generated as diff --git a/package-lock.json b/package-lock.json index e0612ea..2eb6ae6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "preload-webpack-plugin", - "version": "2.1.0", + "version": "2.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/test/spec.js b/test/spec.js index acfcc45..a0c3e74 100644 --- a/test/spec.js +++ b/test/spec.js @@ -367,3 +367,108 @@ describe('filtering unwanted files', function() { compiler.outputFileSystem = new MemoryFileSystem(); }); }); + +describe('multiple html', function() { + it('each one only include their own chunk', function(done) { + const compiler = webpack({ + entry: { + js: path.join(__dirname, 'fixtures', 'file.js'), + moduleA: path.join(__dirname, 'fixtures', 'module-a.js') + }, + output: { + path: OUTPUT_DIR, + filename: '[name].js', + chunkFilename: 'chunk.[chunkhash].js', + publicPath: '/', + }, + devtool: 'cheap-source-map', + plugins: [ + new HtmlWebpackPlugin({ + filename: 'index.html', + chunks: ['js'], + }), + new HtmlWebpackPlugin({ + filename: 'home.html', + chunks: ['moduleA'], + }), + new PreloadPlugin() + ] + }, function(err, result) { + expect(err).toBeFalsy(); + expect(JSON.stringify(result.compilation.errors)).toBe('[]'); + const html = result.compilation.assets['index.html'].source(); + const homeHtml = result.compilation.assets['home.html'].source(); + expect(html).toContain('