forked from johnagan/clean-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (205 loc) · 7 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
'use strict';
var fs = require('fs');
var os = require('os');
var path = require('path');
var rimraf = require('rimraf');
var pluginName = 'clean-webpack-plugin';
// added node .10
// http://stackoverflow.com/questions/21698906/how-to-check-if-a-path-is-absolute-or-relative/30714706#30714706
function isAbsolute(dir) {
return path.normalize(dir + path.sep) === path.normalize(path.resolve(dir) + path.sep);
}
function upperCaseWindowsRoot(dir) {
var splitPath = dir.split(path.sep);
splitPath[0] = splitPath[0].toUpperCase();
return splitPath.join(path.sep);
}
function CleanWebpackPlugin(paths, options) {
//backwards compatibility
if (typeof options === 'string') {
options = {
root: options
}
}
options = options || {};
if (options.verbose === undefined) {
if (process.env.NODE_ENV === 'test') {
options.verbose = false;
} else {
options.verbose = true;
}
}
options.allowExternal = options.allowExternal || false;
if (options.dry === undefined) {
options.dry = false;
}
// determine webpack root
options.root = options.root || path.dirname(module.parent.filename);
// allows for a single string entry or a regular expression
if (typeof paths == 'string' || paths instanceof String || paths instanceof RegExp) {
paths = [paths];
}
// store paths and options
this.paths = paths;
this.options = options;
}
/**
* Gives back file paths that match the given pattern
*
* @param {string} root
* @param {RegExp} pattern
* @returns [string]
*/
function resolveRegexPaths(root, pattern) {
var rootChildren = fs.readdirSync(root);
return rootChildren.filter(pattern.test.bind(pattern));
}
var clean = function() {
var _this = this;
var results = [];
var workingDir;
var dirName;
var projectRootDir;
var webpackDir;
// exit if no paths passed in
if (_this.paths === void 0) {
results.push({ path: _this.paths, output: 'nothing to clean' });
return results;
}
if (!isAbsolute(_this.options.root)) {
_this.options.verbose && console.warn(
'clean-webpack-plugin: ' + _this.options.root +
' project root must be an absolute path. Skipping all...');
results.push({ path: _this.options.root, output: 'project root must be an absolute path' });
return results;
}
workingDir = process.cwd();
dirName = __dirname;
projectRootDir = path.resolve(_this.options.root);
webpackDir = path.dirname(module.parent.filename);
if (os.platform() === 'win32') {
workingDir = upperCaseWindowsRoot(workingDir);
dirName = upperCaseWindowsRoot(dirName);
projectRootDir = upperCaseWindowsRoot(projectRootDir);
webpackDir = upperCaseWindowsRoot(webpackDir);
}
// Resolve RegExp paths.
_this.paths = _this.paths.reduce(function (paths, currentPath) {
if (currentPath instanceof RegExp) {
var resolvedPaths = resolveRegexPaths(_this.options.root, currentPath);
return paths.concat(resolvedPaths);
}
return paths.concat(currentPath);
}, []);
// preform an rm -rf on each path
_this.paths.forEach(function(rimrafPath) {
rimrafPath = path.resolve(_this.options.root, rimrafPath);
if (os.platform() === 'win32') {
rimrafPath = upperCaseWindowsRoot(rimrafPath);
}
// disallow deletion any directories outside of root path.
if (rimrafPath.indexOf(projectRootDir) < 0 && !_this.options.allowExternal) {
_this.options.verbose && console.warn(
'clean-webpack-plugin: ' + rimrafPath + ' is outside of the project root. Skipping...');
results.push({ path: rimrafPath, output: 'must be inside the project root' });
return;
}
if (rimrafPath === projectRootDir) {
_this.options.verbose &&
console.warn(
'clean-webpack-plugin: ' + rimrafPath + ' is equal to project root. Skipping...');
results.push({ path: rimrafPath, output: 'is equal to project root' });
return;
}
if (rimrafPath === webpackDir) {
_this.options.verbose &&
console.warn('clean-webpack-plugin: ' + rimrafPath + ' would delete webpack. Skipping...');
results.push({ path: rimrafPath, output: 'would delete webpack' });
return;
}
if (rimrafPath === dirName || rimrafPath === workingDir) {
_this.options.verbose &&
console.log('clean-webpack-plugin: ' + rimrafPath + ' is working directory. Skipping...');
results.push({ path: rimrafPath, output: 'is working directory' });
return;
}
var childrenAfterExcluding = [];
var excludedChildren = [];
if (_this.options.exclude && _this.options.exclude.length) {
try {
var pathStat = fs.statSync(rimrafPath);
if (pathStat.isDirectory()) {
childrenAfterExcluding = fs.readdirSync(rimrafPath)
.filter(function(childFile) {
var include = _this.options.exclude.indexOf(childFile) < 0;
if (!include) {
excludedChildren.push(childFile);
}
return include;
})
.map(function(file) {
var fullPath = path.join(rimrafPath, file);
if (os.platform() === 'win32') {
fullPath = upperCaseWindowsRoot(fullPath);
}
return fullPath;
});
}
if (_this.options.exclude.indexOf('.') >= 0) {
excludedChildren.push('.');
}
} catch (e) {
childrenAfterExcluding = [];
}
}
if (_this.options.dry !== true) {
if (_this.options.exclude && excludedChildren.length) {
childrenAfterExcluding.forEach(function(child) {
rimraf.sync(child);
});
} else {
rimraf.sync(rimrafPath);
}
}
_this.options.verbose &&
console.warn('clean-webpack-plugin: ' + rimrafPath + ' has been removed.');
_this.options.verbose && excludedChildren.length &&
console.warn('clean-webpack-plugin: ' + excludedChildren.length + ' file(s) excluded - ' + excludedChildren.join(', '));
excludedChildren.length ?
results.push({ path: rimrafPath, output: 'removed with exclusions (' + excludedChildren.length + ')' }) :
results.push({ path: rimrafPath, output: 'removed' });
});
return results;
};
CleanWebpackPlugin.prototype.apply = function(compiler) {
var _this = this;
if (compiler === undefined) {
return clean.call(_this);
} else {
// To check which version of webpack is used
var hooks = compiler.hooks;
if (_this.options.watch) {
var compile = function(params) {
clean.call(_this);
}
if (hooks) {
hooks.compile.tap(pluginName, compile);
} else {
compiler.plugin("compile", compile);
}
} else if (_this.options.beforeEmit && !compiler.options.watch) {
var emit = function(compilation, callback) {
clean.call(_this);
callback();
};
if (hooks) {
hooks.emit.tapAsync(pluginName, emit);
} else {
compiler.plugin("emit", emit);
}
} else {
return clean.call(_this);
}
}
};
module.exports = CleanWebpackPlugin;