move default object redirect logic into separate meta property

This commit is contained in:
Tobias Koppers 2019-12-01 22:46:17 +01:00
parent 26f30cebce
commit ae261ed4c1
12 changed files with 36 additions and 31 deletions

View File

@ -119,8 +119,7 @@ class CommonJsStuffPlugin {
.for("module.exports")
.tap("CommonJsStuffPlugin", expr => {
const module = parser.state.module;
const isHarmony =
module.buildMeta && module.buildMeta.exportsType;
const isHarmony = parser.state.harmonyModule;
if (!isHarmony) {
if (module.moduleArgument === "module") {
// avoid rewriting module.exports for backward-compat
@ -142,9 +141,7 @@ class CommonJsStuffPlugin {
.for("this")
.tap("CommonJsStuffPlugin", expr => {
if (!parser.scope.topLevelScope) return;
const module = parser.state.module;
const isHarmony =
module.buildMeta && module.buildMeta.exportsType;
const isHarmony = parser.state.harmonyModule;
if (!isHarmony) {
return toConstantDependency(parser, "this", [
RuntimeGlobals.thisAsExports
@ -158,9 +155,7 @@ class CommonJsStuffPlugin {
parser.hooks.expression
.for("module")
.tap("CommonJsStuffPlugin", expr => {
const isHarmony =
parser.state.module.buildMeta &&
parser.state.module.buildMeta.exportsType;
const isHarmony = parser.state.harmonyModule;
const dep = new ModuleDecoratorDependency(
isHarmony
? RuntimeGlobals.harmonyModuleDecorator

View File

@ -66,6 +66,7 @@ const makeSerializable = require("./util/makeSerializable");
* @property {boolean=} strict
* @property {string=} moduleConcatenationBailout
* @property {("default" | "namespace")=} exportsType
* @property {(boolean | "redirect")=} defaultObject
* @property {boolean=} strictHarmonyModule
* @property {boolean=} async
*/

View File

@ -133,6 +133,7 @@ class ExportsInfo {
setRedirectToDefaultObject() {
const defaultInfo = this.getExportInfo("default");
defaultInfo.canMangleProvide = false;
defaultInfo.provided = true;
defaultInfo.usedName = "";
const inner = defaultInfo.createNestedExportsInfo();
this._redirectTo = inner;
@ -460,7 +461,7 @@ class ExportsInfo {
if (name.length === 1) {
return arr;
}
if (info.exportsInfo) {
if (info.exportsInfo && info.used === UsageState.OnlyPropertiesUsed) {
const nested = info.exportsInfo.getUsedName(name.slice(1));
if (!nested) return false;
return arr.concat(nested);
@ -957,7 +958,7 @@ class ModuleGraph {
* @returns {void}
*/
finishModule(module) {
if (module.buildMeta.exportsType === "default") {
if (module.buildMeta.defaultObject) {
this.getExportsInfo(module).setRedirectToDefaultObject();
}
}

View File

@ -13,13 +13,15 @@ const AbstractMethodError = require("./AbstractMethodError");
/** @typedef {Record<string, any>} PreparsedAst */
/**
* @typedef {Object} ParserState
* @typedef {Object} ParserStateBase
* @property {NormalModule} current
* @property {NormalModule} module
* @property {Compilation} compilation
* @property {TODO} options
*/
/** @typedef {Record<string, any> & ParserStateBase} ParserState */
class Parser {
/**
* @param {string | Buffer | PreparsedAst} source the source to parse

View File

@ -110,7 +110,7 @@ class CommonJsPlugin {
.tap("CommonJsPlugin", evaluateToString("object"));
parser.hooks.expression.for("exports").tap("CommonJsPlugin", expr => {
const module = parser.state.module;
const isHarmony = module.buildMeta && module.buildMeta.exportsType;
const isHarmony = parser.state.harmonyModule;
if (!isHarmony) {
return toConstantDependency(parser, module.exportsArgument, [
RuntimeGlobals.exports

View File

@ -99,9 +99,9 @@ class HarmonyImportDependency extends ModuleDependency {
if (!exportsType) {
// It's not an harmony module
if (
moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule &&
ids.length > 0 &&
ids[0] !== "default"
ids[0] !== "default" &&
moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
) {
// In strict harmony modules we only support the default export
return [
@ -116,7 +116,13 @@ class HarmonyImportDependency extends ModuleDependency {
}
return;
} else if (exportsType === "default") {
if (ids.length > 0 && ids[0] !== "default") {
if (
ids.length > 0 &&
ids[0] !== "default" &&
(moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
? importedModule.buildMeta.defaultObject !== "redirect"
: importedModule.buildMeta.defaultObject === false)
) {
// For these modules only the default export is supported
return [
new HarmonyLinkingError(
@ -124,7 +130,7 @@ class HarmonyImportDependency extends ModuleDependency {
.map(id => `'${id}'`)
.join(
"."
)} ${additionalMessage} from JSON module (only default export is available)`
)} ${additionalMessage} from default-exporting module (only default export is available)`
)
];
}

View File

@ -13,8 +13,7 @@ class HarmonyTopLevelThisParserPlugin {
.for("this")
.tap("HarmonyTopLevelThisParserPlugin", node => {
if (!parser.scope.topLevelScope) return;
const module = parser.state.module;
const isHarmony = !!(module.buildMeta && module.buildMeta.exportsType);
const isHarmony = parser.state.harmonyModule;
if (isHarmony) {
const dep = new ConstDependency("undefined", node.range, null);
dep.loc = node.loc;

View File

@ -44,6 +44,7 @@ class JsonParser extends Parser {
state.module.buildInfo.jsonData = data;
state.module.buildInfo.strict = true;
state.module.buildMeta.exportsType = "default";
state.module.buildMeta.defaultObject = true;
state.module.addDependency(
new JsonExportsDependency(JsonExportsDependency.getExportsFromData(data))
);

View File

@ -1,17 +1,17 @@
module.exports = [
[
/Can't import the named export '2' \(imported as 'c'\) from JSON module \(only default export is available\)/
/Can't import the named export '2' \(imported as 'c'\) from default-exporting module \(only default export is available\)/
],
[
/Can't import the named export 'aa' \(imported as 'aa'\) from JSON module \(only default export is available\)/
/Can't import the named export 'aa' \(imported as 'aa'\) from default-exporting module \(only default export is available\)/
],
[
/Can't import the named export 'bb' \(imported as 'bb'\) from JSON module \(only default export is available\)/
/Can't import the named export 'bb' \(imported as 'bb'\) from default-exporting module \(only default export is available\)/
],
[
/Can't import the named export 'named' \(imported as 'named'\) from JSON module \(only default export is available\)/
/Can't import the named export 'named' \(imported as 'named'\) from default-exporting module \(only default export is available\)/
],
[
/Can't import the named export 'named' \(imported as 'gnamed'\) from JSON module \(only default export is available\)/
/Can't import the named export 'named' \(imported as 'gnamed'\) from default-exporting module \(only default export is available\)/
]
];

View File

@ -1,17 +1,17 @@
module.exports = [
[
/Can't import the named export '2' \(imported as 'c'\) from JSON module \(only default export is available\)/
/Can't import the named export '2' \(imported as 'c'\) from default-exporting module \(only default export is available\)/
],
[
/Can't import the named export 'aa' \(imported as 'aa'\) from JSON module \(only default export is available\)/
/Can't import the named export 'aa' \(imported as 'aa'\) from default-exporting module \(only default export is available\)/
],
[
/Can't import the named export 'bb' \(imported as 'bb'\) from JSON module \(only default export is available\)/
/Can't import the named export 'bb' \(imported as 'bb'\) from default-exporting module \(only default export is available\)/
],
[
/Can't import the named export 'named' \(imported as 'named'\) from JSON module \(only default export is available\)/
/Can't import the named export 'named' \(imported as 'named'\) from default-exporting module \(only default export is available\)/
],
[
/Can't import the named export 'named' \(imported as 'gnamed'\) from JSON module \(only default export is available\)/
/Can't import the named export 'named' \(imported as 'gnamed'\) from default-exporting module \(only default export is available\)/
]
];

View File

@ -1,5 +1,5 @@
module.exports = [
[
/Can't import the named export 'named' \(reexported as 'fNamed'\) from JSON module \(only default export is available\)/
/Can't import the named export 'named' \(reexported as 'fNamed'\) from default-exporting module \(only default export is available\)/
]
];

View File

@ -1,8 +1,8 @@
module.exports = [
[
/Can't import the named export 'a' \(reexported as 'a'\) from JSON module \(only default export is available\)/
/Can't import the named export 'a' \(reexported as 'a'\) from default-exporting module \(only default export is available\)/
],
[
/Can't import the named export 'b' \(reexported as 'b'\) from JSON module \(only default export is available\)/
/Can't import the named export 'b' \(reexported as 'b'\) from default-exporting module \(only default export is available\)/
]
];