Skip to content

Commit

Permalink
fix(@angular/build): read WASM file from script location on Node.js
Browse files Browse the repository at this point in the history
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.

(cherry picked from commit fee575e)
  • Loading branch information
clydin authored and alan-agius4 committed Jul 9, 2024
1 parent 1772c87 commit 13d2100
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/angular/build/src/tools/esbuild/wasm-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ export function createWasmPlugin(options: WasmPluginOptions): Plugin {
// Read from the file system when on Node.js (SSR) and not inline
if (!inlineWasm && build.initialOptions.platform === 'node') {
initContents += 'import { readFile } from "node:fs/promises";\n';
initContents += 'const wasmData = await readFile(wasmPath);\n';
initContents +=
'const wasmData = await readFile(new URL(wasmPath, import.meta.url));\n';
}

// Create initialization function
Expand Down
15 changes: 13 additions & 2 deletions tests/legacy-cli/e2e/tests/build/wasm-esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { writeFile } from 'node:fs/promises';
import { readFile, writeFile } from 'node:fs/promises';
import assert from 'node:assert/strict';
import { ng } from '../../utils/process';
import { prependToFile, replaceInFile } from '../../utils/fs';
import { updateJsonFile } from '../../utils/project';
import { updateJsonFile, useSha } from '../../utils/project';
import { installWorkspacePackages } from '../../utils/packages';

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

await ng('e2e');

// Setup prerendering and build to test Node.js functionality
await ng('add', '@angular/ssr', '--skip-confirmation');
await useSha();
await installWorkspacePackages();

await ng('build', '--configuration', 'development', '--prerender');
const content = await readFile('dist/test-project/browser/index.html', 'utf-8');
assert.match(content, /Hello, 32/);
}

0 comments on commit 13d2100

Please sign in to comment.