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

Add filesBlacklist to avoid preloading certain files #16

Merged
merged 2 commits into from Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -126,6 +126,25 @@ will inject just this:
<link rel="preload" href="home.31132ae6680e598f8879.js" as="script">
```

Filtering chunks
---------------------

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:

```js
new PreloadWebpackPlugin({
fileBlacklist: [/\.whatever/]
})
```

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:

```js
new PreloadWebpackPlugin({
fileBlacklist: [/\.map./, /\.whatever/]
})
```

Resource Hints
---------------------

Expand Down
8 changes: 6 additions & 2 deletions index.js
Expand Up @@ -20,7 +20,8 @@ const objectAssign = require('object-assign');
const defaultOptions = {
rel: 'preload',
as: 'script',
include: 'asyncChunks'
include: 'asyncChunks',
fileBlacklist: [/\.map/]
};

class PreloadPlugin {
Expand Down Expand Up @@ -70,7 +71,10 @@ class PreloadPlugin {
}

const publicPath = compilation.outputOptions.publicPath || '';
extractedChunks.forEach(entry => {

extractedChunks.filter(entry => {
return this.options.fileBlacklist.every(regex => regex.test(entry) === false);
}).forEach(entry => {
entry = `${publicPath}${entry}`;
if (options.rel === 'preload') {
filesToInclude+= `<link rel="${options.rel}" href="${entry}" as="${options.as}">\n`;
Expand Down
29 changes: 29 additions & 0 deletions test/spec.js
Expand Up @@ -190,3 +190,32 @@ describe('PreloadPlugin filters chunks', function() {
compiler.outputFileSystem = new MemoryFileSystem();
});
});

describe('filtering unwanted files', function() {
it('does not include map files to be preloaded', function(done) {
const compiler = webpack({
entry: {
js: path.join(__dirname, 'fixtures', 'file.js')
},
output: {
path: OUTPUT_DIR,
filename: 'bundle.js',
chunkFilename: 'chunk.[chunkhash].js',
publicPath: '/',
},
devtool: 'cheap-source-map',
plugins: [
new HtmlWebpackPlugin(),
new PreloadPlugin()
]
}, function(err, result) {
expect(err).toBeFalsy();
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
const html = result.compilation.assets['index.html'].source();
expect(html).toContain('<link rel="preload" href="/chunk.');
expect(html).not.toContain('.map"');
done();
});
compiler.outputFileSystem = new MemoryFileSystem();
});
});