Skip to content

Commit 4e2e2af

Browse files
authored
Ensure it's only run once (#8)
1 parent 7da89aa commit 4e2e2af

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
module.exports = class ChildCompilerPlugin {
4+
apply(compiler) {
5+
compiler.hooks.make.tapAsync('ChildCompilerPlugin', (compilation, callback) => {
6+
const childCompiler = compilation.createChildCompiler('ChildCompilerPlugin');
7+
childCompiler.runAsChild(callback);
8+
});
9+
}
10+
};

β€Žfixture/webpack.config.jsβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const AddAssetPlugin = require('..');
3+
const ChildCompilerPlugin = require('./child-compiler-plugin');
34

45
module.exports = {
56
output: {
@@ -9,6 +10,7 @@ module.exports = {
910
plugins: [
1011
new AddAssetPlugin('rainbow.js', 'console.log("🌈")'),
1112
new AddAssetPlugin('cake.js', () => 'console.log("πŸŽ‚")'),
12-
new AddAssetPlugin('cat.js', () => Promise.resolve('console.log("🐈")'))
13+
new AddAssetPlugin('cat.js', () => Promise.resolve('console.log("🐈")')),
14+
new ChildCompilerPlugin()
1315
]
1416
};

β€Žindex.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = class AddAssetPlugin {
1313
}
1414

1515
apply(compiler) {
16-
compiler.hooks.compilation.tap('AddAssetPlugin', compilation => {
16+
compiler.hooks.thisCompilation.tap('AddAssetPlugin', compilation => {
1717
compilation.hooks.processAssets.tapPromise(tapOptions, async () => {
1818
let source;
1919
if (typeof this.source === 'string') {

β€Žtest.jsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ test('main', async t => {
99
const config = require('./fixture/webpack.config');
1010
const cwd = tempy.directory();
1111
config.output.path = cwd;
12-
await pify(webpack)(config);
12+
const stats = await pify(webpack)(config);
13+
t.false(stats.hasErrors());
1314
t.true(fs.readFileSync(path.join(cwd, 'unicorn.js'), 'utf8').includes('πŸ¦„'));
1415
t.true(fs.readFileSync(path.join(cwd, 'rainbow.js'), 'utf8').includes('🌈'));
1516
t.true(fs.readFileSync(path.join(cwd, 'cake.js'), 'utf8').includes('πŸŽ‚'));

0 commit comments

Comments
Β (0)