Skip to content

Commit 2b4662b

Browse files
9aoyCopilot
andauthored
fix: wasm file not found error (#677)
Co-authored-by: Copilot <[email protected]>
1 parent a4758aa commit 2b4662b

File tree

7 files changed

+45
-3
lines changed

7 files changed

+45
-3
lines changed

e2e/wasm/index.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, test } from '@rstest/core';
2+
3+
// TODO: need to fix below error after bump rspack 1.6.2+
4+
// WebAssemb ly.instantiate(): length overflow while decoding section length @+13
5+
test.fails('WASM factorial', async () => {
6+
const { _Z4facti: AsyncFactorial } = await import('./src/factorial.wasm');
7+
8+
expect(AsyncFactorial(1)).toBe(1);
9+
expect(AsyncFactorial(2)).toBe(2);
10+
expect(AsyncFactorial(3)).toBe(6);
11+
});

e2e/wasm/src/factorial.wasm

154 Bytes
Binary file not shown.

e2e/wasm/src/factorial.wasm.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function _Z4facti(n: number): number;

packages/core/src/core/plugins/external.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const autoExternalNodeModules: (
1515
return callback();
1616
}
1717

18-
if (request.startsWith('@swc/helpers/')) {
18+
if (request.startsWith('@swc/helpers/') || request.endsWith('.wasm')) {
1919
// @swc/helper is a special case (Load by require but resolve to esm)
2020
return callback();
2121
}

packages/core/src/core/plugins/mockRuntime.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ class MockRuntimeRspackPlugin {
4848

4949
module.source!.source = Buffer.from(finalSource);
5050
}
51+
52+
if (module.name === 'async_wasm_loading') {
53+
const finalSource = module.source!.source.toString('utf-8').replace(
54+
// Replace readFile with readWasmFile to use the custom WASM file loader
55+
// Hard coded in EJS template https://github.com/web-infra-dev/rspack/tree/7df875eb3ca3bb4bcb21836fdf4e6be1f38a057c/crates/rspack_plugin_wasm/src/runtime
56+
'readFile(',
57+
'readWasmFile(',
58+
);
59+
60+
module.source!.source = Buffer.from(finalSource);
61+
}
5162
},
5263
);
5364
});

packages/core/src/runtime/worker/loadModule.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ export const loadModule = ({
186186
assetFiles,
187187
interopDefault,
188188
),
189+
readWasmFile: (
190+
wasmPath: string,
191+
callback: (err: Error | null, data?: Buffer) => void,
192+
) => {
193+
const joinedPath = isRelativePath(wasmPath)
194+
? path.join(path.dirname(distPath), wasmPath)
195+
: wasmPath;
196+
const content = assetFiles[path.normalize(joinedPath)];
197+
if (content) {
198+
callback(null, Buffer.from(content, 'utf-8'));
199+
} else {
200+
callback(
201+
new Error(`WASM file ${joinedPath} not found in asset files.`),
202+
);
203+
}
204+
},
189205
__rstest_dynamic_import__: defineRstestDynamicImport({
190206
testPath,
191207
interopDefault,

packages/core/src/utils/error.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,11 @@ export async function printError(
6767
fullStack: error.fullStack,
6868
getSourcemap,
6969
});
70-
71-
if (!stackFrames.length && error.stack.length) {
70+
if (
71+
!stackFrames.length &&
72+
!(error.fullStack || isDebug()) &&
73+
!error.stack.endsWith(error.message)
74+
) {
7275
logger.log(
7376
color.gray(
7477
"No error stack found, set 'DEBUG=rstest' to show fullStack.",

0 commit comments

Comments
 (0)