-
-
Notifications
You must be signed in to change notification settings - Fork 280
/
index.js
129 lines (107 loc) · 3.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import path from 'path';
import validateOptions from 'schema-utils';
import log from 'webpack-log';
import patternsSchema from './patterns.json';
import optionsSchema from './options.json';
import preProcessPattern from './preProcessPattern';
import processPattern from './processPattern';
import postProcessPattern from './postProcessPattern';
class CopyPlugin {
constructor(patterns = [], options = {}) {
validateOptions(patternsSchema, patterns, this.constructor.name);
validateOptions(optionsSchema, options, this.constructor.name);
this.patterns = patterns;
this.options = options;
}
apply(compiler) {
const fileDependencies = new Set();
const contextDependencies = new Set();
const written = {};
let context;
if (!this.options.context) {
({ context } = compiler.options);
} else if (!path.isAbsolute(this.options.context)) {
context = path.join(compiler.options.context, this.options.context);
} else {
({ context } = this.options);
}
const logger = log({
name: 'copy-webpack-plugin',
level: this.options.logLevel || 'warn',
});
const plugin = { name: 'CopyPlugin' };
compiler.hooks.emit.tapAsync(plugin, (compilation, callback) => {
logger.debug('starting emit');
const globalRef = {
logger,
compilation,
written,
fileDependencies,
contextDependencies,
context,
inputFileSystem: compiler.inputFileSystem,
output: compiler.options.output.path,
ignore: this.options.ignore || [],
copyUnmodified: this.options.copyUnmodified,
concurrency: this.options.concurrency,
};
if (
globalRef.output === '/' &&
compiler.options.devServer &&
compiler.options.devServer.outputPath
) {
globalRef.output = compiler.options.devServer.outputPath;
}
const { patterns } = this;
Promise.all(
patterns.map((pattern) =>
Promise.resolve()
.then(() => preProcessPattern(globalRef, pattern))
// Every source (from) is assumed to exist here
// eslint-disable-next-line no-shadow
.then((pattern) =>
processPattern(globalRef, pattern).then((files) => {
if (!files) {
return Promise.resolve();
}
return Promise.all(
files
.filter(Boolean)
.map((file) => postProcessPattern(globalRef, pattern, file))
);
})
)
)
)
.catch((error) => {
compilation.errors.push(error);
})
.then(() => {
logger.debug('finishing emit');
callback();
});
});
compiler.hooks.afterEmit.tapAsync(plugin, (compilation, callback) => {
logger.debug('starting after-emit');
// Add file dependencies
if ('addAll' in compilation.fileDependencies) {
compilation.fileDependencies.addAll(fileDependencies);
} else {
for (const fileDependency of fileDependencies) {
compilation.fileDependencies.add(fileDependency);
}
}
// Add context dependencies
if ('addAll' in compilation.contextDependencies) {
compilation.contextDependencies.addAll(contextDependencies);
} else {
for (const contextDependency of contextDependencies) {
compilation.contextDependencies.add(contextDependency);
}
}
logger.debug('finishing after-emit');
callback();
});
}
}
export default CopyPlugin;