change DependencyReference.importedNames to be always an string[][]

This commit is contained in:
Tobias Koppers 2019-05-29 11:51:47 +02:00
parent 43bc7a306e
commit 1b685d1de8
16 changed files with 67 additions and 55 deletions

View File

@ -83,7 +83,7 @@ class Dependency {
if (!module) return null;
return new DependencyReference(
() => moduleGraph.getModule(this),
true,
DependencyReference.NS_OBJECT_IMPORTED,
this.weak
);
}

View File

@ -7,6 +7,7 @@
const { UsageState } = require("./ModuleGraph");
const { STAGE_DEFAULT } = require("./OptimizationStages");
const { NS_OBJECT_IMPORTED } = require("./dependencies/DependencyReference");
const Queue = require("./util/Queue");
/** @typedef {import("./Compiler")} Compiler */
@ -15,9 +16,6 @@ const Queue = require("./util/Queue");
/** @typedef {import("./Module")} Module */
/** @typedef {import("./ModuleGraph").ExportsInfo} ExportsInfo */
const NS_OBJ_USED = [[]];
const NOTHING_USED = [];
class FlagDependencyUsagePlugin {
/**
* @param {Compiler} compiler the compiler instance
@ -92,6 +90,7 @@ class FlagDependencyUsagePlugin {
} else {
// 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
// TODO webpack 6 remove this check
if (module.factoryMeta.sideEffectFree) return;
if (exportsInfo.setUsedForSideEffectsOnly()) {
queue.enqueue(module);
@ -122,14 +121,7 @@ class FlagDependencyUsagePlugin {
const referenceModule = reference.module;
const importedNames = reference.importedNames;
processModule(
referenceModule,
importedNames === false
? NOTHING_USED
: importedNames === true
? NS_OBJ_USED
: importedNames.map(n => (Array.isArray(n) ? n : [n]))
);
processModule(referenceModule, importedNames);
};
for (const module of modules) {
@ -145,7 +137,7 @@ class FlagDependencyUsagePlugin {
for (const dep of deps) {
const module = moduleGraph.getModule(dep);
if (module) {
processModule(module, NS_OBJ_USED);
processModule(module, NS_OBJECT_IMPORTED);
}
}
}

View File

@ -7,6 +7,7 @@
/** @typedef {import("../Module")} Module */
/** @typedef {() => Module} ModuleCallback */
/** @typedef {string[]} StringArray */
class DependencyReference {
// module must be dynamic, you must pass a function returning a module
@ -14,7 +15,7 @@ class DependencyReference {
/**
*
* @param {ModuleCallback} moduleCallback a callback to get the referenced module
* @param {(string | string[])[] | boolean} importedNames imported named from the module
* @param {StringArray[]} importedNames imported named from the module
* @param {boolean=} weak if this is a weak reference
* @param {number} order the order information or NaN if don't care
*/
@ -65,4 +66,7 @@ class DependencyReference {
}
}
DependencyReference.NO_IMPORTED_NAMES = [];
DependencyReference.NS_OBJECT_IMPORTED = [[]];
module.exports = DependencyReference;

View File

@ -339,12 +339,13 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
case "reexport-named-default":
return new DependencyReference(
mode.getModule,
["default"],
[["default"]],
false,
this.sourceOrder
);
case "reexport-partial-namespace-object": {
/** @type {string[][]} */
const importedNames = [];
const processExportsInfo = (prefix, exportsInfo) => {
for (const exportInfo of exportsInfo.orderedExports) {
@ -377,7 +378,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
case "reexport-non-harmony-undefined":
return new DependencyReference(
mode.getModule,
true,
DependencyReference.NS_OBJECT_IMPORTED,
false,
this.sourceOrder
);
@ -393,7 +394,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
case "dynamic-reexport":
return new DependencyReference(
mode.getModule,
true,
DependencyReference.NS_OBJECT_IMPORTED,
false,
this.sourceOrder
);

View File

@ -40,7 +40,7 @@ class HarmonyImportDependency extends ModuleDependency {
if (!moduleGraph.getModule(this)) return null;
return new DependencyReference(
() => moduleGraph.getModule(this),
false,
DependencyReference.NO_IMPORTED_NAMES,
this.weak,
this.sourceOrder
);

View File

@ -81,7 +81,9 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
const ids = this.getIds(moduleGraph);
return new DependencyReference(
() => moduleGraph.getModule(this),
ids.length > 0 && !this.namespaceObjectAsContext ? [ids] : true,
ids.length > 0 && !this.namespaceObjectAsContext
? [ids]
: DependencyReference.NS_OBJECT_IMPORTED,
false,
this.sourceOrder
);

View File

@ -33,7 +33,7 @@ class RequireIncludeDependency extends ModuleDependency {
// This doesn't use any export
return new DependencyReference(
() => moduleGraph.getModule(this),
false,
DependencyReference.NO_IMPORTED_NAMES,
false
);
}

View File

@ -41,7 +41,7 @@ class StaticExportsDependency extends NullDependency {
// TODO introduce a flag whether exports can be mangled or not
return new DependencyReference(
() => moduleGraph.getParentModule(this),
true,
DependencyReference.NS_OBJECT_IMPORTED,
false
);
}

View File

@ -34,7 +34,7 @@ class WebAssemblyExportImportedDependency extends ModuleDependency {
return new DependencyReference(
() => moduleGraph.getModule(this),
[this.name],
[[this.name]],
false
);
}

View File

@ -43,7 +43,7 @@ class WebAssemblyImportDependency extends ModuleDependency {
return new DependencyReference(
() => moduleGraph.getModule(this),
[this.name],
[[this.name]],
false
);
}

View File

@ -391,7 +391,8 @@ class ModuleConcatenationPlugin {
ref =>
ref &&
ref.module &&
(Array.isArray(ref.importedNames) ||
((Array.isArray(ref.importedNames) &&
ref.importedNames.every(i => i.length > 0)) ||
Array.isArray(moduleGraph.getProvidedExports(ref.module)))
)

View File

@ -47,10 +47,9 @@ class WasmFinalizeExportsPlugin {
const importedNames = ref.importedNames;
if (Array.isArray(importedNames)) {
importedNames.forEach(nameOrNames => {
const name = Array.isArray(nameOrNames)
? nameOrNames[0]
: nameOrNames;
importedNames.forEach(names => {
if (names.length === 0) return;
const name = names[0];
// 3. and uses a func with an incompatible JS signature
if (
Object.prototype.hasOwnProperty.call(

View File

@ -2295,6 +2295,7 @@ Built at: Thu Jan 01 1970 00:00:00 GMT
Entrypoint index = index.js
Entrypoint entry = entry.js
[10] ./index.js 150 bytes {826} [built]
ModuleConcatenation bailout: Cannot concat with ./cjs.js (<- Module is not an ECMAScript module)
ModuleConcatenation bailout: Cannot concat with ./entry.js (<- Module is an entry point)
ModuleConcatenation bailout: Cannot concat with ./eval.js (<- Module uses eval())
ModuleConcatenation bailout: Cannot concat with ./module-id.js (<- Module uses module.id)

View File

@ -1,5 +1,8 @@
const DependencyReference = require("../../../../").dependencies
.DependencyReference;
/** @typedef {import("../../../../lib/Compilation")} Compilation */
module.exports = {
optimization: {
usedExports: true,
@ -7,31 +10,38 @@ module.exports = {
},
plugins: [
function() {
this.hooks.compilation.tap("Test", compilation => {
compilation.hooks.dependencyReference.tap("Test", (ref, dep) => {
const module = compilation.moduleGraph.getParentModule(dep);
if (
module.identifier().endsWith("module.js") &&
ref.module &&
ref.module.identifier().endsWith("reference.js") &&
Array.isArray(ref.importedNames) &&
ref.importedNames.some(
names => names.length === 1 && names[0] === "unused"
)
) {
const newExports = ref.importedNames.filter(
names => names.length !== 1 || names[0] !== "unused"
);
return new DependencyReference(
() => ref.module,
newExports.length > 0 ? newExports : false,
ref.weak,
ref.order
);
}
return ref;
});
});
this.hooks.compilation.tap(
"Test",
/**
* @param {Compilation} compilation the compilation
* @returns {void}
*/
compilation => {
compilation.hooks.dependencyReference.tap("Test", (ref, dep) => {
const module = compilation.moduleGraph.getParentModule(dep);
if (
module.identifier().endsWith("module.js") &&
ref.module &&
ref.module.identifier().endsWith("reference.js") &&
Array.isArray(ref.importedNames) &&
ref.importedNames.some(
names => names.length === 1 && names[0] === "unused"
)
) {
const newExports = ref.importedNames.filter(
names => names.length !== 1 || names[0] !== "unused"
);
return new DependencyReference(
() => ref.module,
newExports,
ref.weak,
ref.order
);
}
return ref;
});
}
);
}
]
};

View File

@ -1,5 +1,8 @@
const DependencyReference = require("../../../../").dependencies
.DependencyReference;
/** @typedef {import("../../../../lib/Compilation")} Compilation */
module.exports = {
optimization: {
usedExports: true,
@ -24,7 +27,7 @@ module.exports = {
);
return new DependencyReference(
() => ref.module,
newExports.length > 0 ? newExports : false,
newExports,
ref.weak,
ref.order
);

View File

@ -1,4 +1,3 @@
var webpack = require("../../../../");
module.exports = {
module: {
strictThisContextOnImports: true