@rushstack/webpack4-module-minifier-plugin 0.12.3 → 0.12.4

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.
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.34.6"
8
+ "packageVersion": "7.34.7"
9
9
  }
10
10
  ]
11
11
  }
@@ -1 +1 @@
1
- {"version":3,"file":"AsyncImportCompressionPlugin.d.ts","sourceRoot":"","sources":["../src/AsyncImportCompressionPlugin.ts"],"names":[],"mappings":"AAGA,OAAgB,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAOpD,OAAO,EAEL,0BAA0B,EAE3B,MAAM,8BAA8B,CAAC;AA2EtC;;;;;GAKG;AACH,qBAAa,4BAA6B,YAAW,MAAM;IACzD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA6B;gBAEzC,aAAa,EAAE,0BAA0B;IAIrD,KAAK,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;CAoLvC"}
1
+ {"version":3,"file":"AsyncImportCompressionPlugin.d.ts","sourceRoot":"","sources":["../src/AsyncImportCompressionPlugin.ts"],"names":[],"mappings":"AAGA,OAAgB,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAOpD,OAAO,EAEL,0BAA0B,EAE3B,MAAM,8BAA8B,CAAC;AAmFtC;;;;;GAKG;AACH,qBAAa,4BAA6B,YAAW,MAAM;IACzD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA6B;gBAEzC,aAAa,EAAE,0BAA0B;IAIrD,KAAK,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;CAyMvC"}
@@ -116,31 +116,33 @@ class AsyncImportCompressionPlugin {
116
116
  child.hasDependencies((dep) => {
117
117
  if (dep instanceof ImportDependency) {
118
118
  const { module: targetModule } = dep;
119
- let localAsyncImports = asyncImportMap.get(module);
120
- if (!localAsyncImports) {
121
- asyncImportMap.set(module, (localAsyncImports = new Map()));
119
+ if (targetModule) {
120
+ let localAsyncImports = asyncImportMap.get(module);
121
+ if (!localAsyncImports) {
122
+ asyncImportMap.set(module, (localAsyncImports = new Map()));
123
+ }
124
+ const chunkGroup = dep.block.chunkGroup;
125
+ const chunkIds = chunkGroup
126
+ ? chunkGroup.chunks.map((chunk) => chunk.id).sort()
127
+ : [];
128
+ const idString = chunkIds.join(';');
129
+ let meta = asyncImportGroups.get(idString);
130
+ if (!meta) {
131
+ asyncImportGroups.set(idString, (meta = {
132
+ chunkCount: chunkIds.length,
133
+ chunkIds: chunkIds,
134
+ count: 0,
135
+ index: -1
136
+ }));
137
+ }
138
+ meta.count++;
139
+ const stringKey = `${targetModule.id}`.replace(/[^A-Za-z0-9_$]/g, '_');
140
+ const key = `${ASYNC_IMPORT_PREFIX}${stringKey}`;
141
+ localAsyncImports.set(key, {
142
+ meta,
143
+ module: targetModule
144
+ });
122
145
  }
123
- const chunkGroup = dep.block.chunkGroup;
124
- const chunkIds = chunkGroup
125
- ? chunkGroup.chunks.map((chunk) => chunk.id).sort()
126
- : [];
127
- const idString = chunkIds.join(';');
128
- let meta = asyncImportGroups.get(idString);
129
- if (!meta) {
130
- asyncImportGroups.set(idString, (meta = {
131
- chunkCount: chunkIds.length,
132
- chunkIds: chunkIds,
133
- count: 0,
134
- index: -1
135
- }));
136
- }
137
- meta.count++;
138
- const stringKey = `${targetModule.id}`.replace(/[^A-Za-z0-9_$]/g, '_');
139
- const key = `${ASYNC_IMPORT_PREFIX}${stringKey}`;
140
- localAsyncImports.set(key, {
141
- meta,
142
- module: targetModule
143
- });
144
146
  }
145
147
  });
146
148
  }
@@ -162,16 +164,27 @@ class AsyncImportCompressionPlugin {
162
164
  // console.log(rankedImports[i]);
163
165
  }
164
166
  rankedImportGroups = rankedImports.map((x) => x[1]);
165
- // Have to do this after the official plugin in order to override
166
- compilation.dependencyTemplates.set(ImportDependency, {
167
- apply(dep, source) {
168
- const stringKey = `${dep.module.id}`.replace(/[^A-Za-z0-9_$]/g, '_');
169
- const key = `${ASYNC_IMPORT_PREFIX}${stringKey}`;
170
- const content = `__webpack_require__.ee(${key})`;
171
- source.replace(dep.block.range[0], dep.block.range[1] - 1, content);
167
+ const { dependencyTemplates } = compilation;
168
+ const defaultImplementation = dependencyTemplates.get(ImportDependency);
169
+ if (!defaultImplementation) {
170
+ compilation.errors.push(new Error(`Could not find ImportDependencyTemplate`));
171
+ }
172
+ const customTemplate = {
173
+ apply(dep, source, runtime) {
174
+ if (dep.module) {
175
+ const stringKey = `${dep.module.id}`.replace(/[^A-Za-z0-9_$]/g, '_');
176
+ const key = `${ASYNC_IMPORT_PREFIX}${stringKey}`;
177
+ const content = `__webpack_require__.ee(${key})`;
178
+ source.replace(dep.block.range[0], dep.block.range[1] - 1, content);
179
+ }
180
+ else {
181
+ defaultImplementation === null || defaultImplementation === void 0 ? void 0 : defaultImplementation.apply(dep, source, runtime);
182
+ }
172
183
  }
173
- // Typings in webpack are incorrect. This is a DependencyTemplate object
174
- });
184
+ };
185
+ // Have to do this after the official plugin in order to override
186
+ // Typings in webpack are incorrect. This is a DependencyTemplate object
187
+ dependencyTemplates.set(ImportDependency, customTemplate);
175
188
  });
