Skip to content

Commit c30194b

Browse files
committed
fix(@angular/build): enable oxc WASI fallback in WebContainer environments
In WebContainer environments (such as StackBlitz and angular.dev playground), native Node.js addons cannot run, requiring oxc-parser to fall back to WebAssembly (WASI). However, @oxc-parser/binding-wasm32-wasi@0.156.0 specifies @emnapi/core@2.0.0-alpha.6 while depending on @napi-rs/wasm-runtime@~1.2.6 (which was updated for @emnapi/core@>=2.0.0-beta.2). In wasm-runtime 1.2.6, initWorker is assigned to emnapi_shared.addonModule, which is undefined in alpha.6 (where it was named napiModule), throwing TypeError: Cannot set properties of undefined (setting 'initWorker'). Introducing oxc-wasi-shim aliases addonModule to napiModule on Object.prototype when in WebContainer or when NAPI_RS_FORCE_WASI is enabled, allowing the WASI runtime to initialize successfully. Additionally, remove @oxc-parser/binding-wasm32-wasi from optionalDependencies as oxc-parser's built-in WebContainer fallback downloads and loads it on demand. Fixes #34524 (cherry picked from commit cbc3888)
1 parent 831ade1 commit c30194b

7 files changed

Lines changed: 74 additions & 67 deletions

File tree

‎packages/angular/build/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"xxhash-wasm": "1.1.0"
4848
},
4949
"optionalDependencies": {
50-
"@oxc-parser/binding-wasm32-wasi": "0.150.0",
5150
"lmdb": "3.5.6"
5251
},
5352
"devDependencies": {

‎packages/angular/build/src/tools/i18n/i18n-inliner-worker.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import '../oxc/oxc-wasi-shim';
910
import { type DecodedSourceMap, type SourceMapInput } from '@ampproject/remapping';
1011
import {
1112
type ɵParsedTranslation,

‎packages/angular/build/src/tools/oxc/oxc-transform.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import './oxc-wasi-shim';
910
import type { DecodedSourceMap } from '@ampproject/remapping';
1011
import { needsLinking } from '@angular/compiler-cli/linker';
1112
import type { BindingIdentifier, Class, Node } from '@oxc-project/types';
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
// Workaround for an upstream incompatibility in `@oxc-parser/binding-wasm32-wasi` when
10+
// falling back to WebAssembly (e.g., inside WebContainers or StackBlitz).
11+
//
12+
// `@oxc-parser/binding-wasm32-wasi` declares a dependency on `@emnapi/core@2.0.0-alpha.6`,
13+
// but depends on `@napi-rs/wasm-runtime@~1.2.6` which expects `@emnapi/core@>=2.0.0-beta.2`.
14+
// In `@napi-rs/wasm-runtime@1.2.6`, the runtime attempts to assign `initWorker` to
15+
// `emnapi_shared.addonModule`, which is undefined in `alpha.6` where the property was named
16+
// `napiModule`. This results in `TypeError: Cannot set properties of undefined (setting 'initWorker')`,
17+
// causing WASI binding initialization to fail and surface as a misleading "WASI binding not found" error
18+
// or an `ERR_REQUIRE_CYCLE_MODULE` failure.
19+
//
20+
// Aliasing `addonModule` to `this.napiModule` on `Object.prototype` allows the WASI runtime to
21+
// initialize successfully.
22+
//
23+
// See: https://github.com/oxc-project/oxc/issues/28717 and https://github.com/napi-rs/napi-rs/issues/3674
24+
if (
25+
(globalThis.process?.versions?.['webcontainer'] ||
26+
globalThis.process?.env?.['NAPI_RS_FORCE_WASI'] === 'true') &&
27+
!('addonModule' in Object.prototype)
28+
) {
29+
Object.defineProperty(Object.prototype, 'addonModule', {
30+
get() {
31+
return (this as { napiModule?: unknown }).napiModule;
32+
},
33+
set(value) {
34+
Object.defineProperty(this, 'addonModule', {
35+
value,
36+
writable: true,
37+
configurable: true,
38+
enumerable: true,
39+
});
40+
},
41+
configurable: true,
42+
enumerable: false,
43+
});
44+
}

‎packages/angular/build/src/tools/oxc/traversal.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import './oxc-wasi-shim';
910
import type { Node } from '@oxc-project/types';
1011
import { visitorKeys } from 'oxc-parser';
1112

‎pnpm-lock.yaml‎

Lines changed: 0 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/e2e/tests/vite/wasi.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import assert from 'node:assert';
2+
import { getGlobalVariable } from '../../utils/env';
3+
import { ngServe } from '../../utils/project';
4+
5+
export default async function () {
6+
assert(
7+
getGlobalVariable('argv')['esbuild'],
8+
'This test should not be called in the Webpack suite.',
9+
);
10+
11+
const originalForceWasi = process.env['NAPI_RS_FORCE_WASI'];
12+
process.env['NAPI_RS_FORCE_WASI'] = 'true';
13+
14+
try {
15+
const port = await ngServe();
16+
const response = await fetch(`http://localhost:${port}/`);
17+
assert(response.ok, 'Expected response to be ok');
18+
const text = await response.text();
19+
assert.match(text, /<app-root/);
20+
} finally {
21+
if (originalForceWasi === undefined) {
22+
delete process.env['NAPI_RS_FORCE_WASI'];
23+
} else {
24+
process.env['NAPI_RS_FORCE_WASI'] = originalForceWasi;
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)