Skip to content

Commit 42194f3

Browse files
authoredMay 8, 2020
refactor: code (#459)
BREAKING CHANGE: migrate on `compilation.additionalAssets` hook
1 parent 1e2f3a1 commit 42194f3

File tree

6 files changed

+133
-83
lines changed

6 files changed

+133
-83
lines changed
 

‎src/index.js

+45-67
Original file line numberDiff line numberDiff line change
@@ -17,81 +17,59 @@ class CopyPlugin {
1717
}
1818

1919
apply(compiler) {
20-
const fileDependencies = new Set();
21-
const contextDependencies = new Set();
2220
const plugin = { name: 'CopyPlugin' };
23-
const logger = compiler.getInfrastructureLogger('copy-webpack-plugin');
2421

25-
compiler.hooks.emit.tapAsync(plugin, (compilation, callback) => {
26-
logger.debug('starting emit');
22+
compiler.hooks.compilation.tap(plugin, (compilation) => {
23+
const logger = compilation.getLogger('copy-webpack-plugin');
2724

28-
const globalRef = {
29-
context: compiler.options.context,
30-
logger,
31-
compilation,
32-
fileDependencies,
33-
contextDependencies,
34-
inputFileSystem: compiler.inputFileSystem,
35-
output: compiler.options.output.path,
36-
ignore: this.options.ignore || [],
37-
concurrency: this.options.concurrency,
38-
};
25+
compilation.hooks.additionalAssets.tapAsync(
26+
'copy-webpack-plugin',
27+
(callback) => {
28+
logger.debug('start to adding additionalAssets');
3929

40-
Promise.all(
41-
this.patterns.map((pattern) =>
42-
Promise.resolve()
43-
.then(() => preProcessPattern(globalRef, pattern))
44-
// Every source (from) is assumed to exist here
45-
// eslint-disable-next-line no-shadow
46-
.then((pattern) =>
47-
processPattern(globalRef, pattern).then((files) => {
48-
if (!files) {
49-
return Promise.resolve();
50-
}
30+
const globalRef = {
31+
context: compiler.options.context,
32+
logger,
33+
compilation,
34+
inputFileSystem: compiler.inputFileSystem,
35+
output: compiler.options.output.path,
36+
ignore: this.options.ignore || [],
37+
concurrency: this.options.concurrency,
38+
};
5139

52-
return Promise.all(
53-
files
54-
.filter(Boolean)
55-
.map((file) => postProcessPattern(globalRef, pattern, file))
56-
);
57-
})
58-
)
59-
)
60-
)
61-
.catch((error) => {
62-
compilation.errors.push(error);
63-
})
64-
.then(() => {
65-
logger.debug('finishing emit');
66-
67-
callback();
68-
});
69-
});
70-
71-
compiler.hooks.afterEmit.tapAsync(plugin, (compilation, callback) => {
72-
logger.debug('starting after-emit');
40+
Promise.all(
41+
this.patterns.map((pattern) =>
42+
Promise.resolve()
43+
.then(() => preProcessPattern(globalRef, pattern))
44+
// Every source (from) is assumed to exist here
45+
// eslint-disable-next-line no-shadow
46+
.then((pattern) =>
47+
processPattern(globalRef, pattern).then((files) => {
48+
if (!files) {
49+
return Promise.resolve();
50+
}
7351

74-
// Add file dependencies
75-
if ('addAll' in compilation.fileDependencies) {
76-
compilation.fileDependencies.addAll(fileDependencies);
77-
} else {
78-
for (const fileDependency of fileDependencies) {
79-
compilation.fileDependencies.add(fileDependency);
80-
}
81-
}
52+
return Promise.all(
53+
files
54+
.filter(Boolean)
55+
.map((file) =>
56+
postProcessPattern(globalRef, pattern, file)
57+
)
58+
);
59+
})
60+
)
61+
)
62+
)
63+
.catch((error) => {
64+
compilation.errors.push(error);
65+
})
66+
.then(() => {
67+
logger.debug('end to adding additionalAssets');
8268

83-
// Add context dependencies
84-
if ('addAll' in compilation.contextDependencies) {
85-
compilation.contextDependencies.addAll(contextDependencies);
86-
} else {
87-
for (const contextDependency of contextDependencies) {
88-
compilation.contextDependencies.add(contextDependency);
69+
callback();
70+
});
8971
}
90-
}
91-
92-
logger.debug('finishing after-emit');
93-
94-
callback();
72+
);
9573
});
9674
}
9775
}

‎src/postProcessPattern.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { stat, readFile } from './utils/promisify';
1515
/* eslint-disable no-param-reassign */
1616

1717
export default function postProcessPattern(globalRef, pattern, file) {
18-
const { logger, compilation, fileDependencies, inputFileSystem } = globalRef;
18+
const { logger, compilation, inputFileSystem } = globalRef;
1919

2020
logger.debug(`getting stats for '${file.absoluteFrom}' to write to assets`);
2121

@@ -35,7 +35,8 @@ export default function postProcessPattern(globalRef, pattern, file) {
3535

3636
// If this came from a glob, add it to the file watchlist
3737
if (pattern.fromType === 'glob') {
38-
fileDependencies.add(file.absoluteFrom);
38+
logger.debug(`add ${file.absoluteFrom} as fileDependencies`);
39+
compilation.fileDependencies.add(file.absoluteFrom);
3940
}
4041

4142
logger.debug(`reading '${file.absoluteFrom}' to write to assets`);

‎src/utils/createPatternGlob.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function getAbsoluteContext(context) {
1616
}
1717

1818
function createPatternGlob(pattern, globalRef) {
19-
const { logger, fileDependencies, contextDependencies } = globalRef;
19+
const { logger, compilation } = globalRef;
2020

2121
pattern.globOptions = Object.assign(
2222
{
@@ -29,7 +29,8 @@ function createPatternGlob(pattern, globalRef) {
2929
switch (pattern.fromType) {
3030
case 'dir':
3131
logger.debug(`determined '${pattern.absoluteFrom}' is a directory`);
32-
contextDependencies.add(pattern.absoluteFrom);
32+
logger.debug(`add ${pattern.absoluteFrom} as contextDependencies`);
33+
compilation.contextDependencies.add(pattern.absoluteFrom);
3334

3435
pattern.context = pattern.absoluteFrom;
3536
pattern.glob = path.posix.join(
@@ -44,7 +45,8 @@ function createPatternGlob(pattern, globalRef) {
4445

4546
case 'file':
4647
logger.debug(`determined '${pattern.absoluteFrom}' is a file`);
47-
fileDependencies.add(pattern.absoluteFrom);
48+
logger.debug(`add ${pattern.absoluteFrom} as fileDependencies`);
49+
compilation.fileDependencies.add(pattern.absoluteFrom);
4850

4951
pattern.context = path.dirname(pattern.absoluteFrom);
5052
pattern.glob = getAbsoluteContext(pattern.absoluteFrom);
@@ -55,7 +57,14 @@ function createPatternGlob(pattern, globalRef) {
5557

5658
default:
5759
logger.debug(`determined '${pattern.absoluteFrom}' is a glob`);
58-
contextDependencies.add(path.normalize(globParent(pattern.absoluteFrom)));
60+
61+
// eslint-disable-next-line no-case-declarations
62+
const contextDependencies = path.normalize(
63+
globParent(pattern.absoluteFrom)
64+
);
65+
66+
logger.debug(`add ${contextDependencies} as contextDependencies`);
67+
compilation.contextDependencies.add(contextDependencies);
5968

6069
pattern.fromType = 'glob';
6170
pattern.globOptions = pattern.globOptions || {};

‎test/CopyPlugin.test.js

+29
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,32 @@ describe('apply function', () => {
539539
});
540540
});
541541
});
542+
543+
describe('logging', () => {
544+
it('should logging', (done) => {
545+
const expectedAssetKeys = ['file.txt'];
546+
547+
run({
548+
patterns: [
549+
{
550+
from: 'file.txt',
551+
},
552+
],
553+
})
554+
.then(({ compiler, stats }) => {
555+
const root = path.resolve(__dirname).replace(/\\/g, '/');
556+
const logs = stats.compilation.logging
557+
.get('copy-webpack-plugin')
558+
.map((entry) =>
559+
entry.args[0].replace(/\\/g, '/').split(root).join('.')
560+
);
561+
562+
expect(
563+
Array.from(Object.keys(readAssets(compiler, stats))).sort()
564+
).toEqual(expectedAssetKeys);
565+
expect({ result: logs }).toMatchSnapshot({ result: logs });
566+
})
567+
.then(done)
568+
.catch(done);
569+
});
570+
});
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`logging should logging 1`] = `
4+
Object {
5+
"result": Array [
6+
"start to adding additionalAssets",
7+
"processing from: 'file.txt' to: '.'",
8+
"getting stats for './fixtures/file.txt' to determinate 'fromType'",
9+
"determined './fixtures/file.txt' is a file",
10+
"add ./fixtures/file.txt as fileDependencies",
11+
"begin globbing './fixtures/file.txt' with a context of './fixtures'",
12+
"found ./fixtures/file.txt",
13+
"testing watch/**/* against file.txt",
14+
"watch/**/* doesn't match file.txt",
15+
"testing directory-ln against file.txt",
16+
"directory-ln doesn't match file.txt",
17+
"testing file-ln.txt against file.txt",
18+
"file-ln.txt doesn't match file.txt",
19+
"testing symlink/**/* against file.txt",
20+
"symlink/**/* doesn't match file.txt",
21+
"determined that './fixtures/file.txt' should write to 'file.txt'",
22+
"getting stats for './fixtures/file.txt' to write to assets",
23+
"reading './fixtures/file.txt' to write to assets",
24+
"writing 'file.txt' to compilation assets from './fixtures/file.txt'",
25+
"end to adding additionalAssets",
26+
],
27+
}
28+
`;

‎test/helpers/PreCopyPlugin.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ class PreCopyPlugin {
66
apply(compiler) {
77
const plugin = { name: 'PreCopyPlugin' };
88

9-
compiler.hooks.emit.tapAsync(plugin, (compilation, callback) => {
10-
this.options.existingAssets.forEach((assetName) => {
11-
// eslint-disable-next-line no-param-reassign
12-
compilation.assets[assetName] = {
13-
source() {
14-
return 'existing';
15-
},
16-
};
17-
});
9+
compiler.hooks.compilation.tap(plugin, (compilation) => {
10+
compilation.hooks.additionalAssets.tapAsync(
11+
'copy-webpack-plugin',
12+
(callback) => {
13+
this.options.existingAssets.forEach((assetName) => {
14+
// eslint-disable-next-line no-param-reassign
15+
compilation.assets[assetName] = {
16+
source() {
17+
return 'existing';
18+
},
19+
};
20+
});
1821

19-
callback();
22+
callback();
23+
}
24+
);
2025
});
2126
}
2227
}

0 commit comments

Comments
 (0)
Please sign in to comment.