2015-10-22 03:05:01 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-02-23 23:16:56 +08:00
|
|
|
"use strict";
|
2015-10-22 03:05:01 +08:00
|
|
|
|
2020-07-29 04:05:13 +08:00
|
|
|
const Dependency = require("./Dependency");
|
2020-07-28 00:09:48 +08:00
|
|
|
const { UsageState } = require("./ExportsInfo");
|
2020-10-16 14:55:08 +08:00
|
|
|
const ModuleGraphConnection = require("./ModuleGraphConnection");
|
2018-07-31 04:30:27 +08:00
|
|
|
const { STAGE_DEFAULT } = require("./OptimizationStages");
|
2021-02-02 08:19:37 +08:00
|
|
|
const ArrayQueue = require("./util/ArrayQueue");
|
2020-07-29 04:05:13 +08:00
|
|
|
const TupleQueue = require("./util/TupleQueue");
|
2020-10-16 17:43:48 +08:00
|
|
|
const { getEntryRuntime, mergeRuntimeOwned } = require("./util/runtime");
|
2018-08-07 01:39:43 +08:00
|
|
|
|
2018-07-28 03:12:09 +08:00
|
|
|
/** @typedef {import("./Compiler")} Compiler */
|
2018-06-08 16:34:38 +08:00
|
|
|
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
|
2020-05-28 02:34:55 +08:00
|
|
|
/** @typedef {import("./Dependency").ReferencedExport} ReferencedExport */
|
2025-09-11 08:10:10 +08:00
|
|
|
/** @typedef {import("./Dependency").ReferencedExports} ReferencedExports */
|
2020-07-28 00:09:48 +08:00
|
|
|
/** @typedef {import("./ExportsInfo")} ExportsInfo */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("./Module")} Module */
|
2020-07-28 00:09:48 +08:00
|
|
|
/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
|
2019-03-14 19:06:59 +08:00
|
|
|
|
2020-07-29 04:05:13 +08:00
|
|
|
const { NO_EXPORTS_REFERENCED, EXPORTS_OBJECT_REFERENCED } = Dependency;
|
|
|
|
|
2023-03-31 23:18:47 +08:00
|
|
|
const PLUGIN_NAME = "FlagDependencyUsagePlugin";
|
|
|
|
const PLUGIN_LOGGER_NAME = `webpack.${PLUGIN_NAME}`;
|
|
|
|
|
2017-02-23 23:16:56 +08:00
|
|
|
class FlagDependencyUsagePlugin {
|
2020-07-28 00:09:48 +08:00
|
|
|
/**
|
|
|
|
* @param {boolean} global do a global analysis instead of per runtime
|
|
|
|
*/
|
|
|
|
constructor(global) {
|
|
|
|
this.global = global;
|
|
|
|
}
|
|
|
|
|
2018-07-28 03:12:09 +08:00
|
|
|
/**
|
2020-04-23 16:48:36 +08:00
|
|
|
* Apply the plugin
|
2018-07-28 03:12:09 +08:00
|
|
|
* @param {Compiler} compiler the compiler instance
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-02-23 23:16:56 +08:00
|
|
|
apply(compiler) {
|
2025-07-17 00:13:14 +08:00
|
|
|
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
2018-08-07 01:39:43 +08:00
|
|
|
const moduleGraph = compilation.moduleGraph;
|
2018-05-27 05:07:02 +08:00
|
|
|
compilation.hooks.optimizeDependencies.tap(
|
2023-03-31 23:18:47 +08:00
|
|
|
{ name: PLUGIN_NAME, stage: STAGE_DEFAULT },
|
2025-07-17 00:13:14 +08:00
|
|
|
(modules) => {
|
2021-10-05 19:08:21 +08:00
|
|
|
if (compilation.moduleMemCaches) {
|
|
|
|
throw new Error(
|
|
|
|
"optimization.usedExports can't be used with cacheUnaffected as export usage is a global effect"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-31 23:18:47 +08:00
|
|
|
const logger = compilation.getLogger(PLUGIN_LOGGER_NAME);
|
2019-03-14 19:06:59 +08:00
|
|
|
/** @type {Map<ExportsInfo, Module>} */
|
|
|
|
const exportInfoToModuleMap = new Map();
|
|
|
|
|
2025-04-03 00:02:22 +08:00
|
|
|
/** @type {TupleQueue<Module, RuntimeSpec>} */
|
2020-07-29 04:05:13 +08:00
|
|
|
const queue = new TupleQueue();
|
2020-07-28 00:09:48 +08:00
|
|
|
|
2018-07-20 22:24:35 +08:00
|
|
|
/**
|
|
|
|
* @param {Module} module module to process
|
2025-09-11 08:10:10 +08:00
|
|
|
* @param {ReferencedExports} usedExports list of used exports
|
2020-07-30 17:18:09 +08:00
|
|
|
* @param {RuntimeSpec} runtime part of which runtime
|
2020-10-15 03:08:14 +08:00
|
|
|
* @param {boolean} forceSideEffects always apply side effects
|
2018-07-20 22:24:35 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2020-10-15 03:08:14 +08:00
|
|
|
const processReferencedModule = (
|
|
|
|
module,
|
|
|
|
usedExports,
|
|
|
|
runtime,
|
|
|
|
forceSideEffects
|
|
|
|
) => {
|
2019-01-23 19:12:44 +08:00
|
|
|
const exportsInfo = moduleGraph.getExportsInfo(module);
|
2019-03-14 19:06:59 +08:00
|
|
|
if (usedExports.length > 0) {
|
2020-07-08 16:58:20 +08:00
|
|
|
if (!module.buildMeta || !module.buildMeta.exportsType) {
|
2020-07-28 00:09:48 +08:00
|
|
|
if (exportsInfo.setUsedWithoutInfo(runtime)) {
|
2020-07-29 04:05:13 +08:00
|
|
|
queue.enqueue(module, runtime);
|
2020-07-08 16:58:20 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-06-10 19:31:01 +08:00
|
|
|
for (const usedExportInfo of usedExports) {
|
|
|
|
let usedExport;
|
|
|
|
let canMangle = true;
|
|
|
|
if (Array.isArray(usedExportInfo)) {
|
|
|
|
usedExport = usedExportInfo;
|
|
|
|
} else {
|
|
|
|
usedExport = usedExportInfo.name;
|
|
|
|
canMangle = usedExportInfo.canMangle !== false;
|
|
|
|
}
|
2019-03-14 19:06:59 +08:00
|
|
|
if (usedExport.length === 0) {
|
2020-07-28 00:09:48 +08:00
|
|
|
if (exportsInfo.setUsedInUnknownWay(runtime)) {
|
2020-07-29 04:05:13 +08:00
|
|
|
queue.enqueue(module, runtime);
|
2019-01-28 17:40:32 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-31 06:24:13 +08:00
|
|
|
let currentExportsInfo = exportsInfo;
|
|
|
|
for (let i = 0; i < usedExport.length; i++) {
|
|
|
|
const exportInfo = currentExportsInfo.getExportInfo(
|
2020-06-10 19:31:01 +08:00
|
|
|
usedExport[i]
|
2019-10-31 06:24:13 +08:00
|
|
|
);
|
2020-06-10 19:31:01 +08:00
|
|
|
if (canMangle === false) {
|
|
|
|
exportInfo.canMangleUse = false;
|
2020-05-28 02:34:55 +08:00
|
|
|
}
|
2019-10-31 06:24:13 +08:00
|
|
|
const lastOne = i === usedExport.length - 1;
|
|
|
|
if (!lastOne) {
|
|
|
|
const nestedInfo = exportInfo.getNestedExportsInfo();
|
|
|
|
if (nestedInfo) {
|
2020-07-28 00:09:48 +08:00
|
|
|
if (
|
|
|
|
exportInfo.setUsedConditionally(
|
2025-07-17 00:13:14 +08:00
|
|
|
(used) => used === UsageState.Unused,
|
2020-07-28 00:09:48 +08:00
|
|
|
UsageState.OnlyPropertiesUsed,
|
|
|
|
runtime
|
|
|
|
)
|
|
|
|
) {
|
2019-05-30 03:58:26 +08:00
|
|
|
const currentModule =
|
|
|
|
currentExportsInfo === exportsInfo
|
|
|
|
? module
|
|
|
|
: exportInfoToModuleMap.get(currentExportsInfo);
|
2019-03-14 19:06:59 +08:00
|
|
|
if (currentModule) {
|
2020-07-29 04:05:13 +08:00
|
|
|
queue.enqueue(currentModule, runtime);
|
2019-03-14 19:06:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
currentExportsInfo = nestedInfo;
|
2019-10-31 06:24:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 00:09:48 +08:00
|
|
|
if (
|
|
|
|
exportInfo.setUsedConditionally(
|
2025-07-17 00:13:14 +08:00
|
|
|
(v) => v !== UsageState.Used,
|
2020-07-28 00:09:48 +08:00
|
|
|
UsageState.Used,
|
|
|
|
runtime
|
|
|
|
)
|
|
|
|
) {
|
2019-10-31 06:24:13 +08:00
|
|
|
const currentModule =
|
|
|
|
currentExportsInfo === exportsInfo
|
|
|
|
? module
|
|
|
|
: exportInfoToModuleMap.get(currentExportsInfo);
|
|
|
|
if (currentModule) {
|
2020-07-29 04:05:13 +08:00
|
|
|
queue.enqueue(currentModule, runtime);
|
2019-03-14 19:06:59 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 06:24:13 +08:00
|
|
|
break;
|
2019-01-28 17:40:32 +08:00
|
|
|
}
|
2018-08-07 01:39:43 +08:00
|
|
|
}
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-23 19:12:44 +08:00
|
|
|
// for a module without side effects we stop tracking usage here when no export is used
|
|
|
|
// This module won't be evaluated in this case
|
2019-05-29 17:51:47 +08:00
|
|
|
// TODO webpack 6 remove this check
|
2019-11-08 19:52:32 +08:00
|
|
|
if (
|
2020-10-15 03:08:14 +08:00
|
|
|
!forceSideEffects &&
|
2019-11-08 19:52:32 +08:00
|
|
|
module.factoryMeta !== undefined &&
|
|
|
|
module.factoryMeta.sideEffectFree
|
2020-10-15 03:08:14 +08:00
|
|
|
) {
|
2019-11-08 19:52:32 +08:00
|
|
|
return;
|
2020-10-15 03:08:14 +08:00
|
|
|
}
|
2020-07-28 00:09:48 +08:00
|
|
|
if (exportsInfo.setUsedForSideEffectsOnly(runtime)) {
|
2020-07-29 04:05:13 +08:00
|
|
|
queue.enqueue(module, runtime);
|
2019-03-14 19:06:59 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
|
|
|
};
|
2015-10-22 03:05:01 +08:00
|
|
|
|
2018-08-07 01:39:43 +08:00
|
|
|
/**
|
2020-09-08 00:02:14 +08:00
|
|
|
* @param {DependenciesBlock} module the module
|
2020-07-30 17:18:09 +08:00
|
|
|
* @param {RuntimeSpec} runtime part of which runtime
|
2021-02-10 23:23:36 +08:00
|
|
|
* @param {boolean} forceSideEffects always apply side effects
|
2018-08-07 01:39:43 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-02-10 23:23:36 +08:00
|
|
|
const processModule = (module, runtime, forceSideEffects) => {
|
2025-09-11 08:10:10 +08:00
|
|
|
/** @type {Map<Module, ReferencedExports | Map<string, string[] | ReferencedExport>>} */
|
2020-07-21 20:44:46 +08:00
|
|
|
const map = new Map();
|
2016-12-05 06:47:19 +08:00
|
|
|
|
2021-02-02 08:19:37 +08:00
|
|
|
/** @type {ArrayQueue<DependenciesBlock>} */
|
|
|
|
const queue = new ArrayQueue();
|
|
|
|
queue.enqueue(module);
|
|
|
|
for (;;) {
|
|
|
|
const block = queue.dequeue();
|
|
|
|
if (block === undefined) break;
|
2020-07-21 20:44:46 +08:00
|
|
|
for (const b of block.blocks) {
|
2020-10-16 17:43:48 +08:00
|
|
|
if (
|
|
|
|
!this.global &&
|
|
|
|
b.groupOptions &&
|
|
|
|
b.groupOptions.entryOptions
|
|
|
|
) {
|
2021-07-05 16:53:18 +08:00
|
|
|
processModule(
|
|
|
|
b,
|
|
|
|
b.groupOptions.entryOptions.runtime || undefined,
|
|
|
|
true
|
|
|
|
);
|
2020-09-08 00:02:14 +08:00
|
|
|
} else {
|
2021-02-02 08:19:37 +08:00
|
|
|
queue.enqueue(b);
|
2020-09-08 00:02:14 +08:00
|
|
|
}
|
2020-07-21 20:44:46 +08:00
|
|
|
}
|
|
|
|
for (const dep of block.dependencies) {
|
|
|
|
const connection = moduleGraph.getConnection(dep);
|
2020-10-16 14:55:08 +08:00
|
|
|
if (!connection || !connection.module) {
|
2020-07-21 20:44:46 +08:00
|
|
|
continue;
|
|
|
|
}
|
2020-10-16 14:55:08 +08:00
|
|
|
const activeState = connection.getActiveState(runtime);
|
|
|
|
if (activeState === false) continue;
|
2020-07-21 20:44:46 +08:00
|
|
|
const { module } = connection;
|
2020-10-16 14:55:08 +08:00
|
|
|
if (activeState === ModuleGraphConnection.TRANSITIVE_ONLY) {
|
2021-02-10 23:23:36 +08:00
|
|
|
processModule(module, runtime, false);
|
2020-10-16 14:55:08 +08:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-21 20:44:46 +08:00
|
|
|
const oldReferencedExports = map.get(module);
|
2020-07-29 04:05:13 +08:00
|
|
|
if (oldReferencedExports === EXPORTS_OBJECT_REFERENCED) {
|
2020-07-21 20:44:46 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-11 15:31:46 +08:00
|
|
|
const referencedExports =
|
|
|
|
compilation.getDependencyReferencedExports(dep, runtime);
|
2020-07-21 20:44:46 +08:00
|
|
|
if (
|
|
|
|
oldReferencedExports === undefined ||
|
2020-07-29 04:05:13 +08:00
|
|
|
oldReferencedExports === NO_EXPORTS_REFERENCED ||
|
|
|
|
referencedExports === EXPORTS_OBJECT_REFERENCED
|
2020-07-21 20:44:46 +08:00
|
|
|
) {
|
|
|
|
map.set(module, referencedExports);
|
2020-11-26 18:13:17 +08:00
|
|
|
} else if (
|
|
|
|
oldReferencedExports !== undefined &&
|
|
|
|
referencedExports === NO_EXPORTS_REFERENCED
|
|
|
|
) {
|
2020-07-21 20:44:46 +08:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
let exportsMap;
|
|
|
|
if (Array.isArray(oldReferencedExports)) {
|
|
|
|
exportsMap = new Map();
|
|
|
|
for (const item of oldReferencedExports) {
|
|
|
|
if (Array.isArray(item)) {
|
|
|
|
exportsMap.set(item.join("\n"), item);
|
|
|
|
} else {
|
|
|
|
exportsMap.set(item.name.join("\n"), item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
map.set(module, exportsMap);
|
|
|
|
} else {
|
|
|
|
exportsMap = oldReferencedExports;
|
|
|
|
}
|
|
|
|
for (const item of referencedExports) {
|
|
|
|
if (Array.isArray(item)) {
|
|
|
|
const key = item.join("\n");
|
|
|
|
const oldItem = exportsMap.get(key);
|
|
|
|
if (oldItem === undefined) {
|
|
|
|
exportsMap.set(key, item);
|
|
|
|
}
|
|
|
|
// if oldItem is already an array we have to do nothing
|
|
|
|
// if oldItem is an ReferencedExport object, we don't have to do anything
|
|
|
|
// as canMangle defaults to true for arrays
|
|
|
|
} else {
|
|
|
|
const key = item.name.join("\n");
|
|
|
|
const oldItem = exportsMap.get(key);
|
|
|
|
if (oldItem === undefined || Array.isArray(oldItem)) {
|
|
|
|
exportsMap.set(key, item);
|
|
|
|
} else {
|
|
|
|
exportsMap.set(key, {
|
|
|
|
name: item.name,
|
|
|
|
canMangle: item.canMangle && oldItem.canMangle
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 04:37:59 +08:00
|
|
|
}
|
2019-03-14 19:06:59 +08:00
|
|
|
|
2020-07-21 20:44:46 +08:00
|
|
|
for (const [module, referencedExports] of map) {
|
|
|
|
if (Array.isArray(referencedExports)) {
|
2020-10-15 03:08:14 +08:00
|
|
|
processReferencedModule(
|
|
|
|
module,
|
|
|
|
referencedExports,
|
|
|
|
runtime,
|
2021-02-10 23:23:36 +08:00
|
|
|
forceSideEffects
|
2020-10-15 03:08:14 +08:00
|
|
|
);
|
2020-07-21 20:44:46 +08:00
|
|
|
} else {
|
|
|
|
processReferencedModule(
|
|
|
|
module,
|
2025-07-03 17:06:45 +08:00
|
|
|
[...referencedExports.values()],
|
2020-10-15 03:08:14 +08:00
|
|
|
runtime,
|
2021-02-10 23:23:36 +08:00
|
|
|
forceSideEffects
|
2020-07-21 20:44:46 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
};
|
2015-10-22 03:05:01 +08:00
|
|
|
|
2020-01-30 18:34:33 +08:00
|
|
|
logger.time("initialize exports usage");
|
2018-02-25 09:00:20 +08:00
|
|
|
for (const module of modules) {
|
2019-03-14 19:06:59 +08:00
|
|
|
const exportsInfo = moduleGraph.getExportsInfo(module);
|
|
|
|
exportInfoToModuleMap.set(exportsInfo, module);
|
|
|
|
exportsInfo.setHasUseInfo();
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
2020-01-30 18:34:33 +08:00
|
|
|
logger.timeEnd("initialize exports usage");
|
2015-10-22 03:05:01 +08:00
|
|
|
|
2020-01-30 18:34:33 +08:00
|
|
|
logger.time("trace exports usage in graph");
|
2019-01-23 19:12:44 +08:00
|
|
|
|
2020-07-28 00:09:48 +08:00
|
|
|
/**
|
|
|
|
* @param {Dependency} dep dependency
|
2020-07-30 17:18:09 +08:00
|
|
|
* @param {RuntimeSpec} runtime runtime
|
2020-07-28 00:09:48 +08:00
|
|
|
*/
|
|
|
|
const processEntryDependency = (dep, runtime) => {
|
2020-05-26 06:46:09 +08:00
|
|
|
const module = moduleGraph.getModule(dep);
|
|
|
|
if (module) {
|
2020-10-15 03:08:14 +08:00
|
|
|
processReferencedModule(
|
|
|
|
module,
|
|
|
|
NO_EXPORTS_REFERENCED,
|
|
|
|
runtime,
|
|
|
|
true
|
|
|
|
);
|
2020-05-26 06:46:09 +08:00
|
|
|
}
|
|
|
|
};
|
2020-07-30 17:18:09 +08:00
|
|
|
/** @type {RuntimeSpec} */
|
2024-07-31 06:15:03 +08:00
|
|
|
let globalRuntime;
|
2020-07-28 00:09:48 +08:00
|
|
|
for (const [
|
|
|
|
entryName,
|
|
|
|
{ dependencies: deps, includeDependencies: includeDeps, options }
|
|
|
|
] of compilation.entries) {
|
|
|
|
const runtime = this.global
|
|
|
|
? undefined
|
|
|
|
: getEntryRuntime(compilation, entryName, options);
|
2019-01-21 02:30:27 +08:00
|
|
|
for (const dep of deps) {
|
2020-07-28 00:09:48 +08:00
|
|
|
processEntryDependency(dep, runtime);
|
2020-05-26 06:46:09 +08:00
|
|
|
}
|
|
|
|
for (const dep of includeDeps) {
|
2020-07-28 00:09:48 +08:00
|
|
|
processEntryDependency(dep, runtime);
|
|
|
|
}
|
2020-07-30 17:18:09 +08:00
|
|
|
globalRuntime = mergeRuntimeOwned(globalRuntime, runtime);
|
2020-07-28 00:09:48 +08:00
|
|
|
}
|
2020-07-30 17:18:09 +08:00
|
|
|
for (const dep of compilation.globalEntry.dependencies) {
|
|
|
|
processEntryDependency(dep, globalRuntime);
|
|
|
|
}
|
|
|
|
for (const dep of compilation.globalEntry.includeDependencies) {
|
|
|
|
processEntryDependency(dep, globalRuntime);
|
2017-11-08 18:32:05 +08:00
|
|
|
}
|
2017-02-23 23:16:56 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
while (queue.length) {
|
2023-06-17 06:33:17 +08:00
|
|
|
const [module, runtime] = /** @type {[Module, RuntimeSpec]} */ (
|
|
|
|
queue.dequeue()
|
|
|
|
);
|
2021-02-10 23:23:36 +08:00
|
|
|
processModule(module, runtime, false);
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
2020-01-30 18:34:33 +08:00
|
|
|
logger.timeEnd("trace exports usage in graph");
|
2017-11-08 18:32:05 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2017-02-23 23:16:56 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-07-28 03:12:09 +08:00
|
|
|
|
2017-02-23 23:16:56 +08:00
|
|
|
module.exports = FlagDependencyUsagePlugin;
|