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";
|
|
|
|
|
2019-05-17 03:12:59 +08:00
|
|
|
const glob2regexp = require("glob-to-regexp");
|
2018-07-31 04:30:27 +08:00
|
|
|
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 */
|
2018-05-27 05:07:02 +08:00
|
|
|
/** @typedef {import("../Dependency")} Dependency */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("../Module")} Module */
|
2020-10-05 22:57:31 +08:00
|
|
|
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
2018-05-27 05:07:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} ExportInModule
|
|
|
|
* @property {Module} module the module
|
|
|
|
* @property {string} exportName the name of the export
|
2020-05-19 06:25:41 +08:00
|
|
|
* @property {boolean} checked if the export is conditional
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} ReexportInfo
|
|
|
|
* @property {Map<string, ExportInModule[]>} static
|
|
|
|
* @property {Map<Module, Set<string>>} dynamic
|
2018-05-27 05:07:02 +08:00
|
|
|
*/
|
|
|
|
|
2019-05-17 03:12:59 +08:00
|
|
|
/** @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 {
|
2020-10-26 22:32:34 +08:00
|
|
|
/**
|
|
|
|
* @param {boolean} analyseSource analyse source code for side effects
|
|
|
|
*/
|
|
|
|
constructor(analyseSource = true) {
|
|
|
|
this._analyseSource = analyseSource;
|
|
|
|
}
|
2018-07-20 22:24:35 +08:00
|
|
|
/**
|
2020-04-23 16:48:36 +08:00
|
|
|
* Apply the plugin
|
|
|
|
* @param {Compiler} compiler the compiler instance
|
2018-07-20 22:24:35 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-08-08 15:40:17 +08:00
|
|
|
apply(compiler) {
|
2019-05-17 03:12:59 +08:00
|
|
|
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,
|
2019-05-17 03:12:59 +08:00
|
|
|
sideEffects,
|
|
|
|
cache
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
|
|
|
if (!hasSideEffects) {
|
2019-11-08 19:52:32 +08:00
|
|
|
if (module.factoryMeta === undefined) {
|
|
|
|
module.factoryMeta = {};
|
|
|
|
}
|
2017-12-06 19:09:17 +08:00
|
|
|
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) => {
|
2018-05-29 20:50:40 +08:00
|
|
|
if (data.settings.sideEffects === false) {
|
2019-11-08 19:52:32 +08:00
|
|
|
if (module.factoryMeta === undefined) {
|
|
|
|
module.factoryMeta = {};
|
|
|
|
}
|
2017-12-06 19:09:34 +08:00
|
|
|
module.factoryMeta.sideEffectFree = true;
|
2018-05-29 20:50:40 +08:00
|
|
|
} else if (data.settings.sideEffects === true) {
|
2019-11-08 19:52:32 +08:00
|
|
|
if (module.factoryMeta !== undefined) {
|
|
|
|
module.factoryMeta.sideEffectFree = false;
|
|
|
|
}
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2019-01-05 02:17:37 +08:00
|
|
|
return module;
|
2017-12-06 19:09:34 +08:00
|
|
|
});
|
2020-10-26 22:32:34 +08:00
|
|
|
if (this._analyseSource) {
|
|
|
|
/**
|
|
|
|
* @param {JavascriptParser} parser the parser
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
const parserHandler = parser => {
|
|
|
|
let hasSideEffects = false;
|
|
|
|
parser.hooks.program.tap("SideEffectsFlagPlugin", () => {
|
|
|
|
hasSideEffects = false;
|
|
|
|
});
|
|
|
|
parser.hooks.statement.tap(
|
|
|
|
{ name: "SideEffectsFlagPlugin", stage: -100 },
|
|
|
|
statement => {
|
|
|
|
if (hasSideEffects) return;
|
|
|
|
if (parser.scope.topLevelScope !== true) return;
|
|
|
|
switch (statement.type) {
|
|
|
|
case "ExpressionStatement":
|
|
|
|
if (
|
|
|
|
!parser.isPure(statement.expression, statement.range[0])
|
|
|
|
) {
|
|
|
|
hasSideEffects = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "IfStatement":
|
|
|
|
case "WhileStatement":
|
|
|
|
case "DoWhileStatement":
|
|
|
|
if (!parser.isPure(statement.test, statement.range[0])) {
|
|
|
|
hasSideEffects = true;
|
|
|
|
}
|
|
|
|
// statement hook will be called for child statements too
|
|
|
|
break;
|
|
|
|
case "ForStatement":
|
|
|
|
if (
|
|
|
|
!parser.isPure(statement.init, statement.range[0]) ||
|
|
|
|
!parser.isPure(
|
|
|
|
statement.test,
|
|
|
|
statement.init
|
|
|
|
? statement.init.range[1]
|
|
|
|
: statement.range[0]
|
|
|
|
) ||
|
|
|
|
!parser.isPure(
|
|
|
|
statement.update,
|
|
|
|
statement.test
|
|
|
|
? statement.test.range[1]
|
|
|
|
: statement.init
|
|
|
|
? statement.init.range[1]
|
|
|
|
: statement.range[0]
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
hasSideEffects = true;
|
|
|
|
}
|
|
|
|
// statement hook will be called for child statements too
|
|
|
|
break;
|
|
|
|
case "SwitchStatement":
|
|
|
|
if (
|
|
|
|
!parser.isPure(statement.discriminant, statement.range[0])
|
|
|
|
) {
|
|
|
|
hasSideEffects = true;
|
|
|
|
}
|
|
|
|
// statement hook will be called for child statements too
|
|
|
|
break;
|
|
|
|
case "VariableDeclaration":
|
|
|
|
case "ClassDeclaration":
|
|
|
|
case "FunctionDeclaration":
|
|
|
|
if (!parser.isPure(statement, statement.range[0])) {
|
|
|
|
hasSideEffects = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "ExportDefaultDeclaration":
|
|
|
|
if (
|
|
|
|
!parser.isPure(statement.declaration, statement.range[0])
|
|
|
|
) {
|
|
|
|
hasSideEffects = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "ExportNamedDeclaration":
|
|
|
|
if (statement.source) {
|
|
|
|
hasSideEffects = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "LabeledStatement":
|
|
|
|
case "BlockStatement":
|
|
|
|
// statement hook will be called for child statements too
|
|
|
|
break;
|
|
|
|
case "EmptyStatement":
|
|
|
|
break;
|
|
|
|
case "ImportDeclaration":
|
|
|
|
// imports will be handled by the dependencies
|
|
|
|
break;
|
|
|
|
default:
|
2020-10-05 22:57:31 +08:00
|
|
|
hasSideEffects = true;
|
2020-10-26 22:32:34 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-10-05 22:57:31 +08:00
|
|
|
}
|
2020-10-26 22:32:34 +08:00
|
|
|
);
|
|
|
|
parser.hooks.finish.tap("SideEffectsFlagPlugin", () => {
|
|
|
|
if (!hasSideEffects) {
|
|
|
|
parser.state.module.buildMeta.sideEffectFree = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
for (const key of [
|
|
|
|
"javascript/auto",
|
|
|
|
"javascript/esm",
|
|
|
|
"javascript/dynamic"
|
|
|
|
]) {
|
|
|
|
nmf.hooks.parser.for(key).tap("SideEffectsFlagPlugin", parserHandler);
|
|
|
|
}
|
2020-10-05 22:57:31 +08:00
|
|
|
}
|
2017-08-08 15:40:17 +08:00
|
|
|
});
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.compilation.tap("SideEffectsFlagPlugin", compilation => {
|
2018-07-24 23:35:36 +08:00
|
|
|
const moduleGraph = compilation.moduleGraph;
|
2018-02-25 09:00:20 +08:00
|
|
|
compilation.hooks.optimizeDependencies.tap(
|
2018-12-09 19:54:17 +08:00
|
|
|
{
|
2018-07-31 04:30:27 +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");
|
|
|
|
|
2020-08-13 05:20:45 +08:00
|
|
|
logger.time("update dependencies");
|
2018-02-25 09:00:20 +08:00
|
|
|
for (const module of modules) {
|
2020-10-05 22:57:31 +08:00
|
|
|
if (module.getSideEffectsConnectionState(moduleGraph) === false) {
|
2020-08-13 05:20:45 +08:00
|
|
|
const exportsInfo = moduleGraph.getExportsInfo(module);
|
|
|
|
for (const connection of moduleGraph.getIncomingConnections(
|
|
|
|
module
|
|
|
|
)) {
|
|
|
|
const dep = connection.dependency;
|
2020-11-16 18:18:10 +08:00
|
|
|
let isReexport;
|
2020-08-13 05:20:45 +08:00
|
|
|
if (
|
2020-11-16 18:18:10 +08:00
|
|
|
(isReexport =
|
|
|
|
dep instanceof HarmonyExportImportedSpecifierDependency) ||
|
2020-08-13 05:20:45 +08:00
|
|
|
(dep instanceof HarmonyImportSpecifierDependency &&
|
|
|
|
!dep.namespaceObjectAsContext)
|
|
|
|
) {
|
2020-11-16 18:18:10 +08:00
|
|
|
// TODO improve for export *
|
|
|
|
if (isReexport && dep.name) {
|
|
|
|
const exportInfo = moduleGraph.getExportInfo(
|
|
|
|
connection.originModule,
|
|
|
|
dep.name
|
|
|
|
);
|
|
|
|
exportInfo.moveTarget(
|
|
|
|
moduleGraph,
|
|
|
|
({ module }) =>
|
|
|
|
module.getSideEffectsConnectionState(moduleGraph) ===
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
2020-08-13 05:20:45 +08:00
|
|
|
// TODO improve for nested imports
|
|
|
|
const ids = dep.getIds(moduleGraph);
|
|
|
|
if (ids.length > 0) {
|
|
|
|
const exportInfo = exportsInfo.getExportInfo(ids[0]);
|
2020-11-16 18:18:10 +08:00
|
|
|
const target = exportInfo.moveTarget(
|
2020-08-13 05:20:45 +08:00
|
|
|
moduleGraph,
|
|
|
|
({ module }) =>
|
2020-10-05 22:57:31 +08:00
|
|
|
module.getSideEffectsConnectionState(moduleGraph) ===
|
|
|
|
false
|
2020-08-13 05:20:45 +08:00
|
|
|
);
|
|
|
|
if (!target) continue;
|
2017-08-08 15:40:17 +08:00
|
|
|
|
2020-08-13 05:20:45 +08:00
|
|
|
moduleGraph.updateModule(dep, target.module);
|
2019-03-14 19:06:59 +08:00
|
|
|
moduleGraph.addExplanation(
|
|
|
|
dep,
|
|
|
|
"(skipped side-effect-free modules)"
|
|
|
|
);
|
|
|
|
dep.setIds(
|
|
|
|
moduleGraph,
|
2020-08-13 05:20:45 +08:00
|
|
|
target.export
|
|
|
|
? [...target.export, ...ids.slice(1)]
|
2019-03-14 19:06:59 +08:00
|
|
|
: ids.slice(1)
|
|
|
|
);
|
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
2017-08-08 15:40:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 05:20:45 +08:00
|
|
|
logger.timeEnd("update dependencies");
|
2017-08-08 15:40:17 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2017-08-08 15:40:17 +08:00
|
|
|
});
|
|
|
|
}
|
2018-01-19 00:14:19 +08:00
|
|
|
|
2019-05-17 03:12:59 +08:00
|
|
|
static moduleHasSideEffects(moduleName, flagValue, cache) {
|
2018-02-25 09:00:20 +08:00
|
|
|
switch (typeof flagValue) {
|
2018-01-19 00:14:19 +08:00
|
|
|
case "undefined":
|
|
|
|
return true;
|
|
|
|
case "boolean":
|
|
|
|
return flagValue;
|
|
|
|
case "string":
|
2019-05-17 03:12:59 +08:00
|
|
|
return globToRegexp(flagValue, cache).test(moduleName);
|
2018-01-19 00:14:19 +08:00
|
|
|
case "object":
|
2018-02-25 09:00:20 +08:00
|
|
|
return flagValue.some(glob =>
|
2019-05-17 03:12:59 +08:00
|
|
|
SideEffectsFlagPlugin.moduleHasSideEffects(moduleName, glob, cache)
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2018-01-19 00:14:19 +08:00
|
|
|
}
|
|
|
|
}
|
2017-08-08 15:40:17 +08:00
|
|
|
}
|
2017-09-14 19:35:25 +08:00
|
|
|
module.exports = SideEffectsFlagPlugin;
|