@analogjs/vite-plugin-angular 3.0.0-beta.5 → 3.0.0-beta.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/migrations/migrate-setup-vitest/migrate-setup-vitest.js +2 -1
  2. package/migrations/migrate-setup-vitest/migrate-setup-vitest.js.map +1 -1
  3. package/ngp/LICENSE +21 -0
  4. package/ngp/api.mjs +46806 -0
  5. package/ngp/build.json +4 -0
  6. package/ngp/ng_analyze.cjs +601 -0
  7. package/ngp/ng_analyze_bg.wasm +0 -0
  8. package/package.json +4 -1
  9. package/src/index.d.ts +2 -0
  10. package/src/index.js +3 -1
  11. package/src/index.js.map +1 -1
  12. package/src/lib/analog-plugin-interop.js +1 -1
  13. package/src/lib/angular-build-context.d.ts +215 -0
  14. package/src/lib/angular-build-context.js +361 -0
  15. package/src/lib/angular-build-context.js.map +1 -0
  16. package/src/lib/angular-build-optimizer-plugin.d.ts +6 -4
  17. package/src/lib/angular-build-optimizer-plugin.js +24 -9
  18. package/src/lib/angular-build-optimizer-plugin.js.map +1 -1
  19. package/src/lib/angular-jit-plugin.d.ts +5 -3
  20. package/src/lib/angular-jit-plugin.js +14 -6
  21. package/src/lib/angular-jit-plugin.js.map +1 -1
  22. package/src/lib/angular-vite-plugin.d.ts +6 -109
  23. package/src/lib/angular-vite-plugin.js +8 -1249
  24. package/src/lib/angular-vite-plugin.js.map +1 -1
  25. package/src/lib/angular-vitest-plugin.d.ts +3 -2
  26. package/src/lib/angular-vitest-plugin.js +6 -3
  27. package/src/lib/angular-vitest-plugin.js.map +1 -1
  28. package/src/lib/companion-plugins.d.ts +7 -0
  29. package/src/lib/companion-plugins.js +38 -0
  30. package/src/lib/companion-plugins.js.map +1 -0
  31. package/src/lib/compilation-api/compilation-api-plugin.d.ts +11 -2
  32. package/src/lib/compilation-api/compilation-api-plugin.js +154 -282
  33. package/src/lib/compilation-api/compilation-api-plugin.js.map +1 -1
  34. package/src/lib/compiler/compile.d.ts +1 -1
  35. package/src/lib/compiler/compile.js +190 -145
  36. package/src/lib/compiler/compile.js.map +1 -1
  37. package/src/lib/compiler/defer.d.ts +2 -3
  38. package/src/lib/compiler/defer.js +12 -14
  39. package/src/lib/compiler/defer.js.map +1 -1
  40. package/src/lib/compiler/dts-reader.d.ts +6 -1
  41. package/src/lib/compiler/dts-reader.js +19 -10
  42. package/src/lib/compiler/dts-reader.js.map +1 -1
  43. package/src/lib/compiler/hmr.d.ts +4 -0
  44. package/src/lib/compiler/hmr.js +7 -2
  45. package/src/lib/compiler/hmr.js.map +1 -1
  46. package/src/lib/compiler/index.d.ts +2 -2
  47. package/src/lib/compiler/index.js +4 -4
  48. package/src/lib/compiler/jit-metadata.d.ts +3 -1
  49. package/src/lib/compiler/jit-metadata.js +10 -8
  50. package/src/lib/compiler/jit-metadata.js.map +1 -1
  51. package/src/lib/compiler/jit-transform.js +2 -2
  52. package/src/lib/compiler/jit-transform.js.map +1 -1
  53. package/src/lib/compiler/js-emitter.d.ts +0 -3
  54. package/src/lib/compiler/js-emitter.js +3 -21
  55. package/src/lib/compiler/js-emitter.js.map +1 -1
  56. package/src/lib/compiler/registry.d.ts +7 -1
  57. package/src/lib/compiler/registry.js +23 -3
  58. package/src/lib/compiler/registry.js.map +1 -1
  59. package/src/lib/compiler/resource-inliner.d.ts +8 -9
  60. package/src/lib/compiler/resource-inliner.js +10 -18
  61. package/src/lib/compiler/resource-inliner.js.map +1 -1
  62. package/src/lib/compiler/style-ast.d.ts +9 -1
  63. package/src/lib/compiler/style-ast.js +26 -5
  64. package/src/lib/compiler/style-ast.js.map +1 -1
  65. package/src/lib/compiler/type-elision.d.ts +4 -3
  66. package/src/lib/compiler/type-elision.js +10 -11
  67. package/src/lib/compiler/type-elision.js.map +1 -1
  68. package/src/lib/compiler/utils.d.ts +9 -7
  69. package/src/lib/compiler/utils.js +49 -9
  70. package/src/lib/compiler/utils.js.map +1 -1
  71. package/src/lib/component-scanner.d.ts +74 -0
  72. package/src/lib/component-scanner.js +178 -0
  73. package/src/lib/component-scanner.js.map +1 -0
  74. package/src/lib/component-styles-plugin.d.ts +52 -0
  75. package/src/lib/component-styles-plugin.js +353 -0
  76. package/src/lib/component-styles-plugin.js.map +1 -0
  77. package/src/lib/compose-angular-plugins.d.ts +12 -0
  78. package/src/lib/compose-angular-plugins.js +31 -0
  79. package/src/lib/compose-angular-plugins.js.map +1 -0
  80. package/src/lib/dev-server-mode-plugin.d.ts +7 -0
  81. package/src/lib/dev-server-mode-plugin.js +23 -0
  82. package/src/lib/dev-server-mode-plugin.js.map +1 -0
  83. package/src/lib/fast-compile-api.d.ts +62 -0
  84. package/src/lib/fast-compile-api.js +12 -0
  85. package/src/lib/fast-compile-api.js.map +1 -0
  86. package/src/lib/fast-compile-plugin.d.ts +6 -1
  87. package/src/lib/fast-compile-plugin.js +167 -107
  88. package/src/lib/fast-compile-plugin.js.map +1 -1
  89. package/src/lib/host.d.ts +3 -0
  90. package/src/lib/host.js +4 -2
  91. package/src/lib/host.js.map +1 -1
  92. package/src/lib/live-reload-plugin.d.ts +6 -3
  93. package/src/lib/live-reload-plugin.js +13 -6
  94. package/src/lib/live-reload-plugin.js.map +1 -1
  95. package/src/lib/ngp-compile-api.d.ts +55 -0
  96. package/src/lib/ngp-compile-api.js +12 -0
  97. package/src/lib/ngp-compile-api.js.map +1 -0
  98. package/src/lib/ngp-compile-plugin.d.ts +4 -0
  99. package/src/lib/ngp-compile-plugin.js +662 -0
  100. package/src/lib/ngp-compile-plugin.js.map +1 -0
  101. package/src/lib/ngtsc-compile-plugin.d.ts +23 -0
  102. package/src/lib/ngtsc-compile-plugin.js +778 -0
  103. package/src/lib/ngtsc-compile-plugin.js.map +1 -0
  104. package/src/lib/nx-folder-plugin.d.ts +6 -5
  105. package/src/lib/nx-folder-plugin.js +12 -7
  106. package/src/lib/nx-folder-plugin.js.map +1 -1
  107. package/src/lib/plugins/file-replacements.plugin.d.ts +6 -0
  108. package/src/lib/plugins/file-replacements.plugin.js +1 -0
  109. package/src/lib/plugins/file-replacements.plugin.js.map +1 -1
  110. package/src/lib/router-plugin.d.ts +5 -1
  111. package/src/lib/router-plugin.js +13 -4
  112. package/src/lib/router-plugin.js.map +1 -1
  113. package/src/lib/utils/compilation-shared.js +1 -1
  114. package/src/lib/utils/devkit.js +1 -1
  115. package/src/lib/utils/hmr-component-updates.d.ts +7 -0
  116. package/src/lib/utils/hmr-component-updates.js +87 -0
  117. package/src/lib/utils/hmr-component-updates.js.map +1 -0
  118. package/src/lib/utils/lsp-type-checker.d.ts +75 -0
  119. package/src/lib/utils/lsp-type-checker.js +531 -0
  120. package/src/lib/utils/lsp-type-checker.js.map +1 -0
  121. package/src/lib/utils/ngp-host-fs.d.ts +32 -0
  122. package/src/lib/utils/ngp-host-fs.js +104 -0
  123. package/src/lib/utils/ngp-host-fs.js.map +1 -0
  124. package/src/lib/utils/plugin-config.d.ts +15 -8
  125. package/src/lib/utils/plugin-config.js +18 -5
  126. package/src/lib/utils/plugin-config.js.map +1 -1
  127. package/src/lib/utils/stylesheet-warning.d.ts +2 -0
  128. package/src/lib/utils/stylesheet-warning.js +11 -0
  129. package/src/lib/utils/stylesheet-warning.js.map +1 -0
  130. package/src/lib/utils/tsconfig-resolver.js +1 -1
  131. package/src/lib/virtual-modules-plugin.d.ts +11 -3
  132. package/src/lib/virtual-modules-plugin.js +43 -12
  133. package/src/lib/virtual-modules-plugin.js.map +1 -1
