feat: refactoring project
This commit is contained in:
138
node_modules/webpack/lib/optimize/ModuleConcatenationPlugin.js
generated
vendored
138
node_modules/webpack/lib/optimize/ModuleConcatenationPlugin.js
generated
vendored
@@ -45,19 +45,9 @@ const ConcatenatedModule = require("./ConcatenatedModule");
|
||||
* @param {string} msg message
|
||||
* @returns {string} formatted message
|
||||
*/
|
||||
const formatBailoutReason = msg => {
|
||||
return "ModuleConcatenation bailout: " + msg;
|
||||
};
|
||||
const formatBailoutReason = msg => `ModuleConcatenation bailout: ${msg}`;
|
||||
|
||||
class ModuleConcatenationPlugin {
|
||||
/**
|
||||
* @param {TODO} options options
|
||||
*/
|
||||
constructor(options) {
|
||||
if (typeof options !== "object") options = {};
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the plugin
|
||||
* @param {Compiler} compiler the compiler instance
|
||||
@@ -130,15 +120,14 @@ class ModuleConcatenationPlugin {
|
||||
requestShortener
|
||||
)}${reasonWithPrefix}`
|
||||
);
|
||||
} else {
|
||||
return formatBailoutReason(
|
||||
`Cannot concat with ${module.readableIdentifier(
|
||||
requestShortener
|
||||
)} because of ${problem.readableIdentifier(
|
||||
requestShortener
|
||||
)}${reasonWithPrefix}`
|
||||
);
|
||||
}
|
||||
return formatBailoutReason(
|
||||
`Cannot concat with ${module.readableIdentifier(
|
||||
requestShortener
|
||||
)} because of ${problem.readableIdentifier(
|
||||
requestShortener
|
||||
)}${reasonWithPrefix}`
|
||||
);
|
||||
};
|
||||
|
||||
compilation.hooks.optimizeChunkModules.tapAsync(
|
||||
@@ -170,13 +159,13 @@ class ModuleConcatenationPlugin {
|
||||
|
||||
// Must not be an async module
|
||||
if (moduleGraph.isAsync(module)) {
|
||||
setBailoutReason(module, `Module is async`);
|
||||
setBailoutReason(module, "Module is async");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Must be in strict mode
|
||||
if (!(/** @type {BuildInfo} */ (module.buildInfo).strict)) {
|
||||
setBailoutReason(module, `Module is not in strict mode`);
|
||||
setBailoutReason(module, "Module is not in strict mode");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -189,11 +178,10 @@ class ModuleConcatenationPlugin {
|
||||
// Exports must be known (and not dynamic)
|
||||
const exportsInfo = moduleGraph.getExportsInfo(module);
|
||||
const relevantExports = exportsInfo.getRelevantExports(undefined);
|
||||
const unknownReexports = relevantExports.filter(exportInfo => {
|
||||
return (
|
||||
const unknownReexports = relevantExports.filter(
|
||||
exportInfo =>
|
||||
exportInfo.isReexport() && !exportInfo.getTarget(moduleGraph)
|
||||
);
|
||||
});
|
||||
);
|
||||
if (unknownReexports.length > 0) {
|
||||
setBailoutReason(
|
||||
module,
|
||||
@@ -210,9 +198,7 @@ class ModuleConcatenationPlugin {
|
||||
|
||||
// Root modules must have a static list of exports
|
||||
const unknownProvidedExports = relevantExports.filter(
|
||||
exportInfo => {
|
||||
return exportInfo.provided !== true;
|
||||
}
|
||||
exportInfo => exportInfo.provided !== true
|
||||
);
|
||||
if (unknownProvidedExports.length > 0) {
|
||||
setBailoutReason(
|
||||
@@ -245,12 +231,11 @@ class ModuleConcatenationPlugin {
|
||||
// modules with lower depth are more likely suited as roots
|
||||
// this improves performance, because modules already selected as inner are skipped
|
||||
logger.time("sort relevant modules");
|
||||
relevantModules.sort((a, b) => {
|
||||
return (
|
||||
relevantModules.sort(
|
||||
(a, b) =>
|
||||
/** @type {number} */ (moduleGraph.getDepth(a)) -
|
||||
/** @type {number} */ (moduleGraph.getDepth(b))
|
||||
);
|
||||
});
|
||||
);
|
||||
logger.timeEnd("sort relevant modules");
|
||||
|
||||
/** @type {Statistics} */
|
||||
@@ -279,7 +264,7 @@ class ModuleConcatenationPlugin {
|
||||
// TODO reconsider that when it's only used in a different runtime
|
||||
if (usedAsInner.has(currentRoot)) continue;
|
||||
|
||||
let chunkRuntime = undefined;
|
||||
let chunkRuntime;
|
||||
for (const r of chunkGraph.getModuleRuntimes(currentRoot)) {
|
||||
chunkRuntime = mergeRuntimeOwned(chunkRuntime, r);
|
||||
}
|
||||
@@ -376,11 +361,9 @@ class ModuleConcatenationPlugin {
|
||||
// to get the biggest groups possible. Used modules are marked with usedModules
|
||||
// TODO: Allow to reuse existing configuration while trying to add dependencies.
|
||||
// This would improve performance. O(n^2) -> O(n)
|
||||
logger.time(`sort concat configurations`);
|
||||
concatConfigurations.sort((a, b) => {
|
||||
return b.modules.size - a.modules.size;
|
||||
});
|
||||
logger.timeEnd(`sort concat configurations`);
|
||||
logger.time("sort concat configurations");
|
||||
concatConfigurations.sort((a, b) => b.modules.size - a.modules.size);
|
||||
logger.timeEnd("sort concat configurations");
|
||||
const usedModules = new Set();
|
||||
|
||||
logger.time("create concatenated modules");
|
||||
@@ -399,7 +382,7 @@ class ModuleConcatenationPlugin {
|
||||
|
||||
// Create a new ConcatenatedModule
|
||||
ConcatenatedModule.getCompilationHooks(compilation);
|
||||
let newModule = ConcatenatedModule.create(
|
||||
const newModule = ConcatenatedModule.create(
|
||||
rootModule,
|
||||
modules,
|
||||
concatConfiguration.runtime,
|
||||
@@ -412,8 +395,10 @@ class ModuleConcatenationPlugin {
|
||||
newModule.build(
|
||||
compiler.options,
|
||||
compilation,
|
||||
null,
|
||||
null,
|
||||
/** @type {TODO} */
|
||||
(null),
|
||||
/** @type {TODO} */
|
||||
(null),
|
||||
err => {
|
||||
if (err) {
|
||||
if (!err.module) {
|
||||
@@ -448,15 +433,12 @@ class ModuleConcatenationPlugin {
|
||||
moduleGraph.copyOutgoingModuleConnections(
|
||||
m,
|
||||
newModule,
|
||||
c => {
|
||||
return (
|
||||
c.originModule === m &&
|
||||
!(
|
||||
c.dependency instanceof HarmonyImportDependency &&
|
||||
modules.has(c.module)
|
||||
)
|
||||
);
|
||||
}
|
||||
c =>
|
||||
c.originModule === m &&
|
||||
!(
|
||||
c.dependency instanceof HarmonyImportDependency &&
|
||||
modules.has(c.module)
|
||||
)
|
||||
);
|
||||
// remove module from chunk
|
||||
for (const chunk of chunkGraph.getModuleChunksIterable(
|
||||
@@ -639,11 +621,11 @@ class ModuleConcatenationPlugin {
|
||||
incomingConnections.get(null) || incomingConnections.get(undefined);
|
||||
if (incomingConnectionsFromNonModules) {
|
||||
const activeNonModulesConnections =
|
||||
incomingConnectionsFromNonModules.filter(connection => {
|
||||
incomingConnectionsFromNonModules.filter(connection =>
|
||||
// We are not interested in inactive connections
|
||||
// or connections without dependency
|
||||
return connection.isActive(runtime);
|
||||
});
|
||||
connection.isActive(runtime)
|
||||
);
|
||||
if (activeNonModulesConnections.length > 0) {
|
||||
/**
|
||||
* @param {RequestShortener} requestShortener request shortener
|
||||
@@ -676,7 +658,7 @@ class ModuleConcatenationPlugin {
|
||||
if (chunkGraph.getNumberOfModuleChunks(originModule) === 0) continue;
|
||||
|
||||
// We don't care for connections from other runtimes
|
||||
let originRuntime = undefined;
|
||||
let originRuntime;
|
||||
for (const r of chunkGraph.getModuleRuntimes(originModule)) {
|
||||
originRuntime = mergeRuntimeOwned(originRuntime, r);
|
||||
}
|
||||
@@ -743,19 +725,20 @@ class ModuleConcatenationPlugin {
|
||||
*/
|
||||
const problem = requestShortener => {
|
||||
const names = Array.from(nonHarmonyConnections)
|
||||
.map(([originModule, connections]) => {
|
||||
return `${originModule.readableIdentifier(
|
||||
requestShortener
|
||||
)} (referenced with ${Array.from(
|
||||
new Set(
|
||||
connections
|
||||
.map(c => c.dependency && c.dependency.type)
|
||||
.filter(Boolean)
|
||||
.map(
|
||||
([originModule, connections]) =>
|
||||
`${originModule.readableIdentifier(
|
||||
requestShortener
|
||||
)} (referenced with ${Array.from(
|
||||
new Set(
|
||||
connections
|
||||
.map(c => c.dependency && c.dependency.type)
|
||||
.filter(Boolean)
|
||||
)
|
||||
)
|
||||
)
|
||||
.sort()
|
||||
.join(", ")})`;
|
||||
})
|
||||
.sort()
|
||||
.join(", ")})`
|
||||
)
|
||||
.sort();
|
||||
return `Module ${module.readableIdentifier(
|
||||
requestShortener
|
||||
@@ -779,19 +762,15 @@ class ModuleConcatenationPlugin {
|
||||
/** @type {false | RuntimeSpec} */
|
||||
let currentRuntimeCondition = false;
|
||||
for (const connection of connections) {
|
||||
const runtimeCondition = filterRuntime(runtime, runtime => {
|
||||
return connection.isTargetActive(runtime);
|
||||
});
|
||||
const runtimeCondition = filterRuntime(runtime, runtime =>
|
||||
connection.isTargetActive(runtime)
|
||||
);
|
||||
if (runtimeCondition === false) continue;
|
||||
if (runtimeCondition === true) continue outer;
|
||||
if (currentRuntimeCondition !== false) {
|
||||
currentRuntimeCondition = mergeRuntime(
|
||||
currentRuntimeCondition,
|
||||
runtimeCondition
|
||||
);
|
||||
} else {
|
||||
currentRuntimeCondition = runtimeCondition;
|
||||
}
|
||||
currentRuntimeCondition =
|
||||
currentRuntimeCondition !== false
|
||||
? mergeRuntime(currentRuntimeCondition, runtimeCondition)
|
||||
: runtimeCondition;
|
||||
}
|
||||
if (currentRuntimeCondition !== false) {
|
||||
otherRuntimeConnections.push({
|
||||
@@ -805,8 +784,8 @@ class ModuleConcatenationPlugin {
|
||||
* @param {RequestShortener} requestShortener request shortener
|
||||
* @returns {string} problem description
|
||||
*/
|
||||
const problem = requestShortener => {
|
||||
return `Module ${module.readableIdentifier(
|
||||
const problem = requestShortener =>
|
||||
`Module ${module.readableIdentifier(
|
||||
requestShortener
|
||||
)} is runtime-dependent referenced by these modules: ${Array.from(
|
||||
otherRuntimeConnections,
|
||||
@@ -819,7 +798,6 @@ class ModuleConcatenationPlugin {
|
||||
/** @type {RuntimeSpec} */ (runtimeCondition)
|
||||
)})`
|
||||
).join(", ")}`;
|
||||
};
|
||||
statistics.incorrectRuntimeCondition++;
|
||||
failureCache.set(module, problem); // cache failures for performance
|
||||
return problem;
|
||||
|
||||
Reference in New Issue
Block a user