-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlegacy.js
75 lines (60 loc) · 2.68 KB
/
legacy.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
const webpack = require('webpack');
const path = require('path');
const ModuleFilenameHelpers = require('webpack/lib/ModuleFilenameHelpers');
const getFileName = function(name, ext, opts) {
if (name.match(/([-_.]min)[-_.]/)) {
return name.replace(/[-_.]min/, '');
}
const suffix = (opts.postfix || 'nomin') + '.' + ext;
if (name.match(new RegExp('\.' + ext + '$'))) {
return name.replace(new RegExp(ext + '$'), suffix)
}
return name + suffix;
};
const UnminifiedWebpackPlugin = function(opts) {
this.options = opts || {};
};
UnminifiedWebpackPlugin.prototype.apply = function(compiler) {
const options = this.options;
options.test = options.test || /\.(js|css)($|\?)/i;
const containUgly = compiler.options.plugins.filter(function(plugin) {
return plugin.constructor.name === 'UglifyJsPlugin';
});
if (!containUgly.length && compiler.options.mode === 'development') {
return console.log('Ignore generating unminified version, since no UglifyJsPlugin provided, or running in development mode');
}
compiler.hooks.compilation.tap('UnminifiedWebpackPlugin', function(compilation) {
compilation.hooks.additionalAssets.tap('UnminifiedWebpackPlugin', function() {
const files = [];
compilation.chunks.forEach(function(chunk) {
chunk.files.forEach(function(file) {
files.push(file);
});
});
compilation.additionalChunkAssets.forEach(function(file) {
files.push(file);
});
const finalFiles = files.filter(ModuleFilenameHelpers.matchObject.bind(null, options));
const bannerPlugin = compiler.options.plugins.find(function(plugin) {
return plugin.constructor.name === 'BannerPlugin';
});
finalFiles.forEach(function(file) {
const asset = compilation.assets[file];
let matchedBanners = [];
if (bannerPlugin) {
matchedBanners = [file].filter(ModuleFilenameHelpers.matchObject.bind(null, bannerPlugin.options));
}
const source = matchedBanners.length ? bannerPlugin.banner(bannerPlugin.options) + asset.source() : asset.source();
compilation.assets[getFileName(file, path.extname(file).substr(1), options)] = {
source: function() {
return source;
},
size: function() {
return source.length;
}
};
});
});
});
};
module.exports = UnminifiedWebpackPlugin;