Skip to content

Commit fee575e

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular/build): read WASM file from script location on Node.js
When using a WASM file on Node.js via SSR/SSG/etc. the path for the `readFile` call will now be based on the location of the script using the WASM file instead of the current working directory. This change also adds a general Node.js WASM E2E test via prerendering.
1 parent 4286bb0 commit fee575e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

packages/angular/build/src/tools/esbuild/wasm-plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ export function createWasmPlugin(options: WasmPluginOptions): Plugin {
123123
// Read from the file system when on Node.js (SSR) and not inline
124124
if (!inlineWasm && build.initialOptions.platform === 'node') {
125125
initContents += 'import { readFile } from "node:fs/promises";\n';
126-
initContents += 'const wasmData = await readFile(wasmPath);\n';
126+
initContents +=
127+
'const wasmData = await readFile(new URL(wasmPath, import.meta.url));\n';
127128
}
128129

129130
// Create initialization function

tests/legacy-cli/e2e/tests/build/wasm-esm.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.dev/license
77
*/
8-
import { writeFile } from 'node:fs/promises';
8+
import { readFile, writeFile } from 'node:fs/promises';
9+
import assert from 'node:assert/strict';
910
import { ng } from '../../utils/process';
1011
import { prependToFile, replaceInFile } from '../../utils/fs';
11-
import { updateJsonFile } from '../../utils/project';
12+
import { updateJsonFile, useSha } from '../../utils/project';
13+
import { installWorkspacePackages } from '../../utils/packages';
1214

1315
/**
1416
* Compiled and base64 encoded WASM file for the following WAT:
@@ -94,4 +96,13 @@ export default async function () {
9496
);
9597

9698
await ng('e2e');
99+
100+
// Setup prerendering and build to test Node.js functionality
101+
await ng('add', '@angular/ssr', '--skip-confirmation');
102+
await useSha();
103+
await installWorkspacePackages();
104+
105+
await ng('build', '--configuration', 'development', '--prerender');
106+
const content = await readFile('dist/test-project/browser/index.html', 'utf-8');
107+
assert.match(content, /Hello, 32/);
97108
}

0 commit comments

Comments
 (0)