Skip to content

Commit 42f27c7

Browse files
fix: do not execute on a child compiler
1 parent 6d51a69 commit 42f27c7

File tree

7 files changed

+362
-303
lines changed

7 files changed

+362
-303
lines changed

package-lock.json

Lines changed: 290 additions & 289 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@
4242
},
4343
"dependencies": {
4444
"cacache": "^15.0.4",
45-
"fast-glob": "^3.2.2",
45+
"fast-glob": "^3.2.4",
4646
"find-cache-dir": "^3.3.1",
4747
"glob-parent": "^5.1.1",
4848
"globby": "^11.0.1",
4949
"loader-utils": "^2.0.0",
5050
"normalize-path": "^3.0.0",
51-
"p-limit": "^2.3.0",
51+
"p-limit": "^3.0.1",
5252
"schema-utils": "^2.7.0",
53-
"serialize-javascript": "^3.1.0",
53+
"serialize-javascript": "^4.0.0",
5454
"webpack-sources": "^1.4.3"
5555
},
5656
"devDependencies": {
@@ -66,14 +66,13 @@
6666
"cross-env": "^7.0.2",
6767
"del": "^5.1.0",
6868
"del-cli": "^3.0.1",
69-
"enhanced-resolve": "^4.1.1",
70-
"eslint": "^7.1.0",
69+
"eslint": "^7.2.0",
7170
"eslint-config-prettier": "^6.11.0",
72-
"eslint-plugin-import": "^2.20.2",
71+
"eslint-plugin-import": "^2.21.2",
7372
"husky": "^4.2.5",
7473
"is-gzip": "^2.0.0",
7574
"jest": "^26.0.1",
76-
"lint-staged": "^10.2.8",
75+
"lint-staged": "^10.2.11",
7776
"memfs": "^3.2.0",
7877
"mkdirp": "^1.0.4",
7978
"npm-run-all": "^4.1.5",

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CopyPlugin {
2121
const plugin = { name: 'CopyPlugin' };
2222
const limit = pLimit(this.options.concurrency || 100);
2323

24-
compiler.hooks.compilation.tap(plugin, (compilation) => {
24+
compiler.hooks.thisCompilation.tap(plugin, (compilation) => {
2525
const logger = compilation.getLogger('copy-webpack-plugin');
2626

2727
compilation.hooks.additionalAssets.tapAsync(

test/CopyPlugin.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import path from 'path';
22

3+
import CopyPlugin from '../src';
4+
35
import { run, runEmit, runChange } from './helpers/run';
46

57
import { readAssets } from './helpers';
@@ -485,6 +487,30 @@ describe('CopyPlugin', () => {
485487
.then(done)
486488
.catch(done);
487489
});
490+
491+
it('should run once on child compilation', (done) => {
492+
const expectedAssetKeys = ['file.txt'];
493+
const spy = jest.spyOn(CopyPlugin, 'apply');
494+
495+
run({
496+
withChildCompilation: true,
497+
patterns: [
498+
{
499+
from: 'file.txt',
500+
},
501+
],
502+
})
503+
.then(({ compiler, stats }) => {
504+
// expect(spy).toHaveBeenCalledTimes(1);
505+
expect(
506+
Array.from(Object.keys(readAssets(compiler, stats))).sort()
507+
).toEqual(expectedAssetKeys);
508+
509+
spy.mockRestore();
510+
})
511+
.then(done)
512+
.catch(done);
513+
});
488514
});
489515

490516
describe('logging', () => {

test/helpers/ChildCompiler.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export default class ChildCompiler {
2+
// eslint-disable-next-line class-methods-use-this
3+
apply(compiler) {
4+
compiler.hooks.make.tapAsync('Child Compiler', (compilation, callback) => {
5+
const outputOptions = {
6+
filename: 'output.js',
7+
publicPath: compilation.outputOptions.publicPath,
8+
};
9+
const childCompiler = compilation.createChildCompiler(
10+
'ChildCompiler',
11+
outputOptions
12+
);
13+
childCompiler.runAsChild((error, entries, childCompilation) => {
14+
if (error) {
15+
throw error;
16+
}
17+
18+
const assets = childCompilation.getAssets();
19+
20+
if (assets.length > 0) {
21+
callback(
22+
new Error('Copy plugin should not be ran in child compilations')
23+
);
24+
25+
return;
26+
}
27+
28+
callback();
29+
});
30+
});
31+
}
32+
}

test/helpers/PreCopyPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class PreCopyPlugin {
66
apply(compiler) {
77
const plugin = { name: 'PreCopyPlugin' };
88

9-
compiler.hooks.compilation.tap(plugin, (compilation) => {
9+
compiler.hooks.thisCompilation.tap(plugin, (compilation) => {
1010
compilation.hooks.additionalAssets.tapAsync(
1111
'copy-webpack-plugin',
1212
(callback) => {

test/helpers/run.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'path';
44

55
import CopyPlugin from '../../src';
66

7+
import ChildCompilerPlugin from './ChildCompiler';
78
import PreCopyPlugin from './PreCopyPlugin';
89

910
import removeIllegalCharacterForWindows from './removeIllegalCharacterForWindows';
@@ -48,6 +49,10 @@ function run(opts) {
4849
compiler
4950
);
5051

52+
if (opts.withChildCompilation) {
53+
new ChildCompilerPlugin().apply(compiler);
54+
}
55+
5156
// Execute the functions in series
5257
return compile(compiler)
5358
.then(({ stats }) => {
@@ -188,11 +193,7 @@ function runChange(opts) {
188193

189194
resolve(watching);
190195
});
191-
// eslint-disable-next-line no-unused-vars
192-
}).then((watching) => {
193-
// eslint-disable-next-line no-param-reassign
194-
watching = null;
195-
196+
}).then(() => {
196197
fs.unlinkSync(opts.newFileLoc1);
197198
fs.unlinkSync(opts.newFileLoc2);
198199
});

0 commit comments

Comments
 (0)