|
| 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 | +import { Module, createRequire } from 'node:module'; |
| 10 | + |
| 11 | +// Workaround for an upstream incompatibility in `@oxc-parser/binding-wasm32-wasi` when |
| 12 | +// falling back to WebAssembly (e.g., inside WebContainers or StackBlitz). |
| 13 | +// |
| 14 | +// `@oxc-parser/binding-wasm32-wasi` declares a dependency on `@emnapi/core@2.0.0-alpha.6`, |
| 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. |
| 26 | +// |
| 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']; |
| 29 | +if ( |
| 30 | + (globalThis.process?.versions?.['webcontainer'] || |
| 31 | + forceWasi === 'true' || |
| 32 | + forceWasi === 'error') && |
| 33 | + !('addonModule' in Object.prototype) |
| 34 | +) { |
| 35 | + Object.defineProperty(Object.prototype, 'addonModule', { |
| 36 | + get() { |
| 37 | + return (this as { napiModule?: unknown }).napiModule; |
| 38 | + }, |
| 39 | + set(value) { |
| 40 | + if (this && (typeof this === 'object' || typeof this === 'function')) { |
| 41 | + Object.defineProperty(this, 'addonModule', { |
| 42 | + value, |
| 43 | + writable: true, |
| 44 | + configurable: true, |
| 45 | + enumerable: true, |
| 46 | + }); |
| 47 | + } |
| 48 | + }, |
| 49 | + configurable: true, |
| 50 | + enumerable: false, |
| 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 | + } |
| 81 | +} |
0 commit comments