Skip to content

Commit

Permalink
fix: do not execute on a child compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jun 18, 2020
1 parent 6d51a69 commit 42f27c7
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 303 deletions.
579 changes: 290 additions & 289 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
},
"dependencies": {
"cacache": "^15.0.4",
"fast-glob": "^3.2.2",
"fast-glob": "^3.2.4",
"find-cache-dir": "^3.3.1",
"glob-parent": "^5.1.1",
"globby": "^11.0.1",
"loader-utils": "^2.0.0",
"normalize-path": "^3.0.0",
"p-limit": "^2.3.0",
"p-limit": "^3.0.1",
"schema-utils": "^2.7.0",
"serialize-javascript": "^3.1.0",
"serialize-javascript": "^4.0.0",
"webpack-sources": "^1.4.3"
},
"devDependencies": {
Expand All @@ -66,14 +66,13 @@
"cross-env": "^7.0.2",
"del": "^5.1.0",
"del-cli": "^3.0.1",
"enhanced-resolve": "^4.1.1",
"eslint": "^7.1.0",
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-import": "^2.21.2",
"husky": "^4.2.5",
"is-gzip": "^2.0.0",
"jest": "^26.0.1",
"lint-staged": "^10.2.8",
"lint-staged": "^10.2.11",
"memfs": "^3.2.0",
"mkdirp": "^1.0.4",
"npm-run-all": "^4.1.5",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CopyPlugin {
const plugin = { name: 'CopyPlugin' };
const limit = pLimit(this.options.concurrency || 100);

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

compilation.hooks.additionalAssets.tapAsync(
Expand Down
26 changes: 26 additions & 0 deletions test/CopyPlugin.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import path from 'path';

import CopyPlugin from '../src';

import { run, runEmit, runChange } from './helpers/run';

import { readAssets } from './helpers';
Expand Down Expand Up @@ -485,6 +487,30 @@ describe('CopyPlugin', () => {
.then(done)
.catch(done);
});

it('should run once on child compilation', (done) => {
const expectedAssetKeys = ['file.txt'];
const spy = jest.spyOn(CopyPlugin, 'apply');

run({
withChildCompilation: true,
patterns: [
{
from: 'file.txt',
},
],
})
.then(({ compiler, stats }) => {
// expect(spy).toHaveBeenCalledTimes(1);
expect(
Array.from(Object.keys(readAssets(compiler, stats))).sort()
).toEqual(expectedAssetKeys);

spy.mockRestore();
})
.then(done)
.catch(done);
});
});

describe('logging', () => {
Expand Down
32 changes: 32 additions & 0 deletions test/helpers/ChildCompiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export default class ChildCompiler {
// eslint-disable-next-line class-methods-use-this
apply(compiler) {
compiler.hooks.make.tapAsync('Child Compiler', (compilation, callback) => {
const outputOptions = {
filename: 'output.js',
publicPath: compilation.outputOptions.publicPath,
};
const childCompiler = compilation.createChildCompiler(
'ChildCompiler',
outputOptions
);
childCompiler.runAsChild((error, entries, childCompilation) => {
if (error) {
throw error;
}

const assets = childCompilation.getAssets();

if (assets.length > 0) {
callback(
new Error('Copy plugin should not be ran in child compilations')
);

return;
}

callback();
});
});
}
}
2 changes: 1 addition & 1 deletion test/helpers/PreCopyPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class PreCopyPlugin {
apply(compiler) {
const plugin = { name: 'PreCopyPlugin' };

compiler.hooks.compilation.tap(plugin, (compilation) => {
compiler.hooks.thisCompilation.tap(plugin, (compilation) => {
compilation.hooks.additionalAssets.tapAsync(
'copy-webpack-plugin',
(callback) => {
Expand Down
11 changes: 6 additions & 5 deletions test/helpers/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path';

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

import ChildCompilerPlugin from './ChildCompiler';
import PreCopyPlugin from './PreCopyPlugin';

import removeIllegalCharacterForWindows from './removeIllegalCharacterForWindows';
Expand Down Expand Up @@ -48,6 +49,10 @@ function run(opts) {
compiler
);

if (opts.withChildCompilation) {
new ChildCompilerPlugin().apply(compiler);
}

// Execute the functions in series
return compile(compiler)
.then(({ stats }) => {
Expand Down Expand Up @@ -188,11 +193,7 @@ function runChange(opts) {

resolve(watching);
});
// eslint-disable-next-line no-unused-vars
}).then((watching) => {
// eslint-disable-next-line no-param-reassign
watching = null;

}).then(() => {
fs.unlinkSync(opts.newFileLoc1);
fs.unlinkSync(opts.newFileLoc2);
});
Expand Down

0 comments on commit 42f27c7

Please sign in to comment.