webpack/lib/optimize/SideEffectsFlagPlugin.js

211 lines
6.2 KiB
JavaScript
Raw Normal View History

2017-08-08 15:40:17 +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-08-08 15:40:17 +08:00
"use strict";
const glob2regexp = require("glob-to-regexp");
const { STAGE_DEFAULT } = require("../OptimizationStages");
2017-08-08 15:40:17 +08:00
const HarmonyExportImportedSpecifierDependency = require("../dependencies/HarmonyExportImportedSpecifierDependency");
const HarmonyImportSpecifierDependency = require("../dependencies/HarmonyImportSpecifierDependency");
2018-07-20 22:24:35 +08:00
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Dependency")} Dependency */
2018-07-30 23:08:51 +08:00
/** @typedef {import("../Module")} Module */
/**
* @typedef {Object} ExportInModule
* @property {Module} module the module
* @property {string} exportName the name of the export
*/
/** @type {WeakMap<any, Map<string, RegExp>>} */
const globToRegexpCache = new WeakMap();
/**
* @param {string} glob the pattern
* @param {Map<string, RegExp>} cache the glob to RegExp cache
* @returns {RegExp} a regular expression
*/
const globToRegexp = (glob, cache) => {
const cacheEntry = cache.get(glob);
if (cacheEntry !== undefined) return cacheEntry;
if (!glob.includes("/")) {
glob = `**/${glob}`;
}
const baseRegexp = glob2regexp(glob, { globstar: true, extended: true });
const regexpSource = baseRegexp.source;
const regexp = new RegExp("^(\\./)?" + regexpSource.slice(1));
cache.set(glob, regexp);
return regexp;
};
2017-09-14 19:35:25 +08:00
class SideEffectsFlagPlugin {
2018-07-20 22:24:35 +08:00
/**
* @param {Compiler} compiler webpack compiler
* @returns {void}
*/
2017-08-08 15:40:17 +08:00
apply(compiler) {
let cache = globToRegexpCache.get(compiler.root);
if (cache === undefined) {
cache = new Map();
globToRegexpCache.set(compiler.root, cache);
}
2018-02-25 09:00:20 +08:00
compiler.hooks.normalModuleFactory.tap("SideEffectsFlagPlugin", nmf => {
2017-12-14 04:35:39 +08:00
nmf.hooks.module.tap("SideEffectsFlagPlugin", (module, data) => {
2017-08-08 15:40:17 +08:00
const resolveData = data.resourceResolveData;
2018-02-25 09:00:20 +08:00
if (
resolveData &&
resolveData.descriptionFileData &&
resolveData.relativePath
) {
2017-10-12 23:32:41 +08:00
const sideEffects = resolveData.descriptionFileData.sideEffects;
2018-02-25 09:00:20 +08:00
const hasSideEffects = SideEffectsFlagPlugin.moduleHasSideEffects(
resolveData.relativePath,
sideEffects,
cache
2018-02-25 09:00:20 +08:00
);
if (!hasSideEffects) {
if (module.factoryMeta === undefined) {
module.factoryMeta = {};
}
module.factoryMeta.sideEffectFree = true;
2017-08-08 15:40:17 +08:00
}
}
return module;
});
2017-12-06 19:09:34 +08:00
nmf.hooks.module.tap("SideEffectsFlagPlugin", (module, data) => {
if (data.settings.sideEffects === false) {
if (module.factoryMeta === undefined) {
module.factoryMeta = {};
}
2017-12-06 19:09:34 +08:00
module.factoryMeta.sideEffectFree = true;
} else if (data.settings.sideEffects === true) {
if (module.factoryMeta !== undefined) {
module.factoryMeta.sideEffectFree = false;
}
}
return module;
2017-12-06 19:09:34 +08:00
});
2017-08-08 15:40:17 +08:00
});
2018-02-25 09:00:20 +08:00
compiler.hooks.compilation.tap("SideEffectsFlagPlugin", compilation => {
const moduleGraph = compilation.moduleGraph;
2018-02-25 09:00:20 +08:00
compilation.hooks.optimizeDependencies.tap(
2018-12-09 19:54:17 +08:00
{
name: "SideEffectsFlagPlugin",
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.SideEffectsFlagPlugin");
/** @type {Map<Module, Map<string, ExportInModule>>} */
2018-02-25 09:00:20 +08:00
const reexportMaps = new Map();
2017-08-08 15:40:17 +08:00
2018-02-25 09:00:20 +08:00
// Capture reexports of sideEffectFree modules
2020-01-30 18:34:33 +08:00
logger.time("capture reexports from modules");
2018-02-25 09:00:20 +08:00
for (const module of modules) {
if (
module.factoryMeta !== undefined &&
module.factoryMeta.sideEffectFree
) {
for (const dep of module.dependencies) {
if (dep instanceof HarmonyExportImportedSpecifierDependency) {
const mode = dep.getMode(moduleGraph, true);
if (mode.type === "normal-reexport") {
2018-02-25 09:00:20 +08:00
let map = reexportMaps.get(module);
if (!map) {
reexportMaps.set(module, (map = new Map()));
}
for (const [key, ids] of mode.map) {
2019-10-30 16:25:35 +08:00
// TODO Support reexporting namespace object
if (ids.length > 0 && !mode.checked.has(key)) {
map.set(key, {
module: moduleGraph.getModule(dep),
exportName: ids[0]
});
}
2018-02-25 09:00:20 +08:00
}
2017-08-08 15:40:17 +08:00
}
}
}
}
}
2020-01-30 18:34:33 +08:00
logger.timeEnd("capture reexports from modules");
2017-08-08 15:40:17 +08:00
2018-02-25 09:00:20 +08:00
// Flatten reexports
2020-01-30 18:34:33 +08:00
logger.time("flatten reexports");
2018-02-25 09:00:20 +08:00
for (const map of reexportMaps.values()) {
for (const pair of map) {
let mapping = pair[1];
while (mapping) {
const innerMap = reexportMaps.get(mapping.module);
if (!innerMap) break;
const newMapping = innerMap.get(mapping.exportName);
if (newMapping) {
map.set(pair[0], newMapping);
}
mapping = newMapping;
2017-08-08 15:40:17 +08:00
}
}
}
2020-01-30 18:34:33 +08:00
logger.timeEnd("flatten reexports");
2017-08-08 15:40:17 +08:00
2018-02-25 09:00:20 +08:00
// Update imports along the reexports from sideEffectFree modules
2020-01-30 18:34:33 +08:00
logger.time("update imports");
2019-10-30 16:25:35 +08:00
for (const [module, map] of reexportMaps) {
for (const connection of moduleGraph.getIncomingConnections(
module
)) {
const dep = connection.dependency;
if (
dep instanceof HarmonyExportImportedSpecifierDependency ||
(dep instanceof HarmonyImportSpecifierDependency &&
!dep.namespaceObjectAsContext)
) {
// TODO improve for nested imports
const ids = dep.getIds(moduleGraph);
if (ids.length > 0) {
const mapping = map.get(ids[0]);
if (mapping) {
moduleGraph.updateModule(dep, mapping.module);
moduleGraph.addExplanation(
dep,
"(skipped side-effect-free modules)"
);
dep.setIds(
moduleGraph,
mapping.exportName
? [mapping.exportName, ...ids.slice(1)]
: ids.slice(1)
);
continue;
}
2018-02-25 09:00:20 +08:00
}
2017-08-08 15:40:17 +08:00
}
}
}
2020-01-30 18:34:33 +08:00
logger.timeEnd("update imports");
2017-08-08 15:40:17 +08:00
}
2018-02-25 09:00:20 +08:00
);
2017-08-08 15:40:17 +08:00
});
}
static moduleHasSideEffects(moduleName, flagValue, cache) {
2018-02-25 09:00:20 +08:00
switch (typeof flagValue) {
case "undefined":
return true;
case "boolean":
return flagValue;
case "string":
return globToRegexp(flagValue, cache).test(moduleName);
case "object":
2018-02-25 09:00:20 +08:00
return flagValue.some(glob =>
SideEffectsFlagPlugin.moduleHasSideEffects(moduleName, glob, cache)
2018-02-25 09:00:20 +08:00
);
}
}
2017-08-08 15:40:17 +08:00
}
2017-09-14 19:35:25 +08:00
module.exports = SideEffectsFlagPlugin;