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
|
|
|
|
2018-07-31 04:30:27 +08:00
|
|
|
const { STAGE_DEFAULT } = require("./OptimizationStages");
|
2018-08-07 01:39:43 +08:00
|
|
|
const SortableSet = require("./util/SortableSet");
|
|
|
|
|
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 */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("./Module")} Module */
|
2018-06-08 16:34:38 +08:00
|
|
|
|
2018-08-07 01:39:43 +08:00
|
|
|
/** @typedef {false | true | SortableSet<string>} UsedExports */
|
2018-06-08 16:34:38 +08:00
|
|
|
|
2018-07-20 22:24:35 +08:00
|
|
|
/**
|
2018-08-07 01:39:43 +08:00
|
|
|
* @param {UsedExports} moduleUsedExports the current used exports of the module
|
|
|
|
* @param {false | true | string[]} newUsedExports the new used exports
|
|
|
|
* @returns {boolean} true, if the newUsedExports is part of the moduleUsedExports
|
2018-07-20 22:24:35 +08:00
|
|
|
*/
|
2018-08-07 01:39:43 +08:00
|
|
|
const isContained = (moduleUsedExports, newUsedExports) => {
|
|
|
|
if (moduleUsedExports === null) return false;
|
|
|
|
if (moduleUsedExports === true) return true;
|
|
|
|
if (newUsedExports === true) return false;
|
|
|
|
if (newUsedExports === false) return true;
|
|
|
|
if (moduleUsedExports === false) return false;
|
2018-08-07 03:01:24 +08:00
|
|
|
if (newUsedExports.length > moduleUsedExports.size) return false;
|
2018-08-07 01:39:43 +08:00
|
|
|
return newUsedExports.every(item => moduleUsedExports.has(item));
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
|
|
|
|
2017-02-23 23:16:56 +08:00
|
|
|
class FlagDependencyUsagePlugin {
|
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-07-31 04:30:27 +08:00
|
|
|
/** @type {TODO} */ ({
|
|
|
|
name: "FlagDependencyUsagePlugin",
|
|
|
|
stage: STAGE_DEFAULT
|
|
|
|
}),
|
2018-02-25 09:00:20 +08:00
|
|
|
modules => {
|
2018-07-20 22:24:35 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Module} module module to process
|
2018-08-07 01:39:43 +08:00
|
|
|
* @param {boolean | string[]} usedExports list of used exports
|
2018-07-20 22:24:35 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-02-25 09:00:20 +08:00
|
|
|
const processModule = (module, usedExports) => {
|
2018-08-07 01:39:43 +08:00
|
|
|
let ue = module.getUsedExports(moduleGraph);
|
|
|
|
if (ue === true) return;
|
2018-05-29 20:50:40 +08:00
|
|
|
if (usedExports === true) {
|
2018-08-07 01:39:43 +08:00
|
|
|
module.setUsedExports(moduleGraph, (ue = true));
|
2018-05-29 20:50:40 +08:00
|
|
|
} else if (Array.isArray(usedExports)) {
|
2018-08-07 01:39:43 +08:00
|
|
|
if (!ue) {
|
|
|
|
module.setUsedExports(
|
|
|
|
moduleGraph,
|
|
|
|
(ue = new SortableSet(usedExports))
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const old = ue ? ue.size : -1;
|
|
|
|
for (const exportName of usedExports) {
|
|
|
|
ue.add(exportName);
|
|
|
|
}
|
|
|
|
if (ue.size === old) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-07 03:01:24 +08:00
|
|
|
if (ue !== false) return;
|
|
|
|
module.setUsedExports(moduleGraph, (ue = new SortableSet()));
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2015-10-22 03:05:01 +08:00
|
|
|
|
2018-02-25 09:00:20 +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
|
|
|
|
if (module.factoryMeta.sideEffectFree) {
|
2018-08-07 03:01:24 +08:00
|
|
|
if (ue !== true && ue.size === 0) return;
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
2017-08-08 15:40:17 +08:00
|
|
|
|
2018-08-07 01:39:43 +08:00
|
|
|
queue.push([module, module, ue]);
|
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 {Module} module the module
|
|
|
|
* @param {DependenciesBlock} depBlock the dependencies block
|
|
|
|
* @param {UsedExports} usedExports the used exports
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-06-08 16:34:38 +08:00
|
|
|
const processDependenciesBlock = (module, depBlock, usedExports) => {
|
2018-02-25 09:00:20 +08:00
|
|
|
for (const dep of depBlock.dependencies) {
|
2018-06-08 16:34:38 +08:00
|
|
|
processDependency(module, dep);
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
for (const block of depBlock.blocks) {
|
2018-06-08 16:34:38 +08:00
|
|
|
queue.push([module, block, usedExports]);
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
|
|
|
};
|
2016-12-05 06:47:19 +08:00
|
|
|
|
2018-08-07 01:39:43 +08:00
|
|
|
/**
|
|
|
|
* @param {Module} module the module
|
|
|
|
* @param {Dependency} dep the dependency
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-06-08 16:34:38 +08:00
|
|
|
const processDependency = (module, dep) => {
|
|
|
|
const reference = compilation.getDependencyReference(module, dep);
|
2018-02-25 09:00:20 +08:00
|
|
|
if (!reference) return;
|
2018-06-08 16:34:38 +08:00
|
|
|
const referenceModule = reference.module;
|
2018-02-25 09:00:20 +08:00
|
|
|
const importedNames = reference.importedNames;
|
2018-08-07 01:39:43 +08:00
|
|
|
const oldUsedExports = referenceModule.getUsedExports(moduleGraph);
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
2018-08-07 03:01:24 +08:00
|
|
|
!oldUsedExports ||
|
|
|
|
!isContained(oldUsedExports, importedNames)
|
2018-02-25 09:00:20 +08:00
|
|
|
) {
|
2018-06-08 16:34:38 +08:00
|
|
|
processModule(referenceModule, importedNames);
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
|
|
|
};
|
2015-10-22 03:05:01 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
for (const module of modules) {
|
2018-08-07 03:01:24 +08:00
|
|
|
module.setUsedExports(moduleGraph, false);
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
2015-10-22 03:05:01 +08:00
|
|
|
|
2018-06-08 16:34:38 +08:00
|
|
|
/** @type {[Module, DependenciesBlock, UsedExports][]} */
|
2018-02-25 09:00:20 +08:00
|
|
|
const queue = [];
|
2018-08-14 22:40:37 +08:00
|
|
|
for (const [, deps] of compilation.entryDependencies) {
|
|
|
|
const lastDependency = deps[deps.length - 1];
|
|
|
|
if (lastDependency) {
|
|
|
|
const module = moduleGraph.getModule(lastDependency);
|
|
|
|
if (module) {
|
|
|
|
processModule(module, true);
|
|
|
|
}
|
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) {
|
|
|
|
const queueItem = queue.pop();
|
2018-06-08 16:34:38 +08:00
|
|
|
processDependenciesBlock(queueItem[0], queueItem[1], queueItem[2]);
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
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;
|