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

Commit d123908

Browse files
authored
Merge pull request #16 from jackfranklin/add-files-blacklist
Add `filesBlacklist` to avoid preloading certain files
2 parents 98967bb + b92cadc commit d123908

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ will inject just this:
126126
<link rel="preload" href="home.31132ae6680e598f8879.js" as="script">
127127
```
128128

129+
Filtering chunks
130+
---------------------
131+
132+
There may be chunks that you don't want to have preloaded (sourcemaps, for example). Before preloading each chunk, this plugin checks that the file does not match any regex in the `fileBlacklist` option. The default value of this blacklist is `[/\.map/]`, meaning no sourcemaps will be preloaded. You can easily override this:
133+
134+
```js
135+
new PreloadWebpackPlugin({
136+
fileBlacklist: [/\.whatever/]
137+
})
138+
```
139+
140+
Passing your own array will override the default, so if you want to continue filtering sourcemaps along with your own custom settings, you'll need to include the regex for sourcemaps:
141+
142+
```js
143+
new PreloadWebpackPlugin({
144+
fileBlacklist: [/\.map./, /\.whatever/]
145+
})
146+
```
147+
129148
Resource Hints
130149
---------------------
131150

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const objectAssign = require('object-assign');
2020
const defaultOptions = {
2121
rel: 'preload',
2222
as: 'script',
23-
include: 'asyncChunks'
23+
include: 'asyncChunks',
24+
fileBlacklist: [/\.map/]
2425
};
2526

2627
class PreloadPlugin {
@@ -70,7 +71,10 @@ class PreloadPlugin {
7071
}
7172

7273
const publicPath = compilation.outputOptions.publicPath || '';
73-
extractedChunks.forEach(entry => {
74+
75+
extractedChunks.filter(entry => {
76+
return this.options.fileBlacklist.every(regex => regex.test(entry) === false);
77+
}).forEach(entry => {
7478
entry = `${publicPath}${entry}`;
7579
if (options.rel === 'preload') {
7680
filesToInclude+= `<link rel="${options.rel}" href="${entry}" as="${options.as}">\n`;

test/spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,32 @@ describe('PreloadPlugin filters chunks', function() {
190190
compiler.outputFileSystem = new MemoryFileSystem();
191191
});
192192
});
193+
194+
describe('filtering unwanted files', function() {
195+
it('does not include map files to be preloaded', function(done) {
196+
const compiler = webpack({
197+
entry: {
198+
js: path.join(__dirname, 'fixtures', 'file.js')
199+
},
200+
output: {
201+
path: OUTPUT_DIR,
202+
filename: 'bundle.js',
203+
chunkFilename: 'chunk.[chunkhash].js',
204+
publicPath: '/',
205+
},
206+
devtool: 'cheap-source-map',
207+
plugins: [
208+
new HtmlWebpackPlugin(),
209+
new PreloadPlugin()
210+
]
211+
}, function(err, result) {
212+
expect(err).toBeFalsy();
213+
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
214+
const html = result.compilation.assets['index.html'].source();
215+
expect(html).toContain('<link rel="preload" href="/chunk.');
216+
expect(html).not.toContain('.map"');
217+
done();
218+
});
219+
compiler.outputFileSystem = new MemoryFileSystem();
220+
});
221+
});

0 commit comments

Comments
 (0)