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-21 20:44:46 +08:00
|
|
|
const { Dependency } = require(".");
|
2019-10-30 14:57:55 +08:00
|
|
|
const { NO_EXPORTS_REFERENCED } = require("./Dependency");
|
2019-03-14 19:06:59 +08:00
|
|
|
const { UsageState } = require("./ModuleGraph");
|
2018-07-31 04:30:27 +08:00
|
|
|
const { STAGE_DEFAULT } = require("./OptimizationStages");
|
2019-01-23 19:12:44 +08:00
|
|
|
const Queue = require("./util/Queue");
|
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 */
|
2018-08-07 01:39:43 +08:00
|
|
|
/** @typedef {import("./Dependency")} Dependency */
|
2020-05-28 02:34:55 +08:00
|
|
|
/** @typedef {import("./Dependency").ReferencedExport} ReferencedExport */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("./Module")} Module */
|
2019-03-14 19:06:59 +08:00
|
|
|
/** @typedef {import("./ModuleGraph").ExportsInfo} ExportsInfo */
|
|
|
|
|
2017-02-23 23:16:56 +08:00
|
|
|
class FlagDependencyUsagePlugin {
|
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) {
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.compilation.tap("FlagDependencyUsagePlugin", 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(
|
2018-12-09 19:54:17 +08:00
|
|
|
{
|
2018-07-31 04:30:27 +08:00
|
|
|
name: "FlagDependencyUsagePlugin",
|
|
|
|
stage: STAGE_DEFAULT
|
2018-12-09 19:54:17 +08:00
|
|
|
},
|
2018-02-25 09:00:20 +08:00
|
|
|
modules => {
|
2020-01-30 18:34:33 +08:00
|
|
|
const logger = compilation.getLogger(
|
|
|
|
"webpack.FlagDependencyUsagePlugin"
|
|
|
|
);
|
|
|
|
|
2019-03-14 19:06:59 +08:00
|
|
|
/** @type {Map<ExportsInfo, Module>} */
|
|
|
|
const exportInfoToModuleMap = new Map();
|
|
|
|
|
2018-07-20 22:24:35 +08:00
|
|
|
/**
|
|
|
|
* @param {Module} module module to process
|
2020-05-28 02:34:55 +08:00
|
|
|
* @param {(string[] | ReferencedExport)[]} usedExports list of used exports
|
2018-07-20 22:24:35 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2020-07-21 20:44:46 +08:00
|
|
|
const processReferencedModule = (module, usedExports) => {
|
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) {
|
|
|
|
if (exportsInfo.setUsedWithoutInfo()) {
|
|
|
|
queue.enqueue(module);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
if (exportsInfo.setUsedInUnknownWay()) {
|
|
|
|
queue.enqueue(module);
|
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) {
|
2019-03-14 19:06:59 +08:00
|
|
|
if (exportInfo.used === UsageState.Unused) {
|
|
|
|
exportInfo.used = UsageState.OnlyPropertiesUsed;
|
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) {
|
|
|
|
queue.enqueue(currentModule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
currentExportsInfo = nestedInfo;
|
2019-10-31 06:24:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (exportInfo.used !== UsageState.Used) {
|
|
|
|
exportInfo.used = UsageState.Used;
|
|
|
|
const currentModule =
|
|
|
|
currentExportsInfo === exportsInfo
|
|
|
|
? module
|
|
|
|
: exportInfoToModuleMap.get(currentExportsInfo);
|
|
|
|
if (currentModule) {
|
|
|
|
queue.enqueue(currentModule);
|
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 (
|
|
|
|
module.factoryMeta !== undefined &&
|
|
|
|
module.factoryMeta.sideEffectFree
|
|
|
|
)
|
|
|
|
return;
|
2019-03-14 19:06:59 +08:00
|
|
|
if (exportsInfo.setUsedForSideEffectsOnly()) {
|
|
|
|
queue.enqueue(module);
|
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
|
|
|
};
|
2015-10-22 03:05:01 +08:00
|
|
|
|
2018-08-07 01:39:43 +08:00
|
|
|
/**
|
|
|
|
* @param {DependenciesBlock} depBlock the dependencies block
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-01-23 19:12:44 +08:00
|
|
|
const processDependenciesBlock = depBlock => {
|
2020-07-21 20:44:46 +08:00
|
|
|
/** @type {Map<Module, (string[] | ReferencedExport)[] | Map<string, string[] | ReferencedExport>>} */
|
|
|
|
const map = new Map();
|
2016-12-05 06:47:19 +08:00
|
|
|
|
2020-07-21 20:44:46 +08:00
|
|
|
const queue = [depBlock];
|
|
|
|
for (const block of queue) {
|
|
|
|
for (const b of block.blocks) {
|
|
|
|
queue.push(b);
|
|
|
|
}
|
|
|
|
for (const dep of block.dependencies) {
|
|
|
|
const connection = moduleGraph.getConnection(dep);
|
|
|
|
if (!connection || !connection.module || !connection.active) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const { module } = connection;
|
|
|
|
const oldReferencedExports = map.get(module);
|
|
|
|
if (
|
|
|
|
oldReferencedExports === Dependency.EXPORTS_OBJECT_REFERENCED
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const referencedExports = compilation.getDependencyReferencedExports(
|
|
|
|
dep
|
|
|
|
);
|
|
|
|
if (
|
|
|
|
oldReferencedExports === undefined ||
|
|
|
|
oldReferencedExports === Dependency.NO_EXPORTS_REFERENCED ||
|
|
|
|
referencedExports === Dependency.EXPORTS_OBJECT_REFERENCED
|
|
|
|
) {
|
|
|
|
map.set(module, referencedExports);
|
|
|
|
} else if (
|
|
|
|
oldReferencedExports === Dependency.NO_EXPORTS_REFERENCED
|
|
|
|
) {
|
|
|
|
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)) {
|
|
|
|
processReferencedModule(module, referencedExports);
|
|
|
|
} else {
|
|
|
|
processReferencedModule(
|
|
|
|
module,
|
|
|
|
Array.from(referencedExports.values())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
/** @type {Queue<DependenciesBlock>} */
|
|
|
|
const queue = new Queue();
|
|
|
|
|
2020-05-26 06:46:09 +08:00
|
|
|
const processEntryDependency = dep => {
|
|
|
|
const module = moduleGraph.getModule(dep);
|
|
|
|
if (module) {
|
2020-07-21 20:44:46 +08:00
|
|
|
processReferencedModule(module, NO_EXPORTS_REFERENCED);
|
2020-05-26 06:46:09 +08:00
|
|
|
queue.enqueue(module);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for (const dep of compilation.globalEntry.dependencies) {
|
|
|
|
processEntryDependency(dep);
|
|
|
|
}
|
|
|
|
for (const dep of compilation.globalEntry.includeDependencies) {
|
|
|
|
processEntryDependency(dep);
|
|
|
|
}
|
|
|
|
for (const {
|
|
|
|
dependencies: deps,
|
|
|
|
includeDependencies: includeDeps
|
|
|
|
} of compilation.entries.values()) {
|
2019-01-21 02:30:27 +08:00
|
|
|
for (const dep of deps) {
|
2020-05-26 06:46:09 +08:00
|
|
|
processEntryDependency(dep);
|
|
|
|
}
|
|
|
|
for (const dep of includeDeps) {
|
|
|
|
processEntryDependency(dep);
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
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) {
|
2019-01-23 19:12:44 +08:00
|
|
|
const depBlock = queue.dequeue();
|
|
|
|
processDependenciesBlock(depBlock);
|
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;
|