Skip to content

fix(defer): give each @defer block its own deps fn - #659

Merged
Brooooooklyn merged 1 commit into
mainfrom
fix/defer-per-block-deps
Oct 29, 2026
Merged

Brooooooklyn merged 1 commit into
mainfrom
fix/defer-per-block-deps

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

Summary

Part of #623 (the @defer loader row). The deferredImports form is #654.

When cross-file analysis is on (resolveImportedValues), oxc built one shared resolver per component and passed it to every ɵɵdefer. So the first block to trigger also loaded the chunks of the other blocks. Blocks that used only eager or same-file deps got no deps fn at all.

This PR ports ngtsc's full-compilation PerBlock mode:

  • Each block gets <Cmp>_Defer_<slot>_DepsFn. It lists the imports entries that the block's main content matches: a dynamic import() for a lazy dep, a plain reference for an eager one.
  • A nested block owns its own content. Its @placeholder, @loading and @error count for the outer block.
  • The name drops only the first _Template of the view's fn name, like fnName?.replace('_Template', '').
  • The pool uses the name as it is (getSharedFunctionReference(..., false)), so it does not use up a _cN index.
  • An entry used only inside @defer blocks leaves dependencies, even when it can't be lazy-loaded, because the block's fn provides it. When no entry is left, the component gets no dependencies key.
  • setClassMetadataAsync lists the lazy deps block by block.
  • Same-file standalone classes are matched by their selector too.

Ported from @angular/compiler-cli 22.2.3 ComponentDecoratorHandler (resolveDeferBlocks, resolveAllDeferredDependencies, compileDeferBlocks, handleDependencyCycles) and @angular/compiler 22.2.2 (compileDeferResolverFunction, resolveDeferDepsFns, ConstantPool.getSharedFunctionReference).

Before / after

// imports: [ChildCmp], template: '@defer { <child-cmp /> }'
// before
i0.ɵɵdefer(1, 0, () => [import("./child").then(m => m.ChildCmp)]);
... dependencies: i0.ɵɵgetComponentDepsFactory(App, [])
// after (same as ngtsc)
const App_Defer_1_DepsFn = () => [import("./child").then(m => m.ChildCmp)];
i0.ɵɵdefer(1, 0, App_Defer_1_DepsFn);
// no dependencies key
// class Foo + class Foo_Template in one file
// first draft: two `const Foo_Defer_1_DepsFn` (SyntaxError)
// now:
const Foo_Defer_1_DepsFn = ...;
const Foo_Template_Defer_1_DepsFn = ...;

ngtsc cases checked

I compared deps fns and dependencies with ngtsc on 37 cases. These match: in_if, same_two, nested, fwdref, dir_attr, dir_tpl, placeholder_only, for_loop, pure_fn, i18n, placeholder_defer, loading_err, reverse_order, switch_case, hydrate, ng_container_tpl, two_cmp_share_import, two_cmp_lazy, order_, name_tpl, collide, default_imp, sibling_spec, multi_sel, Card_Template, My_TemplateCmp in @if, a, sib_eager, sib_iface. A dependencies key is now missing exactly where ngtsc has none.

Known gaps that remain (same as before this PR): pipes, barrel re-exports, a self reference, and node_modules deps are not in the deps fn, and dependencies still uses the getComponentDepsFactory form. When several components in one file have the same deps fn, each one still emits its own copy.

Checks

  • cargo test --all-features: 3611 passed, 0 failed
  • conformance: 1264/1264, snapshot unchanged
  • clippy: no new warnings on changed lines
  • napi: build-test ok, vitest 21 files / 397 tests passed, tsc ok (no TS files changed)
  • The new tests fail with src stashed (11 integration + 1 unit) and pass with it.

🤖 Generated with Claude Code

With cross-file analysis (resolveImportedValues), oxc built one shared
resolver per component and passed it to every ɵɵdefer, so the first
block to trigger also fetched the chunks of the other blocks. Blocks
that used only eager or same-file deps got no deps fn at all.

Port ngtsc's full-compilation PerBlock mode: each block lists the
`imports` entries its main content matches (nested blocks own their
content, their @placeholder/@loading/@error count for the outer one),
as a dynamic import() when the dep is lazy and a plain reference when
it is eager. The fn is pooled as `<Cmp>_Defer_<slot>_DepsFn`, where
only the first `_Template` of the view's fn name is dropped, and the
pool takes the name as is, without using up a `_cN` index.
setClassMetadataAsync lists the lazy deps block by block. Same-file
standalone classes are matched by their selector too.

An entry used only inside @defer blocks leaves `dependencies`, even
when it can't be lazy-loaded: the blocks' fns provide it. When no
entry is left, the component gets no `dependencies` key.

Unknown selectors (node_modules, pipes) stay out of the lists, as
before; `dependencies` still provides them. An explicit
`deferredImports`, HMR, JIT and the no-analysis path keep the shared
resolver.

Ported from @angular/compiler-cli 22.2.3 ComponentDecoratorHandler
(resolveDeferBlocks, resolveAllDeferredDependencies, compileDeferBlocks,
handleDependencyCycles) and @angular/compiler 22.2.2
compileDeferResolverFunction, resolveDeferDepsFns and
ConstantPool.getSharedFunctionReference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T08:05:19.345485Z 7da9868 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Brooooooklyn

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 7da98684b5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Brooooooklyn
Brooooooklyn merged commit 4551864 into main Oct 29, 2026
10 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/defer-per-block-deps branch October 29, 2026 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant