Skip to content

Commit

Permalink
improve side-effects handling for dynamic reexports
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed May 19, 2020
1 parent 48229fa commit 8e7b249
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 46 deletions.
11 changes: 7 additions & 4 deletions lib/dependencies/HarmonyExportImportedSpecifierDependency.js
Expand Up @@ -27,6 +27,8 @@ class ExportMode {
this.name = null;
/** @type {Map<string, string>} */
this.map = EMPTY_MAP;
/** @type {Set<string>|null} */
this.ignored = null;
/** @type {Module|null} */
this.module = null;
/** @type {string|null} */
Expand Down Expand Up @@ -212,6 +214,10 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {

const mode = new ExportMode("dynamic-reexport");
mode.module = importedModule;
mode.ignored = new Set([
...this.activeExports,
...activeFromOtherStarExports
]);
return mode;
}

Expand Down Expand Up @@ -580,10 +586,7 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
.join("");

case "dynamic-reexport": {
const activeExports = new Set([
...dep.activeExports,
...dep._discoverActiveExportsFromOtherStartExports()
]);
const activeExports = mode.ignored;
let content =
"/* harmony reexport (unknown) */ for(var __WEBPACK_IMPORT_KEY__ in " +
importVar +
Expand Down
233 changes: 208 additions & 25 deletions lib/optimize/SideEffectsFlagPlugin.js
Expand Up @@ -16,8 +16,89 @@ const HarmonyImportSpecifierDependency = require("../dependencies/HarmonyImportS
* @typedef {Object} ExportInModule
* @property {Module} module the module
* @property {string} exportName the name of the export
* @property {boolean} checked if the export is conditional
*/

/**
* @typedef {Object} ReexportInfo
* @property {Map<string, ExportInModule[]>} static
* @property {Map<Module, Set<string>>} dynamic
*/

/**
* @param {ReexportInfo} info info object
* @param {string} exportName name of export
* @returns {ExportInModule | undefined} static export
*/
const getMappingFromInfo = (info, exportName) => {
const staticMappings = info.static.get(exportName);
if (staticMappings !== undefined) {
if (staticMappings.length === 1) return staticMappings[0];
return undefined;
}
const dynamicMappings = Array.from(info.dynamic).filter(
([_, ignored]) => !ignored.has(exportName)
);
if (dynamicMappings.length === 1) {
return {
module: dynamicMappings[0][0],
exportName,
checked: true
};
}
return undefined;
};

/**
* @param {ReexportInfo} info info object
* @param {string} exportName name of export of source module
* @param {Module} module the target module
* @param {string} innerExportName name of export of target module
* @param {boolean} checked true, if existence of target module is checked
*/
const addStaticReexport = (
info,
exportName,
module,
innerExportName,
checked
) => {
let mappings = info.static.get(exportName);
if (mappings !== undefined) {
for (const mapping of mappings) {
if (mapping.module === module && mapping.exportName === innerExportName) {
mapping.checked = mapping.checked && checked;
return;
}
}
} else {
mappings = [];
info.static.set(exportName, mappings);
}
mappings.push({
module,
exportName: innerExportName,
checked
});
};

/**
* @param {ReexportInfo} info info object
* @param {Module} module the reexport module
* @param {Set<string>} ignored ignore list
* @returns {void}
*/
const addDynamicReexport = (info, module, ignored) => {
const existingList = info.dynamic.get(module);
if (existingList !== undefined) {
for (const key of existingList) {
if (!ignored.has(key)) existingList.delete(key);
}
} else {
info.dynamic.set(module, new Set(ignored));
}
};

class SideEffectsFlagPlugin {
apply(compiler) {
compiler.hooks.normalModuleFactory.tap("SideEffectsFlagPlugin", nmf => {
Expand Down Expand Up @@ -52,7 +133,7 @@ class SideEffectsFlagPlugin {
compilation.hooks.optimizeDependencies.tap(
"SideEffectsFlagPlugin",
modules => {
/** @type {Map<Module, Map<string, ExportInModule>>} */
/** @type {Map<Module, ReexportInfo>} */
const reexportMaps = new Map();

// Capture reexports of sideEffectFree modules
Expand All @@ -69,16 +150,66 @@ class SideEffectsFlagPlugin {
) {
if (module.factoryMeta.sideEffectFree) {
const mode = dep.getMode(true);
if (mode.type === "safe-reexport") {
let map = reexportMaps.get(module);
if (!map) {
reexportMaps.set(module, (map = new Map()));
if (
mode.type === "safe-reexport" ||
mode.type === "checked-reexport" ||
mode.type === "dynamic-reexport" ||
mode.type === "reexport-non-harmony-default" ||
mode.type === "reexport-non-harmony-default-strict" ||
mode.type === "reexport-named-default"
) {
let info = reexportMaps.get(module);
if (!info) {
reexportMaps.set(
module,
(info = {
static: new Map(),
dynamic: new Map()
})
);
}
for (const pair of mode.map) {
map.set(pair[0], {
module: mode.module,
exportName: pair[1]
});
const targetModule = dep._module;
switch (mode.type) {
case "safe-reexport":
for (const [key, id] of mode.map) {
if (id) {
addStaticReexport(
info,
key,
targetModule,
id,
false
);
}
}
break;
case "checked-reexport":
for (const [key, id] of mode.map) {
if (id) {
addStaticReexport(
info,
key,
targetModule,
id,
false
);
}
}
break;
case "dynamic-reexport":
addDynamicReexport(info, targetModule, mode.ignored);
break;
case "reexport-non-harmony-default":
case "reexport-non-harmony-default-strict":
case "reexport-named-default":
addStaticReexport(
info,
mode.name,
targetModule,
"default",
false
);
break;
}
}
}
Expand All @@ -87,35 +218,87 @@ class SideEffectsFlagPlugin {
}

// Flatten reexports
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);
for (const info of reexportMaps.values()) {
const dynamicReexports = info.dynamic;
info.dynamic = new Map();
for (const reexport of dynamicReexports) {
let [targetModule, ignored] = reexport;
for (;;) {
const innerInfo = reexportMaps.get(targetModule);
if (!innerInfo) break;

for (const [key, reexports] of innerInfo.static) {
if (ignored.has(key)) continue;
for (const { module, exportName, checked } of reexports) {
addStaticReexport(info, key, module, exportName, checked);
}
}

// Follow dynamic reexport if there is only one
if (innerInfo.dynamic.size !== 1) {
// When there are more then one, we don't know which one
break;
}

ignored = new Set(ignored);
for (const [innerModule, innerIgnored] of innerInfo.dynamic) {
for (const key of innerIgnored) {
if (ignored.has(key)) continue;
// This reexports ends here
addStaticReexport(info, key, targetModule, key, true);
ignored.add(key);
}
targetModule = innerModule;
}
}

// Update reexport as all other cases has been handled
addDynamicReexport(info, targetModule, ignored);
}
}

for (const info of reexportMaps.values()) {
const staticReexports = info.static;
info.static = new Map();
for (const [key, reexports] of staticReexports) {
for (let mapping of reexports) {
for (;;) {
const innerInfo = reexportMaps.get(mapping.module);
if (!innerInfo) break;

const newMapping = getMappingFromInfo(
innerInfo,
mapping.exportName
);
if (!newMapping) break;
mapping = newMapping;
}
mapping = newMapping;
addStaticReexport(
info,
key,
mapping.module,
mapping.exportName,
mapping.checked
);
}
}
}

// Update imports along the reexports from sideEffectFree modules
for (const pair of reexportMaps) {
const module = pair[0];
const map = pair[1];
const info = pair[1];
let newReasons = undefined;
for (let i = 0; i < module.reasons.length; i++) {
const reason = module.reasons[i];
const dep = reason.dependency;
if (
dep instanceof HarmonyExportImportedSpecifierDependency ||
(dep instanceof HarmonyImportSpecifierDependency &&
!dep.namespaceObjectAsContext)
(dep instanceof HarmonyExportImportedSpecifierDependency ||
(dep instanceof HarmonyImportSpecifierDependency &&
!dep.namespaceObjectAsContext)) &&
dep._id
) {
const mapping = map.get(dep._id);
const mapping = getMappingFromInfo(info, dep._id);
if (mapping) {
dep.redirectedModule = mapping.module;
dep.redirectedId = mapping.exportName;
Expand Down
35 changes: 18 additions & 17 deletions test/__snapshots__/StatsTestCases.test.js.snap
Expand Up @@ -2463,34 +2463,35 @@ Entrypoint main = main.js
`;
exports[`StatsTestCases should print correct stats for side-effects-optimization 1`] = `
"Hash: 62f1c52a30620443eef062f1c52a30620443eef0
"Hash: 23e7c83f3ef68ee1934c62f1c52a30620443eef0
Child
Hash: 62f1c52a30620443eef0
Hash: 23e7c83f3ef68ee1934c
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
main.js 2.13 KiB 0 [emitted] main
main.js 1.16 KiB 0 [emitted] main
Entrypoint main = main.js
[0] ./node_modules/module-with-export/index.js 1.01 KiB {0} [built]
[only some exports used: a, huh, smallVar]
ModuleConcatenation bailout: Module exports are unknown
[1] ./node_modules/big-module/index.js 44 bytes {0} [built]
[only some exports used: a, huh]
[only some exports used: smallVar]
ModuleConcatenation bailout: Module exports are unknown
[2] ./node_modules/big-module/a.js 58 bytes {0} [built]
[only some exports used: a, huh]
ModuleConcatenation bailout: Module exports are unknown
[3] ./node_modules/module-with-export/emptyModule.js 43 bytes {0} [built]
[only some exports used: a, huh]
[1] ./node_modules/module-with-export/emptyModule.js 43 bytes {0} [built]
[only some exports used: huh]
ModuleConcatenation bailout: Module is not an ECMAScript module
[4] ./node_modules/big-module/log.js 92 bytes {0} [built]
[only some exports used: a, huh]
ModuleConcatenation bailout: Module exports are unknown
[5] ./index.js 116 bytes {0} [built]
[2] ./index.js 116 bytes {0} [built]
[no exports]
ModuleConcatenation bailout: Module is an entry point
[6] (webpack)/buildin/harmony-module.js 573 bytes {0} [built]
[3] ./node_modules/big-module/a.js 58 bytes {0} [built]
[only some exports used: a]
ModuleConcatenation bailout: Module exports are unknown
[4] ./node_modules/big-module/index.js 44 bytes [built]
[no exports used]
ModuleConcatenation bailout: Module exports are unknown
[5] (webpack)/buildin/harmony-module.js 573 bytes [built]
[no exports used]
ModuleConcatenation bailout: Module is not an ECMAScript module
[6] ./node_modules/big-module/log.js 92 bytes [built]
[no exports used]
ModuleConcatenation bailout: Module exports are unknown
Child
Hash: 62f1c52a30620443eef0
Time: Xms
Expand Down

0 comments on commit 8e7b249

Please sign in to comment.