diff --git a/lib/dependencies/HarmonyExportImportedSpecifierDependency.js b/lib/dependencies/HarmonyExportImportedSpecifierDependency.js index 1fa54155c..902bf2728 100644 --- a/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +++ b/lib/dependencies/HarmonyExportImportedSpecifierDependency.js @@ -5,7 +5,6 @@ "use strict"; -const HarmonyLinkingError = require("../HarmonyLinkingError"); const InitFragment = require("../InitFragment"); const { UsageState } = require("../ModuleGraph"); const RuntimeGlobals = require("../RuntimeGlobals"); @@ -527,53 +526,12 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency { * @returns {WebpackError[] | undefined} errors */ _getErrors(moduleGraph) { - const importedModule = moduleGraph.getModule(this); - - if (!importedModule) { - return; - } - const ids = this.getIds(moduleGraph); - - if (!importedModule.buildMeta || !importedModule.buildMeta.exportsType) { - // It's not an harmony module - if ( - moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule && - (ids.length === 0 || ids[0] !== "default") - ) { - // In strict harmony modules we only support the default export - const exportName = - ids.length > 0 - ? `the named export ${ids.map(id => `'${id}'`).join(".")}` - : "the namespace object"; - - return [ - new HarmonyLinkingError( - `Can't reexport ${exportName} from non EcmaScript module (only default export is available)` - ) - ]; - } - - return; - } - - if (ids.length === 0) { - return; - } - - if (moduleGraph.isExportProvided(importedModule, ids) !== false) { - // It's provided or we are not sure - return; - } - - // We are sure that it's not provided - const idIsNotNameMessage = - ids.join(".") !== this.name ? ` (reexported as '${this.name}')` : ""; - const errorMessage = `"export ${this.ids - .map(id => `'${id}'`) - .join(".")}${idIsNotNameMessage} was not found in '${this.userRequest}'`; - - return [new HarmonyLinkingError(errorMessage)]; + return this.getLinkingErrors( + moduleGraph, + ids, + `(reexported as '${this.name}')` + ); } /** diff --git a/lib/dependencies/HarmonyImportDependency.js b/lib/dependencies/HarmonyImportDependency.js index 8fadc4c2b..1968b48a1 100644 --- a/lib/dependencies/HarmonyImportDependency.js +++ b/lib/dependencies/HarmonyImportDependency.js @@ -5,6 +5,7 @@ "use strict"; +const HarmonyLinkingError = require("../HarmonyLinkingError"); const InitFragment = require("../InitFragment"); const RuntimeGlobals = require("../RuntimeGlobals"); const Template = require("../Template"); @@ -20,6 +21,7 @@ const ModuleDependency = require("./ModuleDependency"); /** @typedef {import("../Module")} Module */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */ +/** @typedef {import("../WebpackError")} WebpackError */ /** @typedef {import("../util/createHash").Hash} Hash */ class HarmonyImportDependency extends ModuleDependency { @@ -87,6 +89,75 @@ class HarmonyImportDependency extends ModuleDependency { }); } + /** + * @param {ModuleGraph} moduleGraph module graph + * @param {string[]} ids imported ids + * @param {string} additionalMessage extra info included in the error message + * @returns {WebpackError[] | undefined} errors + */ + getLinkingErrors(moduleGraph, ids, additionalMessage) { + const importedModule = moduleGraph.getModule(this); + if (!importedModule) { + return; + } + + const exportsType = + importedModule.buildMeta && importedModule.buildMeta.exportsType; + if (!exportsType) { + // It's not an harmony module + if ( + moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule && + (ids.length === 0 || ids[0] !== "default") + ) { + // In strict harmony modules we only support the default export + const exportName = + ids.length > 0 + ? `the named export ${ids.map(id => `'${id}'`).join(".")}` + : "the namespace object"; + return [ + new HarmonyLinkingError( + `Can't import ${exportName} ${additionalMessage} from non EcmaScript module (only default export is available)` + ) + ]; + } + return; + } else if (exportsType === "named") { + if (ids.length > 0 && ids[0] !== "default") { + // For these modules only the default export is supported + return [ + new HarmonyLinkingError( + `Can't import the named export ${ids + .map(id => `'${id}'`) + .join( + "." + )} ${additionalMessage} from JSON module (only default export is available)` + ) + ]; + } + return; + } + + if (ids.length === 0) { + return; + } + + if (moduleGraph.isExportProvided(importedModule, ids) !== false) { + // It's provided or we are not sure + return; + } + + // We are sure that it's not provided + return [ + new HarmonyLinkingError( + `export ${ids + .map(id => `'${id}'`) + .join(".")} ${additionalMessage} was not found in '${ + this.userRequest + }'` + ) + ]; + } + /** * Update the hash * @param {Hash} hash hash to be updated diff --git a/lib/dependencies/HarmonyImportSpecifierDependency.js b/lib/dependencies/HarmonyImportSpecifierDependency.js index 1064a53d7..bceea068b 100644 --- a/lib/dependencies/HarmonyImportSpecifierDependency.js +++ b/lib/dependencies/HarmonyImportSpecifierDependency.js @@ -5,7 +5,6 @@ "use strict"; -const HarmonyLinkingError = require("../HarmonyLinkingError"); const makeSerializable = require("../util/makeSerializable"); const DependencyReference = require("./DependencyReference"); const HarmonyImportDependency = require("./HarmonyImportDependency"); @@ -122,48 +121,12 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency { * @returns {WebpackError[] | undefined} errors */ _getErrors(moduleGraph) { - const importedModule = moduleGraph.getModule(this); - if (!importedModule) { - return; - } - const ids = this.getIds(moduleGraph); - if (!importedModule.buildMeta || !importedModule.buildMeta.exportsType) { - // It's not an harmony module - if ( - moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule && - (ids.length === 0 || ids[0] !== "default") - ) { - // In strict harmony modules we only support the default export - const exportName = - ids.length > 0 - ? `the named export '${ids[0]}'` - : "the namespace object"; - return [ - new HarmonyLinkingError( - `Can't import ${exportName} from non EcmaScript module (only default export is available)` - ) - ]; - } - return; - } - - if (ids.length === 0) { - return; - } - - if (moduleGraph.isExportProvided(importedModule, ids) !== false) { - // It's provided or we are not sure - return; - } - - // We are sure that it's not provided - const idIsNotNameMessage = - ids[0] !== this.name ? ` (imported as '${this.name}')` : ""; - const errorMessage = `"export ${ids - .map(id => `'${id}'`) - .join(".")}${idIsNotNameMessage} was not found in '${this.userRequest}'`; - return [new HarmonyLinkingError(errorMessage)]; + return this.getLinkingErrors( + moduleGraph, + ids, + `(imported as '${this.name}')` + ); } /** diff --git a/test/__snapshots__/StatsTestCases.test.js.snap b/test/__snapshots__/StatsTestCases.test.js.snap index 37f7acc2c..0bcc8ce56 100644 --- a/test/__snapshots__/StatsTestCases.test.js.snap +++ b/test/__snapshots__/StatsTestCases.test.js.snap @@ -3508,28 +3508,28 @@ WARNING in Terser Plugin: Dropping unused function someUnRemoteUsedFunction5 [./ `; exports[`StatsTestCases should print correct stats for wasm-explorer-examples-sync 1`] = ` -"Hash: 101e738bf68e8ef063d4 +"Hash: 6320d1106f38d42868c5 Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT Asset Size Chunks Chunk Names -0303ec812ef3cb633580.module.wasm 154 bytes {780} [emitted] 1d55f77c08cd19684f13.module.wasm 154 bytes {325} [emitted] +200c03abdc3f4ae1e15c.module.wasm 290 bytes {780} [emitted] 230.bundle.js 212 bytes {230} [emitted] +256e72dd8b9a83a6e45b.module.wasm 120 bytes {325} [emitted] 325.bundle.js 3.96 KiB {325} [emitted] 526.bundle.js 329 bytes {526} [emitted] -7031b3d288670f8a932a.module.wasm 120 bytes {325} [emitted] 780.bundle.js 505 bytes {780} [emitted] 99.bundle.js 210 bytes {99} [emitted] +a0e9dd97d7ced35a5b2c.module.wasm 154 bytes {780} [emitted] bundle.js 10.9 KiB {520} [emitted] main-1df31ce3 -ca33708544fa2cfb2e2e.module.wasm 290 bytes {780} [emitted] d37b3336426771c2a6e2.module.wasm 531 bytes {99} [emitted] -f55b5f397ec1004a5764.module.wasm 156 bytes {230} [emitted] +ebd3f263522776d85971.module.wasm 156 bytes {230} [emitted] Entrypoint main = bundle.js chunk {99} 99.bundle.js, d37b3336426771c2a6e2.module.wasm 50 bytes (javascript) 531 bytes (webassembly) [rendered] [99] ./duff.wasm 50 bytes (javascript) 531 bytes (webassembly) {99} [built] -chunk {230} 230.bundle.js, f55b5f397ec1004a5764.module.wasm 50 bytes (javascript) 156 bytes (webassembly) [rendered] +chunk {230} 230.bundle.js, ebd3f263522776d85971.module.wasm 50 bytes (javascript) 156 bytes (webassembly) [rendered] [230] ./Q_rsqrt.wasm 50 bytes (javascript) 156 bytes (webassembly) {230} [built] -chunk {325} 325.bundle.js, 7031b3d288670f8a932a.module.wasm, 1d55f77c08cd19684f13.module.wasm 1.54 KiB (javascript) 274 bytes (webassembly) [rendered] +chunk {325} 325.bundle.js, 256e72dd8b9a83a6e45b.module.wasm, 1d55f77c08cd19684f13.module.wasm 1.54 KiB (javascript) 274 bytes (webassembly) [rendered] [287] ./popcnt.wasm 50 bytes (javascript) 120 bytes (webassembly) {325} [built] [325] ./tests.js 1.44 KiB {325} [built] [819] ./testFunction.wasm 50 bytes (javascript) 154 bytes (webassembly) {325} [built] @@ -3538,7 +3538,7 @@ chunk {520} bundle.js (main-1df31ce3) 586 bytes (javascript) 5.15 KiB (runtime) + 7 hidden chunk modules chunk {526} 526.bundle.js 34 bytes [rendered] split chunk (cache group: defaultVendors) [526] ./node_modules/env.js 34 bytes {526} [built] -chunk {780} 780.bundle.js, 0303ec812ef3cb633580.module.wasm, ca33708544fa2cfb2e2e.module.wasm 110 bytes (javascript) 444 bytes (webassembly) [rendered] +chunk {780} 780.bundle.js, a0e9dd97d7ced35a5b2c.module.wasm, 200c03abdc3f4ae1e15c.module.wasm 110 bytes (javascript) 444 bytes (webassembly) [rendered] [143] ./fact.wasm 50 bytes (javascript) 154 bytes (webassembly) {780} [built] [151] ./fast-math.wasm 60 bytes (javascript) 290 bytes (webassembly) {780} [built] [10] ./index.js 586 bytes {520} [built] diff --git a/test/cases/json/import-by-name-with-concatenation/warnings.js b/test/cases/json/import-by-name-with-concatenation/warnings.js new file mode 100644 index 000000000..d124326de --- /dev/null +++ b/test/cases/json/import-by-name-with-concatenation/warnings.js @@ -0,0 +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 'aa' \(imported as 'aa'\) from JSON 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 'named' \(imported as 'named'\) from JSON module \(only default export is available\)/ + ], + [ + /Can't import the named export 'named' \(imported as 'gnamed'\) from JSON 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 new file mode 100644 index 000000000..d124326de --- /dev/null +++ b/test/cases/json/import-by-name/warnings.js @@ -0,0 +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 'aa' \(imported as 'aa'\) from JSON 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 'named' \(imported as 'named'\) from JSON module \(only default export is available\)/ + ], + [ + /Can't import the named export 'named' \(imported as 'gnamed'\) from JSON module \(only default export is available\)/ + ] +]; diff --git a/test/cases/mjs/cjs-import-default/errors.js b/test/cases/mjs/cjs-import-default/errors.js index 3ff814f87..5ba43e17b 100644 --- a/test/cases/mjs/cjs-import-default/errors.js +++ b/test/cases/mjs/cjs-import-default/errors.js @@ -1,20 +1,20 @@ module.exports = [ [ - /Can't import the named export 'data' from non EcmaScript module \(only default export is available\)/ + /Can't import the named export 'data' \(imported as 'data'\) from non EcmaScript module \(only default export is available\)/ ], [ - /Can't import the named export 'data' from non EcmaScript module \(only default export is available\)/ + /Can't import the named export 'data' \(imported as 'data'\) from non EcmaScript module \(only default export is available\)/ ], [ - /Can't import the namespace object from non EcmaScript module \(only default export is available\)/ + /Can't import the namespace object \(imported as 'star'\) from non EcmaScript module \(only default export is available\)/ ], [ - /Can't import the namespace object from non EcmaScript module \(only default export is available\)/ + /Can't import the namespace object \(imported as 'star'\) from non EcmaScript module \(only default export is available\)/ ], [ - /Can't reexport the namespace object from non EcmaScript module \(only default export is available\)/ + /Can't import the namespace object \(reexported as 'ns'\) from non EcmaScript module \(only default export is available\)/ ], [ - /Can't reexport the named export 'data' from non EcmaScript module \(only default export is available\)/ + /Can't import the named export 'data' \(reexported as 'data'\) from non EcmaScript module \(only default export is available\)/ ] ]; diff --git a/test/cases/parsing/harmony-info/warnings.js b/test/cases/parsing/harmony-info/warnings.js index e31def805..2e82a80e3 100644 --- a/test/cases/parsing/harmony-info/warnings.js +++ b/test/cases/parsing/harmony-info/warnings.js @@ -1,5 +1,5 @@ module.exports = [ [/export 'default' \(imported as 'def'\) was not found in '\.\/module'/], [/export 'a' \(imported as 'aa'\) was not found in '\.\/module'/], - [/export 'e' was not found in '\.\/module'/] + [/export 'e' \(imported as 'e'\) was not found in '\.\/module'/] ]; diff --git a/test/cases/scope-hoisting/json-reexport-6700/warnings.js b/test/cases/scope-hoisting/json-reexport-6700/warnings.js new file mode 100644 index 000000000..5d963765d --- /dev/null +++ b/test/cases/scope-hoisting/json-reexport-6700/warnings.js @@ -0,0 +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 'b' \(reexported as 'b'\) from JSON module \(only default export is available\)/ + ] +]; diff --git a/test/configCases/split-chunks/module-type-filter/index.js b/test/configCases/split-chunks/module-type-filter/index.js index 7e159cab3..578210208 100644 --- a/test/configCases/split-chunks/module-type-filter/index.js +++ b/test/configCases/split-chunks/module-type-filter/index.js @@ -1,5 +1,5 @@ -import { value } from "./data.json"; +import data from "./data.json"; it("should move the json module into a separate chunk", () => { - expect(value).toBe(42); -}) + expect(data.value).toBe(42); +});