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

Commit a62baa5

Browse files
committed
fix include feature
`include` feature will not work properly when sourcemap is generated (i.e. chunk.files is an array). fix this issue by flattening output array.
1 parent d123908 commit a62baa5

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class PreloadPlugin {
6767
}
6868
return options.include.indexOf(chunkName) > -1;
6969
})
70-
.map(chunk => chunk.files);
70+
.map(chunk => chunk.files)
71+
.reduce((prev, curr) => prev.concat(curr), []);
7172
}
7273

7374
const publicPath = compilation.outputOptions.publicPath || '';

test/spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,37 @@ describe('PreloadPlugin filters chunks', function() {
189189
});
190190
compiler.outputFileSystem = new MemoryFileSystem();
191191
});
192+
it('based on chunkname with sourcemap', function(done) {
193+
const compiler = webpack({
194+
entry: path.join(__dirname, 'fixtures', 'file.js'),
195+
devtool: 'cheap-source-map',
196+
output: {
197+
path: OUTPUT_DIR,
198+
filename: 'bundle.js',
199+
chunkFilename: '[name].js',
200+
publicPath: '/',
201+
},
202+
plugins: [
203+
new HtmlWebpackPlugin(),
204+
new PreloadPlugin({
205+
rel: 'preload',
206+
as: 'script',
207+
include: ['home'],
208+
// disable default file blacklist, to include map file
209+
fileBlacklist: [],
210+
})
211+
]
212+
}, function(err, result) {
213+
expect(err).toBeFalsy();
214+
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
215+
const html = result.compilation.assets['index.html'].source();
216+
expect(html).toContain('<link rel="preload" href="/home.js');
217+
expect(html).toContain('<link rel="preload" href="/home.js.map');
218+
expect(html).not.toContain('<link rel="preload" href="/bundle.js"');
219+
done();
220+
});
221+
compiler.outputFileSystem = new MemoryFileSystem();
222+
});
192223
});
193224

194225
describe('filtering unwanted files', function() {

0 commit comments

Comments
 (0)