-
Notifications
You must be signed in to change notification settings - Fork 0
/
customized-webpack-plugins.js
44 lines (40 loc) · 1.26 KB
/
customized-webpack-plugins.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
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
class CustomizedCleanWebpackPlugin {
constructor({
vendorJsFileRegex = /vendor.*\.js$/,
licenseCommentRegex = /^\/\*\!.+\.LICENSE\.txt\s*\*\/\s*/,
licenseTxtFilePattern = '**/*.LICENSE.txt',
...rest
} = {}) {
this.licenseCommentRegex = licenseCommentRegex;
this.vendorJsFileRegex = vendorJsFileRegex;
this.licenseTxtFilePattern = licenseTxtFilePattern;
this.restCleanWebpackPluginOptions = rest;
}
apply(compiler) {
new CleanWebpackPlugin({
cleanAfterEveryBuildPatterns: [this.licenseTxtFilePattern],
protectWebpackAssets: false,
...this.restCleanWebpackPluginOptions,
}).apply(compiler);
compiler.hooks.compilation.tap('CustomizedCleanWebpackPlugin', (compilation) => {
compilation.hooks.afterProcessAssets.tap(
'CustomizedCleanWebpackPlugin',
(assets) => {
Object.entries(assets).forEach(([fileName, source]) => {
if (fileName.match(this.vendorJsFileRegex)) {
compilation.updateAsset(
fileName,
new webpack.sources.RawSource(
source.source().replace(this.licenseCommentRegex, ''),
),
);
}
});
},
);
});
}
}
module.exports = { CustomizedCleanWebpackPlugin };