@@ -1,22 +1,35 @@
1
1
  import { ANGULAR_DECORATORS, FIELD_DECORATORS } from "./constants.js";
2
- import { findAllClasses, resolveSyntheticImportSpecifier } from "./utils.js";
2
+ import { findAllClasses, parseOxc, resolveSyntheticImportSpecifier, walkOxc } from "./utils.js";
3
3
  import { angularVersionAtLeast } from "./angular-version.js";
4
4
  import { detectTypeOnlyImportNames, elideTypeOnlyImportsMagicString } from "./type-elision.js";
5
- import { emitAngularExpr, emitAngularStmt, setEmitterSourceCode, setEmitterSourceFile } from "./js-emitter.js";
5
+ import { emitAngularExpr, emitAngularStmt, setEmitterSourceCode } from "./js-emitter.js";
6
6
  import { lowerClassFields } from "./class-field-lowering.js";
7
7
  import { collectStringConstants, detectFieldDecorators, detectSignals, extractConstructorDeps, extractMetadata } from "./metadata.js";
8
- import { buildDeferDependencyMap } from "./defer.js";
8
+ import { buildDeferDependencyMap, buildImportMap } from "./defer.js";
9
+ import { buildCtorParameters, buildPropDecorators } from "./jit-metadata.js";
9
10
  import { debugCompile } from "./debug.js";
11
+ import { inlineStyleRanges } from "./style-ast.js";
10
12
  import * as fs from "node:fs";
11
13
  import * as path$1 from "node:path";
12
14
  import * as o from "@angular/compiler";
