diff --git a/lib/CommonJsStuffPlugin.js b/lib/CommonJsStuffPlugin.js index f0aa9e075..fa08ea152 100644 --- a/lib/CommonJsStuffPlugin.js +++ b/lib/CommonJsStuffPlugin.js @@ -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 diff --git a/lib/Module.js b/lib/Module.js index f7e5947c6..3acce26f9 100644 --- a/lib/Module.js +++ b/lib/Module.js @@ -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 */ diff --git a/lib/ModuleGraph.js b/lib/ModuleGraph.js index 2ca028d47..d1afe6a05 100644 --- a/lib/ModuleGraph.js +++ b/lib/ModuleGraph.js @@ -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(); } } diff --git a/lib/Parser.js b/lib/Parser.js index fe59f70e2..5ec26adcc 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -13,13 +13,15 @@ const AbstractMethodError = require("./AbstractMethodError"); /** @typedef {Record} PreparsedAst */ /** - * @typedef {Object} ParserState + * @typedef {Object} ParserStateBase * @property {NormalModule} current * @property {NormalModule} module * @property {Compilation} compilation * @property {TODO} options */ +/** @typedef {Record & ParserStateBase} ParserState */ + class Parser { /** * @param {string | Buffer | PreparsedAst} source the source to parse diff --git a/lib/dependencies/CommonJsPlugin.js b/lib/dependencies/CommonJsPlugin.js index 543165d29..b8d991eb2 100644 --- a/lib/dependencies/CommonJsPlugin.js +++ b/lib/dependencies/CommonJsPlugin.js @@ -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 diff --git a/lib/dependencies/HarmonyImportDependency.js b/lib/dependencies/HarmonyImportDependency.js index 83320bfa6..0034eb7d2 100644 --- a/lib/dependencies/HarmonyImportDependency.js +++ b/lib/dependencies/HarmonyImportDependency.js @@ -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)` ) ]; } diff --git a/lib/dependencies/HarmonyTopLevelThisParserPlugin.js b/lib/dependencies/HarmonyTopLevelThisParserPlugin.js index 1ecc3f7ce..990ff6f42 100644 --- a/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +++ b/lib/dependencies/HarmonyTopLevelThisParserPlugin.js @@ -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; diff --git a/lib/json/JsonParser.js b/lib/json/JsonParser.js index 1c0fcdabd..ce862885f 100644 --- a/lib/json/JsonParser.js +++ b/lib/json/JsonParser.js @@ -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)) ); diff --git a/test/cases/json/import-by-name-with-concatenation/warnings.js b/test/cases/json/import-by-name-with-concatenation/warnings.js index d124326de..474cba061 100644 --- a/test/cases/json/import-by-name-with-concatenation/warnings.js +++ b/test/cases/json/import-by-name-with-concatenation/warnings.js @@ -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\)/ ] ]; diff --git a/test/cases/json/import-by-name/warnings.js b/test/cases/json/import-by-name/warnings.js index d124326de..474cba061 100644 --- a/test/cases/json/import-by-name/warnings.js +++ b/test/cases/json/import-by-name/warnings.js @@ -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\)/ ] ]; diff --git a/test/cases/json/reexport/warnings.js b/test/cases/json/reexport/warnings.js index 50af7f5ec..dbe066e4f 100644 --- a/test/cases/json/reexport/warnings.js +++ b/test/cases/json/reexport/warnings.js @@ -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\)/ ] ]; diff --git a/test/cases/scope-hoisting/json-reexport-6700/warnings.js b/test/cases/scope-hoisting/json-reexport-6700/warnings.js index 5d963765d..d1c01bfc2 100644 --- a/test/cases/scope-hoisting/json-reexport-6700/warnings.js +++ b/test/cases/scope-hoisting/json-reexport-6700/warnings.js @@ -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\)/ ] ];