@analogjs/vite-plugin-angular 3.0.0-alpha.3 → 3.0.0-alpha.30

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 (97) hide show
  1. package/migrations/migrate-setup-vitest/migrate-setup-vitest.d.ts +2 -0
  2. package/migrations/migrate-setup-vitest/migrate-setup-vitest.js +49 -0
  3. package/migrations/migrate-setup-vitest/migrate-setup-vitest.js.map +1 -0
  4. package/migrations/migration.json +7 -1
  5. package/migrations/migrations.json +9 -0
  6. package/migrations/update-3-0-0/migrate-setup-vitest.d.ts +2 -0
  7. package/migrations/update-3-0-0/migrate-setup-vitest.js +36 -0
  8. package/migrations/update-3-0-0/migrate-setup-vitest.js.map +1 -0
  9. package/package.json +24 -12
  10. package/src/index.d.ts +3 -2
  11. package/src/index.js +6 -2
  12. package/src/index.js.map +1 -1
  13. package/src/lib/angular-build-optimizer-plugin.d.ts +4 -4
  14. package/src/lib/angular-build-optimizer-plugin.js +48 -62
  15. package/src/lib/angular-build-optimizer-plugin.js.map +1 -1
  16. package/src/lib/angular-jit-plugin.d.ts +3 -3
  17. package/src/lib/angular-jit-plugin.js +37 -37
  18. package/src/lib/angular-jit-plugin.js.map +1 -1
  19. package/src/lib/angular-pending-tasks.plugin.d.ts +7 -7
  20. package/src/lib/angular-pending-tasks.plugin.js +17 -18
  21. package/src/lib/angular-pending-tasks.plugin.js.map +1 -1
  22. package/src/lib/angular-vite-plugin.d.ts +224 -40
  23. package/src/lib/angular-vite-plugin.js +1929 -964
  24. package/src/lib/angular-vite-plugin.js.map +1 -1
  25. package/src/lib/angular-vitest-plugin.d.ts +19 -15
  26. package/src/lib/angular-vitest-plugin.js +99 -114
  27. package/src/lib/angular-vitest-plugin.js.map +1 -1
  28. package/src/lib/compiler-plugin.d.ts +11 -11
  29. package/src/lib/compiler-plugin.js +43 -44
  30. package/src/lib/compiler-plugin.js.map +1 -1
  31. package/src/lib/component-resolvers.d.ts +23 -5
  32. package/src/lib/component-resolvers.js +153 -63
  33. package/src/lib/component-resolvers.js.map +1 -1
  34. package/src/lib/host.d.ts +10 -8
  35. package/src/lib/host.js +109 -101
  36. package/src/lib/host.js.map +1 -1
  37. package/src/lib/live-reload-plugin.d.ts +5 -5
  38. package/src/lib/live-reload-plugin.js +57 -62
  39. package/src/lib/live-reload-plugin.js.map +1 -1
  40. package/src/lib/models.d.ts +9 -9
  41. package/src/lib/nx-folder-plugin.d.ts +5 -5
  42. package/src/lib/nx-folder-plugin.js +18 -16
  43. package/src/lib/nx-folder-plugin.js.map +1 -1
  44. package/src/lib/plugins/file-replacements.plugin.d.ts +4 -4
  45. package/src/lib/plugins/file-replacements.plugin.js +40 -62
  46. package/src/lib/plugins/file-replacements.plugin.js.map +1 -1
  47. package/src/lib/router-plugin.d.ts +1 -1
  48. package/src/lib/router-plugin.js +23 -23
  49. package/src/lib/router-plugin.js.map +1 -1
  50. package/src/lib/style-pipeline.d.ts +15 -0
  51. package/src/lib/style-pipeline.js +31 -0
  52. package/src/lib/style-pipeline.js.map +1 -0
  53. package/src/lib/style-preprocessor.d.ts +35 -0
  54. package/src/lib/style-preprocessor.js +35 -0
  55. package/src/lib/style-preprocessor.js.map +1 -0
  56. package/src/lib/stylesheet-registry.d.ts +73 -0
  57. package/src/lib/stylesheet-registry.js +168 -0
  58. package/src/lib/stylesheet-registry.js.map +1 -0
  59. package/src/lib/tools/package.json +2 -7
  60. package/src/lib/tools/src/builders/vite/vite-build.impl.js +31 -38
  61. package/src/lib/tools/src/builders/vite/vite-build.impl.js.map +1 -1
  62. package/src/lib/tools/src/builders/vite-dev-server/dev-server.impl.js +51 -62
  63. package/src/lib/tools/src/builders/vite-dev-server/dev-server.impl.js.map +1 -1
  64. package/src/lib/tools/src/index.js +0 -2
  65. package/src/lib/utils/compiler-plugin-options.d.ts +11 -11
  66. package/src/lib/utils/debug-harness.d.ts +23 -0
  67. package/src/lib/utils/debug-harness.js +88 -0
  68. package/src/lib/utils/debug-harness.js.map +1 -0
  69. package/src/lib/utils/debug-log-file.d.ts +5 -0
  70. package/src/lib/utils/debug-log-file.js +56 -0
  71. package/src/lib/utils/debug-log-file.js.map +1 -0
  72. package/src/lib/utils/debug.d.ts +26 -0
  73. package/src/lib/utils/debug.js +35 -0
  74. package/src/lib/utils/debug.js.map +1 -0
  75. package/src/lib/utils/devkit.d.ts +6 -6
  76. package/src/lib/utils/devkit.js +34 -38
  77. package/src/lib/utils/devkit.js.map +1 -1
  78. package/src/lib/utils/hmr-candidates.d.ts +28 -28
  79. package/src/lib/utils/rolldown.d.ts +2 -0
  80. package/src/lib/utils/rolldown.js +12 -0
  81. package/src/lib/utils/rolldown.js.map +1 -0
  82. package/src/lib/utils/source-file-cache.d.ts +8 -15
  83. package/src/lib/utils/source-file-cache.js +35 -37
  84. package/src/lib/utils/source-file-cache.js.map +1 -1
  85. package/src/test-setup.d.ts +2 -0
  86. package/setup-vitest.d.ts +0 -4
  87. package/setup-vitest.js +0 -215
  88. package/setup-vitest.js.map +0 -1
  89. package/src/lib/models.js +0 -1
  90. package/src/lib/models.js.map +0 -1
  91. package/src/lib/tools/README.md +0 -3
  92. package/src/lib/tools/src/index.d.ts +0 -0
  93. package/src/lib/tools/src/index.js.map +0 -1
  94. package/src/lib/utils/compiler-plugin-options.js +0 -1
  95. package/src/lib/utils/compiler-plugin-options.js.map +0 -1
  96. package/src/lib/utils/hmr-candidates.js +0 -272
  97. package/src/lib/utils/hmr-candidates.js.map +0 -1
@@ -1,982 +1,1947 @@
1
- import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
2
- import { basename, dirname, isAbsolute, join, relative, resolve, } from 'node:path';
3
- import * as vite from 'vite';
4
- import * as compilerCli from '@angular/compiler-cli';
5
- import { createRequire } from 'node:module';
6
- import * as ngCompiler from '@angular/compiler';
7
- import { globSync } from 'tinyglobby';
8
- import { defaultClientConditions, normalizePath, preprocessCSS, } from 'vite';
9
- import { buildOptimizerPlugin } from './angular-build-optimizer-plugin.js';
10
- import { jitPlugin } from './angular-jit-plugin.js';
11
- import { createCompilerPlugin, createRolldownCompilerPlugin, } from './compiler-plugin.js';
12
- import { StyleUrlsResolver, TemplateUrlsResolver, } from './component-resolvers.js';
13
- import { augmentHostWithCaching, augmentHostWithResources, augmentProgramWithVersioning, mergeTransformers, } from './host.js';
14
- import { angularVitestPlugins } from './angular-vitest-plugin.js';
15
- import { createAngularCompilation, createJitResourceTransformer, SourceFileCache, angularFullVersion, } from './utils/devkit.js';
16
- const require = createRequire(import.meta.url);
17
- import { pendingTasksPlugin } from './angular-pending-tasks.plugin.js';
18
- import { liveReloadPlugin } from './live-reload-plugin.js';
19
- import { nxFolderPlugin } from './nx-folder-plugin.js';
20
- import { replaceFiles, } from './plugins/file-replacements.plugin.js';
21
- import { routerPlugin } from './router-plugin.js';
22
- import { createHash } from 'node:crypto';
23
- export var DiagnosticModes;
24
- (function (DiagnosticModes) {
25
- DiagnosticModes[DiagnosticModes["None"] = 0] = "None";
26
- DiagnosticModes[DiagnosticModes["Option"] = 1] = "Option";
27
- DiagnosticModes[DiagnosticModes["Syntactic"] = 2] = "Syntactic";
28
- DiagnosticModes[DiagnosticModes["Semantic"] = 4] = "Semantic";
29
- DiagnosticModes[DiagnosticModes["All"] = 7] = "All";
30
- })(DiagnosticModes || (DiagnosticModes = {}));
1
+ import { angularFullVersion, cjt, createAngularCompilation, sourceFileCache } from "./utils/devkit.js";
2
+ import { getJsTransformConfigKey, isRolldown } from "./utils/rolldown.js";
3
+ import { buildOptimizerPlugin } from "./angular-build-optimizer-plugin.js";
4
+ import { activateDeferredDebug, applyDebugOption, debugCompilationApi, debugCompiler, debugCompilerV, debugHmr, debugHmrV, debugStyles, debugStylesV, debugTailwind, debugTailwindV } from "./utils/debug.js";
5
+ import { jitPlugin } from "./angular-jit-plugin.js";
6
+ import { createCompilerPlugin, createRolldownCompilerPlugin } from "./compiler-plugin.js";
7
+ import { StyleUrlsResolver, TemplateUrlsResolver, getAngularComponentMetadata } from "./component-resolvers.js";
8
+ import { composeStylePreprocessors, normalizeStylesheetDependencies } from "./style-preprocessor.js";
9
+ import { AnalogStylesheetRegistry, preprocessStylesheet, preprocessStylesheetResult, registerStylesheetContent, rewriteRelativeCssImports } from "./stylesheet-registry.js";
10
+ import { augmentHostWithCaching, augmentHostWithResources, augmentProgramWithVersioning, mergeTransformers } from "./host.js";
11
+ import { angularVitestPlugins } from "./angular-vitest-plugin.js";
12
+ import { pendingTasksPlugin } from "./angular-pending-tasks.plugin.js";
13
+ import { liveReloadPlugin } from "./live-reload-plugin.js";
14
+ import { nxFolderPlugin } from "./nx-folder-plugin.js";
15
+ import { replaceFiles } from "./plugins/file-replacements.plugin.js";
16
+ import { routerPlugin } from "./router-plugin.js";
17
+ import { configureStylePipelineRegistry, stylePipelinePreprocessorFromPlugins } from "./style-pipeline.js";
18
+ import { union } from "es-toolkit";
19
+ import { createHash } from "node:crypto";
20
+ import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
21
+ import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
22
+ import * as compilerCli from "@angular/compiler-cli";
23
+ import { createRequire } from "node:module";
24
+ import * as ngCompiler from "@angular/compiler";
25
+ import { globSync } from "tinyglobby";
26
+ import { defaultClientConditions, normalizePath, preprocessCSS } from "vite";
27
+ //#region packages/vite-plugin-angular/src/lib/angular-vite-plugin.ts
28
+ var require = createRequire(import.meta.url);
29
+ var DiagnosticModes = /* @__PURE__ */ function(DiagnosticModes) {
30
+ DiagnosticModes[DiagnosticModes["None"] = 0] = "None";
31
+ DiagnosticModes[DiagnosticModes["Option"] = 1] = "Option";
32
+ DiagnosticModes[DiagnosticModes["Syntactic"] = 2] = "Syntactic";
33
+ DiagnosticModes[DiagnosticModes["Semantic"] = 4] = "Semantic";
34
+ DiagnosticModes[DiagnosticModes["All"] = 7] = "All";
35
+ return DiagnosticModes;
36
+ }({});
37
+ function normalizeIncludeGlob(workspaceRoot, glob) {
38
+ const normalizedWorkspaceRoot = normalizePath(resolve(workspaceRoot));
39
+ const normalizedGlob = normalizePath(glob);
40
+ if (normalizedGlob === normalizedWorkspaceRoot || normalizedGlob.startsWith(`${normalizedWorkspaceRoot}/`)) return normalizedGlob;
41
+ if (normalizedGlob.startsWith("/")) return `${normalizedWorkspaceRoot}${normalizedGlob}`;
42
+ return normalizePath(resolve(normalizedWorkspaceRoot, normalizedGlob));
43
+ }
44
+ /**
45
+ * TypeScript file extension regex
46
+ * Match .(c or m)ts, .ts extensions with an optional ? for query params
47
+ * Ignore .tsx extensions
48
+ */
49
+ var TS_EXT_REGEX = /\.[cm]?(ts)[^x]?\??/;
50
+ var classNames = /* @__PURE__ */ new Map();
51
+ function evictDeletedFileMetadata(file, { removeActiveGraphMetadata, removeStyleOwnerMetadata, classNamesMap, fileTransformMap }) {
52
+ const normalizedFile = normalizePath(file.split("?")[0]);
53
+ removeActiveGraphMetadata(normalizedFile);
54
+ removeStyleOwnerMetadata(normalizedFile);
55
+ classNamesMap.delete(normalizedFile);
56
+ fileTransformMap.delete(normalizedFile);
57
+ }
58
+ function injectViteIgnoreForHmrMetadata(code) {
59
+ let patched = code.replace(/\bimport\(([a-zA-Z_$][\w$]*\.\u0275\u0275getReplaceMetadataURL)/g, "import(/* @vite-ignore */ $1");
60
+ if (patched === code) patched = patched.replace(/import\((\S+getReplaceMetadataURL)/g, "import(/* @vite-ignore */ $1");
61
+ return patched;
62
+ }
63
+ function isIgnoredHmrFile(file) {
64
+ return file.endsWith(".tsbuildinfo");
65
+ }
66
+ /**
67
+ * Builds a resolved stylePreprocessor function from plugin options.
68
+ *
69
+ * When `tailwindCss` is configured, creates an injector that prepends
70
+ * `@reference "<rootStylesheet>"` into component CSS that uses Tailwind
71
+ * utilities. Uses absolute paths because Angular's externalRuntimeStyles
72
+ * serves component CSS as virtual modules (hash-based IDs) with no
73
+ * meaningful directory — relative paths can't resolve from a hash.
74
+ *
75
+ * If both `tailwindCss` and `stylePreprocessor` are provided, they are
76
+ * chained: Tailwind reference injection runs first, then the user's
77
+ * custom preprocessor.
78
+ */
79
+ function buildStylePreprocessor(options) {
80
+ const userPreprocessor = options?.stylePreprocessor;
81
+ const stylePipelinePreprocessor = stylePipelinePreprocessorFromPlugins(options?.stylePipeline);
82
+ const tw = options?.tailwindCss;
83
+ if (!tw && !userPreprocessor && !stylePipelinePreprocessor) return;
84
+ let tailwindPreprocessor;
85
+ if (tw) {
86
+ const rootStylesheet = tw.rootStylesheet;
87
+ const prefixes = tw.prefixes;
88
+ debugTailwind("configured", {
89
+ rootStylesheet,
90
+ prefixes
91
+ });
92
+ if (!existsSync(rootStylesheet)) console.warn(`[@analogjs/vite-plugin-angular] tailwindCss.rootStylesheet not found at "${rootStylesheet}". @reference directives will point to a non-existent file, which will cause Tailwind CSS errors. Ensure the path is absolute and the file exists.`);
93
+ tailwindPreprocessor = (code, filename) => {
94
+ if (code.includes("@reference") || code.includes("@import \"tailwindcss\"") || code.includes("@import 'tailwindcss'")) {
95
+ debugTailwindV("skip (already has @reference or is root)", { filename });
96
+ return code;
97
+ }
98
+ if (!(prefixes ? prefixes.some((prefix) => code.includes(prefix)) : code.includes("@apply"))) {
99
+ debugTailwindV("skip (no Tailwind usage detected)", { filename });
100
+ return code;
101
+ }
102
+ debugTailwind("injected @reference via preprocessor", { filename });
103
+ return `@reference "${rootStylesheet}";\n${code}`;
104
+ };
105
+ }
106
+ if (tailwindPreprocessor && (stylePipelinePreprocessor || userPreprocessor)) debugTailwind("chained with style pipeline or user stylePreprocessor");
107
+ return composeStylePreprocessors([
108
+ tailwindPreprocessor,
109
+ stylePipelinePreprocessor,
110
+ userPreprocessor
111
+ ]);
112
+ }
113
+ function angular(options) {
114
+ applyDebugOption(options?.debug, options?.workspaceRoot);
115
+ /**
116
+ * Normalize plugin options so defaults
117
+ * are used for values not provided.
118
+ */
119
+ const pluginOptions = {
120
+ tsconfigGetter: createTsConfigGetter(options?.tsconfig),
121
+ workspaceRoot: options?.workspaceRoot ?? process.cwd(),
122
+ inlineStylesExtension: options?.inlineStylesExtension ?? "css",
123
+ advanced: { tsTransformers: {
124
+ before: options?.advanced?.tsTransformers?.before ?? [],
125
+ after: options?.advanced?.tsTransformers?.after ?? [],
126
+ afterDeclarations: options?.advanced?.tsTransformers?.afterDeclarations ?? []
127
+ } },
128
+ supportedBrowsers: options?.supportedBrowsers ?? ["safari 15"],
129
+ jit: options?.jit,
130
+ include: options?.include ?? [],
131
+ additionalContentDirs: options?.additionalContentDirs ?? [],
132
+ hmr: options?.hmr ?? options?.liveReload ?? true,
133
+ disableTypeChecking: options?.disableTypeChecking ?? true,
134
+ fileReplacements: options?.fileReplacements ?? [],
135
+ useAngularCompilationAPI: options?.experimental?.useAngularCompilationAPI ?? false,
136
+ hasTailwindCss: !!options?.tailwindCss,
137
+ tailwindCss: options?.tailwindCss,
138
+ stylePreprocessor: buildStylePreprocessor(options)
139
+ };
140
+ let resolvedConfig;
141
+ let tsConfigResolutionContext = null;
142
+ const ts = require("typescript");
143
+ let builder;
144
+ let nextProgram;
145
+ const tsconfigOptionsCache = /* @__PURE__ */ new Map();
146
+ let cachedHost;
147
+ let cachedHostKey;
148
+ let includeCache = [];
149
+ function invalidateFsCaches() {
150
+ includeCache = [];
151
+ }
152
+ function invalidateTsconfigCaches() {
153
+ tsconfigOptionsCache.clear();
154
+ cachedHost = void 0;
155
+ cachedHostKey = void 0;
156
+ }
157
+ let watchMode = false;
158
+ let testWatchMode = isTestWatchMode();
159
+ const activeGraphComponentMetadata = /* @__PURE__ */ new Map();
160
+ const selectorOwners = /* @__PURE__ */ new Map();
161
+ const classNameOwners = /* @__PURE__ */ new Map();
162
+ const transformedStyleOwnerMetadata = /* @__PURE__ */ new Map();
163
+ const styleSourceOwners = /* @__PURE__ */ new Map();
164
+ function shouldEnableHmr() {
165
+ return !!((isTest ? testWatchMode : watchMode) && pluginOptions.hmr);
166
+ }
167
+ /**
168
+ * Determines whether Angular should externalize component styles.
169
+ *
170
+ * When true, Angular emits style references (hash-based IDs) instead of
171
+ * inlining CSS strings. Vite's resolveId → load → transform pipeline
172
+ * then serves these virtual modules, allowing @tailwindcss/vite to
173
+ * process @reference directives.
174
+ *
175
+ * Required for TWO independent use-cases:
176
+ * 1. HMR — Vite needs external modules for hot replacement
177
+ * 2. Tailwind CSS (hasTailwindCss) — styles must pass through Vite's
178
+ * CSS pipeline so @tailwindcss/vite can resolve @apply directives
179
+ *
180
+ * In production builds (!watchMode), styles are NOT externalized — they
181
+ * are inlined after preprocessCSS runs eagerly in transformStylesheet.
182
+ */
183
+ function shouldExternalizeStyles() {
184
+ if (!(isTest ? testWatchMode : watchMode)) return false;
185
+ return !!(shouldEnableHmr() || pluginOptions.hasTailwindCss);
186
+ }
187
+ /**
188
+ * Validates the Tailwind CSS integration configuration and emits actionable
189
+ * warnings for common misconfigurations that cause silent failures.
190
+ *
191
+ * Called once during `configResolved` when `tailwindCss` is configured.
192
+ */
193
+ function validateTailwindConfig(config, isWatchMode) {
194
+ const PREFIX = "[@analogjs/vite-plugin-angular]";
195
+ const tw = pluginOptions.tailwindCss;
196
+ if (!tw) return;
197
+ if (!isAbsolute(tw.rootStylesheet)) console.warn(`${PREFIX} tailwindCss.rootStylesheet must be an absolute path. Got: "${tw.rootStylesheet}". Use path.resolve(__dirname, '...') in your vite.config to convert it.`);
198
+ const resolvedPlugins = config.plugins;
199
+ const hasTailwindPlugin = resolvedPlugins.some((p) => p.name.startsWith("@tailwindcss/vite") || p.name.startsWith("tailwindcss"));
200
+ if (isWatchMode && !hasTailwindPlugin) throw new Error(`${PREFIX} tailwindCss is configured but no @tailwindcss/vite plugin was found. Component CSS with @apply directives will not be processed.\n\n Fix: npm install @tailwindcss/vite --save-dev\n Then add tailwindcss() to your vite.config plugins array.\n`);
201
+ if (isWatchMode && tw.rootStylesheet) {
202
+ const projectRoot = config.root;
203
+ if (!tw.rootStylesheet.startsWith(projectRoot)) {
204
+ if (!(config.server?.fs?.allow ?? []).some((allowed) => tw.rootStylesheet.startsWith(allowed))) console.warn(`${PREFIX} tailwindCss.rootStylesheet is outside the Vite project root. The dev server may reject it with 403.\n\n Root: ${projectRoot}\n Stylesheet: ${tw.rootStylesheet}\n\n Fix: server.fs.allow: ['${dirname(tw.rootStylesheet)}']\n`);
205
+ }
206
+ }
207
+ if (tw.prefixes !== void 0 && tw.prefixes.length === 0) console.warn(`${PREFIX} tailwindCss.prefixes is an empty array. No component stylesheets will receive @reference injection. Either remove the prefixes option (to use @apply detection) or specify your prefixes: ['tw:']\n`);
208
+ const analogInstances = resolvedPlugins.filter((p) => p.name === "@analogjs/vite-plugin-angular");
209
+ if (analogInstances.length > 1) throw new Error(`${PREFIX} analog() is registered ${analogInstances.length} times. Each instance creates separate style maps, causing component styles to be lost. Remove duplicate registrations.`);
210
+ if (existsSync(tw.rootStylesheet)) try {
211
+ const rootContent = readFileSync(tw.rootStylesheet, "utf-8");
212
+ if (!rootContent.includes("@import \"tailwindcss\"") && !rootContent.includes("@import 'tailwindcss'")) console.warn(`${PREFIX} tailwindCss.rootStylesheet does not contain @import "tailwindcss". The @reference directive will point to a file without Tailwind configuration.\n\n File: ${tw.rootStylesheet}\n`);
213
+ } catch {}
214
+ }
215
+ function isLikelyPageOnlyComponent(id) {
216
+ return id.includes("/pages/") || /\.page\.[cm]?[jt]sx?$/i.test(id) || /\([^/]+\)\.page\.[cm]?[jt]sx?$/i.test(id);
217
+ }
218
+ function removeActiveGraphMetadata(file) {
219
+ const previous = activeGraphComponentMetadata.get(file);
220
+ if (!previous) return;
221
+ for (const record of previous) {
222
+ const location = `${record.file}#${record.className}`;
223
+ if (record.selector) {
224
+ const selectorSet = selectorOwners.get(record.selector);
225
+ selectorSet?.delete(location);
226
+ if (selectorSet?.size === 0) selectorOwners.delete(record.selector);
227
+ }
228
+ const classNameSet = classNameOwners.get(record.className);
229
+ classNameSet?.delete(location);
230
+ if (classNameSet?.size === 0) classNameOwners.delete(record.className);
231
+ }
232
+ activeGraphComponentMetadata.delete(file);
233
+ }
234
+ function registerActiveGraphMetadata(file, records) {
235
+ removeActiveGraphMetadata(file);
236
+ if (records.length === 0) return;
237
+ activeGraphComponentMetadata.set(file, records);
238
+ for (const record of records) {
239
+ const location = `${record.file}#${record.className}`;
240
+ if (record.selector) {
241
+ let selectorSet = selectorOwners.get(record.selector);
242
+ if (!selectorSet) {
243
+ selectorSet = /* @__PURE__ */ new Set();
244
+ selectorOwners.set(record.selector, selectorSet);
245
+ }
246
+ selectorSet.add(location);
247
+ }
248
+ let classNameSet = classNameOwners.get(record.className);
249
+ if (!classNameSet) {
250
+ classNameSet = /* @__PURE__ */ new Set();
251
+ classNameOwners.set(record.className, classNameSet);
252
+ }
253
+ classNameSet.add(location);
254
+ }
255
+ }
256
+ function removeStyleOwnerMetadata(file) {
257
+ const previous = transformedStyleOwnerMetadata.get(file);
258
+ if (!previous) return;
259
+ for (const record of previous) {
260
+ const owners = styleSourceOwners.get(record.sourcePath);
261
+ owners?.delete(record.ownerFile);
262
+ if (owners?.size === 0) styleSourceOwners.delete(record.sourcePath);
263
+ }
264
+ transformedStyleOwnerMetadata.delete(file);
265
+ }
266
+ function registerStyleOwnerMetadata(file, styleUrls) {
267
+ removeStyleOwnerMetadata(file);
268
+ const records = styleUrls.map((urlSet) => {
269
+ const [, absoluteFileUrl] = urlSet.split("|");
270
+ return absoluteFileUrl ? {
271
+ ownerFile: file,
272
+ sourcePath: normalizePath(absoluteFileUrl)
273
+ } : void 0;
274
+ }).filter((record) => !!record);
275
+ if (records.length === 0) return;
276
+ transformedStyleOwnerMetadata.set(file, records);
277
+ for (const record of records) {
278
+ let owners = styleSourceOwners.get(record.sourcePath);
279
+ if (!owners) {
280
+ owners = /* @__PURE__ */ new Set();
281
+ styleSourceOwners.set(record.sourcePath, owners);
282
+ }
283
+ owners.add(record.ownerFile);
284
+ }
285
+ }
286
+ let stylesheetRegistry;
287
+ const sourceFileCache$1 = new sourceFileCache();
288
+ const isTest = process.env.NODE_ENV === "test" || !!process.env["VITEST"];
289
+ const isVitestVscode = !!process.env["VITEST_VSCODE"];
290
+ const isStackBlitz = !!process.versions["webcontainer"];
291
+ const isAstroIntegration = process.env["ANALOG_ASTRO"] === "true";
292
+ const jit = typeof pluginOptions?.jit !== "undefined" ? pluginOptions.jit : isTest;
293
+ let viteServer;
294
+ const styleUrlsResolver = new StyleUrlsResolver();
295
+ const templateUrlsResolver = new TemplateUrlsResolver();
296
+ let outputFile;
297
+ const outputFiles = /* @__PURE__ */ new Map();
298
+ const fileEmitter = (file) => {
299
+ outputFile?.(file);
300
+ return outputFiles.get(normalizePath(file));
301
+ };
302
+ let initialCompilation = false;
303
+ const declarationFiles = [];
304
+ const fileTransformMap = /* @__PURE__ */ new Map();
305
+ let styleTransform;
306
+ let pendingCompilation;
307
+ let compilationLock = Promise.resolve();
308
+ let angularCompilation;
309
+ function angularPlugin() {
310
+ let isProd = false;
311
+ if (angularFullVersion < 19e4 && pluginOptions.hmr) {
312
+ debugHmr("hmr disabled: Angular version does not support HMR APIs", {
313
+ angularVersion: angularFullVersion,
314
+ isTest
315
+ });
316
+ console.warn("[@analogjs/vite-plugin-angular]: HMR was disabled because Angular v19+ is required for externalRuntimeStyles/_enableHmr support. Detected Angular version: %s.", angularFullVersion);
317
+ pluginOptions.hmr = false;
318
+ }
319
+ if (isTest) {
320
+ pluginOptions.hmr = false;
321
+ debugHmr("hmr disabled", {
322
+ angularVersion: angularFullVersion,
323
+ isTest
324
+ });
325
+ }
326
+ if (pluginOptions.useAngularCompilationAPI) if (angularFullVersion < 200100) {
327
+ pluginOptions.useAngularCompilationAPI = false;
328
+ debugCompilationApi("disabled: Angular version %s < 20.1", angularFullVersion);
329
+ console.warn("[@analogjs/vite-plugin-angular]: The Angular Compilation API is only available with Angular v20.1 and later");
330
+ } else debugCompilationApi("enabled (Angular %s)", angularFullVersion);
331
+ return {
332
+ name: "@analogjs/vite-plugin-angular",
333
+ async config(config, { command }) {
334
+ activateDeferredDebug(command);
335
+ watchMode = command === "serve";
336
+ isProd = config.mode === "production" || process.env.NODE_ENV === "production";
337
+ tsConfigResolutionContext = {
338
+ root: config.root || ".",
339
+ isProd,
340
+ isLib: !!config?.build?.lib
341
+ };
342
+ const preliminaryTsConfigPath = resolveTsConfigPath();
343
+ const esbuild = pluginOptions.useAngularCompilationAPI ? void 0 : config.esbuild ?? false;
344
+ const oxc = pluginOptions.useAngularCompilationAPI ? void 0 : config.oxc ?? false;
345
+ if (pluginOptions.useAngularCompilationAPI) debugCompilationApi("esbuild/oxc disabled, Angular handles transforms");
346
+ const defineOptions = {
347
+ ngJitMode: "false",
348
+ ngI18nClosureMode: "false",
349
+ ...watchMode ? {} : { ngDevMode: "false" }
350
+ };
351
+ const useRolldown = isRolldown();
352
+ const jsTransformConfigKey = getJsTransformConfigKey();
353
+ const jsTransformConfigValue = jsTransformConfigKey === "oxc" ? oxc : esbuild;
354
+ const rolldownOptions = { plugins: [createRolldownCompilerPlugin({
355
+ tsconfig: preliminaryTsConfigPath,
356
+ sourcemap: !isProd,
357
+ advancedOptimizations: isProd,
358
+ jit,
359
+ incremental: watchMode
360
+ }, !isAstroIntegration)] };
361
+ const esbuildOptions = {
362
+ plugins: [createCompilerPlugin({
363
+ tsconfig: preliminaryTsConfigPath,
364
+ sourcemap: !isProd,
365
+ advancedOptimizations: isProd,
366
+ jit,
367
+ incremental: watchMode
368
+ }, isTest, !isAstroIntegration)],
369
+ define: defineOptions
370
+ };
371
+ return {
372
+ [jsTransformConfigKey]: jsTransformConfigValue,
373
+ optimizeDeps: {
374
+ include: [
375
+ "rxjs/operators",
376
+ "rxjs",
377
+ "tslib"
378
+ ],
379
+ exclude: ["@angular/platform-server"],
380
+ ...useRolldown ? { rolldownOptions } : { esbuildOptions }
381
+ },
382
+ resolve: { conditions: ["style", ...config.resolve?.conditions || defaultClientConditions] }
383
+ };
384
+ },
385
+ configResolved(config) {
386
+ resolvedConfig = config;
387
+ if (pluginOptions.hasTailwindCss) validateTailwindConfig(config, watchMode);
388
+ if (pluginOptions.useAngularCompilationAPI) {
389
+ stylesheetRegistry = new AnalogStylesheetRegistry();
390
+ configureStylePipelineRegistry(pluginOptions.stylePipeline, stylesheetRegistry, { workspaceRoot: pluginOptions.workspaceRoot });
391
+ debugStyles("stylesheet registry initialized (Angular Compilation API)");
392
+ }
393
+ if (!jit) styleTransform = (code, filename) => preprocessCSS(code, filename, config);
394
+ if (isTest) testWatchMode = !(config.server.watch === null) || config.test?.watch === true || testWatchMode;
395
+ },
396
+ configureServer(server) {
397
+ viteServer = server;
398
+ const invalidateCompilationOnFsChange = createFsWatcherCacheInvalidator(invalidateFsCaches, invalidateTsconfigCaches, () => performCompilation(resolvedConfig));
399
+ server.watcher.on("add", invalidateCompilationOnFsChange);
400
+ server.watcher.on("unlink", (file) => {
401
+ evictDeletedFileMetadata(file, {
402
+ removeActiveGraphMetadata,
403
+ removeStyleOwnerMetadata,
404
+ classNamesMap: classNames,
405
+ fileTransformMap
406
+ });
407
+ return invalidateCompilationOnFsChange();
408
+ });
409
+ server.watcher.on("change", (file) => {
410
+ if (file.includes("tsconfig")) invalidateTsconfigCaches();
411
+ });
412
+ },
413
+ async buildStart() {
414
+ if (!isVitestVscode) {
415
+ await performCompilation(resolvedConfig);
416
+ pendingCompilation = null;
417
+ initialCompilation = true;
418
+ }
419
+ },
420
+ async handleHotUpdate(ctx) {
421
+ if (isIgnoredHmrFile(ctx.file)) {
422
+ debugHmr("ignored file change", { file: ctx.file });
423
+ return [];
424
+ }
425
+ if (TS_EXT_REGEX.test(ctx.file)) {
426
+ const [fileId] = ctx.file.split("?");
427
+ debugHmr("TS file changed", {
428
+ file: ctx.file,
429
+ fileId
430
+ });
431
+ pendingCompilation = performCompilation(resolvedConfig, [fileId]);
432
+ let result;
433
+ if (shouldEnableHmr()) {
434
+ await pendingCompilation;
435
+ pendingCompilation = null;
436
+ result = fileEmitter(fileId);
437
+ debugHmr("TS file emitted", {
438
+ fileId,
439
+ hmrEligible: !!result?.hmrEligible,
440
+ hasClassName: !!classNames.get(fileId)
441
+ });
442
+ debugHmrV("ts hmr evaluation", {
443
+ file: ctx.file,
444
+ fileId,
445
+ hasResult: !!result,
446
+ hmrEligible: !!result?.hmrEligible,
447
+ hasClassName: !!classNames.get(fileId),
448
+ className: classNames.get(fileId),
449
+ updateCode: result?.hmrUpdateCode ? describeStylesheetContent(result.hmrUpdateCode) : void 0,
450
+ errors: result?.errors?.length ?? 0,
451
+ warnings: result?.warnings?.length ?? 0,
452
+ hint: result?.hmrEligible ? "A TS-side component change, including inline template edits, produced an Angular HMR payload." : "No Angular HMR payload was emitted for this TS change; the change may not affect component template state."
453
+ });
454
+ }
455
+ if (shouldEnableHmr() && result?.hmrEligible && classNames.get(fileId)) {
456
+ const relativeFileId = `${normalizePath(relative(process.cwd(), fileId))}@${classNames.get(fileId)}`;
457
+ debugHmr("sending component update", { relativeFileId });
458
+ debugHmrV("ts hmr component update payload", {
459
+ file: ctx.file,
460
+ fileId,
461
+ relativeFileId,
462
+ className: classNames.get(fileId)
463
+ });
464
+ sendHMRComponentUpdate(ctx.server, relativeFileId);
465
+ return ctx.modules.map((mod) => {
466
+ if (mod.id === ctx.file) return markModuleSelfAccepting(mod);
467
+ return mod;
468
+ });
469
+ }
470
+ }
471
+ if (/\.(html|htm|css|less|sass|scss)$/.test(ctx.file)) {
472
+ debugHmr("resource file changed", { file: ctx.file });
473
+ fileTransformMap.delete(ctx.file.split("?")[0]);
474
+ if (/\.(css|less|sass|scss)$/.test(ctx.file)) refreshStylesheetRegistryForFile(ctx.file, stylesheetRegistry, pluginOptions.stylePreprocessor);
475
+ if (/\.(css|less|sass|scss)$/.test(ctx.file) && existsSync(ctx.file)) try {
476
+ const rawResource = readFileSync(ctx.file, "utf-8");
477
+ debugHmrV("resource source snapshot", {
478
+ file: ctx.file,
479
+ mtimeMs: safeStatMtimeMs(ctx.file),
480
+ ...describeStylesheetContent(rawResource)
481
+ });
482
+ } catch (error) {
483
+ debugHmrV("resource source snapshot failed", {
484
+ file: ctx.file,
485
+ error: String(error)
486
+ });
487
+ }
488
+ const fileModules = await getModulesForChangedFile(ctx.server, ctx.file, ctx.modules, stylesheetRegistry);
489
+ debugHmrV("resource modules resolved", {
490
+ file: ctx.file,
491
+ eventModuleCount: ctx.modules.length,
492
+ fileModuleCount: fileModules.length,
493
+ modules: fileModules.map((mod) => ({
494
+ id: mod.id,
495
+ file: mod.file,
496
+ type: mod.type,
497
+ url: mod.url
498
+ }))
499
+ });
500
+ /**
501
+ * Check to see if this was a direct request
502
+ * for an external resource (styles, html).
503
+ */
504
+ const isDirect = fileModules.find((mod) => !!mod.id && mod.id.includes("?direct") && isModuleForChangedResource(mod, ctx.file, stylesheetRegistry));
505
+ const isInline = fileModules.find((mod) => !!mod.id && mod.id.includes("?inline") && isModuleForChangedResource(mod, ctx.file, stylesheetRegistry));
506
+ debugHmrV("resource direct/inline detection", {
507
+ file: ctx.file,
508
+ hasDirect: !!isDirect,
509
+ directId: isDirect?.id,
510
+ hasInline: !!isInline,
511
+ inlineId: isInline?.id
512
+ });
513
+ if (isDirect || isInline) {
514
+ if (shouldExternalizeStyles() && isDirect?.id && isDirect.file) {
515
+ const isComponentStyle = isDirect.type === "css" && isComponentStyleSheet(isDirect.id);
516
+ debugHmrV("resource direct branch", {
517
+ file: ctx.file,
518
+ directId: isDirect.id,
519
+ directType: isDirect.type,
520
+ shouldExternalize: shouldExternalizeStyles(),
521
+ isComponentStyle
522
+ });
523
+ if (isComponentStyle) {
524
+ const { encapsulation } = getComponentStyleSheetMeta(isDirect.id);
525
+ const wrapperModules = await findComponentStylesheetWrapperModules(ctx.server, ctx.file, isDirect, fileModules, stylesheetRegistry);
526
+ const stylesheetDiagnosis = diagnoseComponentStylesheetPipeline(ctx.file, isDirect, stylesheetRegistry, wrapperModules, pluginOptions.stylePreprocessor);
527
+ debugStylesV("HMR: component stylesheet changed", {
528
+ file: isDirect.file,
529
+ encapsulation
530
+ });
531
+ debugHmrV("component stylesheet wrapper modules", {
532
+ file: ctx.file,
533
+ wrapperCount: wrapperModules.length,
534
+ wrapperIds: wrapperModules.map((mod) => mod.id),
535
+ availableModuleIds: fileModules.map((mod) => mod.id)
536
+ });
537
+ debugHmrV("component stylesheet pipeline diagnosis", stylesheetDiagnosis);
538
+ ctx.server.moduleGraph.invalidateModule(isDirect);
539
+ debugHmrV("component stylesheet direct module invalidated", {
540
+ file: ctx.file,
541
+ directModuleId: isDirect.id,
542
+ directModuleUrl: isDirect.url,
543
+ reason: "Ensure Vite drops stale direct CSS transform results before wrapper or fallback handling continues."
544
+ });
545
+ const trackedWrapperRequestIds = stylesheetDiagnosis.trackedRequestIds.filter((id) => id.includes("?ngcomp="));
546
+ if (encapsulation !== "shadow" && (wrapperModules.length > 0 || trackedWrapperRequestIds.length > 0)) {
547
+ wrapperModules.forEach((mod) => ctx.server.moduleGraph.invalidateModule(mod));
548
+ debugHmrV("sending css-update for component stylesheet", {
549
+ file: ctx.file,
550
+ path: isDirect.url,
551
+ acceptedPath: isDirect.file,
552
+ wrapperCount: wrapperModules.length,
553
+ trackedWrapperRequestIds,
554
+ hint: wrapperModules.length > 0 ? "Live wrapper modules were found and invalidated before sending the CSS update." : "No live wrapper ModuleNode was available, but the wrapper request id is already tracked, so Analog is trusting the browser-visible wrapper identity and patching the direct stylesheet instead of forcing a reload."
555
+ });
556
+ sendCssUpdate(ctx.server, {
557
+ path: isDirect.url,
558
+ acceptedPath: isDirect.file
559
+ });
560
+ logComponentStylesheetHmrOutcome({
561
+ file: ctx.file,
562
+ encapsulation,
563
+ diagnosis: stylesheetDiagnosis,
564
+ outcome: "css-update",
565
+ directModuleId: isDirect.id,
566
+ wrapperIds: wrapperModules.map((mod) => mod.id)
567
+ });
568
+ return union(fileModules.filter((mod) => {
569
+ return mod.file !== ctx.file || mod.id !== isDirect.id;
570
+ }).map((mod) => {
571
+ if (mod.file === ctx.file) return markModuleSelfAccepting(mod);
572
+ return mod;
573
+ }), wrapperModules.map((mod) => markModuleSelfAccepting(mod)));
574
+ }
575
+ debugHmrV("component stylesheet hmr fallback: full reload", {
576
+ file: ctx.file,
577
+ encapsulation,
578
+ reason: trackedWrapperRequestIds.length === 0 ? "missing-wrapper-module" : encapsulation === "shadow" ? "shadow-encapsulation" : "tracked-wrapper-still-not-patchable",
579
+ directId: isDirect.id,
580
+ trackedRequestIds: stylesheetRegistry?.getRequestIdsForSource(ctx.file) ?? []
581
+ });
582
+ const ownerModules = findStyleOwnerModules(ctx.server, ctx.file, styleSourceOwners);
583
+ debugHmrV("component stylesheet owner fallback lookup", {
584
+ file: ctx.file,
585
+ ownerCount: ownerModules.length,
586
+ ownerIds: ownerModules.map((mod) => mod.id),
587
+ ownerFiles: [...styleSourceOwners.get(normalizePath(ctx.file)) ?? []]
588
+ });
589
+ if (ownerModules.length > 0) {
590
+ pendingCompilation = performCompilation(resolvedConfig, [...ownerModules.map((mod) => mod.id).filter(Boolean)]);
591
+ await pendingCompilation;
592
+ pendingCompilation = null;
593
+ const updates = ownerModules.map((mod) => mod.id).filter((id) => !!id && !!classNames.get(id));
594
+ const derivedUpdates = ownerModules.map((mod) => mod.id).filter((id) => !!id).flatMap((ownerId) => resolveComponentClassNamesForStyleOwner(ownerId, ctx.file).map((className) => ({
595
+ ownerId,
596
+ className,
597
+ via: "raw-component-metadata"
598
+ })));
599
+ debugHmrV("component stylesheet owner fallback compilation", {
600
+ file: ctx.file,
601
+ ownerIds: ownerModules.map((mod) => mod.id),
602
+ updateIds: updates,
603
+ classNames: updates.map((id) => ({
604
+ id,
605
+ className: classNames.get(id)
606
+ })),
607
+ derivedUpdates
608
+ });
609
+ if (derivedUpdates.length > 0) debugHmrV("component stylesheet owner fallback derived updates", {
610
+ file: ctx.file,
611
+ updates: derivedUpdates,
612
+ hint: "Angular did not repopulate classNames during CSS-only owner recompilation, so Analog derived component identities from raw component metadata."
613
+ });
614
+ }
615
+ logComponentStylesheetHmrOutcome({
616
+ file: ctx.file,
617
+ encapsulation,
618
+ diagnosis: stylesheetDiagnosis,
619
+ outcome: "full-reload",
620
+ directModuleId: isDirect.id,
621
+ wrapperIds: wrapperModules.map((mod) => mod.id),
622
+ ownerIds: ownerModules.map((mod) => mod.id)
623
+ });
624
+ sendFullReload(ctx.server, {
625
+ file: ctx.file,
626
+ encapsulation,
627
+ reason: wrapperModules.length === 0 ? "missing-wrapper-module-and-no-owner-updates" : "shadow-encapsulation",
628
+ directId: isDirect.id,
629
+ trackedRequestIds: stylesheetRegistry?.getRequestIdsForSource(ctx.file) ?? []
630
+ });
631
+ return [];
632
+ }
633
+ }
634
+ return fileModules;
635
+ }
636
+ if (shouldEnableHmr() && /\.(html|htm)$/.test(ctx.file) && fileModules.length === 0) {
637
+ const ownerModules = findTemplateOwnerModules(ctx.server, ctx.file);
638
+ debugHmrV("template owner lookup", {
639
+ file: ctx.file,
640
+ ownerCount: ownerModules.length,
641
+ ownerIds: ownerModules.map((mod) => mod.id),
642
+ hint: ownerModules.length > 0 ? "The external template has candidate TS owner modules that can be recompiled for HMR." : "No TS owner modules were visible for this external template change; HMR will fall through to the generic importer path."
643
+ });
644
+ if (ownerModules.length > 0) {
645
+ const ownerIds = ownerModules.map((mod) => mod.id).filter(Boolean);
646
+ ownerModules.forEach((mod) => ctx.server.moduleGraph.invalidateModule(mod));
647
+ pendingCompilation = performCompilation(resolvedConfig, ownerIds);
648
+ await pendingCompilation;
649
+ pendingCompilation = null;
650
+ const updates = ownerIds.filter((id) => classNames.get(id));
651
+ debugHmrV("template owner recompilation result", {
652
+ file: ctx.file,
653
+ ownerIds,
654
+ updates,
655
+ updateClassNames: updates.map((id) => ({
656
+ id,
657
+ className: classNames.get(id)
658
+ })),
659
+ hint: updates.length > 0 ? "External template recompilation produced Angular component update targets." : "External template recompilation completed, but no Angular component update targets were surfaced."
660
+ });
661
+ if (updates.length > 0) {
662
+ debugHmr("template owner module invalidation", {
663
+ file: ctx.file,
664
+ ownerIds,
665
+ updateCount: updates.length
666
+ });
667
+ updates.forEach((updateId) => {
668
+ const relativeFileId = `${normalizePath(relative(process.cwd(), updateId))}@${classNames.get(updateId)}`;
669
+ sendHMRComponentUpdate(ctx.server, relativeFileId);
670
+ });
671
+ return ownerModules.map((mod) => markModuleSelfAccepting(mod));
672
+ }
673
+ }
674
+ }
675
+ const mods = [];
676
+ const updates = [];
677
+ fileModules.forEach((mod) => {
678
+ mod.importers.forEach((imp) => {
679
+ ctx.server.moduleGraph.invalidateModule(imp);
680
+ if (shouldExternalizeStyles() && classNames.get(imp.id)) updates.push(imp.id);
681
+ else mods.push(imp);
682
+ });
683
+ });
684
+ debugHmrV("resource importer analysis", {
685
+ file: ctx.file,
686
+ fileModuleCount: fileModules.length,
687
+ importerCount: fileModules.reduce((count, mod) => count + mod.importers.size, 0),
688
+ updates,
689
+ mods: mods.map((mod) => mod.id)
690
+ });
691
+ pendingCompilation = performCompilation(resolvedConfig, [...mods.map((mod) => mod.id).filter(Boolean), ...updates]);
692
+ if (updates.length > 0) {
693
+ await pendingCompilation;
694
+ pendingCompilation = null;
695
+ debugHmr("resource importer component updates", {
696
+ file: ctx.file,
697
+ updateCount: updates.length
698
+ });
699
+ updates.forEach((updateId) => {
700
+ const impRelativeFileId = `${normalizePath(relative(process.cwd(), updateId))}@${classNames.get(updateId)}`;
701
+ sendHMRComponentUpdate(ctx.server, impRelativeFileId);
702
+ });
703
+ return fileModules.map((mod) => {
704
+ if (mod.id === ctx.file) return markModuleSelfAccepting(mod);
705
+ return mod;
706
+ });
707
+ }
708
+ return mods;
709
+ }
710
+ debugHmr("full reload — unrecognized file type", { file: ctx.file });
711
+ classNames.clear();
712
+ return ctx.modules;
713
+ },
714
+ resolveId(id, importer) {
715
+ if (jit && id.startsWith("angular:jit:")) {
716
+ const path = id.split(";")[1];
717
+ return `${normalizePath(resolve(dirname(importer), path))}?${id.includes(":style") ? "inline" : "raw"}`;
718
+ }
719
+ if (isComponentStyleSheet(id)) {
720
+ const filename = getFilenameFromPath(id);
721
+ if (stylesheetRegistry?.hasServed(filename)) {
722
+ debugStylesV("resolveId: kept preprocessed ID", { filename });
723
+ return id;
724
+ }
725
+ const componentStyles = stylesheetRegistry?.resolveExternalSource(filename);
726
+ if (componentStyles) {
727
+ debugStylesV("resolveId: mapped external stylesheet", {
728
+ filename,
729
+ resolvedPath: componentStyles
730
+ });
731
+ return componentStyles + new URL(id, "http://localhost").search;
732
+ }
733
+ debugStyles("resolveId: component stylesheet NOT FOUND in either map", {
734
+ filename,
735
+ inlineMapSize: stylesheetRegistry?.servedCount ?? 0,
736
+ externalMapSize: stylesheetRegistry?.externalCount ?? 0
737
+ });
738
+ }
739
+ },
740
+ async load(id) {
741
+ if (isComponentStyleSheet(id)) {
742
+ const filename = getFilenameFromPath(id);
743
+ const componentStyles = stylesheetRegistry?.getServedContent(filename);
744
+ if (componentStyles) {
745
+ stylesheetRegistry?.registerActiveRequest(id);
746
+ debugHmrV("stylesheet active request registered", {
747
+ requestId: id,
748
+ filename,
749
+ sourcePath: stylesheetRegistry?.resolveExternalSource(filename) ?? stylesheetRegistry?.resolveExternalSource(filename.replace(/^\//, "")),
750
+ trackedRequestIds: stylesheetRegistry?.getRequestIdsForSource(stylesheetRegistry?.resolveExternalSource(filename) ?? stylesheetRegistry?.resolveExternalSource(filename.replace(/^\//, "")) ?? "") ?? []
751
+ });
752
+ debugStylesV("load: served inline component stylesheet", {
753
+ filename,
754
+ length: componentStyles.length,
755
+ requestId: id,
756
+ ...describeStylesheetContent(componentStyles)
757
+ });
758
+ return componentStyles;
759
+ }
760
+ }
761
+ },
762
+ transform: {
763
+ filter: { id: {
764
+ include: [TS_EXT_REGEX],
765
+ exclude: [
766
+ /node_modules/,
767
+ "type=script",
768
+ "@ng/component"
769
+ ]
770
+ } },
771
+ async handler(code, id) {
772
+ /**
773
+ * Check for options.transformFilter
774
+ */
775
+ if (options?.transformFilter && !(options?.transformFilter(code, id) ?? true)) return;
776
+ if (pluginOptions.useAngularCompilationAPI) {
777
+ if (!/(Component|Directive|Pipe|Injectable|NgModule)\(/.test(code)) {
778
+ debugCompilationApi("transform skip (non-Angular file)", { id });
779
+ return;
780
+ }
781
+ }
782
+ /**
783
+ * Skip transforming content files
784
+ */
785
+ if (id.includes("?") && id.includes("analog-content-")) return;
786
+ /**
787
+ * Encapsulate component stylesheets that use emulated encapsulation.
788
+ * Must run whenever styles are externalized (not just HMR), because
789
+ * Angular's externalRuntimeStyles skips its own encapsulation when
790
+ * styles are external — the build tool is expected to handle it.
791
+ */
792
+ if (shouldExternalizeStyles() && isComponentStyleSheet(id)) {
793
+ const { encapsulation, componentId } = getComponentStyleSheetMeta(id);
794
+ if (encapsulation === "emulated" && componentId) {
795
+ debugStylesV("applying emulated view encapsulation", {
796
+ stylesheet: id.split("?")[0],
797
+ componentId
798
+ });
799
+ return {
800
+ code: ngCompiler.encapsulateStyle(code, componentId),
801
+ map: null
802
+ };
803
+ }
804
+ }
805
+ if (id.includes(".ts?")) id = id.replace(/\?(.*)/, "");
806
+ fileTransformMap.set(id, code);
807
+ /**
808
+ * Re-analyze on each transform
809
+ * for test(Vitest)
810
+ */
811
+ if (isTest) {
812
+ if (isVitestVscode && !initialCompilation) {
813
+ pendingCompilation = performCompilation(resolvedConfig);
814
+ initialCompilation = true;
815
+ }
816
+ const tsMod = viteServer?.moduleGraph.getModuleById(id);
817
+ if (tsMod) {
818
+ const invalidated = tsMod.lastInvalidationTimestamp;
819
+ if (testWatchMode && invalidated) pendingCompilation = performCompilation(resolvedConfig, [id]);
820
+ }
821
+ }
822
+ const hasComponent = code.includes("@Component");
823
+ debugCompilerV("transform", {
824
+ id,
825
+ codeLength: code.length,
826
+ hasComponent
827
+ });
828
+ const templateUrls = hasComponent ? templateUrlsResolver.resolve(code, id) : [];
829
+ const styleUrls = hasComponent ? styleUrlsResolver.resolve(code, id) : [];
830
+ if (hasComponent && watchMode) for (const urlSet of [...templateUrls, ...styleUrls]) {
831
+ const [, absoluteFileUrl] = urlSet.split("|");
832
+ this.addWatchFile(absoluteFileUrl);
833
+ }
834
+ if (pendingCompilation) {
835
+ await pendingCompilation;
836
+ pendingCompilation = null;
837
+ }
838
+ const typescriptResult = fileEmitter(id);
839
+ if (typescriptResult?.warnings && typescriptResult?.warnings.length > 0) this.warn(`${typescriptResult.warnings.join("\n")}`);
840
+ if (typescriptResult?.errors && typescriptResult?.errors.length > 0) this.error(`${typescriptResult.errors.join("\n")}`);
841
+ let data = typescriptResult?.content ?? "";
842
+ if (jit && data.includes("angular:jit:")) {
843
+ data = data.replace(/angular:jit:style:inline;/g, "virtual:angular:jit:style:inline;");
844
+ templateUrls.forEach((templateUrlSet) => {
845
+ const [templateFile, resolvedTemplateUrl] = templateUrlSet.split("|");
846
+ data = data.replace(`angular:jit:template:file;${templateFile}`, `${resolvedTemplateUrl}?raw`);
847
+ });
848
+ styleUrls.forEach((styleUrlSet) => {
849
+ const [styleFile, resolvedStyleUrl] = styleUrlSet.split("|");
850
+ data = data.replace(`angular:jit:style:file;${styleFile}`, `${resolvedStyleUrl}?inline`);
851
+ });
852
+ }
853
+ if (data.includes("HmrLoad")) {
854
+ const hasMetaUrl = data.includes("getReplaceMetadataURL");
855
+ debugHmrV("vite-ignore injection", {
856
+ id,
857
+ dataLength: data.length,
858
+ hasMetaUrl
859
+ });
860
+ if (hasMetaUrl) {
861
+ const patched = injectViteIgnoreForHmrMetadata(data);
862
+ if (patched !== data && !patched.includes("@vite-ignore")) debugHmrV("vite-ignore regex fallback", { id });
863
+ data = patched;
864
+ }
865
+ }
866
+ return {
867
+ code: data,
868
+ map: null
869
+ };
870
+ }
871
+ },
872
+ closeBundle() {
873
+ declarationFiles.forEach(({ declarationFileDir, declarationPath, data }) => {
874
+ mkdirSync(declarationFileDir, { recursive: true });
875
+ writeFileSync(declarationPath, data, "utf-8");
876
+ });
877
+ angularCompilation?.close?.();
878
+ angularCompilation = void 0;
879
+ }
880
+ };
881
+ }
882
+ return [
883
+ replaceFiles(pluginOptions.fileReplacements, pluginOptions.workspaceRoot),
884
+ {
885
+ name: "@analogjs/vite-plugin-angular:template-class-binding-guard",
886
+ enforce: "pre",
887
+ transform(code, id) {
888
+ if (id.includes("node_modules")) return;
889
+ const cleanId = id.split("?")[0];
890
+ if (/\.(html|htm)$/i.test(cleanId)) {
891
+ const staticClassIssue = findStaticClassAndBoundClassConflicts(code)[0];
892
+ if (staticClassIssue) throwTemplateClassBindingConflict(cleanId, staticClassIssue);
893
+ const mixedClassIssue = findBoundClassAndNgClassConflicts(code)[0];
894
+ if (mixedClassIssue) this.warn([
895
+ "[Analog Angular] Conflicting class composition.",
896
+ `File: ${cleanId}:${mixedClassIssue.line}:${mixedClassIssue.column}`,
897
+ "This element mixes `[class]` and `[ngClass]`.",
898
+ "Prefer a single class-binding strategy so class merging stays predictable.",
899
+ "Use one `[ngClass]` expression or explicit `[class.foo]` bindings.",
900
+ `Snippet: ${mixedClassIssue.snippet}`
901
+ ].join("\n"));
902
+ return;
903
+ }
904
+ if (TS_EXT_REGEX.test(cleanId)) {
905
+ const rawStyleUrls = styleUrlsResolver.resolve(code, cleanId);
906
+ registerStyleOwnerMetadata(cleanId, rawStyleUrls);
907
+ debugHmrV("component stylesheet owner metadata registered", {
908
+ file: cleanId,
909
+ styleUrlCount: rawStyleUrls.length,
910
+ styleUrls: rawStyleUrls,
911
+ ownerSources: [...transformedStyleOwnerMetadata.get(cleanId)?.map((record) => record.sourcePath) ?? []]
912
+ });
913
+ const components = getAngularComponentMetadata(code);
914
+ const inlineTemplateIssue = components.flatMap((component) => component.inlineTemplates.flatMap((template) => findStaticClassAndBoundClassConflicts(template)))[0];
915
+ if (inlineTemplateIssue) throwTemplateClassBindingConflict(cleanId, inlineTemplateIssue);
916
+ const mixedInlineClassIssue = components.flatMap((component) => component.inlineTemplates.flatMap((template) => findBoundClassAndNgClassConflicts(template)))[0];
917
+ if (mixedInlineClassIssue) this.warn([
918
+ "[Analog Angular] Conflicting class composition.",
919
+ `File: ${cleanId}:${mixedInlineClassIssue.line}:${mixedInlineClassIssue.column}`,
920
+ "This element mixes `[class]` and `[ngClass]`.",
921
+ "Prefer a single class-binding strategy so class merging stays predictable.",
922
+ "Use one `[ngClass]` expression or explicit `[class.foo]` bindings.",
923
+ `Snippet: ${mixedInlineClassIssue.snippet}`
924
+ ].join("\n"));
925
+ registerActiveGraphMetadata(cleanId, components.map((component) => ({
926
+ file: cleanId,
927
+ className: component.className,
928
+ selector: component.selector
929
+ })));
930
+ for (const component of components) {
931
+ if (!component.selector && !isLikelyPageOnlyComponent(cleanId)) throw new Error([
932
+ "[Analog Angular] Selectorless component detected.",
933
+ `File: ${cleanId}`,
934
+ `Component: ${component.className}`,
935
+ "This component has no `selector`, so Angular will render it as `ng-component`.",
936
+ "That increases the chance of component ID collisions and makes diagnostics harder to interpret.",
937
+ "Add an explicit selector for reusable components.",
938
+ "Selectorless components are only supported for page and route-only files."
939
+ ].join("\n"));
940
+ if (component.selector) {
941
+ const selectorEntries = selectorOwners.get(component.selector);
942
+ if (selectorEntries && selectorEntries.size > 1) throw new Error([
943
+ "[Analog Angular] Duplicate component selector detected.",
944
+ `Selector: ${component.selector}`,
945
+ "Multiple components in the active application graph use the same selector.",
946
+ "Selectors must be unique within the active graph to avoid ambiguous rendering and confusing diagnostics.",
947
+ `Locations:\n${formatActiveGraphLocations(selectorEntries)}`
948
+ ].join("\n"));
949
+ }
950
+ const classNameEntries = classNameOwners.get(component.className);
951
+ if (classNameEntries && classNameEntries.size > 1) this.warn([
952
+ "[Analog Angular] Duplicate component class name detected.",
953
+ `Class name: ${component.className}`,
954
+ "Two or more Angular components in the active graph share the same exported class name.",
955
+ "Rename one of them to keep HMR, stack traces, and compiler diagnostics unambiguous.",
956
+ `Locations:\n${formatActiveGraphLocations(classNameEntries)}`
957
+ ].join("\n"));
958
+ }
959
+ }
960
+ }
961
+ },
962
+ pluginOptions.hasTailwindCss && {
963
+ name: "@analogjs/vite-plugin-angular:tailwind-reference",
964
+ enforce: "pre",
965
+ transform(code, id) {
966
+ const tw = pluginOptions.tailwindCss;
967
+ if (!tw || !id.includes(".css")) return;
968
+ if (id.split("?")[0] === tw.rootStylesheet) return;
969
+ if (code.includes("@reference") || code.includes("@import \"tailwindcss\"") || code.includes("@import 'tailwindcss'")) return;
970
+ const rootBasename = basename(tw.rootStylesheet);
971
+ if (code.includes(rootBasename)) return;
972
+ const prefixes = tw.prefixes;
973
+ if (prefixes ? prefixes.some((p) => code.includes(p)) : code.includes("@apply")) {
974
+ debugTailwind("injected @reference via pre-transform", { id: id.split("/").slice(-2).join("/") });
975
+ return `@reference "${tw.rootStylesheet}";\n${code}`;
976
+ }
977
+ }
978
+ },
979
+ angularPlugin(),
980
+ pluginOptions.hmr && liveReloadPlugin({
981
+ classNames,
982
+ fileEmitter
983
+ }),
984
+ ...isTest && !isStackBlitz ? angularVitestPlugins() : [],
985
+ jit && jitPlugin({ inlineStylesExtension: pluginOptions.inlineStylesExtension }),
986
+ buildOptimizerPlugin({
987
+ supportedBrowsers: pluginOptions.supportedBrowsers,
988
+ jit
989
+ }),
990
+ routerPlugin(),
991
+ angularFullVersion < 190004 && pendingTasksPlugin(),
992
+ nxFolderPlugin()
993
+ ].filter(Boolean);
994
+ function findIncludes() {
995
+ return globSync(pluginOptions.include.map((glob) => normalizeIncludeGlob(pluginOptions.workspaceRoot, glob)), {
996
+ dot: true,
997
+ absolute: true
998
+ });
999
+ }
1000
+ function createTsConfigGetter(tsconfigOrGetter) {
1001
+ if (typeof tsconfigOrGetter === "function") return tsconfigOrGetter;
1002
+ return () => tsconfigOrGetter || "";
1003
+ }
1004
+ function getTsConfigPath(root, tsconfig, isProd, isTest, isLib) {
1005
+ if (tsconfig && isAbsolute(tsconfig)) {
1006
+ if (!existsSync(tsconfig)) console.error(`[@analogjs/vite-plugin-angular]: Unable to resolve tsconfig at ${tsconfig}. This causes compilation issues. Check the path or set the "tsconfig" property with an absolute path.`);
1007
+ return tsconfig;
1008
+ }
1009
+ let tsconfigFilePath = "./tsconfig.app.json";
1010
+ if (isLib) tsconfigFilePath = isProd ? "./tsconfig.lib.prod.json" : "./tsconfig.lib.json";
1011
+ if (isTest) tsconfigFilePath = "./tsconfig.spec.json";
1012
+ if (tsconfig) tsconfigFilePath = tsconfig;
1013
+ const resolvedPath = resolve(root, tsconfigFilePath);
1014
+ if (!existsSync(resolvedPath)) console.error(`[@analogjs/vite-plugin-angular]: Unable to resolve tsconfig at ${resolvedPath}. This causes compilation issues. Check the path or set the "tsconfig" property with an absolute path.`);
1015
+ return resolvedPath;
1016
+ }
1017
+ function resolveTsConfigPath() {
1018
+ const tsconfigValue = pluginOptions.tsconfigGetter();
1019
+ return getTsConfigPath(tsConfigResolutionContext.root, tsconfigValue, tsConfigResolutionContext.isProd, isTest, tsConfigResolutionContext.isLib);
1020
+ }
1021
+ /**
1022
+ * Perform compilation using Angular's private Compilation API.
1023
+ *
1024
+ * Key differences from the standard `performCompilation` path:
1025
+ * 1. The compilation instance is reused across rebuilds (nullish-coalescing
1026
+ * assignment below) so Angular retains prior state and can diff it to
1027
+ * produce `templateUpdates` for HMR.
1028
+ * 2. `ids` (modified files) are forwarded to both the source-file cache and
1029
+ * `angularCompilation.update()` so that incremental re-analysis is
1030
+ * scoped to what actually changed.
1031
+ * 3. `fileReplacements` are converted and passed into Angular's host via
1032
+ * `toAngularCompilationFileReplacements`.
1033
+ * 4. `templateUpdates` from the compilation result are mapped back to
1034
+ * file-level HMR metadata (`hmrUpdateCode`, `hmrEligible`, `classNames`).
1035
+ */
1036
+ async function performAngularCompilation(config, ids) {
1037
+ angularCompilation ??= await createAngularCompilation(!!pluginOptions.jit, false);
1038
+ const modifiedFiles = ids?.length ? new Set(ids.map((file) => normalizePath(file))) : void 0;
1039
+ if (modifiedFiles?.size) sourceFileCache$1.invalidate(modifiedFiles);
1040
+ if (modifiedFiles?.size && angularCompilation.update) {
1041
+ debugCompilationApi("incremental update", { files: [...modifiedFiles] });
1042
+ await angularCompilation.update(modifiedFiles);
1043
+ }
1044
+ const resolvedTsConfigPath = resolveTsConfigPath();
1045
+ const compilationResult = await angularCompilation.initialize(resolvedTsConfigPath, {
1046
+ fileReplacements: toAngularCompilationFileReplacements(pluginOptions.fileReplacements, pluginOptions.workspaceRoot),
1047
+ modifiedFiles,
1048
+ async transformStylesheet(data, containingFile, resourceFile, order, className) {
1049
+ const filename = resourceFile ?? containingFile.replace(".ts", `.${pluginOptions.inlineStylesExtension}`);
1050
+ const preprocessed = preprocessStylesheetResult(data, filename, pluginOptions.stylePreprocessor, {
1051
+ filename,
1052
+ containingFile,
1053
+ resourceFile,
1054
+ className,
1055
+ order,
1056
+ inline: !resourceFile
1057
+ });
1058
+ if (shouldEnableHmr() && className && containingFile) classNames.set(normalizePath(containingFile), className);
1059
+ if (shouldExternalizeStyles()) {
1060
+ const stylesheetId = registerStylesheetContent(stylesheetRegistry, {
1061
+ code: preprocessed.code,
1062
+ dependencies: normalizeStylesheetDependencies(preprocessed.dependencies),
1063
+ diagnostics: preprocessed.diagnostics,
1064
+ tags: preprocessed.tags,
1065
+ containingFile,
1066
+ className,
1067
+ order,
1068
+ inlineStylesExtension: pluginOptions.inlineStylesExtension,
1069
+ resourceFile: resourceFile ?? void 0
1070
+ });
1071
+ debugStyles("stylesheet deferred to Vite pipeline", {
1072
+ stylesheetId,
1073
+ resourceFile: resourceFile ?? "(inline)"
1074
+ });
1075
+ debugStylesV("stylesheet deferred content snapshot", {
1076
+ stylesheetId,
1077
+ filename,
1078
+ resourceFile: resourceFile ?? "(inline)",
1079
+ dependencies: preprocessed.dependencies,
1080
+ diagnostics: preprocessed.diagnostics,
1081
+ tags: preprocessed.tags,
1082
+ ...describeStylesheetContent(preprocessed.code)
1083
+ });
1084
+ return stylesheetId;
1085
+ }
1086
+ debugStyles("stylesheet processed inline via preprocessCSS", {
1087
+ filename,
1088
+ resourceFile: resourceFile ?? "(inline)",
1089
+ dataLength: preprocessed.code.length
1090
+ });
1091
+ let stylesheetResult;
1092
+ try {
1093
+ stylesheetResult = await preprocessCSS(preprocessed.code, `${filename}?direct`, resolvedConfig);
1094
+ } catch (e) {
1095
+ debugStyles("preprocessCSS error", {
1096
+ filename,
1097
+ resourceFile: resourceFile ?? "(inline)",
1098
+ error: String(e)
1099
+ });
1100
+ }
1101
+ return stylesheetResult?.code || "";
1102
+ },
1103
+ processWebWorker(workerFile, containingFile) {
1104
+ return "";
1105
+ }
1106
+ }, (tsCompilerOptions) => {
1107
+ if (shouldExternalizeStyles()) tsCompilerOptions["externalRuntimeStyles"] = true;
1108
+ if (shouldEnableHmr()) {
1109
+ tsCompilerOptions["_enableHmr"] = true;
1110
+ tsCompilerOptions["supportTestBed"] = true;
1111
+ }
1112
+ debugCompiler("tsCompilerOptions (compilation API)", {
1113
+ hmr: pluginOptions.hmr,
1114
+ hasTailwindCss: pluginOptions.hasTailwindCss,
1115
+ watchMode,
1116
+ shouldExternalize: shouldExternalizeStyles(),
1117
+ externalRuntimeStyles: !!tsCompilerOptions["externalRuntimeStyles"],
1118
+ hmrEnabled: !!tsCompilerOptions["_enableHmr"]
1119
+ });
1120
+ if (tsCompilerOptions.compilationMode === "partial") {
1121
+ tsCompilerOptions["supportTestBed"] = true;
1122
+ tsCompilerOptions["supportJitMode"] = true;
1123
+ }
1124
+ if (angularFullVersion >= 2e5) tsCompilerOptions["_enableSelectorless"] = true;
1125
+ if (!isTest && config.build?.lib) {
1126
+ tsCompilerOptions["declaration"] = true;
1127
+ tsCompilerOptions["declarationMap"] = watchMode;
1128
+ tsCompilerOptions["inlineSources"] = true;
1129
+ }
1130
+ if (isTest) tsCompilerOptions["supportTestBed"] = true;
1131
+ return tsCompilerOptions;
1132
+ });
1133
+ debugStyles("external stylesheets from compilation API", {
1134
+ count: compilationResult.externalStylesheets?.size ?? 0,
1135
+ hasPreprocessor: !!pluginOptions.stylePreprocessor,
1136
+ hasInlineMap: !!stylesheetRegistry
1137
+ });
1138
+ const preprocessStats = {
1139
+ total: 0,
1140
+ injected: 0,
1141
+ skipped: 0,
1142
+ errors: 0
1143
+ };
1144
+ compilationResult.externalStylesheets?.forEach((value, key) => {
1145
+ preprocessStats.total++;
1146
+ const angularHash = `${value}.css`;
1147
+ stylesheetRegistry?.registerExternalRequest(angularHash, key);
1148
+ if (stylesheetRegistry && pluginOptions.stylePreprocessor && existsSync(key)) try {
1149
+ const rawCss = readFileSync(key, "utf-8");
1150
+ const preprocessed = preprocessStylesheetResult(rawCss, key, pluginOptions.stylePreprocessor);
1151
+ debugStylesV("external stylesheet raw snapshot", {
1152
+ angularHash,
1153
+ resolvedPath: key,
1154
+ mtimeMs: safeStatMtimeMs(key),
1155
+ ...describeStylesheetContent(rawCss)
1156
+ });
1157
+ const servedCss = rewriteRelativeCssImports(preprocessed.code, key);
1158
+ stylesheetRegistry.registerServedStylesheet({
1159
+ publicId: angularHash,
1160
+ sourcePath: key,
1161
+ originalCode: rawCss,
1162
+ normalizedCode: servedCss,
1163
+ dependencies: normalizeStylesheetDependencies(preprocessed.dependencies),
1164
+ diagnostics: preprocessed.diagnostics,
1165
+ tags: preprocessed.tags
1166
+ }, [
1167
+ key,
1168
+ normalizePath(key),
1169
+ basename(key),
1170
+ key.replace(/^\//, "")
1171
+ ]);
1172
+ if (servedCss && servedCss !== rawCss) {
1173
+ preprocessStats.injected++;
1174
+ debugStylesV("preprocessed external stylesheet for Tailwind @reference", {
1175
+ angularHash,
1176
+ resolvedPath: key,
1177
+ mtimeMs: safeStatMtimeMs(key),
1178
+ raw: describeStylesheetContent(rawCss),
1179
+ served: describeStylesheetContent(servedCss),
1180
+ dependencies: preprocessed.dependencies,
1181
+ diagnostics: preprocessed.diagnostics,
1182
+ tags: preprocessed.tags
1183
+ });
1184
+ } else {
1185
+ preprocessStats.skipped++;
1186
+ debugStylesV("external stylesheet unchanged after preprocessing", {
1187
+ angularHash,
1188
+ resolvedPath: key,
1189
+ mtimeMs: safeStatMtimeMs(key),
1190
+ raw: describeStylesheetContent(rawCss),
1191
+ served: describeStylesheetContent(servedCss),
1192
+ dependencies: preprocessed.dependencies,
1193
+ diagnostics: preprocessed.diagnostics,
1194
+ tags: preprocessed.tags,
1195
+ hint: "Registry mapping is still registered so Angular component stylesheet HMR can track and refresh this file even when preprocessing makes no textual changes."
1196
+ });
1197
+ }
1198
+ } catch (e) {
1199
+ preprocessStats.errors++;
1200
+ console.warn(`[@analogjs/vite-plugin-angular] failed to preprocess external stylesheet: ${key}: ${e}`);
1201
+ }
1202
+ else {
1203
+ preprocessStats.skipped++;
1204
+ debugStylesV("external stylesheet preprocessing skipped", {
1205
+ filename: angularHash,
1206
+ resolvedPath: key,
1207
+ reason: !stylesheetRegistry ? "no stylesheetRegistry" : !pluginOptions.stylePreprocessor ? "no stylePreprocessor" : "file not found on disk"
1208
+ });
1209
+ }
1210
+ debugStylesV("external stylesheet registered for resolveId mapping", {
1211
+ filename: angularHash,
1212
+ resolvedPath: key
1213
+ });
1214
+ });
1215
+ debugStyles("external stylesheet preprocessing complete", preprocessStats);
1216
+ const diagnostics = await angularCompilation.diagnoseFiles(pluginOptions.disableTypeChecking ? DiagnosticModes.All & ~DiagnosticModes.Semantic : DiagnosticModes.All);
1217
+ const errors = diagnostics.errors?.length ? diagnostics.errors : [];
1218
+ const warnings = diagnostics.warnings?.length ? diagnostics.warnings : [];
1219
+ const templateUpdates = mapTemplateUpdatesToFiles(compilationResult.templateUpdates);
1220
+ if (templateUpdates.size > 0) debugHmr("compilation API template updates", {
1221
+ count: templateUpdates.size,
1222
+ files: [...templateUpdates.keys()]
1223
+ });
1224
+ for (const file of await angularCompilation.emitAffectedFiles()) {
1225
+ const normalizedFilename = normalizePath(file.filename);
1226
+ const templateUpdate = templateUpdates.get(normalizedFilename);
1227
+ if (templateUpdate) classNames.set(normalizedFilename, templateUpdate.className);
1228
+ outputFiles.set(normalizedFilename, {
1229
+ content: file.contents,
1230
+ dependencies: [],
1231
+ errors: errors.map((error) => error.text || ""),
1232
+ warnings: warnings.map((warning) => warning.text || ""),
1233
+ hmrUpdateCode: templateUpdate?.code,
1234
+ hmrEligible: !!templateUpdate?.code
1235
+ });
1236
+ }
1237
+ }
1238
+ async function performCompilation(config, ids) {
1239
+ let resolve;
1240
+ const previousLock = compilationLock;
1241
+ compilationLock = new Promise((r) => {
1242
+ resolve = r;
1243
+ });
1244
+ try {
1245
+ await previousLock;
1246
+ await _doPerformCompilation(config, ids);
1247
+ } finally {
1248
+ resolve();
1249
+ }
1250
+ }
1251
+ /**
1252
+ * This method share mutable state and performs the actual compilation work.
1253
+ * It should not be called concurrently. Use `performCompilation` which wraps this method in a lock to ensure only one compilation runs at a time.
1254
+ */
1255
+ async function _doPerformCompilation(config, ids) {
1256
+ if (pluginOptions.useAngularCompilationAPI) {
1257
+ debugCompilationApi("using compilation API path", { modifiedFiles: ids?.length ?? 0 });
1258
+ await performAngularCompilation(config, ids);
1259
+ return;
1260
+ }
1261
+ const isProd = config.mode === "production";
1262
+ const modifiedFiles = new Set(ids ?? []);
1263
+ sourceFileCache$1.invalidate(modifiedFiles);
1264
+ if (ids?.length) for (const id of ids || []) fileTransformMap.delete(id);
1265
+ if (pluginOptions.include.length > 0 && includeCache.length === 0) includeCache = findIncludes();
1266
+ const resolvedTsConfigPath = resolveTsConfigPath();
1267
+ const tsconfigKey = [
1268
+ resolvedTsConfigPath,
1269
+ isProd ? "prod" : "dev",
1270
+ isTest ? "test" : "app",
1271
+ config.build?.lib ? "lib" : "nolib",
1272
+ pluginOptions.hmr ? "hmr" : "nohmr",
1273
+ pluginOptions.hasTailwindCss ? "tw" : "notw"
1274
+ ].join("|");
1275
+ let cached = tsconfigOptionsCache.get(tsconfigKey);
1276
+ if (!cached) {
1277
+ const read = compilerCli.readConfiguration(resolvedTsConfigPath, {
1278
+ suppressOutputPathCheck: true,
1279
+ outDir: void 0,
1280
+ sourceMap: false,
1281
+ inlineSourceMap: !isProd,
1282
+ inlineSources: !isProd,
1283
+ declaration: false,
1284
+ declarationMap: false,
1285
+ allowEmptyCodegenFiles: false,
1286
+ annotationsAs: "decorators",
1287
+ enableResourceInlining: false,
1288
+ noEmitOnError: false,
1289
+ mapRoot: void 0,
1290
+ sourceRoot: void 0,
1291
+ supportTestBed: false,
1292
+ supportJitMode: false
1293
+ });
1294
+ cached = {
1295
+ options: read.options,
1296
+ rootNames: read.rootNames
1297
+ };
1298
+ tsconfigOptionsCache.set(tsconfigKey, cached);
1299
+ }
1300
+ const tsCompilerOptions = { ...cached.options };
1301
+ let rootNames = [...cached.rootNames];
1302
+ if (shouldExternalizeStyles()) tsCompilerOptions["externalRuntimeStyles"] = true;
1303
+ if (shouldEnableHmr()) {
1304
+ tsCompilerOptions["_enableHmr"] = true;
1305
+ tsCompilerOptions["supportTestBed"] = true;
1306
+ }
1307
+ debugCompiler("tsCompilerOptions (NgtscProgram path)", {
1308
+ hmr: pluginOptions.hmr,
1309
+ shouldExternalize: shouldExternalizeStyles(),
1310
+ externalRuntimeStyles: !!tsCompilerOptions["externalRuntimeStyles"],
1311
+ hmrEnabled: !!tsCompilerOptions["_enableHmr"]
1312
+ });
1313
+ if (tsCompilerOptions["compilationMode"] === "partial") {
1314
+ tsCompilerOptions["supportTestBed"] = true;
1315
+ tsCompilerOptions["supportJitMode"] = true;
1316
+ }
1317
+ if (angularFullVersion >= 2e5) tsCompilerOptions["_enableSelectorless"] = true;
1318
+ if (!isTest && config.build?.lib) {
1319
+ tsCompilerOptions["declaration"] = true;
1320
+ tsCompilerOptions["declarationMap"] = watchMode;
1321
+ tsCompilerOptions["inlineSources"] = true;
1322
+ }
1323
+ if (isTest) tsCompilerOptions["supportTestBed"] = true;
1324
+ const replacements = pluginOptions.fileReplacements.map((rp) => join(pluginOptions.workspaceRoot, rp.ssr || rp.with));
1325
+ rootNames = union(rootNames, includeCache, replacements);
1326
+ const hostKey = JSON.stringify(tsCompilerOptions);
1327
+ let host;
1328
+ if (cachedHost && cachedHostKey === hostKey) host = cachedHost;
1329
+ else {
1330
+ host = ts.createIncrementalCompilerHost(tsCompilerOptions, {
1331
+ ...ts.sys,
1332
+ readFile(path, encoding) {
1333
+ if (fileTransformMap.has(path)) return fileTransformMap.get(path);
1334
+ const file = ts.sys.readFile.call(null, path, encoding);
1335
+ if (file) fileTransformMap.set(path, file);
1336
+ return file;
1337
+ }
1338
+ });
1339
+ cachedHost = host;
1340
+ cachedHostKey = hostKey;
1341
+ if (watchMode) augmentHostWithCaching(host, sourceFileCache$1);
1342
+ }
1343
+ if (!jit) {
1344
+ const externalizeStyles = !!tsCompilerOptions["externalRuntimeStyles"];
1345
+ stylesheetRegistry = externalizeStyles ? new AnalogStylesheetRegistry() : void 0;
1346
+ if (stylesheetRegistry) configureStylePipelineRegistry(pluginOptions.stylePipeline, stylesheetRegistry, { workspaceRoot: pluginOptions.workspaceRoot });
1347
+ debugStyles("stylesheet registry initialized (NgtscProgram path)", { externalizeStyles });
1348
+ augmentHostWithResources(host, styleTransform, {
1349
+ inlineStylesExtension: pluginOptions.inlineStylesExtension,
1350
+ isProd,
1351
+ stylesheetRegistry,
1352
+ sourceFileCache: sourceFileCache$1,
1353
+ stylePreprocessor: pluginOptions.stylePreprocessor
1354
+ });
1355
+ }
1356
+ /**
1357
+ * Creates a new NgtscProgram to analyze/re-analyze
1358
+ * the source files and create a file emitter.
1359
+ * This is shared between an initial build and a hot update.
1360
+ */
1361
+ let typeScriptProgram;
1362
+ let angularCompiler;
1363
+ const oldBuilder = builder ?? ts.readBuilderProgram(tsCompilerOptions, host);
1364
+ if (!jit) {
1365
+ const angularProgram = new compilerCli.NgtscProgram(rootNames, tsCompilerOptions, host, nextProgram);
1366
+ angularCompiler = angularProgram.compiler;
1367
+ typeScriptProgram = angularProgram.compiler.getCurrentProgram();
1368
+ augmentProgramWithVersioning(typeScriptProgram);
1369
+ builder = ts.createEmitAndSemanticDiagnosticsBuilderProgram(typeScriptProgram, host, oldBuilder);
1370
+ nextProgram = angularProgram;
1371
+ } else {
1372
+ builder = ts.createEmitAndSemanticDiagnosticsBuilderProgram(rootNames, tsCompilerOptions, host, oldBuilder);
1373
+ typeScriptProgram = builder.getProgram();
1374
+ }
1375
+ if (!watchMode) builder = ts.createAbstractBuilder(typeScriptProgram, host, oldBuilder);
1376
+ if (angularCompiler) await angularCompiler.analyzeAsync();
1377
+ const transformers = mergeTransformers({ before: jit ? [compilerCli.constructorParametersDownlevelTransform(builder.getProgram()), cjt(() => builder.getProgram().getTypeChecker())] : [] }, jit ? {} : angularCompiler.prepareEmit().transformers);
1378
+ const fileMetadata = getFileMetadata(builder, angularCompiler, pluginOptions.hmr, pluginOptions.disableTypeChecking);
1379
+ const writeFileCallback = (_filename, content, _a, _b, sourceFiles) => {
1380
+ if (!sourceFiles?.length) return;
1381
+ const filename = normalizePath(sourceFiles[0].fileName);
1382
+ if (filename.includes("ngtypecheck.ts") || filename.includes(".d.")) return;
1383
+ const metadata = watchMode ? fileMetadata(filename) : {};
1384
+ outputFiles.set(filename, {
1385
+ content,
1386
+ dependencies: [],
1387
+ errors: metadata.errors,
1388
+ warnings: metadata.warnings,
1389
+ hmrUpdateCode: metadata.hmrUpdateCode,
1390
+ hmrEligible: metadata.hmrEligible
1391
+ });
1392
+ };
1393
+ const writeOutputFile = (id) => {
1394
+ const sourceFile = builder.getSourceFile(id);
1395
+ if (!sourceFile) return;
1396
+ let content = "";
1397
+ builder.emit(sourceFile, (filename, data) => {
1398
+ if (/\.[cm]?js$/.test(filename)) content = data;
1399
+ if (!watchMode && !isTest && /\.d\.ts/.test(filename) && !filename.includes(".ngtypecheck.")) {
1400
+ const declarationPath = resolve(config.root, config.build.outDir, relative(config.root, filename)).replace("/src/", "/");
1401
+ const declarationFileDir = declarationPath.replace(basename(filename), "").replace("/src/", "/");
1402
+ declarationFiles.push({
1403
+ declarationFileDir,
1404
+ declarationPath,
1405
+ data
1406
+ });
1407
+ }
1408
+ }, void 0, void 0, transformers);
1409
+ writeFileCallback(id, content, false, void 0, [sourceFile]);
1410
+ if (angularCompiler) angularCompiler.incrementalCompilation.recordSuccessfulEmit(sourceFile);
1411
+ };
1412
+ if (watchMode) {
1413
+ if (ids && ids.length > 0) ids.forEach((id) => writeOutputFile(id));
1414
+ else if (isTest) while (builder.emitNextAffectedFile(writeFileCallback, void 0, void 0, transformers));
1415
+ }
1416
+ if (!isTest)
1417
+ /**
1418
+ * Perf: Output files on demand so the dev server
1419
+ * isn't blocked when emitting files.
1420
+ */
1421
+ outputFile = writeOutputFile;
1422
+ }
1423
+ }
1424
+ function createFsWatcherCacheInvalidator(invalidateFsCaches, invalidateTsconfigCaches, performCompilation) {
1425
+ return async () => {
1426
+ invalidateFsCaches();
1427
+ invalidateTsconfigCaches();
1428
+ await performCompilation();
1429
+ };
1430
+ }
1431
+ /**
1432
+ * Convert Analog/Angular CLI-style file replacements into the flat record
1433
+ * expected by `AngularHostOptions.fileReplacements`.
1434
+ *
1435
+ * Only browser replacements (`{ replace, with }`) are converted. SSR-only
1436
+ * replacements (`{ replace, ssr }`) are left for the Vite runtime plugin to
1437
+ * handle — they should not be baked into the Angular compilation host because
1438
+ * that would apply them to both browser and server builds.
1439
+ *
1440
+ * Relative paths are resolved against `workspaceRoot` so that the host
1441
+ * receives the same absolute paths it would get from the Angular CLI.
1442
+ */
1443
+ function toAngularCompilationFileReplacements(replacements, workspaceRoot) {
1444
+ const mappedReplacements = replacements.flatMap((replacement) => {
1445
+ if (!("with" in replacement)) return [];
1446
+ return [[isAbsolute(replacement.replace) ? replacement.replace : resolve(workspaceRoot, replacement.replace), isAbsolute(replacement.with) ? replacement.with : resolve(workspaceRoot, replacement.with)]];
1447
+ });
1448
+ return mappedReplacements.length ? Object.fromEntries(mappedReplacements) : void 0;
1449
+ }
1450
+ /**
1451
+ * Map Angular's `templateUpdates` (keyed by `encodedFilePath@ClassName`)
1452
+ * back to absolute file paths with their associated HMR code and component
1453
+ * class name.
1454
+ *
1455
+ * Angular's private Compilation API emits template update keys in the form
1456
+ * `encodeURIComponent(relativePath + '@' + className)`. We decode and resolve
1457
+ * them so the caller can look up updates by the same normalized absolute path
1458
+ * used elsewhere in the plugin (`outputFiles`, `classNames`, etc.).
1459
+ */
1460
+ function mapTemplateUpdatesToFiles(templateUpdates) {
1461
+ const updatesByFile = /* @__PURE__ */ new Map();
1462
+ templateUpdates?.forEach((code, encodedUpdateId) => {
1463
+ const [file, className = ""] = decodeURIComponent(encodedUpdateId).split("@");
1464
+ const resolvedFile = normalizePath(resolve(process.cwd(), file));
1465
+ updatesByFile.set(resolvedFile, {
1466
+ className,
1467
+ code
1468
+ });
1469
+ });
1470
+ return updatesByFile;
1471
+ }
1472
+ /**
1473
+ * Returns every live Vite module that can legitimately represent a changed
1474
+ * Angular resource file.
1475
+ *
1476
+ * For normal files, `getModulesByFile()` is enough. For Angular component
1477
+ * stylesheets, it is not: the browser often holds virtual hashed requests
1478
+ * (`/abc123.css?direct&ngcomp=...` and `/abc123.css?ngcomp=...`) that are no
1479
+ * longer discoverable from the original source path alone. We therefore merge:
1480
+ * - watcher event modules
1481
+ * - module-graph modules by source file
1482
+ * - registry-tracked live request ids resolved back through the module graph
1483
+ */
1484
+ async function getModulesForChangedFile(server, file, eventModules = [], stylesheetRegistry) {
1485
+ const normalizedFile = normalizePath(file.split("?")[0]);
1486
+ const modules = /* @__PURE__ */ new Map();
1487
+ for (const mod of eventModules) if (mod.id) modules.set(mod.id, mod);
1488
+ server.moduleGraph.getModulesByFile(normalizedFile)?.forEach((mod) => {
1489
+ if (mod.id) modules.set(mod.id, mod);
1490
+ });
1491
+ const stylesheetRequestIds = stylesheetRegistry?.getRequestIdsForSource(normalizedFile) ?? [];
1492
+ const requestIdHits = [];
1493
+ for (const requestId of stylesheetRequestIds) {
1494
+ const candidates = [requestId, requestId.startsWith("/") ? requestId : `/${requestId}`];
1495
+ for (const candidate of candidates) {
1496
+ const mod = await server.moduleGraph.getModuleByUrl(candidate) ?? server.moduleGraph.getModuleById(candidate);
1497
+ requestIdHits.push({
1498
+ requestId,
1499
+ candidate,
1500
+ via: mod?.url === candidate ? "url" : "id",
1501
+ moduleId: mod?.id
1502
+ });
1503
+ if (mod?.id) modules.set(mod.id, mod);
1504
+ }
1505
+ }
1506
+ debugHmrV("getModulesForChangedFile registry lookup", {
1507
+ file: normalizedFile,
1508
+ stylesheetRequestIds,
1509
+ requestIdHits,
1510
+ resolvedModuleIds: [...modules.keys()]
1511
+ });
1512
+ return [...modules.values()];
1513
+ }
1514
+ function isModuleForChangedResource(mod, changedFile, stylesheetRegistry) {
1515
+ const normalizedChangedFile = normalizePath(changedFile.split("?")[0]);
1516
+ if (normalizePath((mod.file ?? "").split("?")[0]) === normalizedChangedFile) return true;
1517
+ if (!mod.id) return false;
1518
+ const requestPath = getFilenameFromPath(mod.id);
1519
+ return normalizePath((stylesheetRegistry?.resolveExternalSource(requestPath) ?? stylesheetRegistry?.resolveExternalSource(requestPath.replace(/^\//, "")) ?? "").split("?")[0]) === normalizedChangedFile;
1520
+ }
1521
+ function describeStylesheetContent(code) {
1522
+ return {
1523
+ length: code.length,
1524
+ digest: createHash("sha256").update(code).digest("hex").slice(0, 12),
1525
+ preview: code.replace(/\s+/g, " ").trim().slice(0, 160)
1526
+ };
1527
+ }
1528
+ function safeStatMtimeMs(file) {
1529
+ try {
1530
+ return statSync(file).mtimeMs;
1531
+ } catch {
1532
+ return;
1533
+ }
1534
+ }
31
1535
  /**
32
- * TypeScript file extension regex
33
- * Match .(c or m)ts, .ts extensions with an optional ? for query params
34
- * Ignore .tsx extensions
35
- */
36
- const TS_EXT_REGEX = /\.[cm]?(ts)[^x]?\??/;
37
- const classNames = new Map();
38
- export function angular(options) {
39
- /**
40
- * Normalize plugin options so defaults
41
- * are used for values not provided.
42
- */
43
- const pluginOptions = {
44
- tsconfigGetter: createTsConfigGetter(options?.tsconfig),
45
- workspaceRoot: options?.workspaceRoot ?? process.cwd(),
46
- inlineStylesExtension: options?.inlineStylesExtension ?? 'css',
47
- advanced: {
48
- tsTransformers: {
49
- before: options?.advanced?.tsTransformers?.before ?? [],
50
- after: options?.advanced?.tsTransformers?.after ?? [],
51
- afterDeclarations: options?.advanced?.tsTransformers?.afterDeclarations ?? [],
52
- },
53
- },
54
- supportedBrowsers: options?.supportedBrowsers ?? ['safari 15'],
55
- jit: options?.jit,
56
- include: options?.include ?? [],
57
- additionalContentDirs: options?.additionalContentDirs ?? [],
58
- liveReload: options?.liveReload ?? false,
59
- disableTypeChecking: options?.disableTypeChecking ?? true,
60
- fileReplacements: options?.fileReplacements ?? [],
61
- useAngularCompilationAPI: options?.experimental?.useAngularCompilationAPI ?? false,
62
- };
63
- let resolvedConfig;
64
- // Store config context needed for getTsConfigPath resolution
65
- let tsConfigResolutionContext = null;
66
- const ts = require('typescript');
67
- let builder;
68
- let nextProgram;
69
- // Caches (always rebuild Angular program per user request)
70
- const tsconfigOptionsCache = new Map();
71
- let cachedHost;
72
- let cachedHostKey;
73
- let includeCache = [];
74
- function invalidateFsCaches() {
75
- includeCache = [];
76
- }
77
- function invalidateTsconfigCaches() {
78
- tsconfigOptionsCache.clear();
79
- cachedHost = undefined;
80
- cachedHostKey = undefined;
81
- }
82
- let watchMode = false;
83
- let testWatchMode = isTestWatchMode();
84
- let inlineComponentStyles;
85
- let externalComponentStyles;
86
- const sourceFileCache = new SourceFileCache();
87
- const isTest = process.env['NODE_ENV'] === 'test' || !!process.env['VITEST'];
88
- const isVitestVscode = !!process.env['VITEST_VSCODE'];
89
- const isStackBlitz = !!process.versions['webcontainer'];
90
- const isAstroIntegration = process.env['ANALOG_ASTRO'] === 'true';
91
- const jit = typeof pluginOptions?.jit !== 'undefined' ? pluginOptions.jit : isTest;
92
- let viteServer;
93
- const styleUrlsResolver = new StyleUrlsResolver();
94
- const templateUrlsResolver = new TemplateUrlsResolver();
95
- let outputFile;
96
- const outputFiles = new Map();
97
- const fileEmitter = (file) => {
98
- outputFile?.(file);
99
- return outputFiles.get(normalizePath(file));
100
- };
101
- let initialCompilation = false;
102
- const declarationFiles = [];
103
- const fileTransformMap = new Map();
104
- let styleTransform;
105
- let pendingCompilation;
106
- let compilationLock = Promise.resolve();
107
- function angularPlugin() {
108
- let isProd = false;
109
- if (angularFullVersion < 190000 || isTest) {
110
- pluginOptions.liveReload = false;
111
- }
112
- if (pluginOptions.useAngularCompilationAPI) {
113
- if (angularFullVersion < 200100) {
114
- pluginOptions.useAngularCompilationAPI = false;
115
- console.warn('[@analogjs/vite-plugin-angular]: The Angular Compilation API is only available with Angular v20.1 and later');
116
- }
117
- if (pluginOptions.liveReload) {
118
- pluginOptions.liveReload = false;
119
- console.warn('[@analogjs-vite-plugin-angular]: Live reload is currently not compatible with the Angular Compilation API option');
120
- }
121
- if (pluginOptions.fileReplacements.length) {
122
- pluginOptions.fileReplacements = [];
123
- console.warn('[@analogjs-vite-plugin-angular]: File replacements are currently not compatible with the Angular Compilation API option');
124
- }
125
- }
126
- return {
127
- name: '@analogjs/vite-plugin-angular',
128
- async config(config, { command }) {
129
- watchMode = command === 'serve';
130
- isProd =
131
- config.mode === 'production' ||
132
- process.env['NODE_ENV'] === 'production';
133
- // Store the config context for later resolution in configResolved
134
- tsConfigResolutionContext = {
135
- root: config.root || '.',
136
- isProd,
137
- isLib: !!config?.build?.lib,
138
- };
139
- // Do a preliminary resolution for esbuild plugin (before configResolved)
140
- const preliminaryTsConfigPath = resolveTsConfigPath();
141
- const esbuild = pluginOptions.useAngularCompilationAPI
142
- ? undefined
143
- : (config.esbuild ?? false);
144
- const oxc = pluginOptions.useAngularCompilationAPI
145
- ? undefined
146
- : (config.oxc ?? false);
147
- const defineOptions = {
148
- ngJitMode: 'false',
149
- ngI18nClosureMode: 'false',
150
- ...(watchMode ? {} : { ngDevMode: 'false' }),
151
- };
152
- const rolldownOptions = {
153
- plugins: [
154
- createRolldownCompilerPlugin({
155
- tsconfig: preliminaryTsConfigPath,
156
- sourcemap: !isProd,
157
- advancedOptimizations: isProd,
158
- jit,
159
- incremental: watchMode,
160
- }),
161
- ],
162
- };
163
- const esbuildOptions = {
164
- plugins: [
165
- createCompilerPlugin({
166
- tsconfig: preliminaryTsConfigPath,
167
- sourcemap: !isProd,
168
- advancedOptimizations: isProd,
169
- jit,
170
- incremental: watchMode,
171
- }, isTest, !isAstroIntegration),
172
- ],
173
- define: defineOptions,
174
- };
175
- return {
176
- ...(vite.rolldownVersion ? { oxc } : { esbuild }),
177
- optimizeDeps: {
178
- include: ['rxjs/operators', 'rxjs'],
179
- exclude: ['@angular/platform-server'],
180
- ...(vite.rolldownVersion
181
- ? { rolldownOptions }
182
- : { esbuildOptions }),
183
- },
184
- resolve: {
185
- conditions: [
186
- 'style',
187
- ...(config.resolve?.conditions || defaultClientConditions),
188
- ],
189
- },
190
- };
191
- },
192
- configResolved(config) {
193
- resolvedConfig = config;
194
- if (pluginOptions.useAngularCompilationAPI) {
195
- externalComponentStyles = new Map();
196
- inlineComponentStyles = new Map();
197
- }
198
- if (!jit) {
199
- styleTransform = (code, filename) => preprocessCSS(code, filename, config);
200
- }
201
- if (isTest) {
202
- // set test watch mode
203
- // - vite override from vitest-angular
204
- // - @nx/vite executor set server.watch explicitly to undefined (watch)/null (watch=false)
205
- // - vite config for test.watch variable
206
- // - vitest watch mode detected from the command line
207
- testWatchMode =
208
- !(config.server.watch === null) ||
209
- config.test?.watch === true ||
210
- testWatchMode;
211
- }
212
- },
213
- configureServer(server) {
214
- viteServer = server;
215
- server.watcher.on('add', async () => {
216
- invalidateFsCaches();
217
- await performCompilation(resolvedConfig);
218
- });
219
- server.watcher.on('unlink', async () => {
220
- invalidateFsCaches();
221
- await performCompilation(resolvedConfig);
222
- });
223
- server.watcher.on('change', (file) => {
224
- if (file.includes('tsconfig')) {
225
- invalidateTsconfigCaches();
226
- }
227
- });
228
- },
229
- async buildStart() {
230
- // Defer the first compilation in test mode
231
- if (!isVitestVscode) {
232
- await performCompilation(resolvedConfig);
233
- pendingCompilation = null;
234
- initialCompilation = true;
235
- }
236
- },
237
- async handleHotUpdate(ctx) {
238
- if (TS_EXT_REGEX.test(ctx.file)) {
239
- const [fileId] = ctx.file.split('?');
240
- pendingCompilation = performCompilation(resolvedConfig, [fileId]);
241
- let result;
242
- if (pluginOptions.liveReload) {
243
- await pendingCompilation;
244
- pendingCompilation = null;
245
- result = fileEmitter(fileId);
246
- }
247
- if (pluginOptions.liveReload &&
248
- result?.hmrEligible &&
249
- classNames.get(fileId)) {
250
- const relativeFileId = `${relative(process.cwd(), fileId)}@${classNames.get(fileId)}`;
251
- sendHMRComponentUpdate(ctx.server, relativeFileId);
252
- return ctx.modules.map((mod) => {
253
- if (mod.id === ctx.file) {
254
- return markModuleSelfAccepting(mod);
255
- }
256
- return mod;
257
- });
258
- }
259
- }
260
- if (/\.(html|htm|css|less|sass|scss)$/.test(ctx.file)) {
261
- fileTransformMap.delete(ctx.file.split('?')[0]);
262
- /**
263
- * Check to see if this was a direct request
264
- * for an external resource (styles, html).
265
- */
266
- const isDirect = ctx.modules.find((mod) => ctx.file === mod.file && mod.id?.includes('?direct'));
267
- const isInline = ctx.modules.find((mod) => ctx.file === mod.file && mod.id?.includes('?inline'));
268
- if (isDirect || isInline) {
269
- if (pluginOptions.liveReload && isDirect?.id && isDirect.file) {
270
- const isComponentStyle = isDirect.type === 'css' && isComponentStyleSheet(isDirect.id);
271
- if (isComponentStyle) {
272
- const { encapsulation } = getComponentStyleSheetMeta(isDirect.id);
273
- // Track if the component uses ShadowDOM encapsulation
274
- // Shadow DOM components currently require a full reload.
275
- // Vite's CSS hot replacement does not support shadow root searching.
276
- if (encapsulation !== 'shadow') {
277
- ctx.server.ws.send({
278
- type: 'update',
279
- updates: [
280
- {
281
- type: 'css-update',
282
- timestamp: Date.now(),
283
- path: isDirect.url,
284
- acceptedPath: isDirect.file,
285
- },
286
- ],
287
- });
288
- return ctx.modules
289
- .filter((mod) => {
290
- // Component stylesheets will have 2 modules (*.component.scss and *.component.scss?direct&ngcomp=xyz&e=x)
291
- // We remove the module with the query params to prevent vite double logging the stylesheet name "hmr update *.component.scss, *.component.scss?direct&ngcomp=xyz&e=x"
292
- return mod.file !== ctx.file || mod.id !== isDirect.id;
293
- })
294
- .map((mod) => {
295
- if (mod.file === ctx.file) {
296
- return markModuleSelfAccepting(mod);
297
- }
298
- return mod;
299
- });
300
- }
301
- }
302
- }
303
- return ctx.modules;
304
- }
305
- const mods = [];
306
- const updates = [];
307
- ctx.modules.forEach((mod) => {
308
- mod.importers.forEach((imp) => {
309
- ctx.server.moduleGraph.invalidateModule(imp);
310
- if (pluginOptions.liveReload && classNames.get(imp.id)) {
311
- updates.push(imp.id);
312
- }
313
- else {
314
- mods.push(imp);
315
- }
316
- });
317
- });
318
- pendingCompilation = performCompilation(resolvedConfig, [
319
- ...mods.map((mod) => mod.id),
320
- ...updates,
321
- ]);
322
- if (updates.length > 0) {
323
- await pendingCompilation;
324
- pendingCompilation = null;
325
- updates.forEach((updateId) => {
326
- const impRelativeFileId = `${relative(process.cwd(), updateId)}@${classNames.get(updateId)}`;
327
- sendHMRComponentUpdate(ctx.server, impRelativeFileId);
328
- });
329
- return ctx.modules.map((mod) => {
330
- if (mod.id === ctx.file) {
331
- return markModuleSelfAccepting(mod);
332
- }
333
- return mod;
334
- });
335
- }
336
- return mods;
337
- }
338
- // clear HMR updates with a full reload
339
- classNames.clear();
340
- return ctx.modules;
341
- },
342
- resolveId(id, importer) {
343
- if (jit && id.startsWith('angular:jit:')) {
344
- const path = id.split(';')[1];
345
- return `${normalizePath(resolve(dirname(importer), path))}?${id.includes(':style') ? 'inline' : 'raw'}`;
346
- }
347
- // Map angular external styleUrls to the source file
348
- if (isComponentStyleSheet(id)) {
349
- const componentStyles = externalComponentStyles?.get(getFilenameFromPath(id));
350
- if (componentStyles) {
351
- return componentStyles + new URL(id, 'http://localhost').search;
352
- }
353
- }
354
- return undefined;
355
- },
356
- async load(id) {
357
- // Map angular inline styles to the source text
358
- if (isComponentStyleSheet(id)) {
359
- const componentStyles = inlineComponentStyles?.get(getFilenameFromPath(id));
360
- if (componentStyles) {
361
- return componentStyles;
362
- }
363
- }
364
- return;
365
- },
366
- transform: {
367
- filter: {
368
- id: {
369
- include: [TS_EXT_REGEX],
370
- exclude: [/node_modules/, 'type=script', '@ng/component'],
371
- },
372
- },
373
- async handler(code, id) {
374
- /**
375
- * Check for options.transformFilter
376
- */
377
- if (options?.transformFilter &&
378
- !(options?.transformFilter(code, id) ?? true)) {
379
- return;
380
- }
381
- if (pluginOptions.useAngularCompilationAPI) {
382
- const isAngular = /(Component|Directive|Pipe|Injectable|NgModule)\(/.test(code);
383
- if (!isAngular) {
384
- return;
385
- }
386
- }
387
- /**
388
- * Skip transforming content files
389
- */
390
- if (id.includes('?') && id.includes('analog-content-')) {
391
- return;
392
- }
393
- /**
394
- * Encapsulate component stylesheets that use emulated encapsulation
395
- */
396
- if (pluginOptions.liveReload && isComponentStyleSheet(id)) {
397
- const { encapsulation, componentId } = getComponentStyleSheetMeta(id);
398
- if (encapsulation === 'emulated' && componentId) {
399
- const encapsulated = ngCompiler.encapsulateStyle(code, componentId);
400
- return {
401
- code: encapsulated,
402
- map: null,
403
- };
404
- }
405
- }
406
- if (id.includes('.ts?')) {
407
- // Strip the query string off the ID
408
- // in case of a dynamically loaded file
409
- id = id.replace(/\?(.*)/, '');
410
- }
411
- fileTransformMap.set(id, code);
412
- /**
413
- * Re-analyze on each transform
414
- * for test(Vitest)
415
- */
416
- if (isTest) {
417
- if (isVitestVscode && !initialCompilation) {
418
- // Do full initial compilation
419
- pendingCompilation = performCompilation(resolvedConfig);
420
- initialCompilation = true;
421
- }
422
- const tsMod = viteServer?.moduleGraph.getModuleById(id);
423
- if (tsMod) {
424
- const invalidated = tsMod.lastInvalidationTimestamp;
425
- if (testWatchMode && invalidated) {
426
- pendingCompilation = performCompilation(resolvedConfig, [id]);
427
- }
428
- }
429
- }
430
- const hasComponent = code.includes('@Component');
431
- const templateUrls = hasComponent
432
- ? templateUrlsResolver.resolve(code, id)
433
- : [];
434
- const styleUrls = hasComponent
435
- ? styleUrlsResolver.resolve(code, id)
436
- : [];
437
- if (hasComponent && watchMode) {
438
- for (const urlSet of [...templateUrls, ...styleUrls]) {
439
- // `urlSet` is a string where a relative path is joined with an
440
- // absolute path using the `|` symbol.
441
- // For example: `./app.component.html|/home/projects/analog/src/app/app.component.html`.
442
- const [, absoluteFileUrl] = urlSet.split('|');
443
- this.addWatchFile(absoluteFileUrl);
444
- }
445
- }
446
- if (pendingCompilation) {
447
- await pendingCompilation;
448
- pendingCompilation = null;
449
- }
450
- const typescriptResult = fileEmitter(id);
451
- if (typescriptResult?.warnings &&
452
- typescriptResult?.warnings.length > 0) {
453
- this.warn(`${typescriptResult.warnings.join('\n')}`);
454
- }
455
- if (typescriptResult?.errors && typescriptResult?.errors.length > 0) {
456
- this.error(`${typescriptResult.errors.join('\n')}`);
457
- }
458
- // return fileEmitter
459
- let data = typescriptResult?.content ?? '';
460
- if (jit && data.includes('angular:jit:')) {
461
- data = data.replace(/angular:jit:style:inline;/g, 'virtual:angular:jit:style:inline;');
462
- templateUrls.forEach((templateUrlSet) => {
463
- const [templateFile, resolvedTemplateUrl] = templateUrlSet.split('|');
464
- data = data.replace(`angular:jit:template:file;${templateFile}`, `${resolvedTemplateUrl}?raw`);
465
- });
466
- styleUrls.forEach((styleUrlSet) => {
467
- const [styleFile, resolvedStyleUrl] = styleUrlSet.split('|');
468
- data = data.replace(`angular:jit:style:file;${styleFile}`, `${resolvedStyleUrl}?inline`);
469
- });
470
- }
471
- return {
472
- code: data,
473
- map: null,
474
- };
475
- },
476
- },
477
- closeBundle() {
478
- declarationFiles.forEach(({ declarationFileDir, declarationPath, data }) => {
479
- mkdirSync(declarationFileDir, { recursive: true });
480
- writeFileSync(declarationPath, data, 'utf-8');
481
- });
482
- },
483
- };
484
- }
485
- return [
486
- replaceFiles(pluginOptions.fileReplacements, pluginOptions.workspaceRoot),
487
- angularPlugin(),
488
- pluginOptions.liveReload && liveReloadPlugin({ classNames, fileEmitter }),
489
- ...(isTest && !isStackBlitz ? angularVitestPlugins() : []),
490
- (jit &&
491
- jitPlugin({
492
- inlineStylesExtension: pluginOptions.inlineStylesExtension,
493
- })),
494
- buildOptimizerPlugin({
495
- supportedBrowsers: pluginOptions.supportedBrowsers,
496
- jit,
497
- }),
498
- routerPlugin(),
499
- angularFullVersion < 190004 && pendingTasksPlugin(),
500
- nxFolderPlugin(),
501
- ].filter(Boolean);
502
- function findIncludes() {
503
- const workspaceRoot = normalizePath(resolve(pluginOptions.workspaceRoot));
504
- // Map include patterns to absolute workspace paths
505
- const globs = [
506
- ...pluginOptions.include.map((glob) => `${workspaceRoot}${glob}`),
507
- ];
508
- // Discover TypeScript files using tinyglobby
509
- return globSync(globs, {
510
- dot: true,
511
- absolute: true,
512
- });
513
- }
514
- function createTsConfigGetter(tsconfigOrGetter) {
515
- if (typeof tsconfigOrGetter === 'function') {
516
- return tsconfigOrGetter;
517
- }
518
- return () => tsconfigOrGetter || '';
519
- }
520
- function getTsConfigPath(root, tsconfig, isProd, isTest, isLib) {
521
- if (tsconfig && isAbsolute(tsconfig)) {
522
- if (!existsSync(tsconfig)) {
523
- console.error(`[@analogjs/vite-plugin-angular]: Unable to resolve tsconfig at ${tsconfig}. This causes compilation issues. Check the path or set the "tsconfig" property with an absolute path.`);
524
- }
525
- return tsconfig;
526
- }
527
- let tsconfigFilePath = './tsconfig.app.json';
528
- if (isLib) {
529
- tsconfigFilePath = isProd
530
- ? './tsconfig.lib.prod.json'
531
- : './tsconfig.lib.json';
532
- }
533
- if (isTest) {
534
- tsconfigFilePath = './tsconfig.spec.json';
535
- }
536
- if (tsconfig) {
537
- tsconfigFilePath = tsconfig;
538
- }
539
- const resolvedPath = resolve(root, tsconfigFilePath);
540
- if (!existsSync(resolvedPath)) {
541
- console.error(`[@analogjs/vite-plugin-angular]: Unable to resolve tsconfig at ${resolvedPath}. This causes compilation issues. Check the path or set the "tsconfig" property with an absolute path.`);
542
- }
543
- return resolvedPath;
544
- }
545
- function resolveTsConfigPath() {
546
- const tsconfigValue = pluginOptions.tsconfigGetter();
547
- return getTsConfigPath(tsConfigResolutionContext.root, tsconfigValue, tsConfigResolutionContext.isProd, isTest, tsConfigResolutionContext.isLib);
548
- }
549
- async function performAngularCompilation(config) {
550
- const compilation = await createAngularCompilation(!!pluginOptions.jit, false);
551
- const resolvedTsConfigPath = resolveTsConfigPath();
552
- const compilationResult = await compilation.initialize(resolvedTsConfigPath, {
553
- async transformStylesheet(data, containingFile, resourceFile, order, className) {
554
- if (pluginOptions.liveReload) {
555
- const id = createHash('sha256')
556
- .update(containingFile)
557
- .update(className)
558
- .update(String(order))
559
- .update(data)
560
- .digest('hex');
561
- const filename = id + '.' + pluginOptions.inlineStylesExtension;
562
- inlineComponentStyles.set(filename, data);
563
- return filename;
564
- }
565
- const filename = resourceFile ??
566
- containingFile.replace('.ts', `.${options?.inlineStylesExtension}`);
567
- let stylesheetResult;
568
- try {
569
- stylesheetResult = await preprocessCSS(data, `${filename}?direct`, resolvedConfig);
570
- }
571
- catch (e) {
572
- console.error(`${e}`);
573
- }
574
- return stylesheetResult?.code || '';
575
- },
576
- processWebWorker(workerFile, containingFile) {
577
- return '';
578
- },
579
- }, (tsCompilerOptions) => {
580
- if (pluginOptions.liveReload && watchMode) {
581
- tsCompilerOptions['_enableHmr'] = true;
582
- tsCompilerOptions['externalRuntimeStyles'] = true;
583
- // Workaround for https://github.com/angular/angular/issues/59310
584
- // Force extra instructions to be generated for HMR w/defer
585
- tsCompilerOptions['supportTestBed'] = true;
586
- }
587
- if (tsCompilerOptions.compilationMode === 'partial') {
588
- // These options can't be false in partial mode
589
- tsCompilerOptions['supportTestBed'] = true;
590
- tsCompilerOptions['supportJitMode'] = true;
591
- }
592
- if (!isTest && config.build?.lib) {
593
- tsCompilerOptions['declaration'] = true;
594
- tsCompilerOptions['declarationMap'] = watchMode;
595
- tsCompilerOptions['inlineSources'] = true;
596
- }
597
- if (isTest) {
598
- // Allow `TestBed.overrideXXX()` APIs.
599
- tsCompilerOptions['supportTestBed'] = true;
600
- }
601
- return tsCompilerOptions;
602
- });
603
- compilationResult.externalStylesheets?.forEach((value, key) => {
604
- externalComponentStyles?.set(`${value}.css`, key);
605
- });
606
- const diagnostics = await compilation.diagnoseFiles(pluginOptions.disableTypeChecking
607
- ? DiagnosticModes.All & ~DiagnosticModes.Semantic
608
- : DiagnosticModes.All);
609
- const errors = diagnostics.errors?.length ? diagnostics.errors : [];
610
- const warnings = diagnostics.warnings?.length ? diagnostics.warnings : [];
611
- for (const file of await compilation.emitAffectedFiles()) {
612
- outputFiles.set(file.filename, {
613
- content: file.contents,
614
- dependencies: [],
615
- errors: errors.map((error) => error.text || ''),
616
- warnings: warnings.map((warning) => warning.text || ''),
617
- });
618
- }
619
- compilation.close?.();
620
- }
621
- async function performCompilation(config, ids) {
622
- let resolve;
623
- const previousLock = compilationLock;
624
- compilationLock = new Promise((r) => {
625
- resolve = r;
626
- });
627
- try {
628
- await previousLock;
629
- await _doPerformCompilation(config, ids);
630
- }
631
- finally {
632
- resolve();
633
- }
634
- }
635
- /**
636
- * This method share mutable state and performs the actual compilation work.
637
- * It should not be called concurrently. Use `performCompilation` which wraps this method in a lock to ensure only one compilation runs at a time.
638
- */
639
- async function _doPerformCompilation(config, ids) {
640
- if (pluginOptions.useAngularCompilationAPI) {
641
- await performAngularCompilation(config);
642
- return;
643
- }
644
- const isProd = config.mode === 'production';
645
- const modifiedFiles = new Set(ids ?? []);
646
- sourceFileCache.invalidate(modifiedFiles);
647
- if (ids?.length) {
648
- for (const id of ids || []) {
649
- fileTransformMap.delete(id);
650
- }
651
- }
652
- // Cached include discovery (invalidated only on FS events)
653
- if (pluginOptions.include.length > 0 && includeCache.length === 0) {
654
- includeCache = findIncludes();
655
- }
656
- const resolvedTsConfigPath = resolveTsConfigPath();
657
- const tsconfigKey = [
658
- resolvedTsConfigPath,
659
- isProd ? 'prod' : 'dev',
660
- isTest ? 'test' : 'app',
661
- config.build?.lib ? 'lib' : 'nolib',
662
- ].join('|');
663
- let cached = tsconfigOptionsCache.get(tsconfigKey);
664
- if (!cached) {
665
- const read = compilerCli.readConfiguration(resolvedTsConfigPath, {
666
- suppressOutputPathCheck: true,
667
- outDir: undefined,
668
- sourceMap: false,
669
- inlineSourceMap: !isProd,
670
- inlineSources: !isProd,
671
- declaration: false,
672
- declarationMap: false,
673
- allowEmptyCodegenFiles: false,
674
- annotationsAs: 'decorators',
675
- enableResourceInlining: false,
676
- noEmitOnError: false,
677
- mapRoot: undefined,
678
- sourceRoot: undefined,
679
- supportTestBed: false,
680
- supportJitMode: false,
681
- });
682
- cached = { options: read.options, rootNames: read.rootNames };
683
- tsconfigOptionsCache.set(tsconfigKey, cached);
684
- }
685
- // Clone options before mutation (preserve cache purity)
686
- const tsCompilerOptions = { ...cached.options };
687
- let rootNames = [...cached.rootNames];
688
- if (pluginOptions.liveReload && watchMode) {
689
- tsCompilerOptions['_enableHmr'] = true;
690
- tsCompilerOptions['externalRuntimeStyles'] = true;
691
- // Workaround for https://github.com/angular/angular/issues/59310
692
- // Force extra instructions to be generated for HMR w/defer
693
- tsCompilerOptions['supportTestBed'] = true;
694
- }
695
- if (tsCompilerOptions['compilationMode'] === 'partial') {
696
- // These options can't be false in partial mode
697
- tsCompilerOptions['supportTestBed'] = true;
698
- tsCompilerOptions['supportJitMode'] = true;
699
- }
700
- if (!isTest && config.build?.lib) {
701
- tsCompilerOptions['declaration'] = true;
702
- tsCompilerOptions['declarationMap'] = watchMode;
703
- tsCompilerOptions['inlineSources'] = true;
704
- }
705
- if (isTest) {
706
- // Allow `TestBed.overrideXXX()` APIs.
707
- tsCompilerOptions['supportTestBed'] = true;
708
- }
709
- const replacements = pluginOptions.fileReplacements.map((rp) => join(pluginOptions.workspaceRoot, rp.ssr || rp.with));
710
- // Merge + dedupe root names
711
- rootNames = [...new Set([...rootNames, ...includeCache, ...replacements])];
712
- const hostKey = JSON.stringify(tsCompilerOptions);
713
- let host;
714
- if (cachedHost && cachedHostKey === hostKey) {
715
- host = cachedHost;
716
- }
717
- else {
718
- host = ts.createIncrementalCompilerHost(tsCompilerOptions, {
719
- ...ts.sys,
720
- readFile(path, encoding) {
721
- if (fileTransformMap.has(path)) {
722
- return fileTransformMap.get(path);
723
- }
724
- const file = ts.sys.readFile.call(null, path, encoding);
725
- if (file) {
726
- fileTransformMap.set(path, file);
727
- }
728
- return file;
729
- },
730
- });
731
- cachedHost = host;
732
- cachedHostKey = hostKey;
733
- // Only store cache if in watch mode
734
- if (watchMode) {
735
- augmentHostWithCaching(host, sourceFileCache);
736
- }
737
- }
738
- if (!jit) {
739
- inlineComponentStyles = tsCompilerOptions['externalRuntimeStyles']
740
- ? new Map()
741
- : undefined;
742
- externalComponentStyles = tsCompilerOptions['externalRuntimeStyles']
743
- ? new Map()
744
- : undefined;
745
- augmentHostWithResources(host, styleTransform, {
746
- inlineStylesExtension: pluginOptions.inlineStylesExtension,
747
- isProd,
748
- inlineComponentStyles,
749
- externalComponentStyles,
750
- sourceFileCache,
751
- });
752
- }
753
- /**
754
- * Creates a new NgtscProgram to analyze/re-analyze
755
- * the source files and create a file emitter.
756
- * This is shared between an initial build and a hot update.
757
- */
758
- let typeScriptProgram;
759
- let angularCompiler;
760
- const oldBuilder = builder ?? ts.readBuilderProgram(tsCompilerOptions, host);
761
- if (!jit) {
762
- // Create the Angular specific program that contains the Angular compiler
763
- const angularProgram = new compilerCli.NgtscProgram(rootNames, tsCompilerOptions, host, nextProgram);
764
- angularCompiler = angularProgram.compiler;
765
- typeScriptProgram = angularProgram.compiler.getCurrentProgram();
766
- augmentProgramWithVersioning(typeScriptProgram);
767
- builder = ts.createEmitAndSemanticDiagnosticsBuilderProgram(typeScriptProgram, host, oldBuilder);
768
- nextProgram = angularProgram;
769
- }
770
- else {
771
- builder = ts.createEmitAndSemanticDiagnosticsBuilderProgram(rootNames, tsCompilerOptions, host, oldBuilder);
772
- typeScriptProgram = builder.getProgram();
773
- }
774
- if (!watchMode) {
775
- // When not in watch mode, the startup cost of the incremental analysis can be avoided by
776
- // using an abstract builder that only wraps a TypeScript program.
777
- builder = ts.createAbstractBuilder(typeScriptProgram, host, oldBuilder);
778
- }
779
- if (angularCompiler) {
780
- await angularCompiler.analyzeAsync();
781
- }
782
- const beforeTransformers = jit
783
- ? [
784
- compilerCli.constructorParametersDownlevelTransform(builder.getProgram()),
785
- createJitResourceTransformer(() => builder.getProgram().getTypeChecker()),
786
- ]
787
- : [];
788
- const transformers = mergeTransformers({ before: beforeTransformers }, jit ? {} : angularCompiler.prepareEmit().transformers);
789
- const fileMetadata = getFileMetadata(builder, angularCompiler, pluginOptions.liveReload, pluginOptions.disableTypeChecking);
790
- const writeFileCallback = (_filename, content, _a, _b, sourceFiles) => {
791
- if (!sourceFiles?.length) {
792
- return;
793
- }
794
- const filename = normalizePath(sourceFiles[0].fileName);
795
- if (filename.includes('ngtypecheck.ts') || filename.includes('.d.')) {
796
- return;
797
- }
798
- const metadata = watchMode ? fileMetadata(filename) : {};
799
- outputFiles.set(filename, {
800
- content,
801
- dependencies: [],
802
- errors: metadata.errors,
803
- warnings: metadata.warnings,
804
- hmrUpdateCode: metadata.hmrUpdateCode,
805
- hmrEligible: metadata.hmrEligible,
806
- });
807
- };
808
- const writeOutputFile = (id) => {
809
- const sourceFile = builder.getSourceFile(id);
810
- if (!sourceFile) {
811
- return;
812
- }
813
- let content = '';
814
- builder.emit(sourceFile, (filename, data) => {
815
- if (/\.[cm]?js$/.test(filename)) {
816
- content = data;
817
- }
818
- if (!watchMode &&
819
- !isTest &&
820
- /\.d\.ts/.test(filename) &&
821
- !filename.includes('.ngtypecheck.')) {
822
- // output to library root instead /src
823
- const declarationPath = resolve(config.root, config.build.outDir, relative(config.root, filename)).replace('/src/', '/');
824
- const declarationFileDir = declarationPath
825
- .replace(basename(filename), '')
826
- .replace('/src/', '/');
827
- declarationFiles.push({
828
- declarationFileDir,
829
- declarationPath,
830
- data,
831
- });
832
- }
833
- }, undefined /* cancellationToken */, undefined /* emitOnlyDtsFiles */, transformers);
834
- writeFileCallback(id, content, false, undefined, [sourceFile]);
835
- if (angularCompiler) {
836
- angularCompiler.incrementalCompilation.recordSuccessfulEmit(sourceFile);
837
- }
838
- };
839
- if (watchMode) {
840
- if (ids && ids.length > 0) {
841
- ids.forEach((id) => writeOutputFile(id));
842
- }
843
- else {
844
- /**
845
- * Only block the server from starting up
846
- * during testing.
847
- */
848
- if (isTest) {
849
- // TypeScript will loop until there are no more affected files in the program
850
- while (builder.emitNextAffectedFile(writeFileCallback, undefined, undefined, transformers)) {
851
- /* empty */
852
- }
853
- }
854
- }
855
- }
856
- if (!isTest) {
857
- /**
858
- * Perf: Output files on demand so the dev server
859
- * isn't blocked when emitting files.
860
- */
861
- outputFile = writeOutputFile;
862
- }
863
- }
1536
+ * Refreshes any already-served stylesheet records that map back to a changed
1537
+ * source file.
1538
+ *
1539
+ * This is the critical bridge for externalized Angular component styles during
1540
+ * HMR. Angular's resource watcher can notice that `/src/...component.css`
1541
+ * changed before Angular recompilation has had a chance to repopulate the
1542
+ * stylesheet registry. If we emit a CSS update against the existing virtual
1543
+ * stylesheet id without first refreshing the registry content, the browser gets
1544
+ * a hot update containing stale CSS. By rewriting the existing served records
1545
+ * from disk up front, HMR always pushes the latest source content.
1546
+ */
1547
+ function refreshStylesheetRegistryForFile(file, stylesheetRegistry, stylePreprocessor) {
1548
+ const normalizedFile = normalizePath(file.split("?")[0]);
1549
+ if (!stylesheetRegistry || !existsSync(normalizedFile)) return;
1550
+ const publicIds = stylesheetRegistry.getPublicIdsForSource(normalizedFile);
1551
+ if (publicIds.length === 0) return;
1552
+ const rawCss = readFileSync(normalizedFile, "utf-8");
1553
+ const preprocessed = preprocessStylesheetResult(rawCss, normalizedFile, stylePreprocessor);
1554
+ const servedCss = rewriteRelativeCssImports(preprocessed.code, normalizedFile);
1555
+ for (const publicId of publicIds) stylesheetRegistry.registerServedStylesheet({
1556
+ publicId,
1557
+ sourcePath: normalizedFile,
1558
+ originalCode: rawCss,
1559
+ normalizedCode: servedCss,
1560
+ dependencies: normalizeStylesheetDependencies(preprocessed.dependencies),
1561
+ diagnostics: preprocessed.diagnostics,
1562
+ tags: preprocessed.tags
1563
+ }, [
1564
+ normalizedFile,
1565
+ normalizePath(normalizedFile),
1566
+ basename(normalizedFile),
1567
+ normalizedFile.replace(/^\//, "")
1568
+ ]);
1569
+ debugStylesV("stylesheet registry refreshed from source file", {
1570
+ file: normalizedFile,
1571
+ publicIds,
1572
+ dependencies: preprocessed.dependencies,
1573
+ diagnostics: preprocessed.diagnostics,
1574
+ tags: preprocessed.tags,
1575
+ source: describeStylesheetContent(rawCss),
1576
+ served: describeStylesheetContent(servedCss)
1577
+ });
1578
+ }
1579
+ function diagnoseComponentStylesheetPipeline(changedFile, directModule, stylesheetRegistry, wrapperModules, stylePreprocessor) {
1580
+ const normalizedFile = normalizePath(changedFile.split("?")[0]);
1581
+ const sourceExists = existsSync(normalizedFile);
1582
+ const sourceCode = sourceExists ? readFileSync(normalizedFile, "utf-8") : void 0;
1583
+ const directRequestPath = directModule.id ? getFilenameFromPath(directModule.id) : void 0;
1584
+ const sourcePath = directRequestPath ? stylesheetRegistry?.resolveExternalSource(directRequestPath) ?? stylesheetRegistry?.resolveExternalSource(directRequestPath.replace(/^\//, "")) : normalizedFile;
1585
+ const registryCode = directRequestPath ? stylesheetRegistry?.getServedContent(directRequestPath) : void 0;
1586
+ const trackedRequestIds = stylesheetRegistry?.getRequestIdsForSource(sourcePath ?? "") ?? [];
1587
+ const dependencies = stylesheetRegistry?.getDependenciesForSource(sourcePath ?? "") ?? [];
1588
+ const diagnostics = stylesheetRegistry?.getDiagnosticsForSource(sourcePath ?? "") ?? [];
1589
+ const tags = stylesheetRegistry?.getTagsForSource(sourcePath ?? "") ?? [];
1590
+ const anomalies = [];
1591
+ const hints = [];
1592
+ if (!sourceExists) {
1593
+ anomalies.push("source_file_missing");
1594
+ hints.push("The stylesheet watcher fired for a file that no longer exists on disk.");
1595
+ }
1596
+ if (!registryCode) {
1597
+ anomalies.push("registry_content_missing");
1598
+ hints.push("The stylesheet registry has no served content for the direct module request path.");
1599
+ }
1600
+ if (sourceCode && registryCode) {
1601
+ let expectedRegistryCode = preprocessStylesheet(sourceCode, normalizedFile, stylePreprocessor);
1602
+ expectedRegistryCode = rewriteRelativeCssImports(expectedRegistryCode, normalizedFile);
1603
+ if (describeStylesheetContent(expectedRegistryCode).digest !== describeStylesheetContent(registryCode).digest) {
1604
+ anomalies.push("source_registry_mismatch");
1605
+ hints.push("The source file changed, but the served stylesheet content in the registry is still stale.");
1606
+ }
1607
+ }
1608
+ if (trackedRequestIds.length === 0) {
1609
+ anomalies.push("no_tracked_requests");
1610
+ hints.push("No live stylesheet requests are tracked for this source file, so HMR has no browser-facing target.");
1611
+ }
1612
+ if (trackedRequestIds.some((id) => id.includes("?ngcomp=")) && wrapperModules.length === 0) {
1613
+ anomalies.push("tracked_wrapper_missing_from_module_graph");
1614
+ hints.push("A wrapper request id is known, but Vite did not expose a live wrapper module during this HMR pass.");
1615
+ }
1616
+ if (trackedRequestIds.every((id) => !id.includes("?ngcomp=")) && wrapperModules.length === 0) {
1617
+ anomalies.push("wrapper_not_yet_tracked");
1618
+ hints.push("Only direct stylesheet requests were tracked during this HMR pass; the wrapper request may be appearing too late.");
1619
+ }
1620
+ return {
1621
+ file: changedFile,
1622
+ sourcePath,
1623
+ source: sourceCode ? describeStylesheetContent(rewriteRelativeCssImports(preprocessStylesheet(sourceCode, normalizedFile, stylePreprocessor), normalizedFile)) : void 0,
1624
+ registry: registryCode ? describeStylesheetContent(registryCode) : void 0,
1625
+ dependencies,
1626
+ diagnostics,
1627
+ tags,
1628
+ directModuleId: directModule.id,
1629
+ directModuleUrl: directModule.url,
1630
+ trackedRequestIds,
1631
+ wrapperCount: wrapperModules.length,
1632
+ anomalies,
1633
+ hints
1634
+ };
1635
+ }
1636
+ async function findComponentStylesheetWrapperModules(server, changedFile, directModule, fileModules, stylesheetRegistry) {
1637
+ const wrapperModules = /* @__PURE__ */ new Map();
1638
+ for (const mod of fileModules) if (mod.id && mod.type === "js" && isComponentStyleSheet(mod.id) && isModuleForChangedResource(mod, changedFile, stylesheetRegistry)) wrapperModules.set(mod.id, mod);
1639
+ const directRequestIds = /* @__PURE__ */ new Set();
1640
+ if (directModule.id) directRequestIds.add(directModule.id);
1641
+ if (directModule.url) directRequestIds.add(directModule.url);
1642
+ const requestPath = directModule.id ? getFilenameFromPath(directModule.id) : void 0;
1643
+ const sourcePath = requestPath ? stylesheetRegistry?.resolveExternalSource(requestPath) ?? stylesheetRegistry?.resolveExternalSource(requestPath.replace(/^\//, "")) : void 0;
1644
+ for (const requestId of stylesheetRegistry?.getRequestIdsForSource(sourcePath ?? "") ?? []) if (requestId.includes("?ngcomp=")) directRequestIds.add(requestId);
1645
+ const candidateWrapperIds = [...directRequestIds].filter((id) => id.includes("?direct&ngcomp=")).map((id) => id.replace("?direct&ngcomp=", "?ngcomp="));
1646
+ const lookupHits = [];
1647
+ for (const candidate of candidateWrapperIds) {
1648
+ const mod = await server.moduleGraph.getModuleByUrl(candidate) ?? server.moduleGraph.getModuleById(candidate);
1649
+ lookupHits.push({
1650
+ candidate,
1651
+ via: mod?.url === candidate ? "url" : mod ? "id" : void 0,
1652
+ moduleId: mod?.id,
1653
+ moduleType: mod?.type
1654
+ });
1655
+ if (mod?.id && mod.type === "js" && isComponentStyleSheet(mod.id) && isModuleForChangedResource(mod, changedFile, stylesheetRegistry)) wrapperModules.set(mod.id, mod);
1656
+ }
1657
+ debugHmrV("component stylesheet wrapper lookup", {
1658
+ file: changedFile,
1659
+ sourcePath,
1660
+ directModuleId: directModule.id,
1661
+ directModuleUrl: directModule.url,
1662
+ candidateWrapperIds,
1663
+ lookupHits
1664
+ });
1665
+ if (wrapperModules.size === 0) debugHmrV("component stylesheet wrapper lookup empty", {
1666
+ file: changedFile,
1667
+ sourcePath,
1668
+ directModuleId: directModule.id,
1669
+ directModuleUrl: directModule.url,
1670
+ candidateWrapperIds
1671
+ });
1672
+ return [...wrapperModules.values()];
864
1673
  }
865
1674
  function sendHMRComponentUpdate(server, id) {
866
- server.ws.send('angular:component-update', {
867
- id: encodeURIComponent(id),
868
- timestamp: Date.now(),
869
- });
870
- classNames.delete(id);
871
- }
872
- export function getFileMetadata(program, angularCompiler, liveReload, disableTypeChecking) {
873
- const ts = require('typescript');
874
- return (file) => {
875
- const sourceFile = program.getSourceFile(file);
876
- if (!sourceFile) {
877
- return {};
878
- }
879
- const diagnostics = getDiagnosticsForSourceFile(sourceFile, !!disableTypeChecking, program, angularCompiler);
880
- const errors = diagnostics
881
- .filter((d) => d.category === ts.DiagnosticCategory?.Error)
882
- .map((d) => typeof d.messageText === 'object'
883
- ? d.messageText.messageText
884
- : d.messageText);
885
- const warnings = diagnostics
886
- .filter((d) => d.category === ts.DiagnosticCategory?.Warning)
887
- .map((d) => d.messageText);
888
- let hmrUpdateCode = undefined;
889
- let hmrEligible = false;
890
- if (liveReload) {
891
- for (const node of sourceFile.statements) {
892
- if (ts.isClassDeclaration(node) && node.name != null) {
893
- hmrUpdateCode = angularCompiler?.emitHmrUpdateModule(node);
894
- if (hmrUpdateCode) {
895
- classNames.set(file, node.name.getText());
896
- hmrEligible = true;
897
- }
898
- }
899
- }
900
- }
901
- return { errors, warnings, hmrUpdateCode, hmrEligible };
902
- };
1675
+ debugHmrV("ws send: angular component update", {
1676
+ id,
1677
+ timestamp: Date.now()
1678
+ });
1679
+ server.ws.send("angular:component-update", {
1680
+ id: encodeURIComponent(id),
1681
+ timestamp: Date.now()
1682
+ });
1683
+ classNames.delete(id);
1684
+ }
1685
+ function sendCssUpdate(server, update) {
1686
+ const timestamp = Date.now();
1687
+ debugHmrV("ws send: css-update", {
1688
+ ...update,
1689
+ timestamp
1690
+ });
1691
+ server.ws.send({
1692
+ type: "update",
1693
+ updates: [{
1694
+ type: "css-update",
1695
+ timestamp,
1696
+ path: update.path,
1697
+ acceptedPath: update.acceptedPath
1698
+ }]
1699
+ });
1700
+ }
1701
+ function sendFullReload(server, details) {
1702
+ debugHmrV("ws send: full-reload", details);
1703
+ server.ws.send("analog:debug-full-reload", details);
1704
+ server.ws.send({ type: "full-reload" });
1705
+ }
1706
+ function resolveComponentClassNamesForStyleOwner(ownerFile, sourcePath) {
1707
+ if (!existsSync(ownerFile)) return [];
1708
+ const components = getAngularComponentMetadata(readFileSync(ownerFile, "utf-8"));
1709
+ const normalizedSourcePath = normalizePath(sourcePath);
1710
+ return components.filter((component) => component.styleUrls.some((styleUrl) => normalizePath(resolve(dirname(ownerFile), styleUrl)) === normalizedSourcePath)).map((component) => component.className);
1711
+ }
1712
+ function findStaticClassAndBoundClassConflicts(template) {
1713
+ const issues = [];
1714
+ for (const { index, snippet } of findOpeningTagSnippets(template)) {
1715
+ if (!snippet.includes("[class]")) continue;
1716
+ const hasStaticClass = /\sclass\s*=\s*(['"])(?:(?!\1)[\s\S])*\1/.test(snippet);
1717
+ const hasBoundClass = /\s\[class\]\s*=\s*(['"])(?:(?!\1)[\s\S])*\1/.test(snippet);
1718
+ if (hasStaticClass && hasBoundClass) {
1719
+ const prefix = template.slice(0, index);
1720
+ const line = prefix.split("\n").length;
1721
+ const column = index - prefix.lastIndexOf("\n");
1722
+ issues.push({
1723
+ line,
1724
+ column,
1725
+ snippet: snippet.replace(/\s+/g, " ").trim()
1726
+ });
1727
+ }
1728
+ }
1729
+ return issues;
1730
+ }
1731
+ function throwTemplateClassBindingConflict(id, issue) {
1732
+ throw new Error([
1733
+ "[Analog Angular] Invalid template class binding.",
1734
+ `File: ${id}:${issue.line}:${issue.column}`,
1735
+ "The same element uses both a static `class=\"...\"` attribute and a whole-element `[class]=\"...\"` binding.",
1736
+ "That pattern can replace or conflict with static Tailwind classes, which makes styles appear to stop applying.",
1737
+ "Use `[ngClass]` or explicit `[class.foo]` bindings instead of `[class]` when the element also has static classes.",
1738
+ `Snippet: ${issue.snippet}`
1739
+ ].join("\n"));
1740
+ }
1741
+ function findBoundClassAndNgClassConflicts(template) {
1742
+ const issues = [];
1743
+ if (!/\[class\]\s*=/.test(template) || !template.includes("[ngClass]")) return issues;
1744
+ for (const { index, snippet } of findOpeningTagSnippets(template)) {
1745
+ if (!/\[class\]\s*=/.test(snippet) || !snippet.includes("[ngClass]")) continue;
1746
+ const prefix = template.slice(0, index);
1747
+ const line = prefix.split("\n").length;
1748
+ const column = index - prefix.lastIndexOf("\n");
1749
+ issues.push({
1750
+ line,
1751
+ column,
1752
+ snippet: snippet.replace(/\s+/g, " ").trim()
1753
+ });
1754
+ }
1755
+ return issues;
1756
+ }
1757
+ function findOpeningTagSnippets(template) {
1758
+ const matches = [];
1759
+ for (let index = 0; index < template.length; index++) {
1760
+ if (template[index] !== "<") continue;
1761
+ const tagStart = template[index + 1];
1762
+ if (!tagStart || !/[a-zA-Z]/.test(tagStart)) continue;
1763
+ let quote = null;
1764
+ for (let end = index + 1; end < template.length; end++) {
1765
+ const char = template[end];
1766
+ if (quote) {
1767
+ if (char === quote) quote = null;
1768
+ continue;
1769
+ }
1770
+ if (char === "\"" || char === "'") {
1771
+ quote = char;
1772
+ continue;
1773
+ }
1774
+ if (char === ">") {
1775
+ matches.push({
1776
+ index,
1777
+ snippet: template.slice(index, end + 1)
1778
+ });
1779
+ index = end;
1780
+ break;
1781
+ }
1782
+ }
1783
+ }
1784
+ return matches;
1785
+ }
1786
+ function formatActiveGraphLocations(entries) {
1787
+ return [...entries].sort().map((entry) => `- ${entry}`).join("\n");
1788
+ }
1789
+ function logComponentStylesheetHmrOutcome(details) {
1790
+ const pitfalls = [];
1791
+ const rejectedPreferredPaths = [];
1792
+ const hints = [];
1793
+ if (details.encapsulation === "shadow") {
1794
+ pitfalls.push("shadow-encapsulation");
1795
+ rejectedPreferredPaths.push("css-update");
1796
+ rejectedPreferredPaths.push("owner-component-update");
1797
+ hints.push("Shadow DOM styles cannot rely on Vite CSS patching because Angular applies them inside a shadow root.");
1798
+ }
1799
+ if (details.diagnosis.anomalies.includes("wrapper_not_yet_tracked")) {
1800
+ pitfalls.push("wrapper-not-yet-tracked");
1801
+ rejectedPreferredPaths.push("css-update");
1802
+ hints.push("The direct stylesheet module exists, but the browser-visible Angular wrapper module was not available in the live graph during this HMR pass.");
1803
+ }
1804
+ if (details.diagnosis.anomalies.includes("tracked_wrapper_missing_from_module_graph")) {
1805
+ pitfalls.push("tracked-wrapper-missing-from-module-graph");
1806
+ rejectedPreferredPaths.push("css-update");
1807
+ hints.push("A wrapper request id is known, but Vite could not resolve a live wrapper module for targeted CSS HMR.");
1808
+ }
1809
+ if ((details.ownerIds?.filter(Boolean).length ?? 0) === 0) {
1810
+ pitfalls.push("no-owner-modules");
1811
+ if (details.outcome === "full-reload") {
1812
+ rejectedPreferredPaths.push("owner-component-update");
1813
+ hints.push("No owning TS component modules were available in the module graph for owner-based fallback.");
1814
+ }
1815
+ } else if ((details.updateIds?.length ?? 0) === 0) {
1816
+ pitfalls.push("owner-modules-without-class-identities");
1817
+ if (details.outcome === "full-reload") {
1818
+ rejectedPreferredPaths.push("owner-component-update");
1819
+ hints.push("Owner modules were found, but Angular did not expose component class identities after recompilation, so no targeted component update could be sent.");
1820
+ }
1821
+ }
1822
+ debugHmrV("component stylesheet hmr outcome", {
1823
+ file: details.file,
1824
+ outcome: details.outcome,
1825
+ encapsulation: details.encapsulation,
1826
+ directModuleId: details.directModuleId,
1827
+ wrapperIds: details.wrapperIds ?? [],
1828
+ ownerIds: details.ownerIds ?? [],
1829
+ updateIds: details.updateIds ?? [],
1830
+ preferredPath: details.encapsulation === "shadow" ? "full-reload" : "css-update",
1831
+ rejectedPreferredPaths: [...new Set(rejectedPreferredPaths)],
1832
+ pitfalls: [...new Set(pitfalls)],
1833
+ anomalies: details.diagnosis.anomalies,
1834
+ hints: [...new Set([...details.diagnosis.hints, ...hints])]
1835
+ });
1836
+ }
1837
+ function findTemplateOwnerModules(server, resourceFile) {
1838
+ const candidateTsFiles = [normalizePath(resourceFile.split("?")[0]).replace(/\.(html|htm)$/i, ".ts")];
1839
+ const modules = /* @__PURE__ */ new Map();
1840
+ for (const candidate of candidateTsFiles) server.moduleGraph.getModulesByFile(candidate)?.forEach((mod) => {
1841
+ if (mod.id) modules.set(mod.id, mod);
1842
+ });
1843
+ return [...modules.values()];
1844
+ }
1845
+ function findStyleOwnerModules(server, resourceFile, styleSourceOwners) {
1846
+ const normalizedResourceFile = normalizePath(resourceFile.split("?")[0]);
1847
+ const candidateOwnerFiles = [...styleSourceOwners.get(normalizedResourceFile) ?? []];
1848
+ const modules = /* @__PURE__ */ new Map();
1849
+ for (const ownerFile of candidateOwnerFiles) server.moduleGraph.getModulesByFile(ownerFile)?.forEach((mod) => {
1850
+ if (mod.id) modules.set(mod.id, mod);
1851
+ });
1852
+ return [...modules.values()];
1853
+ }
1854
+ function getFileMetadata(program, angularCompiler, hmrEnabled, disableTypeChecking) {
1855
+ const ts = require("typescript");
1856
+ return (file) => {
1857
+ const sourceFile = program.getSourceFile(file);
1858
+ if (!sourceFile) return {};
1859
+ const diagnostics = getDiagnosticsForSourceFile(sourceFile, !!disableTypeChecking, program, angularCompiler);
1860
+ const errors = diagnostics.filter((d) => d.category === ts.DiagnosticCategory?.Error).map((d) => typeof d.messageText === "object" ? d.messageText.messageText : d.messageText);
1861
+ const warnings = diagnostics.filter((d) => d.category === ts.DiagnosticCategory?.Warning).map((d) => d.messageText);
1862
+ let hmrUpdateCode = void 0;
1863
+ let hmrEligible = false;
1864
+ if (hmrEnabled) {
1865
+ for (const node of sourceFile.statements) if (ts.isClassDeclaration(node) && node.name != null) {
1866
+ hmrUpdateCode = angularCompiler?.emitHmrUpdateModule(node);
1867
+ if (hmrUpdateCode) {
1868
+ const className = node.name.getText();
1869
+ classNames.set(file, className);
1870
+ hmrEligible = true;
1871
+ debugHmr("NgtscProgram emitHmrUpdateModule", {
1872
+ file,
1873
+ className
1874
+ });
1875
+ }
1876
+ }
1877
+ }
1878
+ return {
1879
+ errors,
1880
+ warnings,
1881
+ hmrUpdateCode,
1882
+ hmrEligible
1883
+ };
1884
+ };
903
1885
  }
904
1886
  function getDiagnosticsForSourceFile(sourceFile, disableTypeChecking, program, angularCompiler) {
905
- const syntacticDiagnostics = program.getSyntacticDiagnostics(sourceFile);
906
- if (disableTypeChecking) {
907
- // Syntax errors are cheap to compute and the app will not run if there are any
908
- // So always show these types of errors regardless if type checking is disabled
909
- return syntacticDiagnostics;
910
- }
911
- const semanticDiagnostics = program.getSemanticDiagnostics(sourceFile);
912
- const angularDiagnostics = angularCompiler
913
- ? angularCompiler.getDiagnosticsForFile(sourceFile, 1)
914
- : [];
915
- return [
916
- ...syntacticDiagnostics,
917
- ...semanticDiagnostics,
918
- ...angularDiagnostics,
919
- ];
1887
+ const syntacticDiagnostics = program.getSyntacticDiagnostics(sourceFile);
1888
+ if (disableTypeChecking) return syntacticDiagnostics;
1889
+ const semanticDiagnostics = program.getSemanticDiagnostics(sourceFile);
1890
+ const angularDiagnostics = angularCompiler ? angularCompiler.getDiagnosticsForFile(sourceFile, 1) : [];
1891
+ return [
1892
+ ...syntacticDiagnostics,
1893
+ ...semanticDiagnostics,
1894
+ ...angularDiagnostics
1895
+ ];
920
1896
  }
921
1897
  function markModuleSelfAccepting(mod) {
922
- // support Vite 6
923
- if ('_clientModule' in mod) {
924
- mod['_clientModule'].isSelfAccepting = true;
925
- }
926
- return {
927
- ...mod,
928
- isSelfAccepting: true,
929
- };
1898
+ if ("_clientModule" in mod) mod["_clientModule"].isSelfAccepting = true;
1899
+ return {
1900
+ ...mod,
1901
+ isSelfAccepting: true
1902
+ };
930
1903
  }
931
1904
  function isComponentStyleSheet(id) {
932
- return id.includes('ngcomp=');
1905
+ return id.includes("ngcomp=");
933
1906
  }
934
1907
  function getComponentStyleSheetMeta(id) {
935
- const params = new URL(id, 'http://localhost').searchParams;
936
- const encapsulationMapping = {
937
- '0': 'emulated',
938
- '2': 'none',
939
- '3': 'shadow',
940
- };
941
- return {
942
- componentId: params.get('ngcomp'),
943
- encapsulation: encapsulationMapping[params.get('e')],
944
- };
1908
+ const params = new URL(id, "http://localhost").searchParams;
1909
+ return {
1910
+ componentId: params.get("ngcomp"),
1911
+ encapsulation: {
1912
+ "0": "emulated",
1913
+ "2": "none",
1914
+ "3": "shadow"
1915
+ }[params.get("e")]
1916
+ };
945
1917
  }
946
1918
  /**
947
- * Removes leading / and query string from a url path
948
- * e.g. /foo.scss?direct&ngcomp=ng-c3153525609&e=0 returns foo.scss
949
- * @param id
950
- */
1919
+ * Removes leading / and query string from a url path
1920
+ * e.g. /foo.scss?direct&ngcomp=ng-c3153525609&e=0 returns foo.scss
1921
+ * @param id
1922
+ */
951
1923
  function getFilenameFromPath(id) {
952
- return new URL(id, 'http://localhost').pathname.replace(/^\//, '');
1924
+ try {
1925
+ return new URL(id, "http://localhost").pathname.replace(/^\//, "");
1926
+ } catch {
1927
+ const queryIndex = id.indexOf("?");
1928
+ return (queryIndex >= 0 ? id.slice(0, queryIndex) : id).replace(/^\//, "");
1929
+ }
953
1930
  }
954
1931
  /**
955
- * Checks for vitest run from the command line
956
- * @returns boolean
957
- */
958
- export function isTestWatchMode(args = process.argv) {
959
- // vitest --run
960
- const hasRun = args.find((arg) => arg.includes('--run'));
961
- if (hasRun) {
962
- return false;
963
- }
964
- // vitest --no-run
965
- const hasNoRun = args.find((arg) => arg.includes('--no-run'));
966
- if (hasNoRun) {
967
- return true;
968
- }
969
- // check for --watch=false or --no-watch
970
- const hasWatch = args.find((arg) => arg.includes('watch'));
971
- if (hasWatch && ['false', 'no'].some((neg) => hasWatch.includes(neg))) {
972
- return false;
973
- }
974
- // check for --watch false
975
- const watchIndex = args.findIndex((arg) => arg.includes('watch'));
976
- const watchArg = args[watchIndex + 1];
977
- if (watchArg && watchArg === 'false') {
978
- return false;
979
- }
980
- return true;
1932
+ * Checks for vitest run from the command line
1933
+ * @returns boolean
1934
+ */
1935
+ function isTestWatchMode(args = process.argv) {
1936
+ if (args.find((arg) => arg.includes("--run"))) return false;
1937
+ if (args.find((arg) => arg.includes("--no-run"))) return true;
1938
+ const hasWatch = args.find((arg) => arg.includes("watch"));
1939
+ if (hasWatch && ["false", "no"].some((neg) => hasWatch.includes(neg))) return false;
1940
+ const watchArg = args[args.findIndex((arg) => arg.includes("watch")) + 1];
1941
+ if (watchArg && watchArg === "false") return false;
1942
+ return true;
981
1943
  }
1944
+ //#endregion
1945
+ export { angular };
1946
+
982
1947
  //# sourceMappingURL=angular-vite-plugin.js.map