|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
| 9 | +import { Module, createRequire } from 'node:module'; |
| 10 | + |
9 | 11 | // Workaround for an upstream incompatibility in `@oxc-parser/binding-wasm32-wasi` when |
10 | 12 | // falling back to WebAssembly (e.g., inside WebContainers or StackBlitz). |
11 | 13 | // |
12 | 14 | // `@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. |
| 15 | +// but depends on `@napi-rs/wasm-runtime@~1.2.6` which expects `@emnapi/core@^1.7.1 || 2.0.0-beta.2`. |
| 16 | +// 1. In `@napi-rs/wasm-runtime@1.2.6`, the runtime attempts to assign `initWorker` to |
| 17 | +// `emnapi_shared.addonModule`, which is undefined in `alpha.6` where the property was named |
| 18 | +// `napiModule`. Aliasing `addonModule` to `this.napiModule` on `Object.prototype` allows the |
| 19 | +// WASI runtime to initialize successfully. |
| 20 | +// 2. Under npm, `@napi-rs/wasm-runtime@1.2.6`'s peerDependencies cause npm to hoist `@emnapi/core@1.x` |
| 21 | +// to top-level `node_modules` while nesting `2.0.0-alpha.6` under `@oxc-parser/binding-wasm32-wasi`, |
| 22 | +// causing an ABI mismatch (`this.bridge.setLastError is not a function`). Under pnpm, `oxc-parser` |
| 23 | +// does not declare `@oxc-parser/binding-wasm32-wasi` in its own dependencies. Resolving these |
| 24 | +// packages relative to `@angular/build` and `@oxc-parser/binding-wasm32-wasi` ensures consistent |
| 25 | +// module resolution across package managers. |
22 | 26 | // |
23 | 27 | // See: https://github.com/oxc-project/oxc/issues/28717 and https://github.com/napi-rs/napi-rs/issues/3674 |
| 28 | +const forceWasi = globalThis.process?.env?.['NAPI_RS_FORCE_WASI']; |
24 | 29 | if ( |
25 | 30 | (globalThis.process?.versions?.['webcontainer'] || |
26 | | - globalThis.process?.env?.['NAPI_RS_FORCE_WASI'] === 'true') && |
| 31 | + forceWasi === 'true' || |
| 32 | + forceWasi === 'error') && |
27 | 33 | !('addonModule' in Object.prototype) |
28 | 34 | ) { |
29 | 35 | Object.defineProperty(Object.prototype, 'addonModule', { |
|
43 | 49 | configurable: true, |
44 | 50 | enumerable: false, |
45 | 51 | }); |
| 52 | + |
| 53 | + const resolvedModules = new Map<string, string>(); |
| 54 | + try { |
| 55 | + const wasiBindingPath = require.resolve('@oxc-parser/binding-wasm32-wasi'); |
| 56 | + resolvedModules.set('@oxc-parser/binding-wasm32-wasi', wasiBindingPath); |
| 57 | + const wasiRequire = createRequire(wasiBindingPath); |
| 58 | + resolvedModules.set('@emnapi/core', wasiRequire.resolve('@emnapi/core')); |
| 59 | + resolvedModules.set('@emnapi/runtime', wasiRequire.resolve('@emnapi/runtime')); |
| 60 | + } catch {} |
| 61 | + |
| 62 | + if (resolvedModules.size > 0) { |
| 63 | + const moduleWithResolve = Module as typeof Module & { |
| 64 | + _resolveFilename: ( |
| 65 | + request: string, |
| 66 | + parent: unknown, |
| 67 | + isMain: boolean, |
| 68 | + options: unknown, |
| 69 | + ) => string; |
| 70 | + }; |
| 71 | + const origResolveFilename = moduleWithResolve._resolveFilename; |
| 72 | + moduleWithResolve._resolveFilename = function (request, parent, isMain, options) { |
| 73 | + const resolved = resolvedModules.get(request); |
| 74 | + if (resolved !== undefined) { |
| 75 | + return resolved; |
| 76 | + } |
| 77 | + |
| 78 | + return origResolveFilename.call(this, request, parent, isMain, options); |
| 79 | + }; |
| 80 | + } |
46 | 81 | } |
0 commit comments