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; if (!module) return null;
return new DependencyReference( return new DependencyReference(
() => moduleGraph.getModule(this), () => moduleGraph.getModule(this),
true, DependencyReference.NS_OBJECT_IMPORTED,
this.weak this.weak
); );
} }

View File

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

View File

@ -7,6 +7,7 @@
/** @typedef {import("../Module")} Module */ /** @typedef {import("../Module")} Module */
/** @typedef {() => Module} ModuleCallback */ /** @typedef {() => Module} ModuleCallback */
/** @typedef {string[]} StringArray */
class DependencyReference { class DependencyReference {
// module must be dynamic, you must pass a function returning a module // 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 {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 {boolean=} weak if this is a weak reference
* @param {number} order the order information or NaN if don't care * @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; module.exports = DependencyReference;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -47,10 +47,9 @@ class WasmFinalizeExportsPlugin {
const importedNames = ref.importedNames; const importedNames = ref.importedNames;
if (Array.isArray(importedNames)) { if (Array.isArray(importedNames)) {
importedNames.forEach(nameOrNames => { importedNames.forEach(names => {
const name = Array.isArray(nameOrNames) if (names.length === 0) return;
? nameOrNames[0] const name = names[0];
: nameOrNames;
// 3. and uses a func with an incompatible JS signature // 3. and uses a func with an incompatible JS signature
if ( if (
Object.prototype.hasOwnProperty.call( 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 index = index.js
Entrypoint entry = entry.js Entrypoint entry = entry.js
[10] ./index.js 150 bytes {826} [built] [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 ./entry.js (<- Module is an entry point)
ModuleConcatenation bailout: Cannot concat with ./eval.js (<- Module uses eval()) ModuleConcatenation bailout: Cannot concat with ./eval.js (<- Module uses eval())
ModuleConcatenation bailout: Cannot concat with ./module-id.js (<- Module uses module.id) ModuleConcatenation bailout: Cannot concat with ./module-id.js (<- Module uses module.id)

View File

@ -1,5 +1,8 @@
const DependencyReference = require("../../../../").dependencies const DependencyReference = require("../../../../").dependencies
.DependencyReference; .DependencyReference;
/** @typedef {import("../../../../lib/Compilation")} Compilation */
module.exports = { module.exports = {
optimization: { optimization: {
usedExports: true, usedExports: true,
@ -7,7 +10,13 @@ module.exports = {
}, },
plugins: [ plugins: [
function() { function() {
this.hooks.compilation.tap("Test", compilation => { this.hooks.compilation.tap(
"Test",
/**
* @param {Compilation} compilation the compilation
* @returns {void}
*/
compilation => {
compilation.hooks.dependencyReference.tap("Test", (ref, dep) => { compilation.hooks.dependencyReference.tap("Test", (ref, dep) => {
const module = compilation.moduleGraph.getParentModule(dep); const module = compilation.moduleGraph.getParentModule(dep);
if ( if (
@ -24,14 +33,15 @@ module.exports = {
); );
return new DependencyReference( return new DependencyReference(
() => ref.module, () => ref.module,
newExports.length > 0 ? newExports : false, newExports,
ref.weak, ref.weak,
ref.order ref.order
); );
} }
return ref; return ref;
}); });
}); }
);
} }
] ]
}; };

View File

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

View File

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