Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit e29cdd4

Browse files
authored
Merge pull request #54 from toxic-johann/master
Add excludeHtmlNames to solve problem mentioned in #48
2 parents 8c2b3bf + cf895ed commit e29cdd4

File tree

4 files changed

+137
-2
lines changed

4 files changed

+137
-2
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,31 @@ new PreloadWebpackPlugin({
180180
})
181181
```
182182

183+
## Filtering Html
184+
185+
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.
186+
187+
If you have multiple html like index.html and example.html, you can exclude index.html like this.
188+
189+
```javascript
190+
plugins: [
191+
new HtmlWebpackPlugin({
192+
filename: 'index.html',
193+
template: 'src/index.html',
194+
chunks: ['main']
195+
}),
196+
new HtmlWebpackPlugin({
197+
filename: 'example.html',
198+
template: 'src/example.html',
199+
chunks: ['exampleEntry']
200+
}),
201+
// I want this to affect only index.html
202+
new PreloadWebpackPlugin({
203+
excludeHtmlNames: ['index.html'],
204+
})
205+
]
206+
```
207+
183208
Resource Hints
184209
---------------------
185210

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ const doesChunkBelongToHTML = (chunk, roots, visitedChunks) => {
4949
const defaultOptions = {
5050
rel: 'preload',
5151
include: 'asyncChunks',
52-
fileBlacklist: [/\.map/]
52+
fileBlacklist: [/\.map/],
53+
excludeHtmlNames: [],
5354
};
5455

5556
class PreloadPlugin {
@@ -61,6 +62,10 @@ class PreloadPlugin {
6162
const options = this.options;
6263
compiler.plugin('compilation', compilation => {
6364
compilation.plugin('html-webpack-plugin-before-html-processing', (htmlPluginData, cb) => {
65+
if (this.options.excludeHtmlNames.indexOf(htmlPluginData.plugin.options.filename) > -1) {
66+
cb(null, htmlPluginData);
67+
return;
68+
}
6469
let filesToInclude = '';
6570
let extractedChunks = [];
6671
// 'asyncChunks' are chunks intended for lazy/async loading usually generated as

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/spec.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,108 @@ describe('filtering unwanted files', function() {
367367
compiler.outputFileSystem = new MemoryFileSystem();
368368
});
369369
});
370+
371+
describe('multiple html', function() {
372+
it('each one only include their own chunk', function(done) {
373+
const compiler = webpack({
374+
entry: {
375+
js: path.join(__dirname, 'fixtures', 'file.js'),
376+
moduleA: path.join(__dirname, 'fixtures', 'module-a.js')
377+
},
378+
output: {
379+
path: OUTPUT_DIR,
380+
filename: '[name].js',
381+
chunkFilename: 'chunk.[chunkhash].js',
382+
publicPath: '/',
383+
},
384+
devtool: 'cheap-source-map',
385+
plugins: [
386+
new HtmlWebpackPlugin({
387+
filename: 'index.html',
388+
chunks: ['js'],
389+
}),
390+
new HtmlWebpackPlugin({
391+
filename: 'home.html',
392+
chunks: ['moduleA'],
393+
}),
394+
new PreloadPlugin()
395+
]
396+
}, function(err, result) {
397+
expect(err).toBeFalsy();
398+
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
399+
const html = result.compilation.assets['index.html'].source();
400+
const homeHtml = result.compilation.assets['home.html'].source();
401+
expect(html).toContain('<link rel="preload" as="script" href="/chunk.');
402+
expect(homeHtml).not.toContain('<link rel="preload" as="script" href="/chunk.');
403+
done();
404+
});
405+
compiler.outputFileSystem = new MemoryFileSystem();
406+
});
407+
408+
it('exclude by html filename', function(done) {
409+
const compiler = webpack({
410+
entry: {
411+
js: path.join(__dirname, 'fixtures', 'file.js')
412+
},
413+
output: {
414+
path: OUTPUT_DIR,
415+
filename: '[name].js',
416+
chunkFilename: 'chunk.[chunkhash].js',
417+
publicPath: '/',
418+
},
419+
devtool: 'cheap-source-map',
420+
plugins: [
421+
new HtmlWebpackPlugin({
422+
filename: 'index.html',
423+
chunks: ['js'],
424+
}),
425+
new HtmlWebpackPlugin({
426+
filename: 'home.html',
427+
chunks: ['js'],
428+
}),
429+
new PreloadPlugin({
430+
excludeHtmlNames: ['index.html'],
431+
})
432+
]
433+
}, function(err, result) {
434+
expect(err).toBeFalsy();
435+
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
436+
const html = result.compilation.assets['index.html'].source();
437+
const homeHtml = result.compilation.assets['home.html'].source();
438+
expect(html).not.toContain('<link rel="preload" as="script" href="/chunk.');
439+
expect(homeHtml).toContain('<link rel="preload" as="script" href="/chunk.');
440+
done();
441+
});
442+
compiler.outputFileSystem = new MemoryFileSystem();
443+
});
444+
});
445+
446+
describe('filtering unwanted html', function() {
447+
it('does not include preload asset into index.html file', function(done) {
448+
const compiler = webpack({
449+
entry: {
450+
js: path.join(__dirname, 'fixtures', 'file.js')
451+
},
452+
output: {
453+
path: OUTPUT_DIR,
454+
filename: 'bundle.js',
455+
chunkFilename: 'chunk.[chunkhash].js',
456+
publicPath: '/',
457+
},
458+
devtool: 'cheap-source-map',
459+
plugins: [
460+
new HtmlWebpackPlugin(),
461+
new PreloadPlugin({
462+
excludeHtmlNames: ['index.html'],
463+
})
464+
]
465+
}, function(err, result) {
466+
expect(err).toBeFalsy();
467+
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
468+
const html = result.compilation.assets['index.html'].source();
469+
expect(html).not.toContain('<link rel="preload" as="script" href="/chunk.');
470+
done();
471+
});
472+
compiler.outputFileSystem = new MemoryFileSystem();
473+
});
474+
});

0 commit comments

Comments
 (0)