176
189
  compilation.mainTemplate.hooks.requireExtensions.tap(PLUGIN_NAME, (source, chunk) => {
177
190
  if (!needChunkOnDemandLoadingCode(chunk)) {
@@ -1 +1 @@
1
- {"version":3,"file":"AsyncImportCompressionPlugin.js","sourceRoot":"","sources":["../src/AsyncImportCompressionPlugin.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;AAE3D,sDAAoD;AAIpD,MAAM,EAAE,QAAQ,EAAE,GAAG,iBAAO,CAAC;AAE7B,2CAA0C;AAO1C,MAAM,WAAW,GAAmC,8BAA8B,CAAC;AAEnF,MAAM,SAAS,GAAuB;IACpC,IAAI,EAAE,WAAW;IACjB,KAAK,EAAE,uBAAW;CACnB,CAAC;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AC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC;wBACxF,CAAC,CAAC,EAAE;oBACN,IAAI;oBACJ,GAAG,SAAS,oDAAoD;oBAChE,QAAQ,CAAC,MAAM,CAAC;wBACd,2HAA2H,SAAS,YAAY;wBAChJ,sBAAsB,SAAS,oCAAoC,SAAS,qBAAqB;qBAClG,CAAC;oBACF,IAAI;oBACJ,MAAM;iBACP,CAAC,CAAC;YACL,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA3LD,oEA2LC","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 webpack, { Compiler, Plugin } from 'webpack';\nimport { ReplaceSource } from 'webpack-sources';\nimport { Tapable, TapOptions } from 'tapable';\n\nconst { Template } = webpack;\n\nimport { STAGE_AFTER } from './Constants';\nimport {\n IExtendedModule,\n IModuleMinifierPluginHooks,\n IPostProcessFragmentContext\n} from './ModuleMinifierPlugin.types';\n\nconst PLUGIN_NAME: 'AsyncImportCompressionPlugin' = 'AsyncImportCompressionPlugin';\n\nconst TAP_AFTER: TapOptions<'sync'> = {\n name: PLUGIN_NAME,\n stage: STAGE_AFTER\n};\n\nconst ASYNC_IMPORT_PREFIX: '__IMPORT_ASYNC' = '__IMPORT_ASYNC';\nconst ASYNC_IMPORT_REGEX: RegExp = /__IMPORT_ASYNC[^\\)]+/g;\n\ndeclare class WebpackImportDependency extends webpack.compilation.Dependency {\n public module: webpack.compilation.Module;\n public block: {\n chunkGroup: webpack.compilation.ChunkGroup;\n range: [number, number];\n };\n}\n\ninterface IAsyncImportMetadata {\n chunkCount: number;\n chunkIds: number[];\n count: number;\n index: number;\n}\n\ninterface ILocalImportMetadata {\n meta: IAsyncImportMetadata;\n module: webpack.compilation.Module;\n}\n\nfunction getImportDependency(compilation: webpack.compilation.Compilation): typeof WebpackImportDependency {\n for (const key of compilation.dependencyTemplates.keys()) {\n if (key.name === 'ImportDependency') {\n return key as unknown as typeof WebpackImportDependency;\n }\n }\n\n throw new Error(`Could not find ImportDependency!`);\n}\n\nfunction getImportTypeExpression(\n module: webpack.compilation.Module,\n originModule: webpack.compilation.Module\n): string {\n const exportsType: string | undefined = module.buildMeta?.exportsType;\n const strict: boolean | undefined = originModule.buildMeta?.strictHarmonyModule;\n\n // Logic translated from:\n // https://github.com/webpack/webpack/blob/3956274f1eada621e105208dcab4608883cdfdb2/lib/RuntimeTemplate.js#L110-L122\n if (exportsType === 'namespace') {\n // Use the raw module directly\n return '';\n } else if (exportsType === 'named') {\n // Create a new namespace object and forward all exports\n return ',3';\n } else if (strict) {\n // Synthetic default export\n return ',1';\n } else {\n // If modules is marked __esModule, return raw module, otherwise create a new namespace object and forward all exports\n return ',7';\n }\n}\n\nfunction needChunkOnDemandLoadingCode(chunk: webpack.compilation.Chunk): boolean {\n for (const chunkGroup of chunk.groupsIterable) {\n if (chunkGroup.getNumberOfChildren() > 0) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Plugin that replaces `Promise.all([__webpack_require__.e(1), __webpack_require__.e(12)]).then(__webpack_require__.t.bind(123,7))`\n * with more concise expressions like `__webpack_require__.ee([1,12],123,7)`, etc.\n *\n * Also ensures that the code seen by the minifier does not contain chunk ids, and is therefore portable across chunks/compilations.\n */\nexport class AsyncImportCompressionPlugin implements Plugin {\n private readonly _minifierHooks: IModuleMinifierPluginHooks;\n\n public constructor(minifierHooks: IModuleMinifierPluginHooks) {\n this._minifierHooks = minifierHooks;\n }\n\n public apply(compiler: Compiler): void {\n const asyncImportMap: Map<webpack.compilation.Module, Map<string, ILocalImportMetadata>> = new Map();\n const asyncImportGroups: Map<string, IAsyncImportMetadata> = new Map();\n let rankedImportGroups: IAsyncImportMetadata[] | undefined;\n\n this._minifierHooks.postProcessCodeFragment.tap(\n {\n name: PLUGIN_NAME,\n stage: -1\n },\n (source: ReplaceSource, context: IPostProcessFragmentContext) => {\n const code: string = source.original().source() as string;\n\n let localImports: Map<string, ILocalImportMetadata> | undefined;\n\n ASYNC_IMPORT_REGEX.lastIndex = -1;\n // RegExp.exec uses null or an array as the return type, explicitly\n let match: RegExpExecArray | null = null;\n while ((match = ASYNC_IMPORT_REGEX.exec(code))) {\n const token: string = match[0];\n\n if (!localImports) {\n if (!context.module) {\n context.compilation.errors.push(\n new Error(`Unexpected async import ${token} in non-module context ${context.loggingName}`)\n );\n return source;\n }\n\n localImports = asyncImportMap.get(context.module);\n if (!localImports) {\n context.compilation.errors.push(\n new Error(`Unexpected async import ${token} in module ${context.loggingName}`)\n );\n return source;\n }\n }\n\n const localImport: ILocalImportMetadata | undefined = localImports.get(token);\n if (!localImport) {\n context.compilation.errors.push(\n new Error(`Missing metadata for ${token} in module ${context.loggingName}`)\n );\n return source;\n }\n const { meta, module } = localImport;\n\n const chunkExpression: string = meta.index < 0 ? JSON.stringify(meta.chunkIds) : `${meta.index}`;\n\n const mapped: string | number | undefined = this._minifierHooks.finalModuleId.call(\n module.id!,\n context.compilation\n );\n const idExpr: string | number = mapped === undefined ? module.id! : mapped;\n\n // Replace with a reference or array of ideas, the target module id, and the type of import\n source.replace(\n match.index,\n ASYNC_IMPORT_REGEX.lastIndex - 1,\n `${chunkExpression},${JSON.stringify(idExpr)}${getImportTypeExpression(module, context.module!)}`\n );\n }\n\n return source;\n }\n );\n\n compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation: webpack.compilation.Compilation) => {\n asyncImportMap.clear();\n asyncImportGroups.clear();\n\n compilation.hooks.beforeChunkAssets.tap(TAP_AFTER, () => {\n const ImportDependency: typeof WebpackImportDependency = getImportDependency(compilation);\n\n for (const module of compilation.modules) {\n const toProcess: IExtendedModule[] = module.modules || [module];\n\n for (const child of toProcess) {\n child.hasDependencies((dep: webpack.compilation.Dependency) => {\n if (dep instanceof ImportDependency) {\n const { module: targetModule } = dep;\n\n let localAsyncImports: Map<string, ILocalImportMetadata> | undefined =\n asyncImportMap.get(module);\n if (!localAsyncImports) {\n asyncImportMap.set(module, (localAsyncImports = new Map()));\n }\n\n const chunkGroup: webpack.compilation.ChunkGroup = dep.block.chunkGroup;\n const chunkIds: number[] = chunkGroup\n ? chunkGroup.chunks.map((chunk) => chunk.id!).sort()\n : [];\n const idString: string = chunkIds.join(';');\n\n let meta: IAsyncImportMetadata | undefined = asyncImportGroups.get(idString);\n if (!meta) {\n asyncImportGroups.set(\n idString,\n (meta = {\n chunkCount: chunkIds.length,\n chunkIds: chunkIds,\n count: 0,\n index: -1\n })\n );\n }\n meta.count++;\n\n const stringKey: string = `${targetModule.id}`.replace(/[^A-Za-z0-9_$]/g, '_');\n\n const key: string = `${ASYNC_IMPORT_PREFIX}${stringKey}`;\n localAsyncImports.set(key, {\n meta,\n module: targetModule\n });\n }\n });\n }\n }\n\n const rankedImports: [string, IAsyncImportMetadata][] = [...asyncImportGroups]\n .filter((x) => x[1].count > 1)\n .sort((x, y) => {\n let diff: number = y[1].count - x[1].count;\n if (!diff) {\n diff = y[1].chunkCount - x[1].chunkCount;\n }\n\n if (!diff) {\n diff = x[0] > y[0] ? 1 : x[0] < y[0] ? -1 : 0;\n }\n\n return diff;\n });\n\n for (let i: number = 0, len: number = rankedImports.length; i < len; i++) {\n rankedImports[i][1].index = i;\n // console.log(rankedImports[i]);\n }\n\n rankedImportGroups = rankedImports.map((x) => x[1]);\n\n // Have to do this after the official plugin in order to override\n compilation.dependencyTemplates.set(ImportDependency, {\n apply(dep: WebpackImportDependency, source: ReplaceSource): void {\n const stringKey: string = `${dep.module.id}`.replace(/[^A-Za-z0-9_$]/g, '_');\n const key: string = `${ASYNC_IMPORT_PREFIX}${stringKey}`;\n const content: string = `__webpack_require__.ee(${key})`;\n source.replace(dep.block.range[0], dep.block.range[1] - 1, content);\n }\n // Typings in webpack are incorrect. This is a DependencyTemplate object\n } as unknown as Tapable);\n });\n\n compilation.mainTemplate.hooks.requireExtensions.tap(\n PLUGIN_NAME,\n (source: string, chunk: webpack.compilation.Chunk) => {\n if (!needChunkOnDemandLoadingCode(chunk)) {\n return source;\n }\n\n const { requireFn } = compilation.mainTemplate;\n return Template.asString([\n `var asyncImportChunkGroups = [`,\n rankedImportGroups\n ? rankedImportGroups.map((x) => Template.indent(JSON.stringify(x.chunkIds))).join(',\\n')\n : '',\n `];`,\n `${requireFn}.ee = function (groupOrId, moduleId, importType) {`,\n Template.indent([\n `return Promise.all((Array.isArray(groupOrId) ? groupOrId : asyncImportChunkGroups[groupOrId]).map(function (x) { return ${requireFn}.e(x); }))`,\n `.then(importType ? ${requireFn}.t.bind(0,moduleId,importType) : ${requireFn}.bind(0,moduleId));`\n ]),\n `};`,\n source\n ]);\n }\n );\n });\n }\n}\n"]}
1
+ {"version":3,"file":"AsyncImportCompressionPlugin.js","sourceRoot":"","sources":["../src/AsyncImportCompressionPlugin.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;AAE3D,sDAAoD;AAIpD,MAAM,EAAE,QAAQ,EAAE,GAAG,iBAAO,CAAC;AAE7B,2CAA0C;AAO1C,MAAM,WAAW,GAAmC,8BAA8B,CAAC;AAEnF,MAAM,SAAS,GAAuB;IACpC,IAAI,EAAE,WAAW;IACjB,KAAK,EAAE,uBAAW;CACnB,CAAC;AAEF,MAAM,mBAAmB,GAAqB,gBAAgB,CAAC;AAC/D,MAAM,kBAAkB,GAAW,uBAAuB,CAAC;AA8B3D,SAAS,mBAAmB,CAAC,WAA4C;IACvE,KAAK,MAAM,GAAG,IAAI,WAAW,CAAC,mBAAmB,CAAC,IAAI,EAAE,EAAE;QACxD,IAAI,GAAG,CAAC,IAAI,KAAK,kBAAkB,EAAE;YACnC,OAAO,GAAgD,CAAC;SACzD;KACF;IAED,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;AACtD,CAAC;AAED,SAAS,uBAAuB,CAC9B,MAAkC,EAClC,YAAwC;;IAExC,MAAM,WAAW,GAAuB,MAAA,MAAM,CAAC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AC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC;wBACxF,CAAC,CAAC,EAAE;oBACN,IAAI;oBACJ,GAAG,SAAS,oDAAoD;oBAChE,QAAQ,CAAC,MAAM,CAAC;wBACd,2HAA2H,SAAS,YAAY;wBAChJ,sBAAsB,SAAS,oCAAoC,SAAS,qBAAqB;qBAClG,CAAC;oBACF,IAAI;oBACJ,MAAM;iBACP,CAAC,CAAC;YACL,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAhND,oEAgNC","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 webpack, { Compiler, Plugin } from 'webpack';\nimport { ReplaceSource } from 'webpack-sources';\nimport { Tapable, TapOptions } from 'tapable';\n\nconst { Template } = webpack;\n\nimport { STAGE_AFTER } from './Constants';\nimport {\n IExtendedModule,\n IModuleMinifierPluginHooks,\n IPostProcessFragmentContext\n} from './ModuleMinifierPlugin.types';\n\nconst PLUGIN_NAME: 'AsyncImportCompressionPlugin' = 'AsyncImportCompressionPlugin';\n\nconst TAP_AFTER: TapOptions<'sync'> = {\n name: PLUGIN_NAME,\n stage: STAGE_AFTER\n};\n\nconst ASYNC_IMPORT_PREFIX: '__IMPORT_ASYNC' = '__IMPORT_ASYNC';\nconst ASYNC_IMPORT_REGEX: RegExp = /__IMPORT_ASYNC[^\\)]+/g;\n\ndeclare class WebpackImportDependency extends webpack.compilation.Dependency {\n public module: webpack.compilation.Module;\n public block: {\n chunkGroup: webpack.compilation.ChunkGroup;\n range: [number, number];\n };\n}\n\ninterface IImportDependencyTemplate {\n apply(\n dependency: WebpackImportDependency,\n source: ReplaceSource,\n runtime: webpack.compilation.RuntimeTemplate\n ): void;\n}\n\ninterface IAsyncImportMetadata {\n chunkCount: number;\n chunkIds: number[];\n count: number;\n index: number;\n}\n\ninterface ILocalImportMetadata {\n meta: IAsyncImportMetadata;\n module: webpack.compilation.Module;\n}\n\nfunction getImportDependency(compilation: webpack.compilation.Compilation): typeof WebpackImportDependency {\n for (const key of compilation.dependencyTemplates.keys()) {\n if (key.name === 'ImportDependency') {\n return key as unknown as typeof WebpackImportDependency;\n }\n }\n\n throw new Error(`Could not find ImportDependency!`);\n}\n\nfunction getImportTypeExpression(\n module: webpack.compilation.Module,\n originModule: webpack.compilation.Module\n): string {\n const exportsType: string | undefined = module.buildMeta?.exportsType;\n const strict: boolean | undefined = originModule.buildMeta?.strictHarmonyModule;\n\n // Logic translated from:\n // https://github.com/webpack/webpack/blob/3956274f1eada621e105208dcab4608883cdfdb2/lib/RuntimeTemplate.js#L110-L122\n if (exportsType === 'namespace') {\n // Use the raw module directly\n return '';\n } else if (exportsType === 'named') {\n // Create a new namespace object and forward all exports\n return ',3';\n } else if (strict) {\n // Synthetic default export\n return ',1';\n } else {\n // If modules is marked __esModule, return raw module, otherwise create a new namespace object and forward all exports\n return ',7';\n }\n}\n\nfunction needChunkOnDemandLoadingCode(chunk: webpack.compilation.Chunk): boolean {\n for (const chunkGroup of chunk.groupsIterable) {\n if (chunkGroup.getNumberOfChildren() > 0) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Plugin that replaces `Promise.all([__webpack_require__.e(1), __webpack_require__.e(12)]).then(__webpack_require__.t.bind(123,7))`\n * with more concise expressions like `__webpack_require__.ee([1,12],123,7)`, etc.\n *\n * Also ensures that the code seen by the minifier does not contain chunk ids, and is therefore portable across chunks/compilations.\n */\nexport class AsyncImportCompressionPlugin implements Plugin {\n private readonly _minifierHooks: IModuleMinifierPluginHooks;\n\n public constructor(minifierHooks: IModuleMinifierPluginHooks) {\n this._minifierHooks = minifierHooks;\n }\n\n public apply(compiler: Compiler): void {\n const asyncImportMap: Map<webpack.compilation.Module, Map<string, ILocalImportMetadata>> = new Map();\n const asyncImportGroups: Map<string, IAsyncImportMetadata> = new Map();\n let rankedImportGroups: IAsyncImportMetadata[] | undefined;\n\n this._minifierHooks.postProcessCodeFragment.tap(\n {\n name: PLUGIN_NAME,\n stage: -1\n },\n (source: ReplaceSource, context: IPostProcessFragmentContext) => {\n const code: string = source.original().source() as string;\n\n let localImports: Map<string, ILocalImportMetadata> | undefined;\n\n ASYNC_IMPORT_REGEX.lastIndex = -1;\n // RegExp.exec uses null or an array as the return type, explicitly\n let match: RegExpExecArray | null = null;\n while ((match = ASYNC_IMPORT_REGEX.exec(code))) {\n const token: string = match[0];\n\n if (!localImports) {\n if (!context.module) {\n context.compilation.errors.push(\n new Error(`Unexpected async import ${token} in non-module context ${context.loggingName}`)\n );\n return source;\n }\n\n localImports = asyncImportMap.get(context.module);\n if (!localImports) {\n context.compilation.errors.push(\n new Error(`Unexpected async import ${token} in module ${context.loggingName}`)\n );\n return source;\n }\n }\n\n const localImport: ILocalImportMetadata | undefined = localImports.get(token);\n if (!localImport) {\n context.compilation.errors.push(\n new Error(`Missing metadata for ${token} in module ${context.loggingName}`)\n );\n return source;\n }\n const { meta, module } = localImport;\n\n const chunkExpression: string = meta.index < 0 ? JSON.stringify(meta.chunkIds) : `${meta.index}`;\n\n const mapped: string | number | undefined = this._minifierHooks.finalModuleId.call(\n module.id!,\n context.compilation\n );\n const idExpr: string | number = mapped === undefined ? module.id! : mapped;\n\n // Replace with a reference or array of ideas, the target module id, and the type of import\n source.replace(\n match.index,\n ASYNC_IMPORT_REGEX.lastIndex - 1,\n `${chunkExpression},${JSON.stringify(idExpr)}${getImportTypeExpression(module, context.module!)}`\n );\n }\n\n return source;\n }\n );\n\n compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation: webpack.compilation.Compilation) => {\n asyncImportMap.clear();\n asyncImportGroups.clear();\n\n compilation.hooks.beforeChunkAssets.tap(TAP_AFTER, () => {\n const ImportDependency: typeof WebpackImportDependency = getImportDependency(compilation);\n\n for (const module of compilation.modules) {\n const toProcess: IExtendedModule[] = module.modules || [module];\n\n for (const child of toProcess) {\n child.hasDependencies((dep: webpack.compilation.Dependency) => {\n if (dep instanceof ImportDependency) {\n const { module: targetModule } = dep;\n\n if (targetModule) {\n let localAsyncImports: Map<string, ILocalImportMetadata> | undefined =\n asyncImportMap.get(module);\n if (!localAsyncImports) {\n asyncImportMap.set(module, (localAsyncImports = new Map()));\n }\n\n const chunkGroup: webpack.compilation.ChunkGroup = dep.block.chunkGroup;\n const chunkIds: number[] = chunkGroup\n ? chunkGroup.chunks.map((chunk) => chunk.id!).sort()\n : [];\n const idString: string = chunkIds.join(';');\n\n let meta: IAsyncImportMetadata | undefined = asyncImportGroups.get(idString);\n if (!meta) {\n asyncImportGroups.set(\n idString,\n (meta = {\n chunkCount: chunkIds.length,\n chunkIds: chunkIds,\n count: 0,\n index: -1\n })\n );\n }\n meta.count++;\n\n const stringKey: string = `${targetModule.id}`.replace(/[^A-Za-z0-9_$]/g, '_');\n\n const key: string = `${ASYNC_IMPORT_PREFIX}${stringKey}`;\n localAsyncImports.set(key, {\n meta,\n module: targetModule\n });\n }\n }\n });\n }\n }\n\n const rankedImports: [string, IAsyncImportMetadata][] = [...asyncImportGroups]\n .filter((x) => x[1].count > 1)\n .sort((x, y) => {\n let diff: number = y[1].count - x[1].count;\n if (!diff) {\n diff = y[1].chunkCount - x[1].chunkCount;\n }\n\n if (!diff) {\n diff = x[0] > y[0] ? 1 : x[0] < y[0] ? -1 : 0;\n }\n\n return diff;\n });\n\n for (let i: number = 0, len: number = rankedImports.length; i < len; i++) {\n rankedImports[i][1].index = i;\n // console.log(rankedImports[i]);\n }\n\n rankedImportGroups = rankedImports.map((x) => x[1]);\n\n const { dependencyTemplates } = compilation;\n\n const defaultImplementation: IImportDependencyTemplate | undefined = dependencyTemplates.get(\n ImportDependency\n ) as unknown as IImportDependencyTemplate;\n if (!defaultImplementation) {\n compilation.errors.push(new Error(`Could not find ImportDependencyTemplate`));\n }\n\n const customTemplate: IImportDependencyTemplate = {\n apply(\n dep: WebpackImportDependency,\n source: ReplaceSource,\n runtime: webpack.compilation.RuntimeTemplate\n ): void {\n if (dep.module) {\n const stringKey: string = `${dep.module.id}`.replace(/[^A-Za-z0-9_$]/g, '_');\n const key: string = `${ASYNC_IMPORT_PREFIX}${stringKey}`;\n const content: string = `__webpack_require__.ee(${key})`;\n source.replace(dep.block.range[0], dep.block.range[1] - 1, content);\n } else {\n defaultImplementation?.apply(dep, source, runtime);\n }\n }\n };\n\n // Have to do this after the official plugin in order to override\n // Typings in webpack are incorrect. This is a DependencyTemplate object\n dependencyTemplates.set(ImportDependency, customTemplate as unknown as Tapable);\n });\n\n compilation.mainTemplate.hooks.requireExtensions.tap(\n PLUGIN_NAME,\n (source: string, chunk: webpack.compilation.Chunk) => {\n if (!needChunkOnDemandLoadingCode(chunk)) {\n return source;\n }\n\n const { requireFn } = compilation.mainTemplate;\n return Template.asString([\n `var asyncImportChunkGroups = [`,\n rankedImportGroups\n ? rankedImportGroups.map((x) => Template.indent(JSON.stringify(x.chunkIds))).join(',\\n')\n : '',\n `];`,\n `${requireFn}.ee = function (groupOrId, moduleId, importType) {`,\n Template.indent([\n `return Promise.all((Array.isArray(groupOrId) ? groupOrId : asyncImportChunkGroups[groupOrId]).map(function (x) { return ${requireFn}.e(x); }))`,\n `.then(importType ? ${requireFn}.t.bind(0,moduleId,importType) : ${requireFn}.bind(0,moduleId));`\n ]),\n `};`,\n source\n ]);\n }\n );\n });\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rushstack/webpack4-module-minifier-plugin",
3
- "version": "0.12.3",
3
+ "version": "0.12.4",
4
4
  "description": "This plugin splits minification of webpack compilations into smaller units.",
5
5
  "main": "lib/index.js",
6
6
  "typings": "dist/webpack4-module-minifier-plugin.d.ts",
@@ -34,8 +34,8 @@
34
34
  "dependencies": {
35
35
  "@types/tapable": "1.0.6",
36
36
  "tapable": "1.1.3",
37
- "@rushstack/module-minifier": "0.3.7",
38
- "@rushstack/worker-pool": "0.3.7"
37
+ "@rushstack/module-minifier": "0.3.8",
38
+ "@rushstack/worker-pool": "0.3.8"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/heft-jest": "1.0.1",
@@ -45,8 +45,8 @@
45
45
  "webpack": "~4.44.2",
46
46
  "webpack-sources": "~1.4.3",
47
47
  "@rushstack/eslint-config": "3.2.0",
48
- "@rushstack/heft": "0.50.3",
49
- "@rushstack/heft-node-rig": "1.12.9"
48
+ "@rushstack/heft": "0.50.4",
49
+ "@rushstack/heft-node-rig": "1.12.10"
50
50
  },
51
51
  "sideEffects": [
52
52
  "./lib/OverrideWebpackIdentifierAllocation"