@rushstack/webpack4-module-minifier-plugin 0.14.14 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +23 -0
- package/CHANGELOG.md +8 -1
- package/dist/tsdoc-metadata.json +1 -1
- package/lib-esm/AsyncImportCompressionPlugin.js +206 -0
- package/lib-esm/AsyncImportCompressionPlugin.js.map +1 -0
- package/lib-esm/Constants.js +30 -0
- package/lib-esm/Constants.js.map +1 -0
- package/lib-esm/GenerateLicenseFileForAsset.js +52 -0
- package/lib-esm/GenerateLicenseFileForAsset.js.map +1 -0
- package/lib-esm/ModuleMinifierPlugin.js +432 -0
- package/lib-esm/ModuleMinifierPlugin.js.map +1 -0
- package/lib-esm/ModuleMinifierPlugin.types.js +4 -0
- package/lib-esm/ModuleMinifierPlugin.types.js.map +1 -0
- package/lib-esm/OverrideWebpackIdentifierAllocation.js +7 -0
- package/lib-esm/OverrideWebpackIdentifierAllocation.js.map +1 -0
- package/lib-esm/ParallelCompiler.js +90 -0
- package/lib-esm/ParallelCompiler.js.map +1 -0
- package/lib-esm/PortableMinifierIdsPlugin.js +123 -0
- package/lib-esm/PortableMinifierIdsPlugin.js.map +1 -0
- package/lib-esm/RehydrateAsset.js +169 -0
- package/lib-esm/RehydrateAsset.js.map +1 -0
- package/lib-esm/index.js +9 -0
- package/lib-esm/index.js.map +1 -0
- package/lib-esm/workerPool/WebpackWorker.js +78 -0
- package/lib-esm/workerPool/WebpackWorker.js.map +1 -0
- package/package.json +29 -7
- /package/{lib → lib-commonjs}/AsyncImportCompressionPlugin.js +0 -0
- /package/{lib → lib-commonjs}/AsyncImportCompressionPlugin.js.map +0 -0
- /package/{lib → lib-commonjs}/Constants.js +0 -0
- /package/{lib → lib-commonjs}/Constants.js.map +0 -0
- /package/{lib → lib-commonjs}/GenerateLicenseFileForAsset.js +0 -0
- /package/{lib → lib-commonjs}/GenerateLicenseFileForAsset.js.map +0 -0
- /package/{lib → lib-commonjs}/ModuleMinifierPlugin.js +0 -0
- /package/{lib → lib-commonjs}/ModuleMinifierPlugin.js.map +0 -0
- /package/{lib → lib-commonjs}/ModuleMinifierPlugin.types.js +0 -0
- /package/{lib → lib-commonjs}/ModuleMinifierPlugin.types.js.map +0 -0
- /package/{lib → lib-commonjs}/OverrideWebpackIdentifierAllocation.js +0 -0
- /package/{lib → lib-commonjs}/OverrideWebpackIdentifierAllocation.js.map +0 -0
- /package/{lib → lib-commonjs}/ParallelCompiler.js +0 -0
- /package/{lib → lib-commonjs}/ParallelCompiler.js.map +0 -0
- /package/{lib → lib-commonjs}/PortableMinifierIdsPlugin.js +0 -0
- /package/{lib → lib-commonjs}/PortableMinifierIdsPlugin.js.map +0 -0
- /package/{lib → lib-commonjs}/RehydrateAsset.js +0 -0
- /package/{lib → lib-commonjs}/RehydrateAsset.js.map +0 -0
- /package/{lib → lib-commonjs}/index.js +0 -0
- /package/{lib → lib-commonjs}/index.js.map +0 -0
- /package/{lib → lib-commonjs}/workerPool/WebpackWorker.js +0 -0
- /package/{lib → lib-commonjs}/workerPool/WebpackWorker.js.map +0 -0
- /package/{lib → lib-dts}/AsyncImportCompressionPlugin.d.ts +0 -0
- /package/{lib → lib-dts}/AsyncImportCompressionPlugin.d.ts.map +0 -0
- /package/{lib → lib-dts}/Constants.d.ts +0 -0
- /package/{lib → lib-dts}/Constants.d.ts.map +0 -0
- /package/{lib → lib-dts}/GenerateLicenseFileForAsset.d.ts +0 -0
- /package/{lib → lib-dts}/GenerateLicenseFileForAsset.d.ts.map +0 -0
- /package/{lib → lib-dts}/ModuleMinifierPlugin.d.ts +0 -0
- /package/{lib → lib-dts}/ModuleMinifierPlugin.d.ts.map +0 -0
- /package/{lib → lib-dts}/ModuleMinifierPlugin.types.d.ts +0 -0
- /package/{lib → lib-dts}/ModuleMinifierPlugin.types.d.ts.map +0 -0
- /package/{lib → lib-dts}/OverrideWebpackIdentifierAllocation.d.ts +0 -0
- /package/{lib → lib-dts}/OverrideWebpackIdentifierAllocation.d.ts.map +0 -0
- /package/{lib → lib-dts}/ParallelCompiler.d.ts +0 -0
- /package/{lib → lib-dts}/ParallelCompiler.d.ts.map +0 -0
- /package/{lib → lib-dts}/PortableMinifierIdsPlugin.d.ts +0 -0
- /package/{lib → lib-dts}/PortableMinifierIdsPlugin.d.ts.map +0 -0
- /package/{lib → lib-dts}/RehydrateAsset.d.ts +0 -0
- /package/{lib → lib-dts}/RehydrateAsset.d.ts.map +0 -0
- /package/{lib → lib-dts}/index.d.ts +0 -0
- /package/{lib → lib-dts}/index.d.ts.map +0 -0
- /package/{lib → lib-dts}/workerPool/WebpackWorker.d.ts +0 -0
- /package/{lib → lib-dts}/workerPool/WebpackWorker.d.ts.map +0 -0
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { CachedSource, ConcatSource, RawSource, ReplaceSource, SourceMapSource } from 'webpack-sources';
|
|
5
|
+
import * as webpack from 'webpack';
|
|
6
|
+
import { AsyncSeriesWaterfallHook, SyncWaterfallHook } from 'tapable';
|
|
7
|
+
import { getIdentifier } from '@rushstack/module-minifier';
|
|
8
|
+
import { CHUNK_MODULES_TOKEN, MODULE_WRAPPER_PREFIX, MODULE_WRAPPER_SUFFIX, STAGE_BEFORE, STAGE_AFTER } from './Constants';
|
|
9
|
+
import { generateLicenseFileForAsset } from './GenerateLicenseFileForAsset';
|
|
10
|
+
import { rehydrateAsset } from './RehydrateAsset';
|
|
11
|
+
import { AsyncImportCompressionPlugin } from './AsyncImportCompressionPlugin';
|
|
12
|
+
import { PortableMinifierModuleIdsPlugin } from './PortableMinifierIdsPlugin';
|
|
13
|
+
import './OverrideWebpackIdentifierAllocation';
|
|
14
|
+
// The name of the plugin, for use in taps
|
|
15
|
+
const PLUGIN_NAME = 'ModuleMinifierPlugin';
|
|
16
|
+
// Monotonically increasing identifier to be incremented any time the code generation logic changes
|
|
17
|
+
// Will be applied to the webpack hash.
|
|
18
|
+
const CODE_GENERATION_REVISION = 1;
|
|
19
|
+
const TAP_BEFORE = {
|
|
20
|
+
name: PLUGIN_NAME,
|
|
21
|
+
stage: STAGE_BEFORE
|
|
22
|
+
};
|
|
23
|
+
const TAP_AFTER = {
|
|
24
|
+
name: PLUGIN_NAME,
|
|
25
|
+
stage: STAGE_AFTER
|
|
26
|
+
};
|
|
27
|
+
const compilationMetadataMap = new WeakMap();
|
|
28
|
+
/**
|
|
29
|
+
* https://github.com/webpack/webpack/blob/30e747a55d9e796ae22f67445ae42c7a95a6aa48/lib/Template.js#L36-47
|
|
30
|
+
* @param a first id to be sorted
|
|
31
|
+
* @param b second id to be sorted against
|
|
32
|
+
* @returns the sort value
|
|
33
|
+
*/
|
|
34
|
+
function stringifyIdSortPredicate(a, b) {
|
|
35
|
+
const aId = a + '';
|
|
36
|
+
const bId = b + '';
|
|
37
|
+
if (aId < bId)
|
|
38
|
+
return -1;
|
|
39
|
+
if (aId > bId)
|
|
40
|
+
return 1;
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
function hashCodeFragment(code) {
|
|
44
|
+
return createHash('sha256').update(code).digest('hex');
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Base implementation of asset rehydration
|
|
48
|
+
*
|
|
49
|
+
* @param dehydratedAssets The dehydrated assets
|
|
50
|
+
* @param compilation The webpack compilation
|
|
51
|
+
*/
|
|
52
|
+
function defaultRehydrateAssets(dehydratedAssets, compilation) {
|
|
53
|
+
const { assets, modules } = dehydratedAssets;
|
|
54
|
+
const compilationMetadata = compilationMetadataMap.get(compilation);
|
|
55
|
+
if (!compilationMetadata) {
|
|
56
|
+
throw new Error(`Could not get compilation metadata`);
|
|
57
|
+
}
|
|
58
|
+
const { metadataByAssetFileName } = compilationMetadata;
|
|
59
|
+
// Now assets/modules contain fully minified code. Rehydrate.
|
|
60
|
+
for (const [assetName, info] of assets) {
|
|
61
|
+
const banner = /\.m?js(\?.+)?$/.test(assetName)
|
|
62
|
+
? generateLicenseFileForAsset(compilation, info, modules)
|
|
63
|
+
: '';
|
|
64
|
+
const outputSource = rehydrateAsset(info, modules, banner, true);
|
|
65
|
+
metadataByAssetFileName.set(assetName, {
|
|
66
|
+
positionByModuleId: info.renderInfo
|
|
67
|
+
});
|
|
68
|
+
compilation.assets[assetName] = outputSource;
|
|
69
|
+
}
|
|
70
|
+
return dehydratedAssets;
|
|
71
|
+
}
|
|
72
|
+
function isMinificationResultError(result) {
|
|
73
|
+
return !!result.error;
|
|
74
|
+
}
|
|
75
|
+
// Matche behavior of terser's "some" option
|
|
76
|
+
function isLicenseComment(comment) {
|
|
77
|
+
// https://github.com/terser/terser/blob/d3d924fa9e4c57bbe286b811c6068bcc7026e902/lib/output.js#L175
|
|
78
|
+
return /@preserve|@lic|@cc_on|^\**!/i.test(comment.value);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Webpack plugin that minifies code on a per-module basis rather than per-asset. The actual minification is handled by the input `minifier` object.
|
|
82
|
+
* @public
|
|
83
|
+
*/
|
|
84
|
+
export class ModuleMinifierPlugin {
|
|
85
|
+
constructor(options) {
|
|
86
|
+
this.hooks = {
|
|
87
|
+
rehydrateAssets: new AsyncSeriesWaterfallHook(['dehydratedContent', 'compilation']),
|
|
88
|
+
finalModuleId: new SyncWaterfallHook(['id']),
|
|
89
|
+
postProcessCodeFragment: new SyncWaterfallHook(['code', 'context'])
|
|
90
|
+
};
|
|
91
|
+
const { minifier, sourceMap, usePortableModules = false, compressAsyncImports = false } = options;
|
|
92
|
+
this._optionsForHash = {
|
|
93
|
+
...options,
|
|
94
|
+
minifier: undefined,
|
|
95
|
+
revision: CODE_GENERATION_REVISION
|
|
96
|
+
};
|
|
97
|
+
this._enhancers = [];
|
|
98
|
+
if (usePortableModules) {
|
|
99
|
+
this._enhancers.push(new PortableMinifierModuleIdsPlugin(this.hooks));
|
|
100
|
+
}
|
|
101
|
+
if (compressAsyncImports) {
|
|
102
|
+
this._enhancers.push(new AsyncImportCompressionPlugin(this.hooks));
|
|
103
|
+
}
|
|
104
|
+
this.hooks.rehydrateAssets.tap(PLUGIN_NAME, defaultRehydrateAssets);
|
|
105
|
+
this.minifier = minifier;
|
|
106
|
+
this._sourceMap = sourceMap;
|
|
107
|
+
}
|
|
108
|
+
static getCompilationStatistics(compilation) {
|
|
109
|
+
return compilationMetadataMap.get(compilation);
|
|
110
|
+
}
|
|
111
|
+
apply(compiler) {
|
|
112
|
+
for (const enhancer of this._enhancers) {
|
|
113
|
+
enhancer.apply(compiler);
|
|
114
|
+
}
|
|
115
|
+
const { options: { devtool, mode } } = compiler;
|
|
116
|
+
// The explicit setting is preferred due to accuracy, but try to guess based on devtool
|
|
117
|
+
const useSourceMaps = typeof this._sourceMap === 'boolean'
|
|
118
|
+
? this._sourceMap
|
|
119
|
+
: typeof devtool === 'string'
|
|
120
|
+
? devtool.endsWith('source-map')
|
|
121
|
+
: mode === 'production' && devtool !== false;
|
|
122
|
+
this._optionsForHash.sourceMap = useSourceMaps;
|
|
123
|
+
const binaryConfig = Buffer.from(JSON.stringify(this._optionsForHash), 'utf-8');
|
|
124
|
+
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation, compilationData) => {
|
|
125
|
+
const { normalModuleFactory } = compilationData;
|
|
126
|
+
function addCommentExtraction(parser) {
|
|
127
|
+
parser.hooks.program.tap(PLUGIN_NAME, (program, comments) => {
|
|
128
|
+
parser.state.module.factoryMeta.comments = comments.filter(isLicenseComment);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
normalModuleFactory.hooks.parser.for('javascript/auto').tap(PLUGIN_NAME, addCommentExtraction);
|
|
132
|
+
normalModuleFactory.hooks.parser.for('javascript/dynamic').tap(PLUGIN_NAME, addCommentExtraction);
|
|
133
|
+
normalModuleFactory.hooks.parser.for('javascript/esm').tap(PLUGIN_NAME, addCommentExtraction);
|
|
134
|
+
/**
|
|
135
|
+
* Set of local module ids that have been processed.
|
|
136
|
+
*/
|
|
137
|
+
const submittedModules = new Set();
|
|
138
|
+
/**
|
|
139
|
+
* The text and comments of all minified modules.
|
|
140
|
+
*/
|
|
141
|
+
const minifiedModules = new Map();
|
|
142
|
+
/**
|
|
143
|
+
* The text and comments of all minified chunks. Most of these are trivial, but the runtime chunk is a bit larger.
|
|
144
|
+
*/
|
|
145
|
+
const minifiedAssets = new Map();
|
|
146
|
+
const metadataByAssetFileName = new Map();
|
|
147
|
+
const compilationStatistics = {
|
|
148
|
+
metadataByAssetFileName
|
|
149
|
+
};
|
|
150
|
+
compilationMetadataMap.set(compilation, compilationStatistics);
|
|
151
|
+
let pendingMinificationRequests = 0;
|
|
152
|
+
/**
|
|
153
|
+
* Indicates that all files have been sent to the minifier and therefore that when pending hits 0, assets can be rehydrated.
|
|
154
|
+
*/
|
|
155
|
+
let allRequestsIssued = false;
|
|
156
|
+
let resolveMinifyPromise;
|
|
157
|
+
const getRealId = (id) => this.hooks.finalModuleId.call(id, compilation);
|
|
158
|
+
const postProcessCode = (code, context) => this.hooks.postProcessCodeFragment.call(code, context);
|
|
159
|
+
/**
|
|
160
|
+
* Callback to invoke when a file has finished minifying.
|
|
161
|
+
*/
|
|
162
|
+
function onFileMinified() {
|
|
163
|
+
if (--pendingMinificationRequests === 0 && allRequestsIssued) {
|
|
164
|
+
resolveMinifyPromise();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const { minifier } = this;
|
|
168
|
+
let minifierConnection;
|
|
169
|
+
const requestShortener = compilation.runtimeTemplate.requestShortener;
|
|
170
|
+
/**
|
|
171
|
+
* Extracts the code for the module and sends it to be minified.
|
|
172
|
+
* Currently source maps are explicitly not supported.
|
|
173
|
+
* @param {Source} source
|
|
174
|
+
* @param {Module} mod
|
|
175
|
+
*/
|
|
176
|
+
function minifyModule(source, mod) {
|
|
177
|
+
const id = mod.id;
|
|
178
|
+
if (id !== null && !submittedModules.has(id)) {
|
|
179
|
+
// options.chunk contains the current chunk, if needed
|
|
180
|
+
// Render the source, then hash, then persist hash -> module, return a placeholder
|
|
181
|
+
// Initially populate the map with unminified version; replace during callback
|
|
182
|
+
submittedModules.add(id);
|
|
183
|
+
const realId = getRealId(id);
|
|
184
|
+
if (realId !== undefined && !mod.factoryMeta.skipMinification) {
|
|
185
|
+
const wrapped = new ConcatSource(MODULE_WRAPPER_PREFIX + '\n', source, '\n' + MODULE_WRAPPER_SUFFIX);
|
|
186
|
+
const nameForMap = `(modules)/${realId}`;
|
|
187
|
+
const { source: wrappedCode, map } = useSourceMaps
|
|
188
|
+
? wrapped.sourceAndMap()
|
|
189
|
+
: {
|
|
190
|
+
source: wrapped.source(),
|
|
191
|
+
map: undefined
|
|
192
|
+
};
|
|
193
|
+
const hash = hashCodeFragment(wrappedCode);
|
|
194
|
+
++pendingMinificationRequests;
|
|
195
|
+
minifier.minify({
|
|
196
|
+
hash,
|
|
197
|
+
code: wrappedCode,
|
|
198
|
+
nameForMap: useSourceMaps ? nameForMap : undefined,
|
|
199
|
+
externals: undefined
|
|
200
|
+
}, (result) => {
|
|
201
|
+
if (isMinificationResultError(result)) {
|
|
202
|
+
compilation.errors.push(result.error);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
try {
|
|
206
|
+
// Have the source map display the module id instead of the minifier boilerplate
|
|
207
|
+
const sourceForMap = `// ${mod.readableIdentifier(requestShortener)}${wrappedCode.slice(MODULE_WRAPPER_PREFIX.length, -MODULE_WRAPPER_SUFFIX.length)}`;
|
|
208
|
+
const { code: minified, map: minifierMap } = result;
|
|
209
|
+
const rawOutput = useSourceMaps
|
|
210
|
+
? new SourceMapSource(minified, // Code
|
|
211
|
+
nameForMap, // File
|
|
212
|
+
minifierMap, // Base source map
|
|
213
|
+
sourceForMap, // Source from before transform
|
|
214
|
+
map, // Source Map from before transform
|
|
215
|
+
false // Remove original source
|
|
216
|
+
)
|
|
217
|
+
: new RawSource(minified);
|
|
218
|
+
const unwrapped = new ReplaceSource(rawOutput);
|
|
219
|
+
const len = minified.length;
|
|
220
|
+
unwrapped.replace(0, MODULE_WRAPPER_PREFIX.length - 1, '');
|
|
221
|
+
unwrapped.replace(len - MODULE_WRAPPER_SUFFIX.length, len - 1, '');
|
|
222
|
+
const withIds = postProcessCode(unwrapped, {
|
|
223
|
+
compilation,
|
|
224
|
+
module: mod,
|
|
225
|
+
loggingName: mod.identifier()
|
|
226
|
+
});
|
|
227
|
+
const cached = new CachedSource(withIds);
|
|
228
|
+
const minifiedSize = Buffer.byteLength(cached.source(), 'utf-8');
|
|
229
|
+
mod.factoryMeta.minifiedSize = minifiedSize;
|
|
230
|
+
minifiedModules.set(realId, {
|
|
231
|
+
source: cached,
|
|
232
|
+
module: mod
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
catch (err) {
|
|
236
|
+
compilation.errors.push(err);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
onFileMinified();
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
// Route any other modules straight through
|
|
244
|
+
const cached = new CachedSource(postProcessCode(new ReplaceSource(source), {
|
|
245
|
+
compilation,
|
|
246
|
+
module: mod,
|
|
247
|
+
loggingName: mod.identifier()
|
|
248
|
+
}));
|
|
249
|
+
const minifiedSize = Buffer.byteLength(cached.source(), 'utf-8');
|
|
250
|
+
mod.factoryMeta.minifiedSize = minifiedSize;
|
|
251
|
+
minifiedModules.set(realId !== undefined ? realId : id, {
|
|
252
|
+
source: cached,
|
|
253
|
+
module: mod
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
// Return something so that this stage still produces valid ECMAScript
|
|
258
|
+
return new RawSource('(function(){})');
|
|
259
|
+
}
|
|
260
|
+
const jsTemplate = compilation.moduleTemplates
|
|
261
|
+
.javascript;
|
|
262
|
+
const innerRender = jsTemplate.render.bind(jsTemplate);
|
|
263
|
+
// The optimizeTree hook is the last async hook that occurs before chunk rendering
|
|
264
|
+
compilation.hooks.optimizeTree.tapPromise(PLUGIN_NAME, async () => {
|
|
265
|
+
minifierConnection = await minifier.connectAsync();
|
|
266
|
+
submittedModules.clear();
|
|
267
|
+
const cache = new WeakSet();
|
|
268
|
+
const defaultSource = new RawSource('');
|
|
269
|
+
// During code generation, send the generated code to the minifier and replace with a placeholder
|
|
270
|
+
// Hacking this to avoid calling .source() on a concatenated module multiple times
|
|
271
|
+
jsTemplate.render = (module, dependencyTemplates, options) => {
|
|
272
|
+
if (!cache.has(module)) {
|
|
273
|
+
cache.add(module);
|
|
274
|
+
const rendered = innerRender(module, dependencyTemplates, options);
|
|
275
|
+
minifyModule(rendered, module);
|
|
276
|
+
}
|
|
277
|
+
return defaultSource;
|
|
278
|
+
};
|
|
279
|
+
});
|
|
280
|
+
// This should happen before any other tasks that operate during optimizeChunkAssets
|
|
281
|
+
compilation.hooks.optimizeChunkAssets.tapPromise(TAP_BEFORE, async (chunks) => {
|
|
282
|
+
// Still need to minify the rendered assets
|
|
283
|
+
for (const chunk of chunks) {
|
|
284
|
+
const externals = [];
|
|
285
|
+
const externalNames = new Map();
|
|
286
|
+
const chunkModuleSet = new Set();
|
|
287
|
+
const allChunkModules = chunk.modulesIterable;
|
|
288
|
+
let hasNonNumber = false;
|
|
289
|
+
for (const mod of allChunkModules) {
|
|
290
|
+
if (mod.id !== null) {
|
|
291
|
+
if (typeof mod.id !== 'number') {
|
|
292
|
+
hasNonNumber = true;
|
|
293
|
+
}
|
|
294
|
+
chunkModuleSet.add(mod.id);
|
|
295
|
+
if (mod.external) {
|
|
296
|
+
// Match the identifiers generated in the AmdMainTemplatePlugin
|
|
297
|
+
// https://github.com/webpack/webpack/blob/444e59f8a427f94f0064cae6765e5a3c4b78596d/lib/AmdMainTemplatePlugin.js#L49
|
|
298
|
+
const key = `__WEBPACK_EXTERNAL_MODULE_${webpack.Template.toIdentifier(`${mod.id}`)}__`;
|
|
299
|
+
// The first two identifiers are used for function (module, exports) at the module site
|
|
300
|
+
const ordinal = 2 + externals.length;
|
|
301
|
+
const miniId = getIdentifier(ordinal);
|
|
302
|
+
externals.push(key);
|
|
303
|
+
externalNames.set(key, miniId);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const chunkModules = Array.from(chunkModuleSet);
|
|
308
|
+
// Sort by id before rehydration in case we rehydrate a given chunk multiple times
|
|
309
|
+
chunkModules.sort(hasNonNumber
|
|
310
|
+
? stringifyIdSortPredicate
|
|
311
|
+
: (x, y) => x - y);
|
|
312
|
+
for (const assetName of chunk.files) {
|
|
313
|
+
const asset = compilation.assets[assetName];
|
|
314
|
+
// Verify that this is a JS asset
|
|
315
|
+
if (/\.m?js(\?.+)?$/.test(assetName)) {
|
|
316
|
+
++pendingMinificationRequests;
|
|
317
|
+
const rawCode = asset.source();
|
|
318
|
+
const nameForMap = `(chunks)/${assetName}`;
|
|
319
|
+
const hash = hashCodeFragment(rawCode);
|
|
320
|
+
minifier.minify({
|
|
321
|
+
hash,
|
|
322
|
+
code: rawCode,
|
|
323
|
+
nameForMap: useSourceMaps ? nameForMap : undefined,
|
|
324
|
+
externals
|
|
325
|
+
}, (result) => {
|
|
326
|
+
if (isMinificationResultError(result)) {
|
|
327
|
+
compilation.errors.push(result.error);
|
|
328
|
+
// eslint-disable-next-line no-console
|
|
329
|
+
console.error(result.error);
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
try {
|
|
333
|
+
const { code: minified, map: minifierMap } = result;
|
|
334
|
+
let codeForMap = rawCode;
|
|
335
|
+
if (useSourceMaps) {
|
|
336
|
+
// Pretend the __WEBPACK_CHUNK_MODULES__ token is an array of module ids, so that the source map contains information about the module ids in the chunk
|
|
337
|
+
codeForMap = codeForMap.replace(CHUNK_MODULES_TOKEN, JSON.stringify(chunkModules, undefined, 2));
|
|
338
|
+
}
|
|
339
|
+
const rawOutput = useSourceMaps
|
|
340
|
+
? new SourceMapSource(minified, // Code
|
|
341
|
+
nameForMap, // File
|
|
342
|
+
minifierMap, // Base source map
|
|
343
|
+
codeForMap, // Source from before transform
|
|
344
|
+
undefined, // Source Map from before transform
|
|
345
|
+
false // Remove original source
|
|
346
|
+
)
|
|
347
|
+
: new RawSource(minified);
|
|
348
|
+
const withIds = postProcessCode(new ReplaceSource(rawOutput), {
|
|
349
|
+
compilation,
|
|
350
|
+
module: undefined,
|
|
351
|
+
loggingName: assetName
|
|
352
|
+
});
|
|
353
|
+
minifiedAssets.set(assetName, {
|
|
354
|
+
source: new CachedSource(withIds),
|
|
355
|
+
modules: chunkModules,
|
|
356
|
+
chunk,
|
|
357
|
+
fileName: assetName,
|
|
358
|
+
renderInfo: new Map(),
|
|
359
|
+
externalNames
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
catch (err) {
|
|
363
|
+
compilation.errors.push(err);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
onFileMinified();
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
// This isn't a JS asset. Don't try to minify the asset wrapper, though if it contains modules, those might still get replaced with minified versions.
|
|
371
|
+
minifiedAssets.set(assetName, {
|
|
372
|
+
// Still need to restore ids
|
|
373
|
+
source: postProcessCode(new ReplaceSource(asset), {
|
|
374
|
+
compilation,
|
|
375
|
+
module: undefined,
|
|
376
|
+
loggingName: assetName
|
|
377
|
+
}),
|
|
378
|
+
modules: chunkModules,
|
|
379
|
+
chunk,
|
|
380
|
+
fileName: assetName,
|
|
381
|
+
renderInfo: new Map(),
|
|
382
|
+
externalNames
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
allRequestsIssued = true;
|
|
388
|
+
if (pendingMinificationRequests) {
|
|
389
|
+
await new Promise((resolve) => {
|
|
390
|
+
resolveMinifyPromise = resolve;
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
// Handle any error from the minifier.
|
|
394
|
+
await (minifierConnection === null || minifierConnection === void 0 ? void 0 : minifierConnection.disconnectAsync());
|
|
395
|
+
// All assets and modules have been minified, hand them off to be rehydrated
|
|
396
|
+
await this.hooks.rehydrateAssets.promise({
|
|
397
|
+
assets: minifiedAssets,
|
|
398
|
+
modules: minifiedModules
|
|
399
|
+
}, compilation);
|
|
400
|
+
});
|
|
401
|
+
function updateChunkHash(hash, chunk) {
|
|
402
|
+
// Apply the options hash
|
|
403
|
+
hash.update(binaryConfig);
|
|
404
|
+
// Apply the hash from the minifier
|
|
405
|
+
if (minifierConnection) {
|
|
406
|
+
hash.update(minifierConnection.configHash, 'utf8');
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
// Need to update chunk hashes with information from this plugin
|
|
410
|
+
compilation.chunkTemplate.hooks.hashForChunk.tap(PLUGIN_NAME, updateChunkHash);
|
|
411
|
+
compilation.mainTemplate.hooks.hashForChunk.tap(PLUGIN_NAME, updateChunkHash);
|
|
412
|
+
// This function is written twice because the parameter order is not the same between the two hooks
|
|
413
|
+
compilation.chunkTemplate.hooks.modules.tap(TAP_AFTER, (source, chunk, moduleTemplate) => {
|
|
414
|
+
if (moduleTemplate !== compilation.moduleTemplates.javascript) {
|
|
415
|
+
// This is not a JavaScript asset
|
|
416
|
+
return source;
|
|
417
|
+
}
|
|
418
|
+
// Discard the rendered modules
|
|
419
|
+
return new RawSource(CHUNK_MODULES_TOKEN);
|
|
420
|
+
});
|
|
421
|
+
compilation.mainTemplate.hooks.modules.tap(TAP_AFTER, (source, chunk, hash, moduleTemplate) => {
|
|
422
|
+
if (moduleTemplate !== compilation.moduleTemplates.javascript) {
|
|
423
|
+
// This is not a JavaScript asset
|
|
424
|
+
return source;
|
|
425
|
+
}
|
|
426
|
+
// Discard the rendered modules
|
|
427
|
+
return new RawSource(CHUNK_MODULES_TOKEN);
|
|
428
|
+
});
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
//# sourceMappingURL=ModuleMinifierP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CAAC;YAC5C,CAAC,CACF,CAAC;YAED,WAAW,CAAC,YAAkD,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAC/E,SAAS,EACT,CAAC,MAAc,EAAE,KAAgC,EAAE,IAAa,EAAE,cAAuB,EAAE,EAAE;gBAC3F,IAAI,cAAc,KAAK,WAAW,CAAC,eAAe,CAAC,UAAU,EAAE,CAAC;oBAC9D,iCAAiC;oBACjC,OAAO,MAAM,CAAC;gBAChB,CAAC;gBAED,+BAA+B;gBAC/B,OAAO,IAAI,SAAS,CAAC,mBAAmB,CAAC,CAAC;YAC5C,CAAC,CACF,CAAC;QACJ,CAAC,CACF,CAAC;IACJ,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { createHash, type Hash } from 'node:crypto';\n\nimport {\n CachedSource,\n ConcatSource,\n RawSource,\n ReplaceSource,\n type Source,\n SourceMapSource\n} from 'webpack-sources';\nimport * as webpack from 'webpack';\nimport { AsyncSeriesWaterfallHook, type SyncHook, SyncWaterfallHook, type TapOptions } from 'tapable';\n\nimport type {\n IMinifierConnection,\n IModuleMinifier,\n IModuleMinificationResult,\n IModuleMinificationErrorResult\n} from '@rushstack/module-minifier';\nimport { getIdentifier } from '@rushstack/module-minifier';\n\nimport {\n CHUNK_MODULES_TOKEN,\n MODULE_WRAPPER_PREFIX,\n MODULE_WRAPPER_SUFFIX,\n STAGE_BEFORE,\n STAGE_AFTER\n} from './Constants';\nimport type {\n IModuleMinifierPluginOptions,\n IModuleMap,\n IAssetMap,\n IExtendedModule,\n IModuleMinifierPluginHooks,\n IPostProcessFragmentContext,\n IDehydratedAssets,\n _IWebpackCompilationData,\n _IAcornComment,\n IModuleMinifierPluginStats,\n IAssetStats\n} from './ModuleMinifierPlugin.types';\nimport { generateLicenseFileForAsset } from './GenerateLicenseFileForAsset';\nimport { rehydrateAsset } from './RehydrateAsset';\nimport { AsyncImportCompressionPlugin } from './AsyncImportCompressionPlugin';\nimport { PortableMinifierModuleIdsPlugin } from './PortableMinifierIdsPlugin';\n\nimport './OverrideWebpackIdentifierAllocation';\n\n// The name of the plugin, for use in taps\nconst PLUGIN_NAME: 'ModuleMinifierPlugin' = 'ModuleMinifierPlugin';\n\n// Monotonically increasing identifier to be incremented any time the code generation logic changes\n// Will be applied to the webpack hash.\nconst CODE_GENERATION_REVISION: number = 1;\n\nconst TAP_BEFORE: TapOptions<'promise'> = {\n name: PLUGIN_NAME,\n stage: STAGE_BEFORE\n};\nconst TAP_AFTER: TapOptions<'sync'> = {\n name: PLUGIN_NAME,\n stage: STAGE_AFTER\n};\n\ninterface IExtendedChunkTemplate {\n hooks: {\n hashForChunk: SyncHook<Hash, webpack.compilation.Chunk>;\n modules: SyncWaterfallHook<Source, webpack.compilation.Chunk>;\n };\n}\n\ninterface IExtendedParser extends webpack.compilation.normalModuleFactory.Parser {\n state: {\n module: IExtendedModule;\n };\n}\n\ninterface IExtendedModuleTemplate extends webpack.compilation.ModuleTemplate {\n render: (module: IExtendedModule, dependencyTemplates: unknown, options: unknown) => Source;\n}\n\ninterface IOptionsForHash extends Omit<IModuleMinifierPluginOptions, 'minifier'> {\n revision: number;\n minifier: undefined;\n}\n\nconst compilationMetadataMap: WeakMap<webpack.compilation.Compilation, IModuleMinifierPluginStats> =\n new WeakMap();\n\n/**\n * https://github.com/webpack/webpack/blob/30e747a55d9e796ae22f67445ae42c7a95a6aa48/lib/Template.js#L36-47\n * @param a first id to be sorted\n * @param b second id to be sorted against\n * @returns the sort value\n */\nfunction stringifyIdSortPredicate(a: string | number, b: string | number): -1 | 0 | 1 {\n const aId: string = a + '';\n const bId: string = b + '';\n if (aId < bId) return -1;\n if (aId > bId) return 1;\n return 0;\n}\n\nfunction hashCodeFragment(code: string): string {\n return createHash('sha256').update(code).digest('hex');\n}\n\n/**\n * Base implementation of asset rehydration\n *\n * @param dehydratedAssets The dehydrated assets\n * @param compilation The webpack compilation\n */\nfunction defaultRehydrateAssets(\n dehydratedAssets: IDehydratedAssets,\n compilation: webpack.compilation.Compilation\n): IDehydratedAssets {\n const { assets, modules } = dehydratedAssets;\n\n const compilationMetadata: IModuleMinifierPluginStats | undefined = compilationMetadataMap.get(compilation);\n if (!compilationMetadata) {\n throw new Error(`Could not get compilation metadata`);\n }\n\n const { metadataByAssetFileName } = compilationMetadata;\n\n // Now assets/modules contain fully minified code. Rehydrate.\n for (const [assetName, info] of assets) {\n const banner: string = /\\.m?js(\\?.+)?$/.test(assetName)\n ? generateLicenseFileForAsset(compilation, info, modules)\n : '';\n\n const outputSource: Source = rehydrateAsset(info, modules, banner, true);\n metadataByAssetFileName.set(assetName, {\n positionByModuleId: info.renderInfo\n });\n compilation.assets[assetName] = outputSource;\n }\n\n return dehydratedAssets;\n}\n\nfunction isMinificationResultError(\n result: IModuleMinificationResult\n): result is IModuleMinificationErrorResult {\n return !!result.error;\n}\n\n// Matche behavior of terser's \"some\" option\nfunction isLicenseComment(comment: _IAcornComment): boolean {\n // https://github.com/terser/terser/blob/d3d924fa9e4c57bbe286b811c6068bcc7026e902/lib/output.js#L175\n return /@preserve|@lic|@cc_on|^\\**!/i.test(comment.value);\n}\n\n/**\n * Webpack plugin that minifies code on a per-module basis rather than per-asset. The actual minification is handled by the input `minifier` object.\n * @public\n */\nexport class ModuleMinifierPlugin implements webpack.Plugin {\n public readonly hooks: IModuleMinifierPluginHooks;\n public minifier: IModuleMinifier;\n\n private readonly _enhancers: webpack.Plugin[];\n private readonly _sourceMap: boolean | undefined;\n\n private readonly _optionsForHash: IOptionsForHash;\n\n public constructor(options: IModuleMinifierPluginOptions) {\n this.hooks = {\n rehydrateAssets: new AsyncSeriesWaterfallHook(['dehydratedContent', 'compilation']),\n\n finalModuleId: new SyncWaterfallHook(['id']),\n\n postProcessCodeFragment: new SyncWaterfallHook(['code', 'context'])\n };\n\n const { minifier, sourceMap, usePortableModules = false, compressAsyncImports = false } = options;\n\n this._optionsForHash = {\n ...options,\n minifier: undefined,\n revision: CODE_GENERATION_REVISION\n };\n\n this._enhancers = [];\n\n if (usePortableModules) {\n this._enhancers.push(new PortableMinifierModuleIdsPlugin(this.hooks));\n }\n\n if (compressAsyncImports) {\n this._enhancers.push(new AsyncImportCompressionPlugin(this.hooks));\n }\n\n this.hooks.rehydrateAssets.tap(PLUGIN_NAME, defaultRehydrateAssets);\n this.minifier = minifier;\n\n this._sourceMap = sourceMap;\n }\n\n public static getCompilationStatistics(\n compilation: webpack.compilation.Compilation\n ): IModuleMinifierPluginStats | undefined {\n return compilationMetadataMap.get(compilation);\n }\n\n public apply(compiler: webpack.Compiler): void {\n for (const enhancer of this._enhancers) {\n enhancer.apply(compiler);\n }\n\n const {\n options: { devtool, mode }\n } = compiler;\n // The explicit setting is preferred due to accuracy, but try to guess based on devtool\n const useSourceMaps: boolean =\n typeof this._sourceMap === 'boolean'\n ? this._sourceMap\n : typeof devtool === 'string'\n ? devtool.endsWith('source-map')\n : mode === 'production' && devtool !== false;\n\n this._optionsForHash.sourceMap = useSourceMaps;\n const binaryConfig: Uint8Array = Buffer.from(JSON.stringify(this._optionsForHash), 'utf-8');\n\n compiler.hooks.thisCompilation.tap(\n PLUGIN_NAME,\n (compilation: webpack.compilation.Compilation, compilationData: _IWebpackCompilationData) => {\n const { normalModuleFactory } = compilationData;\n\n function addCommentExtraction(parser: webpack.compilation.normalModuleFactory.Parser): void {\n parser.hooks.program.tap(PLUGIN_NAME, (program: unknown, comments: _IAcornComment[]) => {\n (parser as IExtendedParser).state.module.factoryMeta.comments = comments.filter(isLicenseComment);\n });\n }\n\n normalModuleFactory.hooks.parser.for('javascript/auto').tap(PLUGIN_NAME, addCommentExtraction);\n normalModuleFactory.hooks.parser.for('javascript/dynamic').tap(PLUGIN_NAME, addCommentExtraction);\n normalModuleFactory.hooks.parser.for('javascript/esm').tap(PLUGIN_NAME, addCommentExtraction);\n\n /**\n * Set of local module ids that have been processed.\n */\n const submittedModules: Set<string | number> = new Set();\n\n /**\n * The text and comments of all minified modules.\n */\n const minifiedModules: IModuleMap = new Map();\n\n /**\n * The text and comments of all minified chunks. Most of these are trivial, but the runtime chunk is a bit larger.\n */\n const minifiedAssets: IAssetMap = new Map();\n\n const metadataByAssetFileName: Map<string, IAssetStats> = new Map();\n const compilationStatistics: IModuleMinifierPluginStats = {\n metadataByAssetFileName\n };\n compilationMetadataMap.set(compilation, compilationStatistics);\n\n let pendingMinificationRequests: number = 0;\n /**\n * Indicates that all files have been sent to the minifier and therefore that when pending hits 0, assets can be rehydrated.\n */\n let allRequestsIssued: boolean = false;\n\n let resolveMinifyPromise: () => void;\n\n const getRealId: (id: number | string) => number | string | undefined = (id: number | string) =>\n this.hooks.finalModuleId.call(id, compilation);\n\n const postProcessCode: (\n code: ReplaceSource,\n context: IPostProcessFragmentContext\n ) => ReplaceSource = (code: ReplaceSource, context: IPostProcessFragmentContext) =>\n this.hooks.postProcessCodeFragment.call(code, context);\n\n /**\n * Callback to invoke when a file has finished minifying.\n */\n function onFileMinified(): void {\n if (--pendingMinificationRequests === 0 && allRequestsIssued) {\n resolveMinifyPromise();\n }\n }\n\n const { minifier } = this;\n\n let minifierConnection: IMinifierConnection | undefined;\n\n const requestShortener: webpack.compilation.RequestShortener =\n compilation.runtimeTemplate.requestShortener;\n\n /**\n * Extracts the code for the module and sends it to be minified.\n * Currently source maps are explicitly not supported.\n * @param {Source} source\n * @param {Module} mod\n */\n function minifyModule(source: Source, mod: IExtendedModule): Source {\n const id: string | number | null = mod.id;\n\n if (id !== null && !submittedModules.has(id)) {\n // options.chunk contains the current chunk, if needed\n // Render the source, then hash, then persist hash -> module, return a placeholder\n\n // Initially populate the map with unminified version; replace during callback\n submittedModules.add(id);\n\n const realId: string | number | undefined = getRealId(id);\n\n if (realId !== undefined && !mod.factoryMeta.skipMinification) {\n const wrapped: ConcatSource = new ConcatSource(\n MODULE_WRAPPER_PREFIX + '\\n',\n source,\n '\\n' + MODULE_WRAPPER_SUFFIX\n );\n\n const nameForMap: string = `(modules)/${realId}`;\n\n const { source: wrappedCode, map } = useSourceMaps\n ? wrapped.sourceAndMap()\n : {\n source: wrapped.source(),\n map: undefined\n };\n\n const hash: string = hashCodeFragment(wrappedCode);\n\n ++pendingMinificationRequests;\n\n minifier.minify(\n {\n hash,\n code: wrappedCode,\n nameForMap: useSourceMaps ? nameForMap : undefined,\n externals: undefined\n },\n (result: IModuleMinificationResult) => {\n if (isMinificationResultError(result)) {\n compilation.errors.push(result.error);\n } else {\n try {\n // Have the source map display the module id instead of the minifier boilerplate\n const sourceForMap: string = `// ${mod.readableIdentifier(\n requestShortener\n )}${wrappedCode.slice(MODULE_WRAPPER_PREFIX.length, -MODULE_WRAPPER_SUFFIX.length)}`;\n\n const { code: minified, map: minifierMap } = result;\n\n const rawOutput: Source = useSourceMaps\n ? new SourceMapSource(\n minified, // Code\n nameForMap, // File\n minifierMap!, // Base source map\n sourceForMap, // Source from before transform\n map!, // Source Map from before transform\n false // Remove original source\n )\n : new RawSource(minified);\n\n const unwrapped: ReplaceSource = new ReplaceSource(rawOutput);\n const len: number = minified.length;\n\n unwrapped.replace(0, MODULE_WRAPPER_PREFIX.length - 1, '');\n unwrapped.replace(len - MODULE_WRAPPER_SUFFIX.length, len - 1, '');\n\n const withIds: Source = postProcessCode(unwrapped, {\n compilation,\n module: mod,\n loggingName: mod.identifier()\n });\n const cached: CachedSource = new CachedSource(withIds);\n\n const minifiedSize: number = Buffer.byteLength(cached.source(), 'utf-8');\n mod.factoryMeta.minifiedSize = minifiedSize;\n\n minifiedModules.set(realId, {\n source: cached,\n module: mod\n });\n } catch (err) {\n compilation.errors.push(err);\n }\n }\n\n onFileMinified();\n }\n );\n } else {\n // Route any other modules straight through\n const cached: CachedSource = new CachedSource(\n postProcessCode(new ReplaceSource(source), {\n compilation,\n module: mod,\n loggingName: mod.identifier()\n })\n );\n\n const minifiedSize: number = Buffer.byteLength(cached.source(), 'utf-8');\n mod.factoryMeta.minifiedSize = minifiedSize;\n\n minifiedModules.set(realId !== undefined ? realId : id, {\n source: cached,\n module: mod\n });\n }\n }\n\n // Return something so that this stage still produces valid ECMAScript\n return new RawSource('(function(){})');\n }\n\n const jsTemplate: IExtendedModuleTemplate = compilation.moduleTemplates\n .javascript as IExtendedModuleTemplate;\n const innerRender: IExtendedModuleTemplate['render'] = jsTemplate.render.bind(jsTemplate);\n\n // The optimizeTree hook is the last async hook that occurs before chunk rendering\n compilation.hooks.optimizeTree.tapPromise(PLUGIN_NAME, async () => {\n minifierConnection = await minifier.connectAsync();\n\n submittedModules.clear();\n\n const cache: WeakSet<IExtendedModule> = new WeakSet();\n const defaultSource: Source = new RawSource('');\n\n // During code generation, send the generated code to the minifier and replace with a placeholder\n // Hacking this to avoid calling .source() on a concatenated module multiple times\n jsTemplate.render = (module: IExtendedModule, dependencyTemplates, options) => {\n if (!cache.has(module)) {\n cache.add(module);\n const rendered: Source = innerRender(module, dependencyTemplates, options);\n\n minifyModule(rendered, module);\n }\n\n return defaultSource;\n };\n });\n\n // This should happen before any other tasks that operate during optimizeChunkAssets\n compilation.hooks.optimizeChunkAssets.tapPromise(\n TAP_BEFORE,\n async (chunks: webpack.compilation.Chunk[]): Promise<void> => {\n // Still need to minify the rendered assets\n for (const chunk of chunks) {\n const externals: string[] = [];\n const externalNames: Map<string, string> = new Map();\n\n const chunkModuleSet: Set<string | number> = new Set();\n const allChunkModules: Iterable<IExtendedModule> =\n chunk.modulesIterable as Iterable<IExtendedModule>;\n let hasNonNumber: boolean = false;\n for (const mod of allChunkModules) {\n if (mod.id !== null) {\n if (typeof mod.id !== 'number') {\n hasNonNumber = true;\n }\n chunkModuleSet.add(mod.id);\n\n if (mod.external) {\n // Match the identifiers generated in the AmdMainTemplatePlugin\n // https://github.com/webpack/webpack/blob/444e59f8a427f94f0064cae6765e5a3c4b78596d/lib/AmdMainTemplatePlugin.js#L49\n const key: string = `__WEBPACK_EXTERNAL_MODULE_${webpack.Template.toIdentifier(\n `${mod.id}`\n )}__`;\n // The first two identifiers are used for function (module, exports) at the module site\n const ordinal: number = 2 + externals.length;\n const miniId: string = getIdentifier(ordinal);\n externals.push(key);\n externalNames.set(key, miniId);\n }\n }\n }\n\n const chunkModules: (string | number)[] = Array.from(chunkModuleSet);\n // Sort by id before rehydration in case we rehydrate a given chunk multiple times\n chunkModules.sort(\n hasNonNumber\n ? stringifyIdSortPredicate\n : (x: string | number, y: string | number) => (x as number) - (y as number)\n );\n\n for (const assetName of chunk.files) {\n const asset: Source = compilation.assets[assetName];\n\n // Verify that this is a JS asset\n if (/\\.m?js(\\?.+)?$/.test(assetName)) {\n ++pendingMinificationRequests;\n\n const rawCode: string = asset.source() as string;\n const nameForMap: string = `(chunks)/${assetName}`;\n\n const hash: string = hashCodeFragment(rawCode);\n\n minifier.minify(\n {\n hash,\n code: rawCode,\n nameForMap: useSourceMaps ? nameForMap : undefined,\n externals\n },\n (result: IModuleMinificationResult) => {\n if (isMinificationResultError(result)) {\n compilation.errors.push(result.error);\n // eslint-disable-next-line no-console\n console.error(result.error);\n } else {\n try {\n const { code: minified, map: minifierMap } = result;\n\n let codeForMap: string = rawCode;\n if (useSourceMaps) {\n // Pretend the __WEBPACK_CHUNK_MODULES__ token is an array of module ids, so that the source map contains information about the module ids in the chunk\n codeForMap = codeForMap.replace(\n CHUNK_MODULES_TOKEN,\n JSON.stringify(chunkModules, undefined, 2)\n );\n }\n\n const rawOutput: Source = useSourceMaps\n ? new SourceMapSource(\n minified, // Code\n nameForMap, // File\n minifierMap!, // Base source map\n codeForMap, // Source from before transform\n undefined, // Source Map from before transform\n false // Remove original source\n )\n : new RawSource(minified);\n\n const withIds: Source = postProcessCode(new ReplaceSource(rawOutput), {\n compilation,\n module: undefined,\n loggingName: assetName\n });\n\n minifiedAssets.set(assetName, {\n source: new CachedSource(withIds),\n modules: chunkModules,\n chunk,\n fileName: assetName,\n renderInfo: new Map(),\n externalNames\n });\n } catch (err) {\n compilation.errors.push(err);\n }\n }\n\n onFileMinified();\n }\n );\n } else {\n // This isn't a JS asset. Don't try to minify the asset wrapper, though if it contains modules, those might still get replaced with minified versions.\n minifiedAssets.set(assetName, {\n // Still need to restore ids\n source: postProcessCode(new ReplaceSource(asset), {\n compilation,\n module: undefined,\n loggingName: assetName\n }),\n modules: chunkModules,\n chunk,\n fileName: assetName,\n renderInfo: new Map(),\n externalNames\n });\n }\n }\n }\n\n allRequestsIssued = true;\n\n if (pendingMinificationRequests) {\n await new Promise<void>((resolve) => {\n resolveMinifyPromise = resolve;\n });\n }\n\n // Handle any error from the minifier.\n await minifierConnection?.disconnectAsync();\n\n // All assets and modules have been minified, hand them off to be rehydrated\n await this.hooks.rehydrateAssets.promise(\n {\n assets: minifiedAssets,\n modules: minifiedModules\n },\n compilation\n );\n }\n );\n\n function updateChunkHash(hash: Hash, chunk: webpack.compilation.Chunk): void {\n // Apply the options hash\n hash.update(binaryConfig);\n // Apply the hash from the minifier\n if (minifierConnection) {\n hash.update(minifierConnection.configHash, 'utf8');\n }\n }\n\n // Need to update chunk hashes with information from this plugin\n (compilation.chunkTemplate as unknown as IExtendedChunkTemplate).hooks.hashForChunk.tap(\n PLUGIN_NAME,\n updateChunkHash\n );\n compilation.mainTemplate.hooks.hashForChunk.tap(PLUGIN_NAME, updateChunkHash);\n\n // This function is written twice because the parameter order is not the same between the two hooks\n (compilation.chunkTemplate as unknown as IExtendedChunkTemplate).hooks.modules.tap(\n TAP_AFTER,\n (source: Source, chunk: webpack.compilation.Chunk, moduleTemplate: unknown) => {\n if (moduleTemplate !== compilation.moduleTemplates.javascript) {\n // This is not a JavaScript asset\n return source;\n }\n\n // Discard the rendered modules\n return new RawSource(CHUNK_MODULES_TOKEN);\n }\n );\n\n (compilation.mainTemplate as unknown as IExtendedChunkTemplate).hooks.modules.tap(\n TAP_AFTER,\n (source: Source, chunk: webpack.compilation.Chunk, hash: unknown, moduleTemplate: unknown) => {\n if (moduleTemplate !== compilation.moduleTemplates.javascript) {\n // This is not a JavaScript asset\n return source;\n }\n\n // Discard the rendered modules\n return new RawSource(CHUNK_MODULES_TOKEN);\n }\n );\n }\n );\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ModuleMinifierPlugin.types.js","sourceRoot":"","sources":["../src/ModuleMinifierPlugin.types.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type { AsyncSeriesWaterfallHook, SyncWaterfallHook } from 'tapable';\nimport type * as webpack from 'webpack';\nimport type { ReplaceSource, Source } from 'webpack-sources';\n\nimport type { IModuleMinifier } from '@rushstack/module-minifier';\n\n/**\n * Information about where the module was rendered in the emitted asset.\n * @public\n */\nexport interface IRenderedModulePosition {\n /**\n * The offset from the start of tha asset to the start of the module, in characters.\n */\n charOffset: number;\n /**\n * The length of the rendered module, in characters.\n */\n charLength: number;\n}\n\n/**\n * Information about a dehydrated webpack ECMAScript asset\n * @public\n */\nexport interface IAssetInfo {\n /**\n * The (minified) boilerplate code for the asset. Will contain a token to be replaced by the minified modules.\n */\n source: Source;\n\n /**\n * The name of the asset, used to index into compilation.assets\n */\n fileName: string;\n\n /**\n * The ids of the modules that are part of the chunk corresponding to this asset\n */\n modules: (string | number)[];\n\n /**\n * Information about the offsets and character lengths for each rendered module in the final asset.\n */\n renderInfo: Map<string | number, IRenderedModulePosition>;\n\n /**\n * The raw chunk object from Webpack, in case information from it is necessary for reconstruction\n */\n chunk: webpack.compilation.Chunk;\n\n /**\n * The set of external names to postprocess\n */\n externalNames: Map<string, string>;\n}\n\n/**\n * Statistics from the plugin. Namely module sizes.\n * @public\n */\nexport interface IModuleMinifierPluginStats {\n metadataByAssetFileName: Map<string, IAssetStats>;\n}\n\n/**\n * Rendered positional data\n * @public\n */\nexport interface IAssetStats {\n positionByModuleId: Map<string | number, IRenderedModulePosition>;\n}\n\n/**\n * Information about a minified module\n * @public\n */\nexport interface IModuleInfo {\n /**\n * The (minified) code of this module. Will be a function expression.\n */\n source: Source;\n\n /**\n * The raw module object from Webpack, in case information from it is necessary for reconstruction\n */\n module: IExtendedModule;\n}\n\n/**\n * Extension of the webpack Module typings with members that are used by this Plugin\n * @public\n */\nexport interface IExtendedModule extends webpack.compilation.Module {\n /**\n * Is this module external?\n */\n external?: boolean;\n /**\n * Concatenated modules\n */\n modules?: IExtendedModule[];\n /**\n * Recursively scan the dependencies of a module\n */\n hasDependencies(callback: (dep: webpack.compilation.Dependency) => boolean | void): boolean;\n /**\n * Id for the module\n */\n // eslint-disable-next-line @rushstack/no-new-null\n id: string | number | null;\n /**\n * Gets a descriptive identifier for the module.\n */\n identifier(): string;\n /**\n * Gets a friendly identifier for the module.\n */\n readableIdentifier(requestShortener: unknown): string;\n /**\n * Path to the physical file this module represents\n */\n resource?: string;\n}\n\ndeclare module 'webpack' {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace compilation {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n interface RuntimeTemplate {\n requestShortener: webpack.compilation.RequestShortener;\n }\n\n // eslint-disable-next-line @typescript-eslint/naming-convention\n interface RequestShortener {}\n }\n}\n\n/**\n * This is the second parameter to the thisCompilation and compilation webpack.Compiler hooks.\n * @internal\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface _IWebpackCompilationData {\n normalModuleFactory: webpack.compilation.NormalModuleFactory;\n}\n\n/**\n * This is the second parameter to the NormalModuleFactory `module` hook\n * @internal\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface _INormalModuleFactoryModuleData {\n resourceResolveData?: {\n /**\n * Contents of the description file (package.json) for the module\n */\n descriptionFileData?: {\n /**\n * The name of the package\n */\n name: string;\n };\n /**\n * Absolute path of the description file (package.json) for the module\n */\n descriptionFilePath?: string;\n /**\n * Absolute path of the directory containing the description file (package.json) for the module\n */\n descriptionFileRoot?: string;\n /**\n * Relative path from the description file (package.json) to the module\n */\n relativePath?: string;\n };\n}\n\n/**\n * A map from file names to dehydrated assets\n * @public\n */\nexport type IAssetMap = Map<string, IAssetInfo>;\n/**\n * A map from module ids to minified modules\n * @public\n */\nexport type IModuleMap = Map<string | number, IModuleInfo>;\n\n/**\n * Options to the ModuleMinifierPlugin constructor\n * @public\n */\nexport interface IModuleMinifierPluginOptions {\n /**\n * Minifier implementation to use. Required.\n */\n minifier: IModuleMinifier;\n\n /**\n * Whether to enable source map processing. If not provided, will attempt to guess based on `mode` and `devtool` in the webpack config.\n * Set to `false` for faster builds at the expense of debuggability.\n */\n sourceMap?: boolean;\n\n /**\n * Instructs the plugin to alter the code of modules to maximize portability across compilations.\n */\n usePortableModules?: boolean;\n\n /**\n * Instructs the plugin to alter the code of async import statements to compress better and be portable across compilations.\n */\n compressAsyncImports?: boolean;\n}\n\n/**\n * The set of data remaining to rehydrate in the current compilation\n * @public\n */\nexport interface IDehydratedAssets {\n /**\n * The set of remaining assets to rehydrate. Each tap may remove some or all assets from this collection\n */\n assets: IAssetMap;\n\n /**\n * The set of modules to use for rehydrating assets.\n */\n modules: IModuleMap;\n}\n\n/**\n * Argument to the postProcessCodeFragment hook for the current execution context\n * @public\n */\nexport interface IPostProcessFragmentContext {\n /**\n * The current webpack compilation, for error reporting\n */\n compilation: webpack.compilation.Compilation;\n /**\n * A name to use for logging\n */\n loggingName: string;\n /**\n * The current module being processed, or `undefined` if not in a module (e.g. the bootstrapper)\n */\n module: webpack.compilation.Module | undefined;\n}\n\n/**\n * Hooks provided by the ModuleMinifierPlugin\n * @public\n */\nexport interface IModuleMinifierPluginHooks {\n /**\n * Hook invoked at the start of optimizeChunkAssets to rehydrate the minified boilerplate and runtime into chunk assets.\n */\n rehydrateAssets: AsyncSeriesWaterfallHook<IDehydratedAssets, webpack.compilation.Compilation>;\n\n /**\n * Hook invoked on a module id to get the final rendered id.\n */\n finalModuleId: SyncWaterfallHook<string | number | undefined, webpack.compilation.Compilation>;\n\n /**\n * Hook invoked on code after it has been returned from the minifier.\n */\n postProcessCodeFragment: SyncWaterfallHook<ReplaceSource, IPostProcessFragmentContext>;\n}\n\n/**\n * The comment objects from the Acorn parser inside of webpack\n * @internal\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface _IAcornComment {\n type: 'Line' | 'Block';\n value: string;\n start: number;\n end: number;\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
import { Template } from 'webpack';
|
|
4
|
+
import { getIdentifier } from '@rushstack/module-minifier';
|
|
5
|
+
// Configure webpack to use the same identifier allocation logic as Terser to maximize gzip compressibility
|
|
6
|
+
Template.numberToIdentifer = getIdentifier;
|
|
7
|
+
//# sourceMappingURL=OverrideWebpackIdentifierAllocation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OverrideWebpackIdentifierAllocation.js","sourceRoot":"","sources":["../src/OverrideWebpackIdentifierAllocation.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAE3D,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,2GAA2G;AAC3G,QAAQ,CAAC,iBAAiB,GAAG,aAAa,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { Template } from 'webpack';\n\nimport { getIdentifier } from '@rushstack/module-minifier';\n\n// Configure webpack to use the same identifier allocation logic as Terser to maximize gzip compressibility\nTemplate.numberToIdentifer = getIdentifier;\n"]}
|