Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7638 from webpack/feature/wasm-initial-error
add helpful error when importing wasm in initial chunk
- Loading branch information
Showing
20 changed files
with
208 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
"use strict"; | ||
|
||
const WebpackError = require("../WebpackError"); | ||
|
||
/** @typedef {import("../Module")} Module */ | ||
/** @typedef {import("../RequestShortener")} RequestShortener */ | ||
|
||
/** | ||
* @param {Module} module module to get chains from | ||
* @param {RequestShortener} requestShortener to make readable identifiers | ||
* @returns {string[]} all chains to the module | ||
*/ | ||
const getInitialModuleChains = (module, requestShortener) => { | ||
const queue = [ | ||
{ head: module, message: module.readableIdentifier(requestShortener) } | ||
]; | ||
/** @type {Set<string>} */ | ||
const results = new Set(); | ||
/** @type {Set<string>} */ | ||
const incompleteResults = new Set(); | ||
/** @type {Set<Module>} */ | ||
const visitedModules = new Set(); | ||
|
||
for (const chain of queue) { | ||
const { head, message } = chain; | ||
let final = true; | ||
/** @type {Set<Module>} */ | ||
const alreadyReferencedModules = new Set(); | ||
for (const reason of head.reasons) { | ||
const newHead = reason.module; | ||
if (newHead) { | ||
if (!newHead.getChunks().some(c => c.canBeInitial())) continue; | ||
final = false; | ||
if (alreadyReferencedModules.has(newHead)) continue; | ||
alreadyReferencedModules.add(newHead); | ||
const moduleName = newHead.readableIdentifier(requestShortener); | ||
const detail = reason.explanation ? ` (${reason.explanation})` : ""; | ||
const newMessage = `${moduleName}${detail} --> ${message}`; | ||
if (visitedModules.has(newHead)) { | ||
incompleteResults.add(`... --> ${newMessage}`); | ||
continue; | ||
} | ||
visitedModules.add(newHead); | ||
queue.push({ | ||
head: newHead, | ||
message: newMessage | ||
}); | ||
} else { | ||
final = false; | ||
const newMessage = reason.explanation | ||
? `(${reason.explanation}) --> ${message}` | ||
: message; | ||
results.add(newMessage); | ||
} | ||
} | ||
if (final) { | ||
results.add(message); | ||
} | ||
} | ||
for (const result of incompleteResults) { | ||
results.add(result); | ||
} | ||
return Array.from(results); | ||
}; | ||
|
||
module.exports = class WebAssemblyInInitialChunkError extends WebpackError { | ||
/** | ||
* @param {Module} module WASM module | ||
* @param {RequestShortener} requestShortener request shortener | ||
*/ | ||
constructor(module, requestShortener) { | ||
const moduleChains = getInitialModuleChains(module, requestShortener); | ||
const message = `WebAssembly module is included in initial chunk. | ||
This is not allowed, because WebAssembly download and compilation must happen asynchronous. | ||
Add an async splitpoint (i. e. import()) somewhere between your entrypoint and the WebAssembly module: | ||
${moduleChains.map(s => `* ${s}`).join("\n")}`; | ||
|
||
super(message); | ||
this.name = "WebAssemblyInInitialChunkError"; | ||
this.hideStack = true; | ||
this.module = module; | ||
|
||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
14 changes: 14 additions & 0 deletions
14
test/configCases/wasm/wasm-in-initial-chunk-error/errors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = [ | ||
[ | ||
/\.\/wasm.wat/, | ||
/WebAssembly module is included in initial chunk/, | ||
/\* \.\/index.js --> \.\/module.js --> \.\/wasm.wat/, | ||
/\* \.\.\. --> \.\/module.js --> \.\/module2.js --> \.\/wasm.wat/, | ||
/\* \.\.\. --> \.\/module2.js --> \.\/module3.js --> \.\/wasm.wat/ | ||
], | ||
[ | ||
/\.\/wasm2\.wat/, | ||
/WebAssembly module is included in initial chunk/, | ||
/\* \.\/index.js --> \.\/module.js --> \.\/module2.js --> \.\/module3.js --> \.\/wasm2.wat/ | ||
] | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "./module"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { getNumber } from "./wasm.wat"; | ||
|
||
import("./async.js"); | ||
|
||
require("./module2"); | ||
|
||
getNumber(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require("./wasm.wat"); | ||
require("./module3"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require("./wasm.wat"); | ||
require("./wasm2.wat"); |
5 changes: 5 additions & 0 deletions
5
test/configCases/wasm/wasm-in-initial-chunk-error/test.filter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var supportsWebAssembly = require("../../../helpers/supportsWebAssembly"); | ||
|
||
module.exports = function() { | ||
return supportsWebAssembly(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(module | ||
(func $getNumber (export "getNumber") (result i32) | ||
(i32.const 42))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(module | ||
(func $getNumber (export "getNumber") (result i32) | ||
(i32.const 42))) | ||
|
12 changes: 12 additions & 0 deletions
12
test/configCases/wasm/wasm-in-initial-chunk-error/webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = { | ||
entry: "./index", | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.wat$/, | ||
loader: "wast-loader", | ||
type: "webassembly/experimental" | ||
} | ||
] | ||
} | ||
}; |