This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 144
/
unminify.js
118 lines (106 loc) · 2.96 KB
/
unminify.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
/**
* Repurposed UnminifiedWebpackPlugin.
*
* See: https://github.com/leftstick/unminified-webpack-plugin/blob/master/index.js
*
* Changes:
* 1. Remove check for UglifyJsPlugin - Terser is the successor.
* 2. Remove check for development mode - we always want unminified files.
* 3. Remove BannerPlugin support - we don't use it.
* 4. Remove the 'min' suffix from the chunk loaded in the new `mainEntry` option.
* 5. Hook into compilation later so we're running after Source Map generation. (https://webpack.js.org/api/compilation-hooks/: PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE)
*/
const path = require( 'path' );
const ModuleFilenameHelpers = require( 'webpack/lib/ModuleFilenameHelpers' );
const webpack = require( 'webpack' );
const 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;
};
class UnminifyWebpackPlugin {
constructor( opts ) {
const options = opts || {};
this.options = {
test: options.test || /\.(js|css)($|\?)/i,
mainEntry: options.mainEntry || false,
};
}
apply( compiler ) {
const options = this.options;
const outputNormal = {};
compiler.hooks.compilation.tap(
'UnminifyWebpackPlugin',
( compilation ) => {
compilation.hooks.processAssets.tap(
{
name: 'UnminifyWebpackPlugin',
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
},
( assets ) => {
Object.entries( assets ).forEach(
( [ pathname, source ] ) => {
if (
! ModuleFilenameHelpers.matchObject(
options,
pathname
)
) {
return;
}
let sourceCode = source.source();
if (
options.mainEntry &&
pathname === options.mainEntry
) {
sourceCode = sourceCode.replace(
/ \+ "\.min\.js"$/m,
' + ".js"'
);
}
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 = UnminifyWebpackPlugin;