Skip to content

Commit 9e061a2

Browse files
committed
fix(@angular/build): fall back to Babel linker in WebContainer environments
PR #34540 declared `@oxc-parser/binding-wasm32-wasi` as an optional dependency of `@angular/build` to avoid runtime package manager execution inside WebContainer environments. However, upstream WASI bindings for `oxc-parser` currently fail to initialize due to a version and ABI incompatibility between `@napi-rs/wasm-runtime@1.2.6` and `@emnapi/core` (napi-rs/napi-rs#3674). When initialization fails, `oxc-parser` falls back to shelling out to `pnpm i` in `/tmp` from inside the Piscina worker thread, which surfaces as a fatal `ERR_REQUIRE_CYCLE_MODULE` error and crashes `ng serve` and `ng build`. Furthermore, `oxc-transform` was eagerly imported at top-level in `javascript-transformer-worker.ts`, causing worker evaluation to fail immediately upon startup even when OXC was not needed. This change resolves the issue in WebContainer environments: 1. Defaults `useBabelLinker` to `true` when running within WebContainer (`process.versions.webcontainer`), mirroring `useSassEmbedded`. 2. Lazily imports `oxc-transform` in `javascript-transformer-worker.ts` so it is never evaluated during worker module initialization. 3. Skips attempting OXC in WebContainer (unless explicitly requested via `NG_BUILD_BABEL_LINKER=false`), and gracefully falls back to the pure-JS Babel linker if OXC fails to load. Fixes #34524
1 parent ef600f2 commit 9e061a2

4 files changed

Lines changed: 111 additions & 7 deletions

File tree

‎packages/angular/build/src/tools/javascript-transformer/javascript-transformer-worker.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
loadInputSourceMapFromUrl,
2020
removeSourceMappingURL,
2121
} from '../../utils/source-map';
22-
import { transform as transformWithOxc } from '../oxc/oxc-transform.js';
2322
import type { JavaScriptTransformerOptions } from './javascript-transformer';
2423

