@analogjs/vite-plugin-angular 3.0.0-alpha.4 → 3.0.0-alpha.41

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