-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsupport5.js
128 lines (107 loc) · 3.37 KB
/
support5.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
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
const ModuleFilenameHelpers = require('webpack/lib/ModuleFilenameHelpers');
function isEmpty(obj) {
return obj === null || obj === undefined;
}
function getFileName(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;
}
function shouldIgnore(mode, minimize) {
// default config of mode => prod
if (mode === 'production' && isEmpty(minimize)) {
return false;
}
// specify minimize
if (!isEmpty(minimize)) {
return false;
}
// ignore for all rest cases
return true;
}
function getBannerPlugin(plugins) {
return (plugins || []).find((p) => p.constructor.name === 'BannerPlugin');
}
const pluginName = 'UnminifiedWebpackPlugin';
class UnminifiedWebpackPlugin {
constructor(opts) {
this.options = opts || {};
this.options.test = this.options.test || /\.(js|css)($|\?)/i;
}
apply(compiler) {
const options = this.options;
if (
shouldIgnore(
compiler.options.mode,
compiler.options.optimization.minimize,
)
) {
return console.log(
'Ignore generating unminified version, since no UglifyJsPlugin provided, or running in development mode',
);
}
const bannerPlugin = getBannerPlugin(compiler.options.plugins);
const outputNormal = {};
compiler.hooks.compilation.tap('UnminifiedWebpackPlugin', (compilation) => {
compilation.hooks.processAssets.tap(
{
name: 'UnminifiedWebpackPlugin',
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_DERIVED,
},
(assets) => {
Object.entries(assets).forEach(([pathname, source]) => {
if (!ModuleFilenameHelpers.matchObject(options, pathname)) {
return;
}
let matchedBanners = [];
if (bannerPlugin) {
matchedBanners = [pathname].filter(
ModuleFilenameHelpers.matchObject.bind(
null,
bannerPlugin.options,
),
);
}
const sourceCode = matchedBanners.length
? bannerPlugin.banner(bannerPlugin.options) + source.source()
: source.source();
const dest = compiler.options.output.path;
const outputPath = path.resolve(
dest,
getFileName(pathname, path.extname(pathname).substr(1), options),
);
outputNormal[outputPath] = {
filename: getFileName(
pathname,
path.extname(pathname).substr(1),
options,
),
content: sourceCode,
size: Buffer.from(sourceCode, 'utf-8').length,
};
});
},
);
compilation.hooks.afterProcessAssets.tap(
'UnminifiedWebpackPlugin',
() => {
for (const [key, value] of Object.entries(outputNormal)) {
compilation.emitAsset(
value.filename,
new webpack.sources.RawSource(value.content),
);
}
},
);
});
}
}
module.exports = UnminifiedWebpackPlugin;