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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (194) hide show
  1. package/README.md +30 -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 +41 -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 +39 -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 +180 -44
  24. package/src/lib/angular-vite-plugin.js +1233 -969
  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/compilation-api/compilation-api-plugin.d.ts +29 -0
  30. package/src/lib/compilation-api/compilation-api-plugin.js +469 -0
  31. package/src/lib/compilation-api/compilation-api-plugin.js.map +1 -0
  32. package/src/lib/compilation-api/index.d.ts +1 -0
  33. package/src/lib/compilation-api/index.js +1 -0
  34. package/src/lib/compiler/angular-version.d.ts +19 -0
  35. package/src/lib/compiler/angular-version.js +16 -0
  36. package/src/lib/compiler/angular-version.js.map +1 -0
  37. package/src/lib/compiler/class-field-lowering.d.ts +23 -0
  38. package/src/lib/compiler/class-field-lowering.js +131 -0
  39. package/src/lib/compiler/class-field-lowering.js.map +1 -0
  40. package/src/lib/compiler/compile.d.ts +44 -0
  41. package/src/lib/compiler/compile.js +731 -0
  42. package/src/lib/compiler/compile.js.map +1 -0
  43. package/src/lib/compiler/constants.d.ts +18 -0
  44. package/src/lib/compiler/constants.js +52 -0
  45. package/src/lib/compiler/constants.js.map +1 -0
  46. package/src/lib/compiler/debug.d.ts +22 -0
  47. package/src/lib/compiler/debug.js +20 -0
  48. package/src/lib/compiler/debug.js.map +1 -0
  49. package/src/lib/compiler/defer.d.ts +57 -0
  50. package/src/lib/compiler/defer.js +154 -0
  51. package/src/lib/compiler/defer.js.map +1 -0
  52. package/src/lib/compiler/dts-reader.d.ts +35 -0
  53. package/src/lib/compiler/dts-reader.js +365 -0
  54. package/src/lib/compiler/dts-reader.js.map +1 -0
  55. package/src/lib/compiler/hmr.d.ts +16 -0
  56. package/src/lib/compiler/hmr.js +69 -0
  57. package/src/lib/compiler/hmr.js.map +1 -0
  58. package/src/lib/compiler/index.d.ts +7 -0
  59. package/src/lib/compiler/index.js +7 -0
  60. package/src/lib/compiler/jit-metadata.d.ts +14 -0
  61. package/src/lib/compiler/jit-metadata.js +146 -0
  62. package/src/lib/compiler/jit-metadata.js.map +1 -0
  63. package/src/lib/compiler/jit-transform.d.ts +24 -0
  64. package/src/lib/compiler/jit-transform.js +200 -0
  65. package/src/lib/compiler/jit-transform.js.map +1 -0
  66. package/src/lib/compiler/js-emitter.d.ts +10 -0
  67. package/src/lib/compiler/js-emitter.js +445 -0
  68. package/src/lib/compiler/js-emitter.js.map +1 -0
  69. package/src/lib/compiler/metadata.d.ts +45 -0
  70. package/src/lib/compiler/metadata.js +633 -0
  71. package/src/lib/compiler/metadata.js.map +1 -0
  72. package/src/lib/compiler/registry.d.ts +49 -0
  73. package/src/lib/compiler/registry.js +164 -0
  74. package/src/lib/compiler/registry.js.map +1 -0
  75. package/src/lib/compiler/resource-inliner.d.ts +21 -0
  76. package/src/lib/compiler/resource-inliner.js +152 -0
  77. package/src/lib/compiler/resource-inliner.js.map +1 -0
  78. package/src/lib/compiler/style-ast.d.ts +8 -0
  79. package/src/lib/compiler/style-ast.js +54 -0
  80. package/src/lib/compiler/style-ast.js.map +1 -0
  81. package/src/lib/compiler/styles.d.ts +13 -0
  82. package/src/lib/compiler/test-helpers.d.ts +7 -0
  83. package/src/lib/compiler/type-elision.d.ts +26 -0
  84. package/src/lib/compiler/type-elision.js +211 -0
  85. package/src/lib/compiler/type-elision.js.map +1 -0
  86. package/src/lib/compiler/utils.d.ts +10 -0
  87. package/src/lib/compiler/utils.js +35 -0
  88. package/src/lib/compiler/utils.js.map +1 -0
  89. package/src/lib/compiler-plugin.d.ts +11 -11
  90. package/src/lib/compiler-plugin.js +43 -44
  91. package/src/lib/compiler-plugin.js.map +1 -1
  92. package/src/lib/component-resolvers.d.ts +23 -5
  93. package/src/lib/component-resolvers.js +153 -63
  94. package/src/lib/component-resolvers.js.map +1 -1
  95. package/src/lib/encapsulation-plugin.d.ts +13 -0
  96. package/src/lib/encapsulation-plugin.js +48 -0
  97. package/src/lib/encapsulation-plugin.js.map +1 -0
  98. package/src/lib/fast-compile-plugin.d.ts +28 -0
  99. package/src/lib/fast-compile-plugin.js +289 -0
  100. package/src/lib/fast-compile-plugin.js.map +1 -0
  101. package/src/lib/host.d.ts +10 -8
  102. package/src/lib/host.js +113 -101
  103. package/src/lib/host.js.map +1 -1
  104. package/src/lib/live-reload-plugin.d.ts +5 -5
  105. package/src/lib/live-reload-plugin.js +61 -62
  106. package/src/lib/live-reload-plugin.js.map +1 -1
  107. package/src/lib/models.d.ts +9 -9
  108. package/src/lib/nx-folder-plugin.d.ts +5 -5
  109. package/src/lib/nx-folder-plugin.js +18 -16
  110. package/src/lib/nx-folder-plugin.js.map +1 -1
  111. package/src/lib/plugins/file-replacements.plugin.d.ts +4 -4
  112. package/src/lib/plugins/file-replacements.plugin.js +40 -62
  113. package/src/lib/plugins/file-replacements.plugin.js.map +1 -1
  114. package/src/lib/router-plugin.d.ts +1 -1
  115. package/src/lib/router-plugin.js +23 -23
  116. package/src/lib/router-plugin.js.map +1 -1
  117. package/src/lib/style-pipeline.d.ts +15 -0
  118. package/src/lib/style-pipeline.js +31 -0
  119. package/src/lib/style-pipeline.js.map +1 -0
  120. package/src/lib/style-preprocessor.d.ts +35 -0
  121. package/src/lib/style-preprocessor.js +35 -0
  122. package/src/lib/style-preprocessor.js.map +1 -0
  123. package/src/lib/stylesheet-registry.d.ts +73 -0
  124. package/src/lib/stylesheet-registry.js +168 -0
  125. package/src/lib/stylesheet-registry.js.map +1 -0
  126. package/src/lib/tailwind-plugin.d.ts +34 -0
  127. package/src/lib/tailwind-plugin.js +116 -0
  128. package/src/lib/tailwind-plugin.js.map +1 -0
  129. package/src/lib/template-class-binding-guard-plugin.d.ts +30 -0
  130. package/src/lib/template-class-binding-guard-plugin.js +237 -0
  131. package/src/lib/template-class-binding-guard-plugin.js.map +1 -0
  132. package/src/lib/tools/package.json +2 -7
  133. package/src/lib/tools/src/builders/vite/vite-build.impl.js +31 -38
  134. package/src/lib/tools/src/builders/vite/vite-build.impl.js.map +1 -1
  135. package/src/lib/tools/src/builders/vite-dev-server/dev-server.impl.js +51 -62
  136. package/src/lib/tools/src/builders/vite-dev-server/dev-server.impl.js.map +1 -1
  137. package/src/lib/tools/src/index.js +0 -2
  138. package/src/lib/utils/compilation-shared.d.ts +58 -0
  139. package/src/lib/utils/compilation-shared.js +133 -0
  140. package/src/lib/utils/compilation-shared.js.map +1 -0
  141. package/src/lib/utils/compiler-plugin-options.d.ts +11 -11
  142. package/src/lib/utils/debug-harness.d.ts +23 -0
  143. package/src/lib/utils/debug-harness.js +88 -0
  144. package/src/lib/utils/debug-harness.js.map +1 -0
  145. package/src/lib/utils/debug-log-file.d.ts +5 -0
  146. package/src/lib/utils/debug-log-file.js +56 -0
  147. package/src/lib/utils/debug-log-file.js.map +1 -0
  148. package/src/lib/utils/debug.d.ts +28 -0
  149. package/src/lib/utils/debug.js +39 -0
  150. package/src/lib/utils/debug.js.map +1 -0
  151. package/src/lib/utils/devkit.d.ts +6 -6
  152. package/src/lib/utils/devkit.js +34 -38
  153. package/src/lib/utils/devkit.js.map +1 -1
  154. package/src/lib/utils/hmr-candidates.d.ts +28 -28
  155. package/src/lib/utils/plugin-config.d.ts +37 -0
  156. package/src/lib/utils/plugin-config.js +71 -0
  157. package/src/lib/utils/plugin-config.js.map +1 -0
  158. package/src/lib/utils/rolldown.d.ts +2 -0
  159. package/src/lib/utils/rolldown.js +12 -0
  160. package/src/lib/utils/rolldown.js.map +1 -0
  161. package/src/lib/utils/safe-module-paths.d.ts +16 -0
  162. package/src/lib/utils/safe-module-paths.js +29 -0
  163. package/src/lib/utils/safe-module-paths.js.map +1 -0
  164. package/src/lib/utils/source-file-cache.d.ts +8 -15
  165. package/src/lib/utils/source-file-cache.js +35 -37
  166. package/src/lib/utils/source-file-cache.js.map +1 -1
  167. package/src/lib/utils/tailwind-reference.d.ts +12 -0
  168. package/src/lib/utils/tailwind-reference.js +99 -0
  169. package/src/lib/utils/tailwind-reference.js.map +1 -0
  170. package/src/lib/utils/tsconfig-resolver.d.ts +28 -0
  171. package/src/lib/utils/tsconfig-resolver.js +191 -0
  172. package/src/lib/utils/tsconfig-resolver.js.map +1 -0
  173. package/src/lib/utils/virtual-ids.d.ts +4 -0
  174. package/src/lib/utils/virtual-ids.js +23 -0
  175. package/src/lib/utils/virtual-ids.js.map +1 -0
  176. package/src/lib/utils/virtual-resources.d.ts +19 -0
  177. package/src/lib/utils/virtual-resources.js +38 -0
  178. package/src/lib/utils/virtual-resources.js.map +1 -0
  179. package/src/lib/virtual-modules-plugin.d.ts +5 -0
  180. package/src/lib/virtual-modules-plugin.js +23 -0
  181. package/src/lib/virtual-modules-plugin.js.map +1 -0
  182. package/src/test-setup.d.ts +2 -0
  183. package/setup-vitest.d.ts +0 -4
  184. package/setup-vitest.js +0 -215
  185. package/setup-vitest.js.map +0 -1
  186. package/src/lib/models.js +0 -1
  187. package/src/lib/models.js.map +0 -1
  188. package/src/lib/tools/README.md +0 -3
  189. package/src/lib/tools/src/index.d.ts +0 -0
  190. package/src/lib/tools/src/index.js.map +0 -1
  191. package/src/lib/utils/compiler-plugin-options.js +0 -1
  192. package/src/lib/utils/compiler-plugin-options.js.map +0 -1
  193. package/src/lib/utils/hmr-candidates.js +0 -272
  194. package/src/lib/utils/hmr-candidates.js.map +0 -1
@@ -1,982 +1,1246 @@
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 = {}));
31
- /**
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
- }
1
+ import { angularFullVersion, cjt, 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, debugCompiler, debugCompilerV, debugEmit, debugEmitV, debugHmr, debugHmrV, debugStyles, debugStylesV } from "./utils/debug.js";
5
+ import { jitPlugin } from "./angular-jit-plugin.js";
6
+ import { createCompilerPlugin, createRolldownCompilerPlugin } from "./compiler-plugin.js";
7
+ import { StyleUrlsResolver, TemplateUrlsResolver, getAngularComponentMetadata } from "./component-resolvers.js";
8
+ import { AnalogStylesheetRegistry, preprocessStylesheet, rewriteRelativeCssImports } from "./stylesheet-registry.js";
9
+ import { augmentHostWithCaching, augmentHostWithResources, augmentProgramWithVersioning, mergeTransformers } from "./host.js";
10
+ import { TS_EXT_REGEX, createTsConfigGetter, getTsConfigPath } from "./utils/plugin-config.js";
11
+ import { TsconfigResolver } from "./utils/tsconfig-resolver.js";
12
+ import { configureStylePipelineRegistry } from "./style-pipeline.js";
13
+ import { describeStylesheetContent, injectViteIgnoreForHmrMetadata, isIgnoredHmrFile, isTestWatchMode } from "./utils/compilation-shared.js";
14
+ import { toVirtualRawId } from "./utils/virtual-ids.js";
15
+ import { loadVirtualRawModule, rewriteHtmlRawImport } from "./utils/virtual-resources.js";
16
+ import { compilationAPIPlugin } from "./compilation-api/compilation-api-plugin.js";
17
+ import "./compilation-api/index.js";
18
+ import { markStylePathSafe } from "./utils/safe-module-paths.js";
19
+ import { fastCompilePlugin } from "./fast-compile-plugin.js";
20
+ import { removeActiveGraphMetadata, removeStyleOwnerMetadata, templateClassBindingGuardPlugin } from "./template-class-binding-guard-plugin.js";
21
+ import { buildStylePreprocessor, tailwindReferencePlugin, validateTailwindConfig } from "./tailwind-plugin.js";
22
+ import { encapsulationPlugin, getComponentStyleSheetMeta, isComponentStyleSheet } from "./encapsulation-plugin.js";
23
+ import { virtualModulesPlugin } from "./virtual-modules-plugin.js";
24
+ import { angularVitestPlugins } from "./angular-vitest-plugin.js";
25
+ import { pendingTasksPlugin } from "./angular-pending-tasks.plugin.js";
26
+ import { liveReloadPlugin } from "./live-reload-plugin.js";
27
+ import { nxFolderPlugin } from "./nx-folder-plugin.js";
28
+ import { replaceFiles } from "./plugins/file-replacements.plugin.js";
29
+ import { routerPlugin } from "./router-plugin.js";
30
+ import { union } from "es-toolkit";
31
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
32
+ import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
33
+ import * as compilerCli from "@angular/compiler-cli";
34
+ import { createRequire } from "node:module";
35
+ import { defaultClientConditions, normalizePath, preprocessCSS } from "vite";
36
+ //#region packages/vite-plugin-angular/src/lib/angular-vite-plugin.ts
37
+ var require = createRequire(import.meta.url);
38
+ var classNames = /* @__PURE__ */ new Map();
39
+ function evictDeletedFileMetadata(file, { removeActiveGraphMetadata, removeStyleOwnerMetadata, classNamesMap, fileTransformMap }) {
40
+ const normalizedFile = normalizePath(file.split("?")[0]);
41
+ removeActiveGraphMetadata(normalizedFile);
42
+ removeStyleOwnerMetadata(normalizedFile);
43
+ classNamesMap.delete(normalizedFile);
44
+ fileTransformMap.delete(normalizedFile);
45
+ }
46
+ function angular(options) {
47
+ applyDebugOption(options?.debug, options?.workspaceRoot);
48
+ const liveReload = options?.liveReload ?? true;
49
+ /**
50
+ * Normalize plugin options so defaults
51
+ * are used for values not provided.
52
+ */
53
+ const pluginOptions = {
54
+ tsconfigGetter: createTsConfigGetter(options?.tsconfig),
55
+ workspaceRoot: options?.workspaceRoot ?? process.cwd(),
56
+ inlineStylesExtension: options?.inlineStylesExtension ?? "css",
57
+ advanced: { tsTransformers: {
58
+ before: options?.advanced?.tsTransformers?.before ?? [],
59
+ after: options?.advanced?.tsTransformers?.after ?? [],
60
+ afterDeclarations: options?.advanced?.tsTransformers?.afterDeclarations ?? []
61
+ } },
62
+ supportedBrowsers: options?.supportedBrowsers ?? ["safari 15"],
63
+ jit: options?.jit,
64
+ include: options?.include ?? [],
65
+ additionalContentDirs: options?.additionalContentDirs ?? [],
66
+ liveReload,
67
+ disableTypeChecking: options?.disableTypeChecking ?? true,
68
+ fileReplacements: options?.fileReplacements ?? [],
69
+ useAngularCompilationAPI: options?.experimental?.useAngularCompilationAPI ?? false,
70
+ fastCompile: options?.fastCompile ?? false,
71
+ fastCompileMode: options?.fastCompileMode ?? "full",
72
+ hasTailwindCss: !!options?.tailwindCss,
73
+ tailwindCss: options?.tailwindCss,
74
+ stylePreprocessor: buildStylePreprocessor(options)
75
+ };
76
+ let resolvedConfig;
77
+ let tsConfigResolutionContext = null;
78
+ const ts = require("typescript");
79
+ let builder;
80
+ let nextProgram;
81
+ let cachedHost;
82
+ let cachedHostKey;
83
+ const isTest = process.env.NODE_ENV === "test" || !!process.env["VITEST"];
84
+ const tsconfigResolver = new TsconfigResolver({
85
+ workspaceRoot: pluginOptions.workspaceRoot,
86
+ include: pluginOptions.include,
87
+ liveReload: pluginOptions.liveReload,
88
+ hasTailwindCss: pluginOptions.hasTailwindCss,
89
+ isTest
90
+ });
91
+ function invalidateFsCaches() {
92
+ tsconfigResolver.invalidateIncludeCache();
93
+ }
94
+ function invalidateTsconfigCaches() {
95
+ tsconfigResolver.invalidateTsconfigCaches();
96
+ cachedHost = void 0;
97
+ cachedHostKey = void 0;
98
+ }
99
+ let watchMode = false;
100
+ let testWatchMode = isTestWatchMode();
101
+ const activeGraphComponentMetadata = /* @__PURE__ */ new Map();
102
+ const selectorOwners = /* @__PURE__ */ new Map();
103
+ const classNameOwners = /* @__PURE__ */ new Map();
104
+ const transformedStyleOwnerMetadata = /* @__PURE__ */ new Map();
105
+ const styleSourceOwners = /* @__PURE__ */ new Map();
106
+ function hasViteHmrTransport() {
107
+ return resolvedConfig ? resolvedConfig.server.hmr !== false : true;
108
+ }
109
+ function shouldEnableLiveReload() {
110
+ return !!((isTest ? testWatchMode : watchMode) && pluginOptions.liveReload && hasViteHmrTransport());
111
+ }
112
+ /**
113
+ * Determines whether Angular should externalize component styles.
114
+ *
115
+ * When true, Angular emits style references (hash-based IDs) instead of
116
+ * inlining CSS strings. Vite's resolveId → load → transform pipeline
117
+ * then serves these virtual modules, allowing @tailwindcss/vite to
118
+ * process @reference directives.
119
+ *
120
+ * Required for TWO independent use-cases:
121
+ * 1. HMR — Vite needs external modules for hot replacement
122
+ * 2. Tailwind CSS (hasTailwindCss) — styles must pass through Vite's
123
+ * CSS pipeline so @tailwindcss/vite can resolve @apply directives
124
+ *
125
+ * In production builds (!watchMode), styles are NOT externalized — they
126
+ * are inlined after preprocessCSS runs eagerly in transformStylesheet.
127
+ */
128
+ function shouldExternalizeStyles() {
129
+ if (!(isTest ? testWatchMode : watchMode)) return false;
130
+ return !!(shouldEnableLiveReload() || pluginOptions.hasTailwindCss);
131
+ }
132
+ function validateNoDuplicateAnalogPlugins(config) {
133
+ const analogInstances = (config.plugins ?? []).filter((p) => p.name === "@analogjs/vite-plugin-angular");
134
+ if (analogInstances.length > 1 && !config.build?.ssr) throw new Error(`[@analogjs/vite-plugin-angular] analog() is registered ${analogInstances.length} times. Each instance creates separate style maps, causing component styles to be lost. Remove duplicate registrations.`);
135
+ }
136
+ let stylesheetRegistry;
137
+ const sourceFileCache$1 = new sourceFileCache();
138
+ const isVitestVscode = !!process.env["VITEST_VSCODE"];
139
+ const isStackBlitz = !!process.versions["webcontainer"];
140
+ const isAstroIntegration = process.env["ANALOG_ASTRO"] === "true";
141
+ const jit = typeof pluginOptions?.jit !== "undefined" ? pluginOptions.jit : isTest;
142
+ let viteServer;
143
+ const styleUrlsResolver = new StyleUrlsResolver();
144
+ const guardContext = {
145
+ styleUrlsResolver,
146
+ activeGraphComponentMetadata,
147
+ selectorOwners,
148
+ classNameOwners,
149
+ transformedStyleOwnerMetadata,
150
+ styleSourceOwners
151
+ };
152
+ const templateUrlsResolver = new TemplateUrlsResolver();
153
+ let outputFile;
154
+ const outputFiles = /* @__PURE__ */ new Map();
155
+ const normalizeEmitterLookupId = (file) => {
156
+ const normalizedFile = normalizePath(file);
157
+ if (!normalizedFile.startsWith("/@fs/")) return normalizedFile;
158
+ return normalizePath(normalizedFile.slice(4).replace(/^\/([A-Za-z]:\/)/, "$1"));
159
+ };
160
+ const describeEmitMarkers = (content) => ({
161
+ contentLength: content.length,
162
+ hasCmp: content.includes("ɵcmp"),
163
+ hasFac: content.includes("ɵfac"),
164
+ hasProv: content.includes("ɵprov"),
165
+ hasDecorate: content.includes("__decorate"),
166
+ hasMetadata: content.includes("__metadata")
167
+ });
168
+ const fileEmitter = (file) => {
169
+ const normalizedFile = normalizeEmitterLookupId(file);
170
+ const hadCachedEmit = outputFiles.has(normalizedFile);
171
+ outputFile?.(normalizedFile);
172
+ const emittedResult = outputFiles.get(normalizedFile);
173
+ debugEmitV("fileEmitter lookup", {
174
+ requestFile: file,
175
+ normalizedFile,
176
+ hadCachedEmit,
177
+ hasOutputFileHook: !!outputFile,
178
+ emitted: !!emittedResult,
179
+ knownOutputCount: outputFiles.size,
180
+ contentLength: emittedResult?.content?.length ?? 0,
181
+ errorCount: emittedResult?.errors?.length ?? 0,
182
+ warningCount: emittedResult?.warnings?.length ?? 0
183
+ });
184
+ return emittedResult;
185
+ };
186
+ let initialCompilation = false;
187
+ const declarationFiles = [];
188
+ const fileTransformMap = /* @__PURE__ */ new Map();
189
+ let styleTransform;
190
+ let pendingCompilation;
191
+ let compilationLock = Promise.resolve();
192
+ function angularPlugin() {
193
+ let isProd = false;
194
+ if (angularFullVersion < 19e4 && pluginOptions.liveReload) {
195
+ debugHmr("hmr disabled: Angular version does not support HMR APIs", {
196
+ angularVersion: angularFullVersion,
197
+ isTest
198
+ });
199
+ console.warn("[@analogjs/vite-plugin-angular]: HMR was disabled because Angular v19+ is required for externalRuntimeStyles/_enableHmr support. Detected Angular version: %s.", angularFullVersion);
200
+ pluginOptions.liveReload = false;
201
+ }
202
+ if (isTest) {
203
+ pluginOptions.liveReload = false;
204
+ debugHmr("hmr disabled", {
205
+ angularVersion: angularFullVersion,
206
+ isTest
207
+ });
208
+ }
209
+ return {
210
+ name: "@analogjs/vite-plugin-angular",
211
+ async config(config, { command }) {
212
+ activateDeferredDebug(command);
213
+ watchMode = command === "serve";
214
+ isProd = config.mode === "production" || process.env.NODE_ENV === "production";
215
+ tsConfigResolutionContext = {
216
+ root: config.root || ".",
217
+ isProd,
218
+ isLib: !!config?.build?.lib
219
+ };
220
+ const preliminaryTsConfigPath = resolveTsConfigPath();
221
+ const esbuild = config.esbuild ?? false;
222
+ const oxc = config.oxc ?? false;
223
+ const defineOptions = {
224
+ ngJitMode: "false",
225
+ ngI18nClosureMode: "false",
226
+ ...watchMode ? {} : { ngDevMode: "false" }
227
+ };
228
+ const useRolldown = isRolldown();
229
+ const jsTransformConfigKey = getJsTransformConfigKey();
230
+ const jsTransformConfigValue = jsTransformConfigKey === "oxc" ? oxc : esbuild;
231
+ const rolldownOptions = { plugins: [createRolldownCompilerPlugin({
232
+ tsconfig: preliminaryTsConfigPath,
233
+ sourcemap: !isProd,
234
+ advancedOptimizations: isProd,
235
+ jit,
236
+ incremental: watchMode
237
+ }, !isAstroIntegration)] };
238
+ const esbuildOptions = {
239
+ plugins: [createCompilerPlugin({
240
+ tsconfig: preliminaryTsConfigPath,
241
+ sourcemap: !isProd,
242
+ advancedOptimizations: isProd,
243
+ jit,
244
+ incremental: watchMode
245
+ }, isTest, !isAstroIntegration)],
246
+ define: defineOptions
247
+ };
248
+ return {
249
+ [jsTransformConfigKey]: jsTransformConfigValue,
250
+ optimizeDeps: {
251
+ include: [
252
+ "rxjs/operators",
253
+ "rxjs",
254
+ "tslib"
255
+ ],
256
+ exclude: ["@angular/platform-server"],
257
+ ...useRolldown ? { rolldownOptions } : { esbuildOptions }
258
+ },
259
+ resolve: { conditions: ["style", ...config.resolve?.conditions || defaultClientConditions] }
260
+ };
261
+ },
262
+ configResolved(config) {
263
+ resolvedConfig = config;
264
+ if (config.logger?.warnOnce) {
265
+ const originalWarnOnce = config.logger.warnOnce;
266
+ config.logger.warnOnce = (msg, options) => {
267
+ if (typeof msg === "string" && msg.includes("Sourcemap") && msg.includes("node_modules")) return;
268
+ originalWarnOnce(msg, options);
269
+ };
270
+ }
271
+ if (pluginOptions.hasTailwindCss) {
272
+ validateTailwindConfig(pluginOptions.tailwindCss, config, watchMode);
273
+ validateNoDuplicateAnalogPlugins(config);
274
+ }
275
+ if (!jit) styleTransform = (code, filename) => preprocessCSS(code, filename, config);
276
+ if (isTest) testWatchMode = !(config.server.watch === null) || config.test?.watch === true || testWatchMode;
277
+ },
278
+ configureServer(server) {
279
+ viteServer = server;
280
+ const invalidateCompilationOnFsChange = createFsWatcherCacheInvalidator(invalidateFsCaches, invalidateTsconfigCaches, () => performCompilation(resolvedConfig));
281
+ server.watcher.on("add", invalidateCompilationOnFsChange);
282
+ server.watcher.on("unlink", (file) => {
283
+ evictDeletedFileMetadata(file, {
284
+ removeActiveGraphMetadata: (f) => removeActiveGraphMetadata(guardContext, f),
285
+ removeStyleOwnerMetadata: (f) => removeStyleOwnerMetadata(guardContext, f),
286
+ classNamesMap: classNames,
287
+ fileTransformMap
288
+ });
289
+ return invalidateCompilationOnFsChange();
290
+ });
291
+ server.watcher.on("change", (file) => {
292
+ if (file.includes("tsconfig")) invalidateTsconfigCaches();
293
+ });
294
+ },
295
+ async buildStart() {
296
+ if (!isVitestVscode) {
297
+ await performCompilation(resolvedConfig);
298
+ pendingCompilation = null;
299
+ initialCompilation = true;
300
+ }
301
+ },
302
+ async handleHotUpdate(ctx) {
303
+ if (isIgnoredHmrFile(ctx.file)) {
304
+ debugHmr("ignored file change", { file: ctx.file });
305
+ return [];
306
+ }
307
+ if (TS_EXT_REGEX.test(ctx.file)) {
308
+ const [fileId] = ctx.file.split("?");
309
+ debugHmr("TS file changed", {
310
+ file: ctx.file,
311
+ fileId
312
+ });
313
+ pendingCompilation = performCompilation(resolvedConfig, [fileId]);
314
+ let result;
315
+ if (shouldEnableLiveReload()) {
316
+ await pendingCompilation;
317
+ pendingCompilation = null;
318
+ result = fileEmitter(fileId);
319
+ debugHmr("TS file emitted", {
320
+ fileId,
321
+ hmrEligible: !!result?.hmrEligible,
322
+ hasClassName: !!classNames.get(fileId)
323
+ });
324
+ debugHmrV("ts hmr evaluation", {
325
+ file: ctx.file,
326
+ fileId,
327
+ hasResult: !!result,
328
+ hmrEligible: !!result?.hmrEligible,
329
+ hasClassName: !!classNames.get(fileId),
330
+ className: classNames.get(fileId),
331
+ updateCode: result?.hmrUpdateCode ? describeStylesheetContent(result.hmrUpdateCode) : void 0,
332
+ errors: result?.errors?.length ?? 0,
333
+ warnings: result?.warnings?.length ?? 0,
334
+ 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."
335
+ });
336
+ }
337
+ if (shouldEnableLiveReload() && result?.hmrEligible && classNames.get(fileId)) {
338
+ const relativeFileId = `${normalizePath(relative(process.cwd(), fileId))}@${classNames.get(fileId)}`;
339
+ debugHmr("sending component update", { relativeFileId });
340
+ debugHmrV("ts hmr component update payload", {
341
+ file: ctx.file,
342
+ fileId,
343
+ relativeFileId,
344
+ className: classNames.get(fileId)
345
+ });
346
+ sendHMRComponentUpdate(ctx.server, relativeFileId);
347
+ return ctx.modules.map((mod) => {
348
+ if (mod.id === ctx.file) return markModuleSelfAccepting(mod);
349
+ return mod;
350
+ });
351
+ }
352
+ }
353
+ if (/\.(html|htm|css|less|sass|scss)$/.test(ctx.file)) {
354
+ debugHmr("resource file changed", { file: ctx.file });
355
+ fileTransformMap.delete(ctx.file.split("?")[0]);
356
+ /**
357
+ * Check to see if this was a direct request
358
+ * for an external resource (styles, html).
359
+ */
360
+ const isDirect = fileModules.find((mod) => !!mod.id && mod.id.includes("?direct") && isModuleForChangedResource(mod, ctx.file, stylesheetRegistry));
361
+ const isInline = fileModules.find((mod) => !!mod.id && mod.id.includes("?inline") && isModuleForChangedResource(mod, ctx.file, stylesheetRegistry));
362
+ debugHmrV("resource direct/inline detection", {
363
+ file: ctx.file,
364
+ hasDirect: !!isDirect,
365
+ directId: isDirect?.id,
366
+ hasInline: !!isInline,
367
+ inlineId: isInline?.id
368
+ });
369
+ if (isDirect || isInline) {
370
+ if (shouldExternalizeStyles() && isDirect?.id && isDirect.file) {
371
+ const isComponentStyle = isDirect.type === "css" && isComponentStyleSheet(isDirect.id);
372
+ debugHmrV("resource direct branch", {
373
+ file: ctx.file,
374
+ directId: isDirect.id,
375
+ directType: isDirect.type,
376
+ shouldExternalize: shouldExternalizeStyles(),
377
+ isComponentStyle
378
+ });
379
+ if (isComponentStyle) {
380
+ const { encapsulation } = getComponentStyleSheetMeta(isDirect.id);
381
+ const wrapperModules = await findComponentStylesheetWrapperModules(ctx.server, ctx.file, isDirect, fileModules, stylesheetRegistry);
382
+ const stylesheetDiagnosis = diagnoseComponentStylesheetPipeline(ctx.file, isDirect, stylesheetRegistry, wrapperModules, pluginOptions.stylePreprocessor);
383
+ debugStylesV("HMR: component stylesheet changed", {
384
+ file: isDirect.file,
385
+ encapsulation
386
+ });
387
+ debugHmrV("component stylesheet wrapper modules", {
388
+ file: ctx.file,
389
+ wrapperCount: wrapperModules.length,
390
+ wrapperIds: wrapperModules.map((mod) => mod.id),
391
+ availableModuleIds: fileModules.map((mod) => mod.id)
392
+ });
393
+ debugHmrV("component stylesheet pipeline diagnosis", stylesheetDiagnosis);
394
+ ctx.server.moduleGraph.invalidateModule(isDirect);
395
+ debugHmrV("component stylesheet direct module invalidated", {
396
+ file: ctx.file,
397
+ directModuleId: isDirect.id,
398
+ directModuleUrl: isDirect.url,
399
+ reason: "Ensure Vite drops stale direct CSS transform results before wrapper or fallback handling continues."
400
+ });
401
+ const trackedWrapperRequestIds = stylesheetDiagnosis.trackedRequestIds.filter((id) => id.includes("?ngcomp="));
402
+ if (encapsulation !== "shadow" && (wrapperModules.length > 0 || trackedWrapperRequestIds.length > 0)) {
403
+ wrapperModules.forEach((mod) => ctx.server.moduleGraph.invalidateModule(mod));
404
+ debugHmrV("sending css-update for component stylesheet", {
405
+ file: ctx.file,
406
+ path: isDirect.url,
407
+ acceptedPath: isDirect.file,
408
+ wrapperCount: wrapperModules.length,
409
+ trackedWrapperRequestIds,
410
+ 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."
411
+ });
412
+ sendCssUpdate(ctx.server, {
413
+ path: isDirect.url,
414
+ acceptedPath: isDirect.file
415
+ });
416
+ logComponentStylesheetHmrOutcome({
417
+ file: ctx.file,
418
+ encapsulation,
419
+ diagnosis: stylesheetDiagnosis,
420
+ outcome: "css-update",
421
+ directModuleId: isDirect.id,
422
+ wrapperIds: wrapperModules.map((mod) => mod.id)
423
+ });
424
+ return union(fileModules.filter((mod) => {
425
+ return mod.file !== ctx.file || mod.id !== isDirect.id;
426
+ }).map((mod) => {
427
+ if (mod.file === ctx.file) return markModuleSelfAccepting(mod);
428
+ return mod;
429
+ }), wrapperModules.map((mod) => markModuleSelfAccepting(mod)));
430
+ }
431
+ debugHmrV("component stylesheet hmr fallback: full reload", {
432
+ file: ctx.file,
433
+ encapsulation,
434
+ reason: trackedWrapperRequestIds.length === 0 ? "missing-wrapper-module" : encapsulation === "shadow" ? "shadow-encapsulation" : "tracked-wrapper-still-not-patchable",
435
+ directId: isDirect.id,
436
+ trackedRequestIds: stylesheetRegistry?.getRequestIdsForSource(ctx.file) ?? []
437
+ });
438
+ const ownerModules = findStyleOwnerModules(ctx.server, ctx.file, styleSourceOwners);
439
+ debugHmrV("component stylesheet owner fallback lookup", {
440
+ file: ctx.file,
441
+ ownerCount: ownerModules.length,
442
+ ownerIds: ownerModules.map((mod) => mod.id),
443
+ ownerFiles: [...styleSourceOwners.get(normalizePath(ctx.file)) ?? []]
444
+ });
445
+ if (ownerModules.length > 0) {
446
+ pendingCompilation = performCompilation(resolvedConfig, [...ownerModules.map((mod) => mod.id).filter(Boolean)]);
447
+ await pendingCompilation;
448
+ pendingCompilation = null;
449
+ const updates = ownerModules.map((mod) => mod.id).filter((id) => !!id && !!classNames.get(id));
450
+ const derivedUpdates = ownerModules.map((mod) => mod.id).filter((id) => !!id).flatMap((ownerId) => resolveComponentClassNamesForStyleOwner(ownerId, ctx.file).map((className) => ({
451
+ ownerId,
452
+ className,
453
+ via: "raw-component-metadata"
454
+ })));
455
+ debugHmrV("component stylesheet owner fallback compilation", {
456
+ file: ctx.file,
457
+ ownerIds: ownerModules.map((mod) => mod.id),
458
+ updateIds: updates,
459
+ classNames: updates.map((id) => ({
460
+ id,
461
+ className: classNames.get(id)
462
+ })),
463
+ derivedUpdates
464
+ });
465
+ if (derivedUpdates.length > 0) debugHmrV("component stylesheet owner fallback derived updates", {
466
+ file: ctx.file,
467
+ updates: derivedUpdates,
468
+ hint: "Angular did not repopulate classNames during CSS-only owner recompilation, so Analog derived component identities from raw component metadata."
469
+ });
470
+ }
471
+ logComponentStylesheetHmrOutcome({
472
+ file: ctx.file,
473
+ encapsulation,
474
+ diagnosis: stylesheetDiagnosis,
475
+ outcome: "full-reload",
476
+ directModuleId: isDirect.id,
477
+ wrapperIds: wrapperModules.map((mod) => mod.id),
478
+ ownerIds: ownerModules.map((mod) => mod.id)
479
+ });
480
+ sendFullReload(ctx.server, {
481
+ file: ctx.file,
482
+ encapsulation,
483
+ reason: wrapperModules.length === 0 ? "missing-wrapper-module-and-no-owner-updates" : "shadow-encapsulation",
484
+ directId: isDirect.id,
485
+ trackedRequestIds: stylesheetRegistry?.getRequestIdsForSource(ctx.file) ?? []
486
+ });
487
+ return [];
488
+ }
489
+ }
490
+ return fileModules;
491
+ }
492
+ if (shouldEnableLiveReload() && /\.(html|htm)$/.test(ctx.file) && fileModules.length === 0) {
493
+ const ownerModules = findTemplateOwnerModules(ctx.server, ctx.file);
494
+ debugHmrV("template owner lookup", {
495
+ file: ctx.file,
496
+ ownerCount: ownerModules.length,
497
+ ownerIds: ownerModules.map((mod) => mod.id),
498
+ 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."
499
+ });
500
+ if (ownerModules.length > 0) {
501
+ const ownerIds = ownerModules.map((mod) => mod.id).filter(Boolean);
502
+ ownerModules.forEach((mod) => ctx.server.moduleGraph.invalidateModule(mod));
503
+ pendingCompilation = performCompilation(resolvedConfig, ownerIds);
504
+ await pendingCompilation;
505
+ pendingCompilation = null;
506
+ const updates = ownerIds.filter((id) => classNames.get(id));
507
+ debugHmrV("template owner recompilation result", {
508
+ file: ctx.file,
509
+ ownerIds,
510
+ updates,
511
+ updateClassNames: updates.map((id) => ({
512
+ id,
513
+ className: classNames.get(id)
514
+ })),
515
+ hint: updates.length > 0 ? "External template recompilation produced Angular component update targets." : "External template recompilation completed, but no Angular component update targets were surfaced."
516
+ });
517
+ if (updates.length > 0) {
518
+ debugHmr("template owner module invalidation", {
519
+ file: ctx.file,
520
+ ownerIds,
521
+ updateCount: updates.length
522
+ });
523
+ updates.forEach((updateId) => {
524
+ const relativeFileId = `${normalizePath(relative(process.cwd(), updateId))}@${classNames.get(updateId)}`;
525
+ sendHMRComponentUpdate(ctx.server, relativeFileId);
526
+ });
527
+ return ownerModules.map((mod) => markModuleSelfAccepting(mod));
528
+ }
529
+ }
530
+ }
531
+ const mods = [];
532
+ const updates = [];
533
+ fileModules.forEach((mod) => {
534
+ mod.importers.forEach((imp) => {
535
+ ctx.server.moduleGraph.invalidateModule(imp);
536
+ if (shouldExternalizeStyles() && classNames.get(imp.id)) updates.push(imp.id);
537
+ else mods.push(imp);
538
+ });
539
+ });
540
+ debugHmrV("resource importer analysis", {
541
+ file: ctx.file,
542
+ fileModuleCount: fileModules.length,
543
+ importerCount: fileModules.reduce((count, mod) => count + mod.importers.size, 0),
544
+ updates,
545
+ mods: mods.map((mod) => mod.id)
546
+ });
547
+ pendingCompilation = performCompilation(resolvedConfig, [...mods.map((mod) => mod.id).filter(Boolean), ...updates]);
548
+ if (updates.length > 0) {
549
+ await pendingCompilation;
550
+ pendingCompilation = null;
551
+ debugHmr("resource importer component updates", {
552
+ file: ctx.file,
553
+ updateCount: updates.length
554
+ });
555
+ updates.forEach((updateId) => {
556
+ const impRelativeFileId = `${normalizePath(relative(process.cwd(), updateId))}@${classNames.get(updateId)}`;
557
+ sendHMRComponentUpdate(ctx.server, impRelativeFileId);
558
+ });
559
+ return fileModules.map((mod) => {
560
+ if (mod.id === ctx.file) return markModuleSelfAccepting(mod);
561
+ return mod;
562
+ });
563
+ }
564
+ return mods;
565
+ }
566
+ debugHmr("full reload — unrecognized file type", { file: ctx.file });
567
+ classNames.clear();
568
+ return ctx.modules;
569
+ },
570
+ resolveId(id, importer) {
571
+ if (id.startsWith("virtual:@analogjs/vite-plugin-angular:raw:")) return `\0${id}`;
572
+ if (jit && id.startsWith("angular:jit:")) {
573
+ const filePath = normalizePath(resolve(dirname(importer), id.split(";")[1]));
574
+ if (id.includes(":style")) {
575
+ markStylePathSafe(resolvedConfig, filePath);
576
+ return filePath + "?inline";
577
+ }
578
+ return toVirtualRawId(filePath);
579
+ }
580
+ const rawRewrite = rewriteHtmlRawImport(id, importer);
581
+ if (rawRewrite) return rawRewrite;
582
+ if (/\.(css|scss|sass|less)\?inline$/.test(id) && importer) {
583
+ const filePath = id.split("?")[0];
584
+ const resolved = isAbsolute(filePath) ? normalizePath(filePath) : normalizePath(resolve(dirname(importer), filePath));
585
+ markStylePathSafe(resolvedConfig, resolved);
586
+ return resolved + "?inline";
587
+ }
588
+ if (isComponentStyleSheet(id)) {
589
+ const filename = getFilenameFromPath(id);
590
+ if (stylesheetRegistry?.hasServed(filename)) {
591
+ debugStylesV("resolveId: kept preprocessed ID", { filename });
592
+ return id;
593
+ }
594
+ const componentStyles = stylesheetRegistry?.resolveExternalSource(filename);
595
+ if (componentStyles) {
596
+ debugStylesV("resolveId: mapped external stylesheet", {
597
+ filename,
598
+ resolvedPath: componentStyles
599
+ });
600
+ return componentStyles + new URL(id, "http://localhost").search;
601
+ }
602
+ debugStyles("resolveId: component stylesheet NOT FOUND in either map", {
603
+ filename,
604
+ inlineMapSize: stylesheetRegistry?.servedCount ?? 0,
605
+ externalMapSize: stylesheetRegistry?.externalCount ?? 0
606
+ });
607
+ }
608
+ },
609
+ async load(id) {
610
+ const rawModule = await loadVirtualRawModule(this, id);
611
+ if (rawModule !== void 0) return rawModule;
612
+ if (/\.(css|scss|sass|less)\?inline$/.test(id)) markStylePathSafe(resolvedConfig, id.split("?")[0]);
613
+ if (isComponentStyleSheet(id)) {
614
+ const filename = getFilenameFromPath(id);
615
+ const componentStyles = stylesheetRegistry?.getServedContent(filename);
616
+ if (componentStyles) {
617
+ stylesheetRegistry?.registerActiveRequest(id);
618
+ debugHmrV("stylesheet active request registered", {
619
+ requestId: id,
620
+ filename,
621
+ sourcePath: stylesheetRegistry?.resolveExternalSource(filename) ?? stylesheetRegistry?.resolveExternalSource(filename.replace(/^\//, "")),
622
+ trackedRequestIds: stylesheetRegistry?.getRequestIdsForSource(stylesheetRegistry?.resolveExternalSource(filename) ?? stylesheetRegistry?.resolveExternalSource(filename.replace(/^\//, "")) ?? "") ?? []
623
+ });
624
+ debugStylesV("load: served inline component stylesheet", {
625
+ filename,
626
+ length: componentStyles.length,
627
+ requestId: id,
628
+ ...describeStylesheetContent(componentStyles)
629
+ });
630
+ return componentStyles;
631
+ }
632
+ }
633
+ },
634
+ transform: {
635
+ filter: { id: {
636
+ include: [TS_EXT_REGEX],
637
+ exclude: [
638
+ /node_modules/,
639
+ "type=script",
640
+ "@ng/component"
641
+ ]
642
+ } },
643
+ async handler(code, id) {
644
+ /**
645
+ * Check for options.transformFilter
646
+ */
647
+ if (options?.transformFilter && !(options?.transformFilter(code, id) ?? true)) return;
648
+ /**
649
+ * Skip transforming content files
650
+ */
651
+ if (id.includes("?") && id.includes("analog-content-")) return;
652
+ if (id.includes(".ts?")) id = id.replace(/\?(.*)/, "");
653
+ fileTransformMap.set(id, code);
654
+ /**
655
+ * Re-analyze on each transform
656
+ * for test(Vitest)
657
+ */
658
+ if (isTest) {
659
+ if (isVitestVscode && !initialCompilation) {
660
+ pendingCompilation = performCompilation(resolvedConfig);
661
+ initialCompilation = true;
662
+ }
663
+ const tsMod = viteServer?.moduleGraph.getModuleById(id);
664
+ if (tsMod) {
665
+ const invalidated = tsMod.lastInvalidationTimestamp;
666
+ if (testWatchMode && invalidated) pendingCompilation = performCompilation(resolvedConfig, [id]);
667
+ }
668
+ }
669
+ const hasComponent = code.includes("@Component");
670
+ debugCompilerV("transform", {
671
+ id,
672
+ codeLength: code.length,
673
+ hasComponent
674
+ });
675
+ const templateUrls = hasComponent ? templateUrlsResolver.resolve(code, id) : [];
676
+ const styleUrls = hasComponent ? styleUrlsResolver.resolve(code, id) : [];
677
+ if (hasComponent && watchMode) for (const urlSet of [...templateUrls, ...styleUrls]) {
678
+ const [, absoluteFileUrl] = urlSet.split("|");
679
+ this.addWatchFile(absoluteFileUrl);
680
+ }
681
+ if (pendingCompilation) {
682
+ await pendingCompilation;
683
+ pendingCompilation = null;
684
+ }
685
+ const typescriptResult = fileEmitter(id);
686
+ if (!typescriptResult) {
687
+ debugCompilerV("transform skip (file not emitted by Angular)", { id });
688
+ const isAngular = !id.includes("@ng/component") && /(Component|Directive|Pipe|Injectable|NgModule)\(/.test(code);
689
+ debugEmit("transform emit miss", {
690
+ id,
691
+ normalizedId: normalizeEmitterLookupId(id),
692
+ knownOutputCount: outputFiles.size,
693
+ hasOutputFileHook: !!outputFile,
694
+ isAngular
695
+ });
696
+ 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.`);
697
+ return;
698
+ }
699
+ if (typescriptResult.warnings && typescriptResult.warnings.length > 0) this.warn(`${typescriptResult.warnings.join("\n")}`);
700
+ if (typescriptResult.errors && typescriptResult.errors.length > 0) this.error(`${typescriptResult.errors.join("\n")}`);
701
+ let data = typescriptResult.content ?? "";
702
+ debugEmitV("transform emit hit", {
703
+ id,
704
+ normalizedId: normalizeEmitterLookupId(id),
705
+ ...describeEmitMarkers(data),
706
+ errorCount: typescriptResult.errors?.length ?? 0,
707
+ warningCount: typescriptResult.warnings?.length ?? 0
708
+ });
709
+ if (jit && data.includes("angular:jit:")) {
710
+ data = data.replace(/angular:jit:style:inline;/g, "virtual:angular:jit:style:inline;");
711
+ templateUrls.forEach((templateUrlSet) => {
712
+ const [templateFile, resolvedTemplateUrl] = templateUrlSet.split("|");
713
+ data = data.replace(`angular:jit:template:file;${templateFile}`, toVirtualRawId(resolvedTemplateUrl));
714
+ });
715
+ styleUrls.forEach((styleUrlSet) => {
716
+ const [styleFile, resolvedStyleUrl] = styleUrlSet.split("|");
717
+ markStylePathSafe(resolvedConfig, resolvedStyleUrl);
718
+ data = data.replace(`angular:jit:style:file;${styleFile}`, resolvedStyleUrl + "?inline");
719
+ });
720
+ }
721
+ if (data.includes("HmrLoad")) {
722
+ const hasMetaUrl = data.includes("getReplaceMetadataURL");
723
+ debugHmrV("vite-ignore injection", {
724
+ id,
725
+ dataLength: data.length,
726
+ hasMetaUrl
727
+ });
728
+ if (hasMetaUrl) {
729
+ const patched = injectViteIgnoreForHmrMetadata(data);
730
+ if (patched !== data && !patched.includes("@vite-ignore")) debugHmrV("vite-ignore regex fallback", { id });
731
+ data = patched;
732
+ }
733
+ }
734
+ return {
735
+ code: data,
736
+ map: null
737
+ };
738
+ }
739
+ },
740
+ closeBundle() {
741
+ declarationFiles.forEach(({ declarationFileDir, declarationPath, data }) => {
742
+ mkdirSync(declarationFileDir, { recursive: true });
743
+ writeFileSync(declarationPath, data, "utf-8");
744
+ });
745
+ }
746
+ };
747
+ }
748
+ const compilationPlugin = pluginOptions.useAngularCompilationAPI ? compilationAPIPlugin({
749
+ tsconfigGetter: pluginOptions.tsconfigGetter,
750
+ workspaceRoot: pluginOptions.workspaceRoot,
751
+ inlineStylesExtension: pluginOptions.inlineStylesExtension,
752
+ jit,
753
+ liveReload: pluginOptions.liveReload,
754
+ disableTypeChecking: pluginOptions.disableTypeChecking,
755
+ supportedBrowsers: pluginOptions.supportedBrowsers,
756
+ transformFilter: options?.transformFilter,
757
+ fileReplacements: pluginOptions.fileReplacements,
758
+ stylePreprocessor: pluginOptions.stylePreprocessor,
759
+ stylePipeline: options?.stylePipeline,
760
+ hasTailwindCss: pluginOptions.hasTailwindCss,
761
+ tailwindCss: pluginOptions.tailwindCss,
762
+ isTest,
763
+ isAstroIntegration,
764
+ include: pluginOptions.include,
765
+ additionalContentDirs: pluginOptions.additionalContentDirs,
766
+ debug: options?.debug
767
+ }) : pluginOptions.fastCompile ? fastCompilePlugin({
768
+ tsconfigGetter: pluginOptions.tsconfigGetter,
769
+ workspaceRoot: pluginOptions.workspaceRoot,
770
+ inlineStylesExtension: pluginOptions.inlineStylesExtension,
771
+ jit,
772
+ liveReload: pluginOptions.liveReload,
773
+ supportedBrowsers: pluginOptions.supportedBrowsers,
774
+ transformFilter: options?.transformFilter,
775
+ isTest,
776
+ isAstroIntegration,
777
+ fastCompileMode: pluginOptions.fastCompileMode
778
+ }) : angularPlugin();
779
+ return [
780
+ replaceFiles(pluginOptions.fileReplacements, pluginOptions.workspaceRoot),
781
+ virtualModulesPlugin({ jit }),
782
+ templateClassBindingGuardPlugin(guardContext),
783
+ pluginOptions.hasTailwindCss && tailwindReferencePlugin({ tailwindCss: pluginOptions.tailwindCss }),
784
+ pluginOptions.liveReload && liveReloadPlugin({
785
+ classNames,
786
+ fileEmitter
787
+ }),
788
+ compilationPlugin,
789
+ !pluginOptions.fastCompile && pluginOptions.liveReload && liveReloadPlugin({
790
+ classNames,
791
+ fileEmitter
792
+ }),
793
+ ...isTest && !isStackBlitz ? angularVitestPlugins() : [],
794
+ jit && jitPlugin({ inlineStylesExtension: pluginOptions.inlineStylesExtension }),
795
+ buildOptimizerPlugin({
796
+ supportedBrowsers: pluginOptions.supportedBrowsers,
797
+ jit
798
+ }),
799
+ routerPlugin(),
800
+ angularFullVersion < 190004 && pendingTasksPlugin(),
801
+ nxFolderPlugin(),
802
+ encapsulationPlugin(shouldExternalizeStyles)
803
+ ].filter(Boolean);
804
+ function resolveTsConfigPath() {
805
+ const tsconfigValue = pluginOptions.tsconfigGetter();
806
+ return getTsConfigPath(tsConfigResolutionContext.root, tsconfigValue, tsConfigResolutionContext.isProd, isTest, tsConfigResolutionContext.isLib);
807
+ }
808
+ async function performCompilation(config, ids) {
809
+ let resolve;
810
+ const previousLock = compilationLock;
811
+ compilationLock = new Promise((r) => {
812
+ resolve = r;
813
+ });
814
+ try {
815
+ await previousLock;
816
+ await _doPerformCompilation(config, ids);
817
+ } finally {
818
+ resolve();
819
+ }
820
+ }
821
+ /**
822
+ * This method share mutable state and performs the actual compilation work.
823
+ * It should not be called concurrently. Use `performCompilation` which wraps this method in a lock to ensure only one compilation runs at a time.
824
+ */
825
+ async function _doPerformCompilation(config, ids) {
826
+ const isProd = config.mode === "production";
827
+ const modifiedFiles = new Set(ids ?? []);
828
+ sourceFileCache$1.invalidate(modifiedFiles);
829
+ if (ids?.length) for (const id of ids || []) fileTransformMap.delete(id);
830
+ const resolvedTsConfigPath = resolveTsConfigPath();
831
+ const cached = tsconfigResolver.getCachedTsconfigOptions(resolvedTsConfigPath, config);
832
+ const tsCompilerOptions = { ...cached.options };
833
+ let rootNames = [...cached.rootNames];
834
+ if (shouldExternalizeStyles()) tsCompilerOptions["externalRuntimeStyles"] = true;
835
+ if (shouldEnableLiveReload()) {
836
+ tsCompilerOptions["_enableHmr"] = true;
837
+ tsCompilerOptions["supportTestBed"] = true;
838
+ }
839
+ debugCompiler("tsCompilerOptions (NgtscProgram path)", {
840
+ liveReload: pluginOptions.liveReload,
841
+ viteHmr: hasViteHmrTransport(),
842
+ shouldExternalize: shouldExternalizeStyles(),
843
+ externalRuntimeStyles: !!tsCompilerOptions["externalRuntimeStyles"],
844
+ hmrEnabled: !!tsCompilerOptions["_enableHmr"]
845
+ });
846
+ if (tsCompilerOptions["compilationMode"] === "partial") {
847
+ tsCompilerOptions["supportTestBed"] = true;
848
+ tsCompilerOptions["supportJitMode"] = true;
849
+ }
850
+ if (!isTest && config.build?.lib) {
851
+ tsCompilerOptions["declaration"] = true;
852
+ tsCompilerOptions["declarationMap"] = watchMode;
853
+ tsCompilerOptions["inlineSources"] = true;
854
+ }
855
+ if (isTest) tsCompilerOptions["supportTestBed"] = true;
856
+ const replacements = pluginOptions.fileReplacements.map((rp) => join(pluginOptions.workspaceRoot, rp.ssr || rp.with));
857
+ rootNames = union(rootNames, tsconfigResolver.ensureIncludeCache(), replacements);
858
+ const hostKey = JSON.stringify(tsCompilerOptions);
859
+ let host;
860
+ if (cachedHost && cachedHostKey === hostKey) host = cachedHost;
861
+ else {
862
+ host = ts.createIncrementalCompilerHost(tsCompilerOptions, {
863
+ ...ts.sys,
864
+ readFile(path, encoding) {
865
+ if (fileTransformMap.has(path)) return fileTransformMap.get(path);
866
+ const file = ts.sys.readFile.call(null, path, encoding);
867
+ if (file) fileTransformMap.set(path, file);
868
+ return file;
869
+ }
870
+ });
871
+ cachedHost = host;
872
+ cachedHostKey = hostKey;
873
+ if (watchMode) augmentHostWithCaching(host, sourceFileCache$1);
874
+ }
875
+ if (!jit) {
876
+ const externalizeStyles = !!tsCompilerOptions["externalRuntimeStyles"];
877
+ stylesheetRegistry = externalizeStyles ? new AnalogStylesheetRegistry() : void 0;
878
+ if (stylesheetRegistry) configureStylePipelineRegistry(pluginOptions.stylePipeline, stylesheetRegistry, { workspaceRoot: pluginOptions.workspaceRoot });
879
+ debugStyles("stylesheet registry initialized (NgtscProgram path)", { externalizeStyles });
880
+ augmentHostWithResources(host, styleTransform, {
881
+ inlineStylesExtension: pluginOptions.inlineStylesExtension,
882
+ isProd,
883
+ stylesheetRegistry,
884
+ sourceFileCache: sourceFileCache$1,
885
+ stylePreprocessor: pluginOptions.stylePreprocessor
886
+ });
887
+ }
888
+ /**
889
+ * Creates a new NgtscProgram to analyze/re-analyze
890
+ * the source files and create a file emitter.
891
+ * This is shared between an initial build and a hot update.
892
+ */
893
+ let typeScriptProgram;
894
+ let angularCompiler;
895
+ const oldBuilder = builder ?? ts.readBuilderProgram(tsCompilerOptions, host);
896
+ if (!jit) {
897
+ const angularProgram = new compilerCli.NgtscProgram(rootNames, tsCompilerOptions, host, nextProgram);
898
+ angularCompiler = angularProgram.compiler;
899
+ typeScriptProgram = angularProgram.compiler.getCurrentProgram();
900
+ augmentProgramWithVersioning(typeScriptProgram);
901
+ builder = ts.createEmitAndSemanticDiagnosticsBuilderProgram(typeScriptProgram, host, oldBuilder);
902
+ nextProgram = angularProgram;
903
+ } else {
904
+ builder = ts.createEmitAndSemanticDiagnosticsBuilderProgram(rootNames, tsCompilerOptions, host, oldBuilder);
905
+ typeScriptProgram = builder.getProgram();
906
+ }
907
+ if (!watchMode) builder = ts.createAbstractBuilder(typeScriptProgram, host, oldBuilder);
908
+ if (angularCompiler) await angularCompiler.analyzeAsync();
909
+ const transformers = mergeTransformers({ before: jit ? [compilerCli.constructorParametersDownlevelTransform(builder.getProgram()), cjt(() => builder.getProgram().getTypeChecker())] : [] }, jit ? {} : angularCompiler.prepareEmit().transformers);
910
+ const fileMetadata = getFileMetadata(builder, angularCompiler, pluginOptions.liveReload, pluginOptions.disableTypeChecking);
911
+ const writeFileCallback = (_filename, content, _a, _b, sourceFiles) => {
912
+ if (!sourceFiles?.length) return;
913
+ const filename = normalizePath(sourceFiles[0].fileName);
914
+ if (filename.includes("ngtypecheck.ts") || filename.includes(".d.")) return;
915
+ const metadata = watchMode ? fileMetadata(filename) : {};
916
+ outputFiles.set(filename, {
917
+ content,
918
+ dependencies: [],
919
+ errors: metadata.errors,
920
+ warnings: metadata.warnings,
921
+ hmrUpdateCode: metadata.hmrUpdateCode,
922
+ hmrEligible: metadata.hmrEligible
923
+ });
924
+ debugEmitV("registered ngtsc output", {
925
+ filename,
926
+ ...describeEmitMarkers(content),
927
+ errorCount: metadata.errors?.length ?? 0,
928
+ warningCount: metadata.warnings?.length ?? 0,
929
+ hmrEligible: !!metadata.hmrEligible,
930
+ knownOutputCount: outputFiles.size
931
+ });
932
+ };
933
+ const writeOutputFile = (id) => {
934
+ const sourceFile = builder.getSourceFile(id);
935
+ if (!sourceFile) return;
936
+ let content = "";
937
+ builder.emit(sourceFile, (filename, data) => {
938
+ if (/\.[cm]?js$/.test(filename)) content = data;
939
+ if (!watchMode && !isTest && /\.d\.ts/.test(filename) && !filename.includes(".ngtypecheck.")) {
940
+ const declarationPath = resolve(config.root, config.build.outDir, relative(config.root, filename)).replace("/src/", "/");
941
+ const declarationFileDir = declarationPath.replace(basename(filename), "").replace("/src/", "/");
942
+ declarationFiles.push({
943
+ declarationFileDir,
944
+ declarationPath,
945
+ data
946
+ });
947
+ }
948
+ }, void 0, void 0, transformers);
949
+ writeFileCallback(id, content, false, void 0, [sourceFile]);
950
+ if (angularCompiler) angularCompiler.incrementalCompilation.recordSuccessfulEmit(sourceFile);
951
+ };
952
+ if (watchMode) {
953
+ if (ids && ids.length > 0) ids.forEach((id) => writeOutputFile(id));
954
+ else if (isTest) while (builder.emitNextAffectedFile(writeFileCallback, void 0, void 0, transformers));
955
+ }
956
+ if (!isTest)
957
+ /**
958
+ * Perf: Output files on demand so the dev server
959
+ * isn't blocked when emitting files.
960
+ */
961
+ outputFile = writeOutputFile;
962
+ }
963
+ }
964
+ function createFsWatcherCacheInvalidator(invalidateFsCaches, invalidateTsconfigCaches, performCompilation) {
965
+ return async () => {
966
+ invalidateFsCaches();
967
+ invalidateTsconfigCaches();
968
+ await performCompilation();
969
+ };
970
+ }
971
+ function isModuleForChangedResource(mod, changedFile, stylesheetRegistry) {
972
+ const normalizedChangedFile = normalizePath(changedFile.split("?")[0]);
973
+ if (normalizePath((mod.file ?? "").split("?")[0]) === normalizedChangedFile) return true;
974
+ if (!mod.id) return false;
975
+ const requestPath = getFilenameFromPath(mod.id);
976
+ return normalizePath((stylesheetRegistry?.resolveExternalSource(requestPath) ?? stylesheetRegistry?.resolveExternalSource(requestPath.replace(/^\//, "")) ?? "").split("?")[0]) === normalizedChangedFile;
977
+ }
978
+ function diagnoseComponentStylesheetPipeline(changedFile, directModule, stylesheetRegistry, wrapperModules, stylePreprocessor) {
979
+ const normalizedFile = normalizePath(changedFile.split("?")[0]);
980
+ const sourceExists = existsSync(normalizedFile);
981
+ const sourceCode = sourceExists ? readFileSync(normalizedFile, "utf-8") : void 0;
982
+ const directRequestPath = directModule.id ? getFilenameFromPath(directModule.id) : void 0;
983
+ const sourcePath = directRequestPath ? stylesheetRegistry?.resolveExternalSource(directRequestPath) ?? stylesheetRegistry?.resolveExternalSource(directRequestPath.replace(/^\//, "")) : normalizedFile;
984
+ const registryCode = directRequestPath ? stylesheetRegistry?.getServedContent(directRequestPath) : void 0;
985
+ const trackedRequestIds = stylesheetRegistry?.getRequestIdsForSource(sourcePath ?? "") ?? [];
986
+ const dependencies = stylesheetRegistry?.getDependenciesForSource(sourcePath ?? "") ?? [];
987
+ const diagnostics = stylesheetRegistry?.getDiagnosticsForSource(sourcePath ?? "") ?? [];
988
+ const tags = stylesheetRegistry?.getTagsForSource(sourcePath ?? "") ?? [];
989
+ const anomalies = [];
990
+ const hints = [];
991
+ if (!sourceExists) {
992
+ anomalies.push("source_file_missing");
993
+ hints.push("The stylesheet watcher fired for a file that no longer exists on disk.");
994
+ }
995
+ if (!registryCode) {
996
+ anomalies.push("registry_content_missing");
997
+ hints.push("The stylesheet registry has no served content for the direct module request path.");
998
+ }
999
+ if (sourceCode && registryCode) {
1000
+ let expectedRegistryCode = preprocessStylesheet(sourceCode, normalizedFile, stylePreprocessor);
1001
+ expectedRegistryCode = rewriteRelativeCssImports(expectedRegistryCode, normalizedFile);
1002
+ if (describeStylesheetContent(expectedRegistryCode).digest !== describeStylesheetContent(registryCode).digest) {
1003
+ anomalies.push("source_registry_mismatch");
1004
+ hints.push("The source file changed, but the served stylesheet content in the registry is still stale.");
1005
+ }
1006
+ }
1007
+ if (trackedRequestIds.length === 0) {
1008
+ anomalies.push("no_tracked_requests");
1009
+ hints.push("No live stylesheet requests are tracked for this source file, so HMR has no browser-facing target.");
1010
+ }
1011
+ if (trackedRequestIds.some((id) => id.includes("?ngcomp=")) && wrapperModules.length === 0) {
1012
+ anomalies.push("tracked_wrapper_missing_from_module_graph");
1013
+ hints.push("A wrapper request id is known, but Vite did not expose a live wrapper module during this HMR pass.");
1014
+ }
1015
+ if (trackedRequestIds.every((id) => !id.includes("?ngcomp=")) && wrapperModules.length === 0) {
1016
+ anomalies.push("wrapper_not_yet_tracked");
1017
+ hints.push("Only direct stylesheet requests were tracked during this HMR pass; the wrapper request may be appearing too late.");
1018
+ }
1019
+ return {
1020
+ file: changedFile,
1021
+ sourcePath,
1022
+ source: sourceCode ? describeStylesheetContent(rewriteRelativeCssImports(preprocessStylesheet(sourceCode, normalizedFile, stylePreprocessor), normalizedFile)) : void 0,
1023
+ registry: registryCode ? describeStylesheetContent(registryCode) : void 0,
1024
+ dependencies,
1025
+ diagnostics,
1026
+ tags,
1027
+ directModuleId: directModule.id,
1028
+ directModuleUrl: directModule.url,
1029
+ trackedRequestIds,
1030
+ wrapperCount: wrapperModules.length,
1031
+ anomalies,
1032
+ hints
1033
+ };
1034
+ }
1035
+ async function findComponentStylesheetWrapperModules(server, changedFile, directModule, fileModules, stylesheetRegistry) {
1036
+ const wrapperModules = /* @__PURE__ */ new Map();
1037
+ for (const mod of fileModules) if (mod.id && mod.type === "js" && isComponentStyleSheet(mod.id) && isModuleForChangedResource(mod, changedFile, stylesheetRegistry)) wrapperModules.set(mod.id, mod);
1038
+ const directRequestIds = /* @__PURE__ */ new Set();
1039
+ if (directModule.id) directRequestIds.add(directModule.id);
1040
+ if (directModule.url) directRequestIds.add(directModule.url);
1041
+ const requestPath = directModule.id ? getFilenameFromPath(directModule.id) : void 0;
1042
+ const sourcePath = requestPath ? stylesheetRegistry?.resolveExternalSource(requestPath) ?? stylesheetRegistry?.resolveExternalSource(requestPath.replace(/^\//, "")) : void 0;
1043
+ for (const requestId of stylesheetRegistry?.getRequestIdsForSource(sourcePath ?? "") ?? []) if (requestId.includes("?ngcomp=")) directRequestIds.add(requestId);
1044
+ const candidateWrapperIds = [...directRequestIds].filter((id) => id.includes("?direct&ngcomp=")).map((id) => id.replace("?direct&ngcomp=", "?ngcomp="));
1045
+ const lookupHits = [];
1046
+ for (const candidate of candidateWrapperIds) {
1047
+ const mod = await server.moduleGraph.getModuleByUrl(candidate) ?? server.moduleGraph.getModuleById(candidate);
1048
+ lookupHits.push({
1049
+ candidate,
1050
+ via: mod?.url === candidate ? "url" : mod ? "id" : void 0,
1051
+ moduleId: mod?.id,
1052
+ moduleType: mod?.type
1053
+ });
1054
+ if (mod?.id && mod.type === "js" && isComponentStyleSheet(mod.id) && isModuleForChangedResource(mod, changedFile, stylesheetRegistry)) wrapperModules.set(mod.id, mod);
1055
+ }
1056
+ debugHmrV("component stylesheet wrapper lookup", {
1057
+ file: changedFile,
1058
+ sourcePath,
1059
+ directModuleId: directModule.id,
1060
+ directModuleUrl: directModule.url,
1061
+ candidateWrapperIds,
1062
+ lookupHits
1063
+ });
1064
+ if (wrapperModules.size === 0) debugHmrV("component stylesheet wrapper lookup empty", {
1065
+ file: changedFile,
1066
+ sourcePath,
1067
+ directModuleId: directModule.id,
1068
+ directModuleUrl: directModule.url,
1069
+ candidateWrapperIds
1070
+ });
1071
+ return [...wrapperModules.values()];
864
1072
  }
865
1073
  function sendHMRComponentUpdate(server, id) {
866
- server.ws.send('angular:component-update', {
867
- id: encodeURIComponent(id),
868
- timestamp: Date.now(),
869
- });
870
- classNames.delete(id);
1074
+ debugHmrV("ws send: angular component update", {
1075
+ id,
1076
+ timestamp: Date.now()
1077
+ });
1078
+ server.ws.send("angular:component-update", {
1079
+ id: encodeURIComponent(id),
1080
+ timestamp: Date.now()
1081
+ });
1082
+ classNames.delete(id);
871
1083
  }
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
- };
1084
+ function sendCssUpdate(server, update) {
1085
+ const timestamp = Date.now();
1086
+ debugHmrV("ws send: css-update", {
1087
+ ...update,
1088
+ timestamp
1089
+ });
1090
+ server.ws.send({
1091
+ type: "update",
1092
+ updates: [{
1093
+ type: "css-update",
1094
+ timestamp,
1095
+ path: update.path,
1096
+ acceptedPath: update.acceptedPath
1097
+ }]
1098
+ });
903
1099
  }
904
- 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
- ];
1100
+ function sendFullReload(server, details) {
1101
+ debugHmrV("ws send: full-reload", details);
1102
+ server.ws.send("analog:debug-full-reload", details);
1103
+ server.ws.send({ type: "full-reload" });
920
1104
  }
921
- 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
- };
1105
+ function resolveComponentClassNamesForStyleOwner(ownerFile, sourcePath) {
1106
+ if (!existsSync(ownerFile)) return [];
1107
+ const components = getAngularComponentMetadata(readFileSync(ownerFile, "utf-8"));
1108
+ const normalizedSourcePath = normalizePath(sourcePath);
1109
+ return components.filter((component) => component.styleUrls.some((styleUrl) => normalizePath(resolve(dirname(ownerFile), styleUrl)) === normalizedSourcePath)).map((component) => component.className);
930
1110
  }
931
- function isComponentStyleSheet(id) {
932
- return id.includes('ngcomp=');
1111
+ function logComponentStylesheetHmrOutcome(details) {
1112
+ const pitfalls = [];
1113
+ const rejectedPreferredPaths = [];
1114
+ const hints = [];
1115
+ if (details.encapsulation === "shadow") {
1116
+ pitfalls.push("shadow-encapsulation");
1117
+ rejectedPreferredPaths.push("css-update");
1118
+ rejectedPreferredPaths.push("owner-component-update");
1119
+ hints.push("Shadow DOM styles cannot rely on Vite CSS patching because Angular applies them inside a shadow root.");
1120
+ }
1121
+ if (details.diagnosis.anomalies.includes("wrapper_not_yet_tracked")) {
1122
+ pitfalls.push("wrapper-not-yet-tracked");
1123
+ rejectedPreferredPaths.push("css-update");
1124
+ 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.");
1125
+ }
1126
+ if (details.diagnosis.anomalies.includes("tracked_wrapper_missing_from_module_graph")) {
1127
+ pitfalls.push("tracked-wrapper-missing-from-module-graph");
1128
+ rejectedPreferredPaths.push("css-update");
1129
+ hints.push("A wrapper request id is known, but Vite could not resolve a live wrapper module for targeted CSS HMR.");
1130
+ }
1131
+ if ((details.ownerIds?.filter(Boolean).length ?? 0) === 0) {
1132
+ pitfalls.push("no-owner-modules");
1133
+ if (details.outcome === "full-reload") {
1134
+ rejectedPreferredPaths.push("owner-component-update");
1135
+ hints.push("No owning TS component modules were available in the module graph for owner-based fallback.");
1136
+ }
1137
+ } else if ((details.updateIds?.length ?? 0) === 0) {
1138
+ pitfalls.push("owner-modules-without-class-identities");
1139
+ if (details.outcome === "full-reload") {
1140
+ rejectedPreferredPaths.push("owner-component-update");
1141
+ hints.push("Owner modules were found, but Angular did not expose component class identities after recompilation, so no targeted component update could be sent.");
1142
+ }
1143
+ }
1144
+ debugHmrV("component stylesheet hmr outcome", {
1145
+ file: details.file,
1146
+ outcome: details.outcome,
1147
+ encapsulation: details.encapsulation,
1148
+ directModuleId: details.directModuleId,
1149
+ wrapperIds: details.wrapperIds ?? [],
1150
+ ownerIds: details.ownerIds ?? [],
1151
+ updateIds: details.updateIds ?? [],
1152
+ preferredPath: details.encapsulation === "shadow" ? "full-reload" : "css-update",
1153
+ rejectedPreferredPaths: [...new Set(rejectedPreferredPaths)],
1154
+ pitfalls: [...new Set(pitfalls)],
1155
+ anomalies: details.diagnosis.anomalies,
1156
+ hints: [...new Set([...details.diagnosis.hints, ...hints])]
1157
+ });
933
1158
  }
934
- 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
- };
1159
+ function findTemplateOwnerModules(server, resourceFile) {
1160
+ const candidateTsFiles = [normalizePath(resourceFile.split("?")[0]).replace(/\.(html|htm)$/i, ".ts")];
1161
+ const modules = /* @__PURE__ */ new Map();
1162
+ for (const candidate of candidateTsFiles) server.moduleGraph.getModulesByFile(candidate)?.forEach((mod) => {
1163
+ if (mod.id) modules.set(mod.id, mod);
1164
+ });
1165
+ return [...modules.values()];
1166
+ }
1167
+ function findStyleOwnerModules(server, resourceFile, styleSourceOwners) {
1168
+ const normalizedResourceFile = normalizePath(resourceFile.split("?")[0]);
1169
+ const candidateOwnerFiles = [...styleSourceOwners.get(normalizedResourceFile) ?? []];
1170
+ const modules = /* @__PURE__ */ new Map();
1171
+ for (const ownerFile of candidateOwnerFiles) server.moduleGraph.getModulesByFile(ownerFile)?.forEach((mod) => {
1172
+ if (mod.id) modules.set(mod.id, mod);
1173
+ });
1174
+ return [...modules.values()];
1175
+ }
1176
+ function getFileMetadata(program, angularCompiler, hmrEnabled, disableTypeChecking) {
1177
+ const ts = require("typescript");
1178
+ return (file) => {
1179
+ const sourceFile = program.getSourceFile(file);
1180
+ if (!sourceFile) return {};
1181
+ const diagnostics = getDiagnosticsForSourceFile(sourceFile, !!disableTypeChecking, program, angularCompiler);
1182
+ const errors = diagnostics.filter((d) => d.category === ts.DiagnosticCategory?.Error).map((d) => typeof d.messageText === "object" ? d.messageText.messageText : d.messageText);
1183
+ const warnings = diagnostics.filter((d) => d.category === ts.DiagnosticCategory?.Warning).map((d) => d.messageText);
1184
+ let hmrUpdateCode = void 0;
1185
+ let hmrEligible = false;
1186
+ if (hmrEnabled) {
1187
+ for (const node of sourceFile.statements) if (ts.isClassDeclaration(node) && node.name != null) {
1188
+ hmrUpdateCode = angularCompiler?.emitHmrUpdateModule(node);
1189
+ if (hmrUpdateCode) {
1190
+ const className = node.name.getText();
1191
+ classNames.set(file, className);
1192
+ hmrEligible = true;
1193
+ debugHmr("NgtscProgram emitHmrUpdateModule", {
1194
+ file,
1195
+ className
1196
+ });
1197
+ }
1198
+ }
1199
+ }
1200
+ return {
1201
+ errors,
1202
+ warnings,
1203
+ hmrUpdateCode,
1204
+ hmrEligible
1205
+ };
1206
+ };
1207
+ }
1208
+ function getDiagnosticsForSourceFile(sourceFile, disableTypeChecking, program, angularCompiler) {
1209
+ const syntacticDiagnostics = program.getSyntacticDiagnostics(sourceFile);
1210
+ if (disableTypeChecking) return syntacticDiagnostics;
1211
+ const semanticDiagnostics = program.getSemanticDiagnostics(sourceFile);
1212
+ const angularDiagnostics = angularCompiler ? angularCompiler.getDiagnosticsForFile(sourceFile, 1) : [];
1213
+ return [
1214
+ ...syntacticDiagnostics,
1215
+ ...semanticDiagnostics,
1216
+ ...angularDiagnostics
1217
+ ];
1218
+ }
1219
+ function markModuleSelfAccepting(mod) {
1220
+ if ("_clientModule" in mod) mod["_clientModule"].isSelfAccepting = true;
1221
+ return {
1222
+ ...mod,
1223
+ isSelfAccepting: true
1224
+ };
945
1225
  }
946
1226
  /**
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
- */
1227
+ * Removes leading / and query string from a url path
1228
+ * e.g. /foo.scss?direct&ngcomp=ng-c3153525609&e=0 returns foo.scss
1229
+ * @param id
1230
+ */
951
1231
  function getFilenameFromPath(id) {
952
- return new URL(id, 'http://localhost').pathname.replace(/^\//, '');
1232
+ try {
1233
+ return new URL(id, "http://localhost").pathname.replace(/^\//, "");
1234
+ } catch {
1235
+ const queryIndex = id.indexOf("?");
1236
+ return (queryIndex >= 0 ? id.slice(0, queryIndex) : id).replace(/^\//, "");
1237
+ }
953
1238
  }
954
1239
  /**
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;
981
- }
1240
+ * Checks for vitest run from the command line
1241
+ * @returns boolean
1242
+ */
1243
+ //#endregion
1244
+ export { angular };
1245
+
982
1246
  //# sourceMappingURL=angular-vite-plugin.js.map