Skip to content

fix(@angular/build): elide Angular class metadata in static blocks in oxc-transform - #34770

Draft
mattrbeck wants to merge 2 commits into
angular:mainfrom
mattrbeck:fix/oxc-elide-static-block-metadata
Draft

mattrbeck wants to merge 2 commits into
angular:mainfrom
mattrbeck:fix/oxc-elide-static-block-metadata

Conversation

@mattrbeck

Copy link
Copy Markdown
Member

oxc-transform previously only elided ɵsetClassMetadata / ɵsetClassMetadataAsync / ɵsetClassDebugInfo calls inside FunctionExpression / ArrowFunctionExpression IIFEs, and analyzeClassStaticProperties() set shouldWrap = false whenever a class contained any StaticBlock other than this.<prop> = <expr>.

When full-mode Angular classes emit metadata inside an ES2022 static { ... } block (as ngp already does, and as proposed for ngtsc in angular/angular#72136 — including @angular/core/fesm2022/core.mjs, where Rollup bundles internal setClassMetadata calls without the ɵ prefix), oxc-transform previously left the static {} block intact and skipped wrapping the class in a /*#__PURE__*/ IIFE, preventing unused classes such as PlatformRef and ApplicationModule from being tree-shaken (+10.0 kB in standalone apps).

This updates oxc-transform and traversal to:

  • Recognize ɵsetClassMetadata* calls and ngDevMode/ngJitMode-guarded (ɵ)setClassMetadata* statements inside StaticBlocks, removing metadata-only StaticBlocks (and individual metadata statements inside multi-statement StaticBlocks) when advancedOptimizations is enabled.
  • Allow analyzeClassStaticProperties() to continue wrapping classes whose StaticBlocks contain only elidable Angular metadata and/or pure this.<prop> = <expr> assignments, while still leaving user side-effectful StaticBlocks unwrapped.

Minimal failure example

Input (@angular/core / ngp / ngtsc ES2022 output):

export class PlatformRef {
  static ɵfac = function PlatformRef_Factory(t) { return new (t || PlatformRef)(); };
  static ɵprov = /*@__PURE__*/ ɵɵdefineInjectable({ token: PlatformRef, factory: PlatformRef.ɵfac, providedIn: 'platform' });
  static {
    (typeof ngDevMode === "undefined" || ngDevMode) &&
      setClassMetadata(PlatformRef, [{ type: Injectable, args: [{ providedIn: 'platform' }] }], null, null);
  }
}

Before (main):
Left unchanged (shouldWrap = false), preventing PlatformRef from being wrapped in /*#__PURE__*/ (() => { ... })() and retaining it in standalone application bundles.

After:

export let PlatformRef = /*#__PURE__*/ (() => {
  class PlatformRef {
    static ɵfac = function PlatformRef_Factory(t) { return new (t || PlatformRef)(); };
    static ɵprov = /*@__PURE__*/ ɵɵdefineInjectable({ token: PlatformRef, factory: PlatformRef.ɵfac, providedIn: 'platform' });
  }
  return PlatformRef;
})();

…data elision

Add specs verifying that oxc-transform elides ɵsetClassMetadata,
ɵsetClassMetadataAsync, and ngDevMode-guarded internal setClassMetadata calls
inside ES2022 class static blocks while allowing pure static class wrapping.
… oxc-transform

Recognize Angular class metadata statements inside ES2022 class static blocks
(as emitted by ngp and ngtsc, including ngDevMode/ngJitMode-guarded internal
setClassMetadata calls in @angular/core FESM bundles) so oxc-transform removes
the metadata static block and continues wrapping pure static Angular classes.
mattrbeck added a commit to mattrbeck/angular that referenced this pull request Nov 20, 2026
Temporarily patch @angular/build oxc-transform (matching upstream PR
angular/angular-cli#34770) so Angular class metadata inside ES2022 static
blocks (including ngDevMode-guarded internal setClassMetadata calls in
@angular/core FESM bundles) is elided and pure static Angular classes continue
to be wrapped for tree-shaking.
mattrbeck added a commit to mattrbeck/angular that referenced this pull request Nov 20, 2026
Temporarily patch @angular/build oxc-transform (matching upstream PR
angular/angular-cli#34770) so Angular class metadata inside ES2022 static
blocks (including ngDevMode-guarded internal setClassMetadata calls in
@angular/core FESM bundles) is elided and pure static Angular classes continue
to be wrapped for tree-shaking.
mattrbeck added a commit to mattrbeck/angular that referenced this pull request Nov 20, 2026
Temporarily patch @angular/build oxc-transform (matching upstream PR
angular/angular-cli#34770) so Angular class metadata inside ES2022 static
blocks (including ngDevMode-guarded internal setClassMetadata calls in
@angular/core FESM bundles) is elided and pure static Angular classes continue
to be wrapped for tree-shaking.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant