@analogjs/vite-plugin-angular 3.0.0-beta.5 → 3.0.0-beta.51
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.
- package/migrations/migrate-setup-vitest/migrate-setup-vitest.js +2 -1
- package/migrations/migrate-setup-vitest/migrate-setup-vitest.js.map +1 -1
- package/ngp/LICENSE +21 -0
- package/ngp/api.mjs +46806 -0
- package/ngp/build.json +4 -0
- package/ngp/ng_analyze.cjs +601 -0
- package/ngp/ng_analyze_bg.wasm +0 -0
- package/package.json +4 -1
- package/src/index.d.ts +2 -0
- package/src/index.js +3 -1
- package/src/index.js.map +1 -1
- package/src/lib/analog-plugin-interop.js +1 -1
- package/src/lib/angular-build-context.d.ts +215 -0
- package/src/lib/angular-build-context.js +361 -0
- package/src/lib/angular-build-context.js.map +1 -0
- package/src/lib/angular-build-optimizer-plugin.d.ts +6 -4
- package/src/lib/angular-build-optimizer-plugin.js +24 -9
- package/src/lib/angular-build-optimizer-plugin.js.map +1 -1
- package/src/lib/angular-jit-plugin.d.ts +5 -3
- package/src/lib/angular-jit-plugin.js +14 -6
- package/src/lib/angular-jit-plugin.js.map +1 -1
- package/src/lib/angular-vite-plugin.d.ts +6 -109
- package/src/lib/angular-vite-plugin.js +8 -1249
- package/src/lib/angular-vite-plugin.js.map +1 -1
- package/src/lib/angular-vitest-plugin.d.ts +3 -2
- package/src/lib/angular-vitest-plugin.js +6 -3
- package/src/lib/angular-vitest-plugin.js.map +1 -1
- package/src/lib/companion-plugins.d.ts +7 -0
- package/src/lib/companion-plugins.js +38 -0
- package/src/lib/companion-plugins.js.map +1 -0
- package/src/lib/compilation-api/compilation-api-plugin.d.ts +11 -2
- package/src/lib/compilation-api/compilation-api-plugin.js +154 -282
- package/src/lib/compilation-api/compilation-api-plugin.js.map +1 -1
- package/src/lib/compiler/compile.d.ts +1 -1
- package/src/lib/compiler/compile.js +190 -145
- package/src/lib/compiler/compile.js.map +1 -1
- package/src/lib/compiler/defer.d.ts +2 -3
- package/src/lib/compiler/defer.js +12 -14
- package/src/lib/compiler/defer.js.map +1 -1
- package/src/lib/compiler/dts-reader.d.ts +6 -1
- package/src/lib/compiler/dts-reader.js +19 -10
- package/src/lib/compiler/dts-reader.js.map +1 -1
- package/src/lib/compiler/hmr.d.ts +4 -0
- package/src/lib/compiler/hmr.js +7 -2
- package/src/lib/compiler/hmr.js.map +1 -1
- package/src/lib/compiler/index.d.ts +2 -2
- package/src/lib/compiler/index.js +4 -4
- package/src/lib/compiler/jit-metadata.d.ts +3 -1
- package/src/lib/compiler/jit-metadata.js +10 -8
- package/src/lib/compiler/jit-metadata.js.map +1 -1
- package/src/lib/compiler/jit-transform.js +2 -2
- package/src/lib/compiler/jit-transform.js.map +1 -1
- package/src/lib/compiler/js-emitter.d.ts +0 -3
- package/src/lib/compiler/js-emitter.js +3 -21
- package/src/lib/compiler/js-emitter.js.map +1 -1
- package/src/lib/compiler/registry.d.ts +7 -1
- package/src/lib/compiler/registry.js +23 -3
- package/src/lib/compiler/registry.js.map +1 -1
- package/src/lib/compiler/resource-inliner.d.ts +8 -9
- package/src/lib/compiler/resource-inliner.js +10 -18
- package/src/lib/compiler/resource-inliner.js.map +1 -1
- package/src/lib/compiler/style-ast.d.ts +9 -1
- package/src/lib/compiler/style-ast.js +26 -5
- package/src/lib/compiler/style-ast.js.map +1 -1
- package/src/lib/compiler/type-elision.d.ts +4 -3
- package/src/lib/compiler/type-elision.js +10 -11
- package/src/lib/compiler/type-elision.js.map +1 -1
- package/src/lib/compiler/utils.d.ts +9 -7
- package/src/lib/compiler/utils.js +49 -9
- package/src/lib/compiler/utils.js.map +1 -1
- package/src/lib/component-scanner.d.ts +74 -0
- package/src/lib/component-scanner.js +178 -0
- package/src/lib/component-scanner.js.map +1 -0
- package/src/lib/component-styles-plugin.d.ts +52 -0
- package/src/lib/component-styles-plugin.js +353 -0
- package/src/lib/component-styles-plugin.js.map +1 -0
- package/src/lib/compose-angular-plugins.d.ts +12 -0
- package/src/lib/compose-angular-plugins.js +31 -0
- package/src/lib/compose-angular-plugins.js.map +1 -0
- package/src/lib/dev-server-mode-plugin.d.ts +7 -0
- package/src/lib/dev-server-mode-plugin.js +23 -0
- package/src/lib/dev-server-mode-plugin.js.map +1 -0
- package/src/lib/fast-compile-api.d.ts +62 -0
- package/src/lib/fast-compile-api.js +12 -0
- package/src/lib/fast-compile-api.js.map +1 -0
- package/src/lib/fast-compile-plugin.d.ts +6 -1
- package/src/lib/fast-compile-plugin.js +167 -107
- package/src/lib/fast-compile-plugin.js.map +1 -1
- package/src/lib/host.d.ts +3 -0
- package/src/lib/host.js +4 -2
- package/src/lib/host.js.map +1 -1
- package/src/lib/live-reload-plugin.d.ts +6 -3
- package/src/lib/live-reload-plugin.js +13 -6
- package/src/lib/live-reload-plugin.js.map +1 -1
- package/src/lib/ngp-compile-api.d.ts +55 -0
- package/src/lib/ngp-compile-api.js +12 -0
- package/src/lib/ngp-compile-api.js.map +1 -0
- package/src/lib/ngp-compile-plugin.d.ts +4 -0
- package/src/lib/ngp-compile-plugin.js +662 -0
- package/src/lib/ngp-compile-plugin.js.map +1 -0
- package/src/lib/ngtsc-compile-plugin.d.ts +23 -0
- package/src/lib/ngtsc-compile-plugin.js +778 -0
- package/src/lib/ngtsc-compile-plugin.js.map +1 -0
- package/src/lib/nx-folder-plugin.d.ts +6 -5
- package/src/lib/nx-folder-plugin.js +12 -7
- package/src/lib/nx-folder-plugin.js.map +1 -1
- package/src/lib/plugins/file-replacements.plugin.d.ts +6 -0
- package/src/lib/plugins/file-replacements.plugin.js +1 -0
- package/src/lib/plugins/file-replacements.plugin.js.map +1 -1
- package/src/lib/router-plugin.d.ts +5 -1
- package/src/lib/router-plugin.js +13 -4
- package/src/lib/router-plugin.js.map +1 -1
- package/src/lib/utils/compilation-shared.js +1 -1
- package/src/lib/utils/devkit.js +1 -1
- package/src/lib/utils/hmr-component-updates.d.ts +7 -0
- package/src/lib/utils/hmr-component-updates.js +87 -0
- package/src/lib/utils/hmr-component-updates.js.map +1 -0
- package/src/lib/utils/lsp-type-checker.d.ts +75 -0
- package/src/lib/utils/lsp-type-checker.js +531 -0
- package/src/lib/utils/lsp-type-checker.js.map +1 -0
- package/src/lib/utils/ngp-host-fs.d.ts +32 -0
- package/src/lib/utils/ngp-host-fs.js +104 -0
- package/src/lib/utils/ngp-host-fs.js.map +1 -0
- package/src/lib/utils/plugin-config.d.ts +15 -8
- package/src/lib/utils/plugin-config.js +18 -5
- package/src/lib/utils/plugin-config.js.map +1 -1
- package/src/lib/utils/stylesheet-warning.d.ts +2 -0
- package/src/lib/utils/stylesheet-warning.js +11 -0
- package/src/lib/utils/stylesheet-warning.js.map +1 -0
- package/src/lib/utils/tsconfig-resolver.js +1 -1
- package/src/lib/virtual-modules-plugin.d.ts +11 -3
- package/src/lib/virtual-modules-plugin.js +43 -12
- 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
|
|
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
|
|
19
|
-
|
|
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(
|
|
68
|
+
function collectIdentifiers(root) {
|
|
43
69
|
const ids = /* @__PURE__ */ new Set();
|
|
44
|
-
|
|
45
|
-
if (
|
|
46
|
-
|
|
47
|
-
|
|
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
|
|
53
|
-
function getDefinedNames(
|
|
54
|
-
if (
|
|
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
|
|
80
|
+
for (const d of decl.declarations) collectBindingNames(d.id, names);
|
|
57
81
|
return names;
|
|
58
82
|
}
|
|
59
|
-
if (
|
|
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
|
|
64
|
-
function collectBindingNames(
|
|
65
|
-
if (
|
|
66
|
-
|
|
67
|
-
|
|
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
|
|
80
|
-
const
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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.
|
|
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
|
-
|
|
121
|
-
|
|
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
|
|
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
|
|
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
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
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
|
-
|
|
358
|
-
|
|
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:
|
|
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 =
|
|
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
|
|
645
|
-
const
|
|
646
|
-
const
|
|
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 &&
|
|
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 (
|
|
655
|
-
rewrittenProps.push(prop
|
|
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.
|
|
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
|
|
709
|
+
} else rewrittenProps.push(textOf(prop));
|
|
666
710
|
}
|
|
667
|
-
metadataArgsExpr = needsTransform ? `{${rewrittenProps.join(", ")}}` : decArgsNode
|
|
668
|
-
} else metadataArgsExpr = decArgsNode
|
|
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(
|
|
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
|
|
686
|
-
|
|
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.
|
|
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.
|
|
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)
|
|
710
|
-
|
|
711
|
-
importedNames.
|
|
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
|
|
717
|
-
firstClassPos =
|
|
718
|
-
if (
|
|
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
|
|
722
|
-
if (
|
|
723
|
-
if (
|
|
724
|
-
|
|
725
|
-
if (
|
|
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
|
|
733
|
-
|
|
734
|
-
if (
|
|
735
|
-
|
|
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(
|
|
781
|
+
if (referencesNonHoistable) for (const name of getDefinedNames(decl)) nonHoistableNames.add(name);
|
|
743
782
|
else {
|
|
744
|
-
ms.appendLeft(
|
|
745
|
-
ms.move(
|
|
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.
|
|
752
|
-
let trimEnd = dec.
|
|
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
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
insertPos = stmt.
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
if (insertPos === 0) ms.appendLeft(0,
|
|
782
|
-
else ms.appendRight(insertPos, "\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
|
-
|
|
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
|
-
|
|
801
|
-
|
|
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 };
|