13
- import { ConstantPool, FactoryTarget, ParseLocation, ParseSourceFile, ParseSourceSpan, R3NgModuleMetadataKind, R3SelectorScopeMode, compileClassMetadata, compileComponentFromMetadata, compileDeclareClassMetadata, compileDeclareComponentFromMetadata, compileDeclareDirectiveFromMetadata, compileDeclareFactoryFunction, compileDeclareInjectableFromMetadata, compileDeclareInjectorFromMetadata, compileDeclareNgModuleFromMetadata, compileDeclarePipeFromMetadata, compileDirectiveFromMetadata, compileFactoryFunction, compileInjector, compileNgModule, compilePipeFromMetadata, makeBindingParser, parseHostBindings, parseTemplate } from "@angular/compiler";
14
- import { parseSync } from "oxc-parser";
15
- import * as ts from "typescript";
15
+ import { ConstantPool, FactoryTarget, ParseLocation, ParseSourceFile, ParseSourceSpan, R3NgModuleMetadataKind, R3SelectorScopeMode, compileClassMetadata, compileComponentClassMetadata, compileComponentFromMetadata, compileDeclareClassMetadata, compileDeclareComponentFromMetadata, compileDeclareDirectiveFromMetadata, compileDeclareFactoryFunction, compileDeclareInjectableFromMetadata, compileDeclareInjectorFromMetadata, compileDeclareNgModuleFromMetadata, compileDeclarePipeFromMetadata, compileDirectiveFromMetadata, compileFactoryFunction, compileInjector, compileNgModule, compilePipeFromMetadata, makeBindingParser, parseHostBindings, parseTemplate } from "@angular/compiler";
16
16
  import MagicString from "magic-string";
17
17
  //#region packages/vite-plugin-angular/src/lib/compiler/compile.ts
18
- function hasExportModifier(node) {
19
- return ts.canHaveModifiers(node) && ts.getModifiers(node)?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) === true;
18
+ function toTopLevelStatement(stmt) {
19
+ const isExport = (stmt.type === "ExportNamedDeclaration" || stmt.type === "ExportDefaultDeclaration") && stmt.declaration;
20
+ const decl = isExport ? stmt.declaration : stmt;
21
+ return {
22
+ decl,
23
+ exported: !!isExport,
24
+ start: Math.min(stmt.start, decl.decorators?.[0]?.start ?? stmt.start),
25
+ end: stmt.end
26
+ };
27
+ }
28
+ /** Get the callee name of a decorator call, e.g. `Component` for `@Component(...)`. */
29
+ function decoratorCallName(dec, sourceCode) {
30
+ const callee = dec.expression;
31
+ if (callee?.type !== "CallExpression") return void 0;
32
+ return sourceCode.slice(callee.callee.start, callee.callee.end);
20
33
  }
21
34
  /**
22
35
  * Normalize a `providers` / `viewProviders` metadata value into the single
@@ -34,37 +47,59 @@ function providersToExpr(value) {
34
47
  if (Array.isArray(value)) return value.length ? new o.LiteralArrayExpr(value) : null;
35
48
  return value;
36
49
  }
50
+ var LAZY_SCOPE_TYPES = /* @__PURE__ */ new Set([
51
+ "FunctionDeclaration",
52
+ "FunctionExpression",
53
+ "ArrowFunctionExpression",
54
+ "ClassDeclaration",
55
+ "ClassExpression",
56
+ "TSFunctionType",
57
+ "TSConstructorType",
58
+ "TSMethodSignature",
59
+ "TSCallSignatureDeclaration",
60
+ "TSConstructSignatureDeclaration"
61
+ ]);
37
62
  /**
38
63
  * Collect identifier names referenced in eagerly-evaluated code only.
39
64
  * Skips function/arrow/class bodies so that lazy references
40
65
  * (e.g. `const TOKEN = () => LaterClass`) are not treated as dependencies.
66
+ * The root node's own children are always walked.
41
67
  */