2524
interface JavaScriptTransformRequest {
@@ -54,6 +53,8 @@ let babelLinkerDeps:
5453

5554
let remapping: typeof import('@ampproject/remapping').default | undefined;
5655

56+
let oxcTransform: typeof import('../oxc/oxc-transform.js').transform | undefined;
57+
5758
const textDecoder = new TextDecoder();
5859
const textEncoder = new TextEncoder();
5960

@@ -181,7 +182,18 @@ async function transformJavaScriptImpl(
181182
coverageMap = result.map;
182183
}
183184

184-
if (shouldLink && useBabelLinker) {
185+
let oxcLink = shouldLink && !useBabelLinker;
186+
const shouldRunOxc = oxcLink || (!process.versions.webcontainer && advancedOptimizations);
187+
188+
if (shouldRunOxc) {
189+
try {
190+
oxcTransform ??= (await import('../oxc/oxc-transform.js')).transform;
191+
} catch {
192+
oxcLink = false;
193+
}
194+
}
195+
196+
if (shouldLink && (useBabelLinker || !oxcLink)) {
185197
babelLinkerDeps ??= Promise.all([
186198
import('@angular/compiler-cli/linker/babel'),
187199
import('@angular/compiler-cli'),
@@ -222,14 +234,13 @@ async function transformJavaScriptImpl(
222234
}
223235

224236
// Run Oxc linking and/or advanced optimizations in a single unified AST traversal pass
225-
const oxcLink = shouldLink && !useBabelLinker;
226-
if (oxcLink || advancedOptimizations) {
237+
if (oxcTransform && (oxcLink || advancedOptimizations)) {
227238
const sideEffectFree = options.sideEffects === false;
228239
const safeAngularPackage =
229240
sideEffectFree && /[\\/]node_modules[\\/]@angular[\\/]/.test(filename);
230241
const topLevelSafeMode = !safeAngularPackage;
231242

232-
const result = transformWithOxc(filename, code, {
243+
const result = oxcTransform(filename, code, {
233244
link: oxcLink,
234245
jit,
235246
advancedOptimizations,

‎packages/angular/build/src/tools/javascript-transformer/javascript-transformer_spec.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,39 @@ describe('JavaScriptTransformer sourcemaps', () => {
343343
expect(text).not.toContain('i0.ɵɵngDeclareDirective');
344344
});
345345

346+
it('should link declarations using the Babel linker when NG_BUILD_BABEL_LINKER is enabled', async () => {
347+
process.env['NG_BUILD_BABEL_LINKER'] = 'true';
348+
try {
349+
transformer = new JavaScriptTransformer({
350+
sourcemap: false,
351+
maxConcurrency: 1,
352+
});
353+
354+
const input = `
355+
import * as i0 from "@angular/core";
356+
export class MyDirective {}
357+
MyDirective.ɵdir = i0.ɵɵngDeclareDirective({
358+
minVersion: "12.0.0",
359+
version: "14.0.0",
360+
ngImport: i0,
361+
type: MyDirective,
362+
selector: "[my-dir]"
363+
});
364+
`;
365+
366+
const result = await transformer.transformData(
367+
'node_modules/@angular/compiler-cli/test.js',
368+
input,
369+
{ skipLinker: false },
370+
);
371+
const text = Buffer.from(result).toString('utf-8');
372+
373+
expect(text).not.toContain('i0.ɵɵngDeclareDirective');
374+
} finally {
375+
delete process.env['NG_BUILD_BABEL_LINKER'];
376+
}
377+
});
378+
346379
describe('advanced optimizations fast-path pre-filter', () => {
347380
it('should bypass worker and return input buffer directly when no candidate tokens are present', async () => {
348381
transformer = new JavaScriptTransformer({

‎packages/angular/build/src/utils/environment-options.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,14 @@ export const useComponentTemplateHmr = parseTristate(process.env['NG_HMR_TEMPLAT
218218
export const usePartialSsrBuild = parseTristate(process.env['NG_BUILD_PARTIAL_SSR']) === true;
219219

220220
/**
221-
* When `NG_BUILD_BABEL_LINKER` is enabled (`1` or `true`), the Babel-based
221+
* When `NG_BUILD_BABEL_LINKER` is enabled (`1` or `true`), or when running within a
222+
* WebContainer environment (unless explicitly disabled), the Babel-based
222223
* Angular Linker (`@angular/compiler-cli/linker/babel`) will be used instead of the
223224
* default OXC in-place linker.
224225
*/
225-
export const useBabelLinker = parseTristate(process.env['NG_BUILD_BABEL_LINKER']) === true;
226+
export const useBabelLinker = process.versions.webcontainer
227+
? parseTristate(process.env['NG_BUILD_BABEL_LINKER']) !== false
228+
: parseTristate(process.env['NG_BUILD_BABEL_LINKER']) === true;
226229

227230
/**
228231
* When `NG_BUILD_SASS_EMBEDDED` is set to `0` or `false`, or when running within a

‎packages/angular/build/src/utils/environment-options_spec.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,60 @@ describe('environment options - maxInlinerWorkers', () => {
245245
expect(maxInlinerWorkers).toBe(Math.min(8, availableParallelism()));
246246
});
247247
});
248+
249+
describe('environment options - useBabelLinker', () => {
250+
const originalEnvValue = process.env['NG_BUILD_BABEL_LINKER'];
251+
const originalWebcontainer = process.versions.webcontainer;
252+
253+
function loadEnvironmentOptions(): typeof import('./environment-options') {
254+
delete require.cache[require.resolve('./environment-options')];
255+
256+
return require('./environment-options');
257+
}
258+
259+
afterEach(() => {
260+
if (originalEnvValue !== undefined) {
261+
process.env['NG_BUILD_BABEL_LINKER'] = originalEnvValue;
262+
} else {
263+
delete process.env['NG_BUILD_BABEL_LINKER'];
264+
}
265+
if (originalWebcontainer !== undefined) {
266+
process.versions.webcontainer = originalWebcontainer;
267+
} else {
268+
delete process.versions.webcontainer;
269+
}
270+
delete require.cache[require.resolve('./environment-options')];
271+
});
272+
273+
it('defaults useBabelLinker to false in a standard environment', () => {
274+
delete process.env['NG_BUILD_BABEL_LINKER'];
275+
delete process.versions.webcontainer;
276+
const { useBabelLinker } = loadEnvironmentOptions();
277+
278+
expect(useBabelLinker).toBeFalse();
279+
});
280+
281+
it('defaults useBabelLinker to true in a webcontainer environment', () => {
282+
delete process.env['NG_BUILD_BABEL_LINKER'];
283+
process.versions.webcontainer = '1.0.0';
284+
const { useBabelLinker } = loadEnvironmentOptions();
285+
286+
expect(useBabelLinker).toBeTrue();
287+
});
288+
289+
it('allows disabling Babel linker in a webcontainer environment via NG_BUILD_BABEL_LINKER=false', () => {
290+
process.env['NG_BUILD_BABEL_LINKER'] = 'false';
291+
process.versions.webcontainer = '1.0.0';
292+
const { useBabelLinker } = loadEnvironmentOptions();
293+
294+
expect(useBabelLinker).toBeFalse();
295+
});
296+
297+
it('allows enabling Babel linker in a standard environment via NG_BUILD_BABEL_LINKER=true', () => {
298+
process.env['NG_BUILD_BABEL_LINKER'] = 'true';
299+
delete process.versions.webcontainer;
300+
const { useBabelLinker } = loadEnvironmentOptions();
301+
302+
expect(useBabelLinker).toBeTrue();
303+
});
304+
});

0 commit comments

Comments
 (0)