-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
index.js
175 lines (148 loc) · 5.68 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import path from 'path';
import preProcessPattern from './preProcessPattern';
import processPattern from './processPattern';
function CopyWebpackPlugin(patterns = [], options = {}) {
if (!Array.isArray(patterns)) {
throw new Error('[copy-webpack-plugin] patterns must be an array');
}
// Defaults debug level to 'warning'
options.debug = options.debug || 'warning';
// Defaults debugging to info if only true is specified
if (options.debug === true) {
options.debug = 'info';
}
const debugLevels = ['warning', 'info', 'debug'];
const debugLevelIndex = debugLevels.indexOf(options.debug);
function log(msg, level) {
if (level === 0) {
msg = `WARNING - ${msg}`;
} else {
level = level || 1;
}
if (level <= debugLevelIndex) {
console.log('[copy-webpack-plugin] ' + msg); // eslint-disable-line no-console
}
}
function warning(msg) {
log(msg, 0);
}
function info(msg) {
log(msg, 1);
}
function debug(msg) {
log(msg, 2);
}
const apply = (compiler) => {
let fileDependencies;
let contextDependencies;
const written = {};
let context;
if (!options.context) {
context = compiler.options.context;
} else if (!path.isAbsolute(options.context)) {
context = path.join(compiler.options.context, options.context);
} else {
context = options.context;
}
const emit = (compilation, cb) => {
debug('starting emit');
const callback = () => {
debug('finishing emit');
cb();
};
fileDependencies = [];
contextDependencies = [];
const globalRef = {
info,
debug,
warning,
compilation,
written,
fileDependencies,
contextDependencies,
context,
inputFileSystem: compiler.inputFileSystem,
output: compiler.options.output.path,
ignore: options.ignore || [],
copyUnmodified: options.copyUnmodified,
concurrency: options.concurrency
};
if (globalRef.output === '/' &&
compiler.options.devServer &&
compiler.options.devServer.outputPath) {
globalRef.output = compiler.options.devServer.outputPath;
}
const tasks = [];
patterns.forEach((pattern) => {
tasks.push(
Promise.resolve()
.then(() => preProcessPattern(globalRef, pattern))
// Every source (from) is assumed to exist here
.then((pattern) => processPattern(globalRef, pattern))
);
});
Promise.all(tasks)
.catch((err) => {
compilation.errors.push(err);
})
.then(() => callback());
};
const afterEmit = (compilation, cb) => {
debug('starting after-emit');
const callback = () => {
debug('finishing after-emit');
cb();
};
let compilationFileDependencies;
let addFileDependency;
if (Array.isArray(compilation.fileDependencies)) {
compilationFileDependencies = new Set(compilation.fileDependencies);
addFileDependency = (file) => compilation.fileDependencies.push(file);
} else {
compilationFileDependencies = compilation.fileDependencies;
addFileDependency = (file) => compilation.fileDependencies.add(file);
}
let compilationContextDependencies;
let addContextDependency;
if (Array.isArray(compilation.contextDependencies)) {
compilationContextDependencies = new Set(compilation.contextDependencies);
addContextDependency = (file) => compilation.contextDependencies.push(file);
} else {
compilationContextDependencies = compilation.contextDependencies;
addContextDependency = (file) => compilation.contextDependencies.add(file);
}
// Add file dependencies if they're not already tracked
for (const file of fileDependencies) {
if (compilationFileDependencies.has(file)) {
debug(`not adding ${file} to change tracking, because it's already tracked`);
} else {
debug(`adding ${file} to change tracking`);
addFileDependency(file);
}
}
// Add context dependencies if they're not already tracked
for (const context of contextDependencies) {
if (compilationContextDependencies.has(context)) {
debug(`not adding ${context} to change tracking, because it's already tracked`);
} else {
debug(`adding ${context} to change tracking`);
addContextDependency(context);
}
}
callback();
};
if (compiler.hooks) {
const plugin = { name: 'CopyPlugin' };
compiler.hooks.emit.tapAsync(plugin, emit);
compiler.hooks.afterEmit.tapAsync(plugin, afterEmit);
} else {
compiler.plugin('emit', emit);
compiler.plugin('after-emit', afterEmit);
}
};
return {
apply
};
}
CopyWebpackPlugin['default'] = CopyWebpackPlugin;
module.exports = CopyWebpackPlugin;