42
- function collectIdentifiers(node) {
68
+ function collectIdentifiers(root) {
43
69
  const ids = /* @__PURE__ */ new Set();
44
- function walk(n) {
45
- if (ts.isIdentifier(n)) ids.add(n.text);
46
- if (ts.isFunctionLike(n) || ts.isClassLike(n)) return;
47
- ts.forEachChild(n, walk);
48
- }
49
- ts.forEachChild(node, walk);
70
+ walkOxc(root, (n) => {
71
+ if (n.type === "Identifier") ids.add(n.name);
72
+ return n === root || !LAZY_SCOPE_TYPES.has(n.type);
73
+ });
50
74
  return ids;
51
75
  }
52
- /** Extract the names defined by a top-level statement. */
53
- function getDefinedNames(stmt) {
54
- if (ts.isVariableStatement(stmt)) {
76
+ /** Extract the names defined by a top-level declaration. */
77
+ function getDefinedNames(decl) {
78
+ if (decl.type === "VariableDeclaration") {
55
79
  const names = [];
56
- for (const decl of stmt.declarationList.declarations) collectBindingNames(decl.name, names);
80
+ for (const d of decl.declarations) collectBindingNames(d.id, names);
57
81
  return names;
58
82
  }
59
- if (ts.isFunctionDeclaration(stmt) && stmt.name) return [stmt.name.text];
60
- if (ts.isClassDeclaration(stmt) && stmt.name) return [stmt.name.text];
83
+ if ((decl.type === "FunctionDeclaration" || decl.type === "ClassDeclaration") && decl.id) return [decl.id.name];
61
84
  return [];
62
85
  }
63
- /** Recursively collect all bound identifiers from a binding name/pattern. */
64
- function collectBindingNames(name, out) {
65
- if (ts.isIdentifier(name)) out.push(name.text);
66
- else if (ts.isObjectBindingPattern(name) || ts.isArrayBindingPattern(name)) {
67
- for (const el of name.elements) if (!ts.isOmittedExpression(el)) collectBindingNames(el.name, out);
86
+ /** Recursively collect all bound identifiers from a binding pattern. */
87
+ function collectBindingNames(pattern, out) {
88
+ if (!pattern) return;
89
+ switch (pattern.type) {
90
+ case "Identifier":
91
+ out.push(pattern.name);
92
+ break;
93
+ case "ObjectPattern":
94
+ for (const prop of pattern.properties) collectBindingNames(prop.type === "RestElement" ? prop.argument : prop.value, out);
95
+ break;
96
+ case "ArrayPattern":
97
+ for (const el of pattern.elements) collectBindingNames(el, out);
98
+ break;
99
+ case "RestElement":
100
+ collectBindingNames(pattern.argument, out);
101
+ break;
102
+ case "AssignmentPattern": collectBindingNames(pattern.left, out);
68
103
  }
69
104
  }
70
105
  function compile(sourceCode, fileName, optionsOrRegistry) {
@@ -76,8 +111,8 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
76
111
  const isPartial = opts.compilationMode === "partial";
77
112
  const startTimeMs = debugCompile.enabled ? performance.now() : 0;
78
113
  debugCompile("compile %s (mode=%s, registry=%d entries)", fileName, isPartial ? "partial" : "full", registry?.size ?? 0);
79
- const origSourceFile = ts.createSourceFile(fileName, sourceCode, ts.ScriptTarget.Latest, false);
80
- const oxcProgram = opts.oxcProgram ?? parseSync(fileName, sourceCode).program;
114
+ const oxcProgram = opts.oxcProgram ?? parseOxc(fileName, sourceCode).program;
115
+ const styleRanges = resolvedInlineStyles && inlineStyleRanges(oxcProgram);
81
116
  const oxcClassMap = /* @__PURE__ */ new Map();
82
117
  for (const stmt of oxcProgram.body) {
83
118
  const decl = stmt.type === "ExportNamedDeclaration" || stmt.type === "ExportDefaultDeclaration" ? stmt.declaration : stmt;
@@ -92,19 +127,27 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
92
127
  const typeOnlyImports = detectTypeOnlyImportNames(sourceCode, oxcProgram);
93
128
  const importSpecifierByName = /* @__PURE__ */ new Map();
94
129
  const importedNames = /* @__PURE__ */ new Set();
95
- for (const stmt of origSourceFile.statements) {
96
- if (!ts.isImportDeclaration(stmt) || !stmt.importClause?.namedBindings || !ts.isNamedImports(stmt.importClause.namedBindings)) continue;
97
- const moduleSpecifier = stmt.moduleSpecifier.text;
98
- const isDeclarationTypeOnly = stmt.importClause.isTypeOnly;
99
- for (const element of stmt.importClause.namedBindings.elements) {
100
- const localName = element.name.text;
101
- importedNames.add(localName);
130
+ const typeOnlyImportedNames = /* @__PURE__ */ new Set();
131
+ for (const stmt of oxcProgram.body) {
132
+ if (stmt.type !== "ImportDeclaration") continue;
133
+ const moduleSpecifier = stmt.source.value;
134
+ const isDeclarationTypeOnly = stmt.importKind === "type";
135
+ for (const element of stmt.specifiers) {
136
+ if (element.type !== "ImportSpecifier") continue;
137
+ const localName = element.local.name;
102
138
  importSpecifierByName.set(localName, moduleSpecifier);
103
- if (isDeclarationTypeOnly || element.isTypeOnly) typeOnlyImports.add(localName);
139
+ if (isDeclarationTypeOnly || element.importKind === "type") {
140
+ typeOnlyImports.add(localName);
141
+ typeOnlyImportedNames.add(localName);
142
+ } else importedNames.add(localName);
104
143
  }
105
144
  }
106
- const sourceFile = injectAngularImport(origSourceFile);
107
145
  const localSelectors = /* @__PURE__ */ new Map();
146
+ /**
147
+ * Per component class: the dependencies only its @defer blocks use, which
148
+ * its class metadata must load lazily (`ɵsetClassMetadataAsync`).
149
+ */
150
+ const deferredMetadataDeps = /* @__PURE__ */ new Map();
108
151
  if (!registry) for (const [clsName, oxcNode] of oxcClassMap) {
109
152
  const decs = oxcNode.decorators || [];
110
153
  if (decs.length > 0) {
@@ -113,20 +156,17 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
113
156
  }
114
157
  }
115
158
  const bindingParser = isPartial ? void 0 : makeBindingParser();
116
- setEmitterSourceFile(origSourceFile);
117
159
  setEmitterSourceCode(sourceCode);
118
160
  const classResults = [];
161
+ const topLevelClasses = new Set(oxcProgram.body.map((stmt) => toTopLevelStatement(stmt).decl));
119
162
  const syntheticImports = /* @__PURE__ */ new Map();
120
- for (const node of findAllClasses(origSourceFile)) {
121
- const decorators = ts.getDecorators(node);
163
+ const dependencyLocalName = (name) => typeOnlyImportedNames.has(name) ? `ɵngDep_${name}` : name;
164
+ for (const node of findAllClasses(oxcProgram)) {
165
+ const decorators = node.decorators;
122
166
  if (!decorators || decorators.length === 0) continue;
123
- const className = node.name?.text;
167
+ const className = node.id?.name;
124
168
  if (!className) continue;
125
- const angularDecorators = decorators.filter((dec) => {
126
- if (!ts.isCallExpression(dec.expression)) return false;
127
- const name = dec.expression.expression.getText(origSourceFile);
128
- return ANGULAR_DECORATORS.has(name);
129
- });
169
+ const angularDecorators = decorators.filter((dec) => ANGULAR_DECORATORS.has(decoratorCallName(dec, sourceCode) ?? ""));
130
170
  if (angularDecorators.length === 0) continue;
131
171
  const ivyCode = [];
132
172
  let targetType = FactoryTarget.Injectable;
@@ -135,31 +175,13 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
135
175
  value: new o.WrappedNodeExpr(className),
136
176
  type: new o.WrappedNodeExpr(className)
137
177
  };
138
- const extendsClause = node.heritageClauses?.find((h) => h.token === ts.SyntaxKind.ExtendsKeyword);
139
- const usesInheritance = !!extendsClause && extendsClause.types.length > 0;
140
- const oxcNode = oxcClassMap.get(className);
178
+ const usesInheritance = !!node.superClass;
141
179
  let classCompileError = null;
142
180
  angularDecorators.forEach((dec) => {
143
- const decoratorName = dec.expression.expression.getText(origSourceFile);
144
- const oxcDec = oxcNode?.decorators?.find((d) => {
145
- const expr = d.expression;
146
- return expr?.type === "CallExpression" && expr.callee?.name === decoratorName;
147
- });
148
- const meta = extractMetadata(oxcDec, sourceCode, stringConsts);
149
- const sigs = oxcNode ? detectSignals(oxcNode, sourceCode) : {
150
- inputs: {},
151
- outputs: {},
152
- viewQueries: [],
153
- contentQueries: []
154
- };
155
- const fields = oxcNode ? detectFieldDecorators(oxcNode, sourceCode) : {
156
- inputs: {},
157
- outputs: {},
158
- viewQueries: [],
159
- contentQueries: [],
160
- hostProperties: {},
161
- hostListeners: {}
162
- };
181
+ const decoratorName = decoratorCallName(dec, sourceCode);
182
+ const meta = extractMetadata(dec, sourceCode, stringConsts);
183
+ const sigs = detectSignals(node, sourceCode);
184
+ const fields = detectFieldDecorators(node, sourceCode);
163
185
  const hostBindings = parseHostBindings(meta.hostRaw || {});
164
186
  const hostMetadata = {
165
187
  attributes: hostBindings.attributes,
@@ -206,7 +228,7 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
206
228
  for (const memberName of expanded) {
207
229
  const memberEntry = registry?.get(memberName);
208
230
  if (!memberEntry || memberEntry.kind === "tuple") continue;
209
- const memberRef = new o.WrappedNodeExpr(memberName);
231
+ const memberRef = new o.WrappedNodeExpr(dependencyLocalName(memberName));
210
232
  if (!importedNames.has(memberName)) {
211
233
  const memberSpecifier = resolveSyntheticImportSpecifier(fileName, memberEntry, tupleSpecifier);
212
234
  if (memberSpecifier) syntheticImports.set(memberName, memberSpecifier);
@@ -247,7 +269,7 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
247
269
  const exportedEntry = registry?.get(exportedName);
248
270
  if (exportedEntry) {
249
271
  const kind = exportedEntry.kind === "pipe" ? 1 : 0;
250
- const exportedRef = new o.WrappedNodeExpr(exportedName);
272
+ const exportedRef = new o.WrappedNodeExpr(dependencyLocalName(exportedName));
251
273
  const exportedSpecifier = resolveSyntheticImportSpecifier(fileName, exportedEntry, moduleSpecifier);
252
274
  if (exportedSpecifier) syntheticImports.set(exportedName, exportedSpecifier);
253
275
  const decl = {
@@ -326,7 +348,7 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
326
348
  }
327
349
  const kind = entry.kind === "pipe" ? 1 : 0;
328
350
  const decl = {
329
- type: new o.WrappedNodeExpr(name),
351
+ type: new o.WrappedNodeExpr(dependencyLocalName(name)),
330
352
  selector: entry.selector || `_unresolved-${name}`,
331
353
  kind,
332
354
  ...kind === 1 ? { name: entry.pipeName } : {}
@@ -354,8 +376,10 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
354
376
  } catch {
355
377
  console.warn(`[fast-compile] Could not read style file "${url}" for ${className}`);
356
378
  }
357
- if (resolvedInlineStyles) {
358
- for (const [idx, css] of resolvedInlineStyles) if (idx < meta.styles.length) meta.styles[idx] = css;
379
+ const styleRange = styleRanges?.get(dec);
380
+ if (resolvedInlineStyles && styleRange) for (let k = 0; k < styleRange.count; k++) {
381
+ const css = resolvedInlineStyles.get(styleRange.start + k);
382
+ if (css !== void 0) meta.styles[k] = css;
359
383
  }
360
384
  resolvedResources.set(dec, {
361
385
  template: templateContent || void 0,
@@ -386,6 +410,23 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
386
410
  classCompileError = /* @__PURE__ */ new Error(`[fast-compile] Template parse error in ${fileName} (${className}): ${firstError.msg}`);
387
411
  return;
388
412
  }
413
+ const deferDependencies = buildDeferDependencyMap(parsedTemplate, oxcProgram, registry, localSelectors);
414
+ if (!isPartial && deferDependencies.deferredImports.size > 0) {
415
+ const importMap = buildImportMap(oxcProgram);
416
+ const metadataDeps = [];
417
+ for (let i = declarations.length - 1; i >= 0; i--) {
418
+ const name = declarationName(declarations[i]);
419
+ const info = name !== null ? importMap.get(name) : void 0;
420
+ if (name === null || info === void 0 || !deferDependencies.deferredImports.has(name)) continue;
421
+ declarations.splice(i, 1);
422
+ metadataDeps.unshift({
423
+ symbolName: name,
424
+ importPath: info.path,
425
+ isDefaultImport: info.isDefault
426
+ });
427
+ }
428
+ if (metadataDeps.length > 0) deferredMetadataDeps.set(className, metadataDeps);
429
+ }
389
430
  const componentMeta = {
390
431
  ...meta,
391
432
  name: className,
@@ -423,7 +464,7 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
423
464
  usesInheritance,
424
465
  defer: {
425
466
  mode: 0,
426
- blocks: buildDeferDependencyMap(parsedTemplate, sourceFile, registry, localSelectors).blocks
467
+ blocks: deferDependencies.blocks
427
468
  },
428
469
  declarationListEmitMode: 1,
429
470
  i18nUseExternalIds: opts.i18nUseExternalIds ?? false,
@@ -619,7 +660,7 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
619
660
  }
620
661
  });
621
662
  if (classCompileError) throw classCompileError;
622
- const deps = oxcNode ? extractConstructorDeps(oxcNode, sourceCode, typeOnlyImports) : [];
663
+ const deps = extractConstructorDeps(node, sourceCode, typeOnlyImports);
623
664
  if (deps === null) {
624
665
  const baseVar = `ɵ${className}_BaseFactory`;
625
666
  ivyCode.unshift(`static ɵfac = /*@__PURE__*/ (() => { let ${baseVar}; return function ${className}_Factory(__ngFactoryType__) { return (${baseVar} || (${baseVar} = i0.ɵɵgetInheritedFactory(${className})))(__ngFactoryType__ || ${className}); }; })()`);
@@ -640,36 +681,39 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
640
681
  ivyCode.unshift(`static ɵfac = /*@__PURE__*/ ${emitAngularExpr(fac.expression)}`);
641
682
  }
642
683
  }
684
+ const ctorParameters = buildCtorParameters(node, sourceCode, typeOnlyImports);
685
+ const propDecorators = buildPropDecorators(node, sourceCode, "i0.");
686
+ const scopedMetadata = [];
643
687
  angularDecorators.forEach((dec) => {
644
- const call = dec.expression;
645
- const decName = call.expression.getText(origSourceFile);
646
- const decArgsNode = call.arguments[0];
688
+ const decName = decoratorCallName(dec, sourceCode);
689
+ const decArgsNode = dec.expression.arguments[0];
690
+ const textOf = (n) => sourceCode.slice(n.start, n.end);
647
691
  const resources = resolvedResources.get(dec);
648
692
  let metadataArgsExpr = null;
649
693
  if (decArgsNode) {
650
- if (resources && ts.isObjectLiteralExpression(decArgsNode)) {
694
+ if (resources && decArgsNode.type === "ObjectExpression") {
651
695
  const rewrittenProps = [];
652
696
  let needsTransform = false;
653
697
  for (const prop of decArgsNode.properties) {
654
- if (!ts.isPropertyAssignment(prop)) {
655
- rewrittenProps.push(prop.getText(origSourceFile));
698
+ if (prop.type !== "Property" || prop.shorthand || prop.method || prop.kind !== "init") {
699
+ rewrittenProps.push(textOf(prop));
656
700
  continue;
657
701
  }
658
- const propName = prop.name?.getText(origSourceFile);
702
+ const propName = textOf(prop.key);
659
703
  if (propName === "templateUrl" && resources.template) {
660
704
  rewrittenProps.push(`template: ${JSON.stringify(resources.template)}`);
661
705
  needsTransform = true;
662
706
  } else if ((propName === "styleUrl" || propName === "styleUrls") && resources.styles?.length) {
663
707
  rewrittenProps.push(`styles: [${resources.styles.map((s) => JSON.stringify(s)).join(", ")}]`);
664
708
  needsTransform = true;
665
- } else rewrittenProps.push(prop.getText(origSourceFile));
709
+ } else rewrittenProps.push(textOf(prop));
666
710
  }
667
- metadataArgsExpr = needsTransform ? `{${rewrittenProps.join(", ")}}` : decArgsNode.getText(origSourceFile);
668
- } else metadataArgsExpr = decArgsNode.getText(origSourceFile);
711
+ metadataArgsExpr = needsTransform ? `{${rewrittenProps.join(", ")}}` : textOf(decArgsNode);
712
+ } else metadataArgsExpr = textOf(decArgsNode);
669
713
  }
670
714
  try {
671
715
  const classMetaInput = {
672
- type: new o.WrappedNodeExpr(ts.factory.createIdentifier(className)),
716
+ type: new o.WrappedNodeExpr(className),
673
717
  decorators: new o.LiteralArrayExpr([new o.LiteralMapExpr([{
674
718
  key: "type",
675
719
  value: new o.WrappedNodeExpr(decName),
@@ -679,77 +723,72 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
679
723
  value: new o.LiteralArrayExpr([new o.WrappedNodeExpr(metadataArgsExpr)]),
680
724
  quoted: false
681
725
  }] : []])]),
682
- ctorParameters: null,
683
- propDecorators: null
726
+ ctorParameters: ctorParameters ? new o.WrappedNodeExpr(`() => [${ctorParameters}]`) : null,
727
+ propDecorators: propDecorators ? new o.WrappedNodeExpr(propDecorators) : null
684
728
  };
685
- const classMetadataExpr = isPartial ? compileDeclareClassMetadata(classMetaInput) : compileClassMetadata(classMetaInput);
686
- constantPool.statements.push(new o.ExpressionStatement(classMetadataExpr));
729
+ const deferredDeps = decName === "Component" ? deferredMetadataDeps.get(className) : void 0;
730
+ const classMetadataExpr = isPartial ? compileDeclareClassMetadata(classMetaInput) : deferredDeps ? compileComponentClassMetadata(classMetaInput, deferredDeps) : compileClassMetadata(classMetaInput);
731
+ const metadataStmt = new o.ExpressionStatement(classMetadataExpr);
732
+ if (topLevelClasses.has(node)) constantPool.statements.push(metadataStmt);
733
+ else scopedMetadata.push(metadataStmt);
687
734
  } catch (e) {
688
735
  if (debugCompile.enabled) debugCompile("compileClassMetadata failed for %s in %s: %s", className, fileName, e?.message);
689
736
  }
690
737
  });
691
738
  const memberDecorators = [];
692
- for (const member of node.members) {
693
- const mDecorators = ts.getDecorators(member);
694
- if (!mDecorators) continue;
695
- for (const dec of mDecorators) {
696
- if (!ts.isCallExpression(dec.expression)) continue;
697
- const decName = dec.expression.expression.getText(origSourceFile);
698
- if (FIELD_DECORATORS.has(decName)) memberDecorators.push(dec);
699
- }
700
- }
739
+ for (const member of node.body.body) for (const dec of member.decorators ?? []) if (FIELD_DECORATORS.has(decoratorCallName(dec, sourceCode) ?? "")) memberDecorators.push(dec);
701
740
  classResults.push({
702
741
  ivyCode,
703
742
  decorators: [...angularDecorators, ...memberDecorators],
704
- classEnd: node.getEnd()
743
+ classEnd: node.end,
744
+ scopedMetadata
705
745
  });
706
746
  }
707
747
  const ms = new MagicString(sourceCode, { filename: fileName });
708
748
  if (!sourceCode.includes("import * as i0 from")) ms.prepend("import * as i0 from \"@angular/core\";\n");
709
- for (const [name, specifier] of syntheticImports) if (!importedNames.has(name)) {
710
- ms.prepend(`import { ${name} } from "${specifier}";\n`);
711
- importedNames.add(name);
749
+ for (const [name, specifier] of syntheticImports) {
750
+ const localName = dependencyLocalName(name);
751
+ if (!importedNames.has(localName)) {
752
+ const binding = localName === name ? name : `${name} as ${localName}`;
753
+ ms.prepend(`import { ${binding} } from "${specifier}";\n`);
754
+ importedNames.add(localName);
755
+ }
712
756
  }
713
757
  if (classResults.length > 0) {
758
+ const statements = oxcProgram.body.map(toTopLevelStatement);
714
759
  let firstClassPos = Infinity;
715
760
  const nonHoistableNames = /* @__PURE__ */ new Set();
716
- for (const stmt of origSourceFile.statements) if (ts.isClassDeclaration(stmt)) {
717
- firstClassPos = stmt.getStart(origSourceFile);
718
- if (stmt.name) nonHoistableNames.add(stmt.name.text);
761
+ for (const { decl, start } of statements) if (decl.type === "ClassDeclaration") {
762
+ firstClassPos = start;
763
+ if (decl.id) nonHoistableNames.add(decl.id.name);
719
764
  break;
720
765
  }
721
- for (const stmt of origSourceFile.statements) {
722
- if (stmt.getStart(origSourceFile) <= firstClassPos) continue;
723
- if (ts.isClassDeclaration(stmt) && stmt.name) nonHoistableNames.add(stmt.name.text);
724
- if (ts.isEnumDeclaration(stmt)) nonHoistableNames.add(stmt.name.text);
725
- if (ts.isVariableStatement(stmt) && hasExportModifier(stmt)) for (const decl of stmt.declarationList.declarations) {
726
- const names = [];
727
- collectBindingNames(decl.name, names);
728
- for (const n of names) nonHoistableNames.add(n);
729
- }
730
- if (ts.isFunctionDeclaration(stmt) && hasExportModifier(stmt) && stmt.name) nonHoistableNames.add(stmt.name.text);
766
+ for (const { decl, exported, start } of statements) {
767
+ if (start <= firstClassPos) continue;
768
+ if (decl.type === "ClassDeclaration" || decl.type === "TSEnumDeclaration") {
769
+ if (decl.id) nonHoistableNames.add(decl.id.name);
770
+ } else if (exported && (decl.type === "VariableDeclaration" || decl.type === "FunctionDeclaration")) for (const n of getDefinedNames(decl)) nonHoistableNames.add(n);
731
771
  }
732
- for (const stmt of origSourceFile.statements) {
733
- const stmtStart = stmt.getStart(origSourceFile);
734
- if (stmtStart <= firstClassPos) continue;
735
- if (!(ts.isVariableStatement(stmt) && !hasExportModifier(stmt) || ts.isFunctionDeclaration(stmt) && !hasExportModifier(stmt))) continue;
736
- const referencedIds = collectIdentifiers(stmt);
772
+ for (const { decl, exported, start, end } of statements) {
773
+ if (start <= firstClassPos) continue;
774
+ if (!(!exported && (decl.type === "VariableDeclaration" || decl.type === "FunctionDeclaration"))) continue;
775
+ const referencedIds = collectIdentifiers(decl);
737
776
  let referencesNonHoistable = false;
738
777
  for (const id of referencedIds) if (nonHoistableNames.has(id)) {
739
778
  referencesNonHoistable = true;
740
779
  break;
741
780
  }
742
- if (referencesNonHoistable) for (const name of getDefinedNames(stmt)) nonHoistableNames.add(name);
781
+ if (referencesNonHoistable) for (const name of getDefinedNames(decl)) nonHoistableNames.add(name);
743
782
  else {
744
- ms.appendLeft(stmt.getEnd(), "\n");
745
- ms.move(stmtStart, stmt.getEnd(), firstClassPos);
783
+ ms.appendLeft(end, "\n");
784
+ ms.move(start, end, firstClassPos);
746
785
  }
747
786
  }
748
787
  }
749
788
  for (const cr of classResults) {
750
789
  for (const dec of cr.decorators) {
751
- const start = dec.getStart(origSourceFile);
752
- let trimEnd = dec.getEnd();
790
+ const start = dec.start;
791
+ let trimEnd = dec.end;
753
792
  while (trimEnd < sourceCode.length && (sourceCode[trimEnd] === " " || sourceCode[trimEnd] === "\n" || sourceCode[trimEnd] === "\r")) trimEnd++;
754
793
  ms.remove(start, trimEnd);
755
794
  }
@@ -757,6 +796,7 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
757
796
  const memberCode = cr.ivyCode.map((c) => " " + c + ";").join("\n");
758
797
  ms.appendLeft(cr.classEnd - 1, "\n" + memberCode + "\n");
759
798
  }
799
+ for (const stmt of cr.scopedMetadata) ms.appendLeft(cr.classEnd, `\n/*@__PURE__*/ ${emitAngularStmt(stmt)}`);
760
800
  }
761
801
  const helpers = [];
762
802
  const sideEffects = [];
@@ -765,25 +805,26 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
765
805
  if (s instanceof o.ExpressionStatement && s.expr instanceof o.InvokeFunctionExpr) sideEffects.push(`/*@__PURE__*/ ${code}`);
766
806
  else helpers.push(code);
767
807
  }
808
+ if (sideEffects.length > 0) ms.append("\n\n" + sideEffects.join("\n"));
809
+ if (!useDefineForClassFields) lowerClassFields(ms, sourceCode, oxcProgram);
810
+ const helperCode = helpers.join("\n");
811
+ const willElide = detectTypeOnlyImportNames(ms.toString() + "\n" + helperCode);
768
812
  if (helpers.length > 0) {
769
- const willElide = detectTypeOnlyImportNames(ms.toString());
770
813
  let insertPos = 0;
771
- for (const stmt of origSourceFile.statements) if (ts.isImportDeclaration(stmt)) {
772
- if (willElide.size > 0 && stmt.importClause) {
773
- const clause = stmt.importClause;
774
- const namedBindings = clause.namedBindings && ts.isNamedImports(clause.namedBindings) ? clause.namedBindings.elements : void 0;
775
- const defaultName = clause.name;
776
- if ((!defaultName || willElide.has(defaultName.text)) && (!namedBindings || namedBindings.every((el) => el.isTypeOnly || willElide.has(el.name.text)))) continue;
777
- }
778
- insertPos = stmt.getEnd();
779
- } else if (ts.isVariableStatement(stmt) && !stmt.getText(origSourceFile).includes("class")) insertPos = stmt.getEnd();
780
- else if (!ts.isExportAssignment(stmt)) break;
781
- if (insertPos === 0) ms.appendLeft(0, helpers.join("\n") + "\n");
782
- else ms.appendRight(insertPos, "\n" + helpers.join("\n") + "\n");
814
+ for (const stmt of oxcProgram.body) {
815
+ const { decl } = toTopLevelStatement(stmt);
816
+ if (stmt.type === "ImportDeclaration") {
817
+ if (willElide.size > 0 && stmt.specifiers.length > 0) {
818
+ if (stmt.specifiers.every((spec) => spec.type === "ImportNamespaceSpecifier" || spec.importKind === "type" || willElide.has(spec.local.name))) continue;
819
+ }
820
+ insertPos = stmt.end;
821
+ } else if (decl.type === "VariableDeclaration" && !sourceCode.slice(stmt.start, stmt.end).includes("class")) insertPos = stmt.end;
822
+ else if (stmt.type !== "TSExportAssignment" && !(stmt.type === "ExportDefaultDeclaration" && decl.type !== "ClassDeclaration" && decl.type !== "FunctionDeclaration")) break;
823
+ }
824
+ if (insertPos === 0) ms.appendLeft(0, helperCode + "\n");
825
+ else ms.appendRight(insertPos, "\n" + helperCode + "\n");
783
826
  }
784
- if (sideEffects.length > 0) ms.append("\n\n" + sideEffects.join("\n"));
785
- if (!useDefineForClassFields) lowerClassFields(ms, sourceCode, oxcProgram);
786
- elideTypeOnlyImportsMagicString(ms, oxcProgram);
827
+ elideTypeOnlyImportsMagicString(ms, oxcProgram, willElide);
787
828
  const map = ms.generateMap({
788
829
  source: fileName,
789
830
  file: fileName + ".js",
@@ -797,8 +838,12 @@ function compile(sourceCode, fileName, optionsOrRegistry) {
797
838
  resourceDependencies
798
839
  };
799
840
  }
800
- function injectAngularImport(sf) {
801
- return ts.factory.updateSourceFile(sf, [ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamespaceImport(ts.factory.createIdentifier("i0"))), ts.factory.createStringLiteral("@angular/core")), ...sf.statements]);
841
+ /** The local class name an `imports` entry declares, when it is a plain identifier. */
842
+ function declarationName(declaration) {
843
+ const node = declaration.type.node;
844
+ if (typeof node === "string") return node;
845
+ if (node?.type === "Identifier" && typeof node.name === "string") return node.name;
846
+ return declaration.className ?? null;
802
847
  }
803
848
  //#endregion
804
849
  export { compile };