mirror of https://github.com/webpack/webpack.git
optimize SideEffectsFlagPlugin performance
This commit is contained in:
parent
8445f06fa9
commit
e6113e9787
|
@ -15,7 +15,7 @@ class ModuleGraphConnection {
|
||||||
* @param {Module} module the referenced module
|
* @param {Module} module the referenced module
|
||||||
* @param {string=} explanation some extra detail
|
* @param {string=} explanation some extra detail
|
||||||
* @param {boolean=} weak the reference is weak
|
* @param {boolean=} weak the reference is weak
|
||||||
* @param {function(): boolean=} condition condition for the connection
|
* @param {function(ModuleGraphConnection): boolean=} condition condition for the connection
|
||||||
*/
|
*/
|
||||||
constructor(
|
constructor(
|
||||||
originModule,
|
originModule,
|
||||||
|
@ -33,7 +33,7 @@ class ModuleGraphConnection {
|
||||||
this.weak = weak;
|
this.weak = weak;
|
||||||
this.conditional = !!condition;
|
this.conditional = !!condition;
|
||||||
this._active = true;
|
this._active = true;
|
||||||
/** @type {function(): boolean} */
|
/** @type {function(ModuleGraphConnection): boolean} */
|
||||||
this.condition = condition;
|
this.condition = condition;
|
||||||
/** @type {Set<string>} */
|
/** @type {Set<string>} */
|
||||||
this.explanations = undefined;
|
this.explanations = undefined;
|
||||||
|
@ -44,13 +44,13 @@ class ModuleGraphConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {function(): boolean} condition condition for the connection
|
* @param {function(ModuleGraphConnection): boolean} condition condition for the connection
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
addCondition(condition) {
|
addCondition(condition) {
|
||||||
if (this.conditional) {
|
if (this.conditional) {
|
||||||
const old = this.condition;
|
const old = this.condition;
|
||||||
this.condition = () => old() && condition();
|
this.condition = c => old(c) && condition(c);
|
||||||
} else if (this._active) {
|
} else if (this._active) {
|
||||||
this.conditional = true;
|
this.conditional = true;
|
||||||
this.condition = condition;
|
this.condition = condition;
|
||||||
|
@ -75,7 +75,7 @@ class ModuleGraphConnection {
|
||||||
|
|
||||||
get active() {
|
get active() {
|
||||||
if (!this.conditional) return this._active;
|
if (!this.conditional) return this._active;
|
||||||
return this.condition();
|
return this.condition(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
set active(value) {
|
set active(value) {
|
||||||
|
|
|
@ -23,6 +23,21 @@ class HarmonyImportSideEffectDependency extends HarmonyImportDependency {
|
||||||
get type() {
|
get type() {
|
||||||
return "harmony side effect evaluation";
|
return "harmony side effect evaluation";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ModuleGraph} moduleGraph module graph
|
||||||
|
* @returns {function(): boolean} function to determine if the connection is active
|
||||||
|
*/
|
||||||
|
getCondition(moduleGraph) {
|
||||||
|
return connection => {
|
||||||
|
const refModule = connection.resolvedModule;
|
||||||
|
return (
|
||||||
|
!refModule ||
|
||||||
|
refModule.factoryMeta === undefined ||
|
||||||
|
!refModule.factoryMeta.sideEffectFree
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
makeSerializable(
|
makeSerializable(
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
const glob2regexp = require("glob-to-regexp");
|
const glob2regexp = require("glob-to-regexp");
|
||||||
const { STAGE_DEFAULT } = require("../OptimizationStages");
|
const { STAGE_DEFAULT } = require("../OptimizationStages");
|
||||||
const HarmonyExportImportedSpecifierDependency = require("../dependencies/HarmonyExportImportedSpecifierDependency");
|
const HarmonyExportImportedSpecifierDependency = require("../dependencies/HarmonyExportImportedSpecifierDependency");
|
||||||
const HarmonyImportSideEffectDependency = require("../dependencies/HarmonyImportSideEffectDependency");
|
|
||||||
const HarmonyImportSpecifierDependency = require("../dependencies/HarmonyImportSpecifierDependency");
|
const HarmonyImportSpecifierDependency = require("../dependencies/HarmonyImportSpecifierDependency");
|
||||||
|
|
||||||
/** @typedef {import("../Compiler")} Compiler */
|
/** @typedef {import("../Compiler")} Compiler */
|
||||||
|
@ -104,12 +103,12 @@ class SideEffectsFlagPlugin {
|
||||||
|
|
||||||
// Capture reexports of sideEffectFree modules
|
// Capture reexports of sideEffectFree modules
|
||||||
for (const module of modules) {
|
for (const module of modules) {
|
||||||
for (const dep of module.dependencies) {
|
if (
|
||||||
if (dep instanceof HarmonyExportImportedSpecifierDependency) {
|
module.factoryMeta !== undefined &&
|
||||||
if (
|
module.factoryMeta.sideEffectFree
|
||||||
module.factoryMeta !== undefined &&
|
) {
|
||||||
module.factoryMeta.sideEffectFree
|
for (const dep of module.dependencies) {
|
||||||
) {
|
if (dep instanceof HarmonyExportImportedSpecifierDependency) {
|
||||||
const mode = dep.getMode(moduleGraph, true);
|
const mode = dep.getMode(moduleGraph, true);
|
||||||
if (mode.type === "normal-reexport") {
|
if (mode.type === "normal-reexport") {
|
||||||
let map = reexportMaps.get(module);
|
let map = reexportMaps.get(module);
|
||||||
|
@ -127,18 +126,6 @@ class SideEffectsFlagPlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (dep instanceof HarmonyImportSideEffectDependency) {
|
|
||||||
const connection = moduleGraph.getConnection(dep);
|
|
||||||
if (connection) {
|
|
||||||
const refModule = connection.resolvedModule;
|
|
||||||
if (
|
|
||||||
refModule &&
|
|
||||||
refModule.factoryMeta !== undefined &&
|
|
||||||
refModule.factoryMeta.sideEffectFree
|
|
||||||
) {
|
|
||||||
connection.active = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue