add warnings for non-default json module imports

unify linking error generation
This commit is contained in:
Tobias Koppers 2019-06-12 14:31:36 +02:00
parent ab280135b1
commit ba77df23dd
10 changed files with 141 additions and 107 deletions

View File

@ -5,7 +5,6 @@
"use strict"; "use strict";
const HarmonyLinkingError = require("../HarmonyLinkingError");
const InitFragment = require("../InitFragment"); const InitFragment = require("../InitFragment");
const { UsageState } = require("../ModuleGraph"); const { UsageState } = require("../ModuleGraph");
const RuntimeGlobals = require("../RuntimeGlobals"); const RuntimeGlobals = require("../RuntimeGlobals");
@ -527,53 +526,12 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
* @returns {WebpackError[] | undefined} errors * @returns {WebpackError[] | undefined} errors
*/ */
_getErrors(moduleGraph) { _getErrors(moduleGraph) {
const importedModule = moduleGraph.getModule(this);
if (!importedModule) {
return;
}
const ids = this.getIds(moduleGraph); const ids = this.getIds(moduleGraph);
return this.getLinkingErrors(
if (!importedModule.buildMeta || !importedModule.buildMeta.exportsType) { moduleGraph,
// It's not an harmony module ids,
if ( `(reexported as '${this.name}')`
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)];
} }
/** /**

View File

@ -5,6 +5,7 @@
"use strict"; "use strict";
const HarmonyLinkingError = require("../HarmonyLinkingError");
const InitFragment = require("../InitFragment"); const InitFragment = require("../InitFragment");
const RuntimeGlobals = require("../RuntimeGlobals"); const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template"); const Template = require("../Template");
@ -20,6 +21,7 @@ const ModuleDependency = require("./ModuleDependency");
/** @typedef {import("../Module")} Module */ /** @typedef {import("../Module")} Module */
/** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */ /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("../WebpackError")} WebpackError */
/** @typedef {import("../util/createHash").Hash} Hash */ /** @typedef {import("../util/createHash").Hash} Hash */
class HarmonyImportDependency extends ModuleDependency { 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 * Update the hash
* @param {Hash} hash hash to be updated * @param {Hash} hash hash to be updated

View File

@ -5,7 +5,6 @@
"use strict"; "use strict";
const HarmonyLinkingError = require("../HarmonyLinkingError");
const makeSerializable = require("../util/makeSerializable"); const makeSerializable = require("../util/makeSerializable");
const DependencyReference = require("./DependencyReference"); const DependencyReference = require("./DependencyReference");
const HarmonyImportDependency = require("./HarmonyImportDependency"); const HarmonyImportDependency = require("./HarmonyImportDependency");
@ -122,48 +121,12 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
* @returns {WebpackError[] | undefined} errors * @returns {WebpackError[] | undefined} errors
*/ */
_getErrors(moduleGraph) { _getErrors(moduleGraph) {
const importedModule = moduleGraph.getModule(this);
if (!importedModule) {
return;
}
const ids = this.getIds(moduleGraph); const ids = this.getIds(moduleGraph);
if (!importedModule.buildMeta || !importedModule.buildMeta.exportsType) { return this.getLinkingErrors(
// It's not an harmony module moduleGraph,
if ( ids,
moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule && `(imported as '${this.name}')`
(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)];
} }
/** /**

View File

@ -3508,28 +3508,28 @@ WARNING in Terser Plugin: Dropping unused function someUnRemoteUsedFunction5 [./
`; `;
exports[`StatsTestCases should print correct stats for wasm-explorer-examples-sync 1`] = ` exports[`StatsTestCases should print correct stats for wasm-explorer-examples-sync 1`] = `
"Hash: 101e738bf68e8ef063d4 "Hash: 6320d1106f38d42868c5
Time: Xms Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names Asset Size Chunks Chunk Names
0303ec812ef3cb633580.module.wasm 154 bytes {780} [emitted]
1d55f77c08cd19684f13.module.wasm 154 bytes {325} [emitted] 1d55f77c08cd19684f13.module.wasm 154 bytes {325} [emitted]
200c03abdc3f4ae1e15c.module.wasm 290 bytes {780} [emitted]
230.bundle.js 212 bytes {230} [emitted] 230.bundle.js 212 bytes {230} [emitted]
256e72dd8b9a83a6e45b.module.wasm 120 bytes {325} [emitted]
325.bundle.js 3.96 KiB {325} [emitted] 325.bundle.js 3.96 KiB {325} [emitted]
526.bundle.js 329 bytes {526} [emitted] 526.bundle.js 329 bytes {526} [emitted]
7031b3d288670f8a932a.module.wasm 120 bytes {325} [emitted]
780.bundle.js 505 bytes {780} [emitted] 780.bundle.js 505 bytes {780} [emitted]
99.bundle.js 210 bytes {99} [emitted] 99.bundle.js 210 bytes {99} [emitted]
a0e9dd97d7ced35a5b2c.module.wasm 154 bytes {780} [emitted]
bundle.js 10.9 KiB {520} [emitted] main-1df31ce3 bundle.js 10.9 KiB {520} [emitted] main-1df31ce3
ca33708544fa2cfb2e2e.module.wasm 290 bytes {780} [emitted]
d37b3336426771c2a6e2.module.wasm 531 bytes {99} [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 Entrypoint main = bundle.js
chunk {99} 99.bundle.js, d37b3336426771c2a6e2.module.wasm 50 bytes (javascript) 531 bytes (webassembly) [rendered] 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] [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] [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] [287] ./popcnt.wasm 50 bytes (javascript) 120 bytes (webassembly) {325} [built]
[325] ./tests.js 1.44 KiB {325} [built] [325] ./tests.js 1.44 KiB {325} [built]
[819] ./testFunction.wasm 50 bytes (javascript) 154 bytes (webassembly) {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 + 7 hidden chunk modules
chunk {526} 526.bundle.js 34 bytes [rendered] split chunk (cache group: defaultVendors) chunk {526} 526.bundle.js 34 bytes [rendered] split chunk (cache group: defaultVendors)
[526] ./node_modules/env.js 34 bytes {526} [built] [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] [143] ./fact.wasm 50 bytes (javascript) 154 bytes (webassembly) {780} [built]
[151] ./fast-math.wasm 60 bytes (javascript) 290 bytes (webassembly) {780} [built] [151] ./fast-math.wasm 60 bytes (javascript) 290 bytes (webassembly) {780} [built]
[10] ./index.js 586 bytes {520} [built] [10] ./index.js 586 bytes {520} [built]

View File

@ -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\)/
]
];

View File

@ -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\)/
]
];

View File

@ -1,20 +1,20 @@
module.exports = [ 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\)/
] ]
]; ];

View File

@ -1,5 +1,5 @@
module.exports = [ module.exports = [
[/export 'default' \(imported as 'def'\) was not found in '\.\/module'/], [/export 'default' \(imported as 'def'\) was not found in '\.\/module'/],
[/export 'a' \(imported as 'aa'\) 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'/]
]; ];

View File

@ -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\)/
]
];

View File

@ -1,5 +1,5 @@
import { value } from "./data.json"; import data from "./data.json";
it("should move the json module into a separate chunk", () => { it("should move the json module into a separate chunk", () => {
expect(value).toBe(42); expect(data.value).toBe(42);
}) });