@analogjs/vite-plugin-angular 0.2.29 → 0.2.30-beta.1

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.
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createFileEmitter = exports.angular = void 0;
4
- const tslib_1 = require("tslib");
4
+ const vite_1 = require("vite");
5
5
  const core_1 = require("@babel/core");
6
6
  const ts = require("typescript");
7
7
  const path = require("path");
@@ -17,28 +17,29 @@ const angular_vitest_plugin_1 = require("./angular-vitest-plugin");
17
17
  * Match .(c or m)ts, .ts extensions with an optional ? for query params
18
18
  * Ignore .tsx extensions
19
19
  */
20
- const TS_EXT_REGEX = /\.[cm]?ts[^x]?\??/;
20
+ const TS_EXT_REGEX = /\.[cm]?(ts|ng)[^x]?\??/;
21
21
  function angular(options) {
22
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
23
22
  /**
24
23
  * Normalize plugin options so defaults
25
24
  * are used for values not provided.
26
25
  */
27
26
  const pluginOptions = {
28
- tsconfig: (_a = options === null || options === void 0 ? void 0 : options.tsconfig) !== null && _a !== void 0 ? _a : (process.env['NODE_ENV'] === 'test'
29
- ? './tsconfig.spec.json'
30
- : './tsconfig.app.json'),
31
- workspaceRoot: (_b = options === null || options === void 0 ? void 0 : options.workspaceRoot) !== null && _b !== void 0 ? _b : process.cwd(),
32
- inlineStylesExtension: (_c = options === null || options === void 0 ? void 0 : options.inlineStylesExtension) !== null && _c !== void 0 ? _c : 'css',
27
+ tsconfig: options?.tsconfig ??
28
+ (process.env['NODE_ENV'] === 'test'
29
+ ? './tsconfig.spec.json'
30
+ : './tsconfig.app.json'),
31
+ workspaceRoot: options?.workspaceRoot ?? process.cwd(),
32
+ inlineStylesExtension: options?.inlineStylesExtension ?? 'css',
33
33
  advanced: {
34
34
  tsTransformers: {
35
- before: (_f = (_e = (_d = options === null || options === void 0 ? void 0 : options.advanced) === null || _d === void 0 ? void 0 : _d.tsTransformers) === null || _e === void 0 ? void 0 : _e.before) !== null && _f !== void 0 ? _f : [],
36
- after: (_j = (_h = (_g = options === null || options === void 0 ? void 0 : options.advanced) === null || _g === void 0 ? void 0 : _g.tsTransformers) === null || _h === void 0 ? void 0 : _h.after) !== null && _j !== void 0 ? _j : [],
37
- afterDeclarations: (_m = (_l = (_k = options === null || options === void 0 ? void 0 : options.advanced) === null || _k === void 0 ? void 0 : _k.tsTransformers) === null || _l === void 0 ? void 0 : _l.afterDeclarations) !== null && _m !== void 0 ? _m : [],
35
+ before: options?.advanced?.tsTransformers?.before ?? [],
36
+ after: options?.advanced?.tsTransformers?.after ?? [],
37
+ afterDeclarations: options?.advanced?.tsTransformers?.afterDeclarations ?? [],
38
38
  },
39
39
  },
40
- supportedBrowsers: (_o = options === null || options === void 0 ? void 0 : options.supportedBrowsers) !== null && _o !== void 0 ? _o : ['safari 15'],
41
- jit: options === null || options === void 0 ? void 0 : options.jit,
40
+ supportedBrowsers: options?.supportedBrowsers ?? ['safari 15'],
41
+ jit: options?.jit,
42
+ supportNgFormat: options?.experimental?.dangerouslySupportNgFormat,
42
43
  };
43
44
  // The file emitter created during `onStart` that will be used during the build in `onLoad` callbacks for TS files
44
45
  let fileEmitter;
@@ -47,6 +48,7 @@ function angular(options) {
47
48
  const { mergeTransformers, replaceBootstrap, } = require('@ngtools/webpack/src/ivy/transformation');
48
49
  const { augmentProgramWithVersioning, augmentHostWithCaching, } = require('@ngtools/webpack/src/ivy/host');
49
50
  let compilerCli;
51
+ let userConfig;
50
52
  let rootNames;
51
53
  let host;
52
54
  let nextProgram;
@@ -55,7 +57,7 @@ function angular(options) {
55
57
  const sourceFileCache = new devkit_1.SourceFileCache();
56
58
  const isProd = process.env['NODE_ENV'] === 'production';
57
59
  const isTest = process.env['NODE_ENV'] === 'test' || !!process.env['VITEST'];
58
- const jit = typeof (pluginOptions === null || pluginOptions === void 0 ? void 0 : pluginOptions.jit) !== 'undefined' ? pluginOptions.jit : isTest;
60
+ const jit = typeof pluginOptions?.jit !== 'undefined' ? pluginOptions.jit : isTest;
59
61
  let viteServer;
60
62
  let cssPlugin;
61
63
  let styleTransform;
@@ -64,213 +66,216 @@ function angular(options) {
64
66
  function angularPlugin() {
65
67
  return {
66
68
  name: '@analogjs/vite-plugin-angular',
67
- config(config, { command }) {
68
- var _a;
69
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
70
- watchMode = command === 'serve';
71
- pluginOptions.tsconfig =
72
- (_a = options === null || options === void 0 ? void 0 : options.tsconfig) !== null && _a !== void 0 ? _a : path.resolve(config.root || '.', process.env['NODE_ENV'] === 'test'
69
+ async config(config, { command }) {
70
+ watchMode = command === 'serve';
71
+ userConfig = config;
72
+ pluginOptions.tsconfig =
73
+ options?.tsconfig ??
74
+ path.resolve(config.root || '.', process.env['NODE_ENV'] === 'test'
73
75
  ? './tsconfig.spec.json'
74
76
  : './tsconfig.app.json');
75
- compilerCli = yield (0, devkit_1.loadEsmModule)('@angular/compiler-cli');
76
- return {
77
- optimizeDeps: {
78
- include: ['rxjs/operators', 'rxjs'],
79
- exclude: ['@angular/platform-server'],
80
- esbuildOptions: {
81
- plugins: [
82
- (0, compiler_plugin_1.createCompilerPlugin)({
83
- tsconfig: pluginOptions.tsconfig,
84
- sourcemap: !isProd,
85
- advancedOptimizations: isProd,
86
- jit,
87
- incremental: watchMode,
88
- }, isTest),
89
- ],
90
- define: Object.assign({ ngJitMode: 'false', ngI18nClosureMode: 'false' }, (watchMode ? {} : { ngDevMode: 'false' })),
77
+ compilerCli = await (0, devkit_1.loadEsmModule)('@angular/compiler-cli');
78
+ return {
79
+ optimizeDeps: {
80
+ include: ['rxjs/operators', 'rxjs'],
81
+ exclude: ['@angular/platform-server'],
82
+ esbuildOptions: {
83
+ plugins: [
84
+ (0, compiler_plugin_1.createCompilerPlugin)({
85
+ tsconfig: pluginOptions.tsconfig,
86
+ sourcemap: !isProd,
87
+ advancedOptimizations: isProd,
88
+ jit,
89
+ incremental: watchMode,
90
+ }, isTest),
91
+ ],
92
+ define: {
93
+ ngJitMode: 'false',
94
+ ngI18nClosureMode: 'false',
95
+ ...(watchMode ? {} : { ngDevMode: 'false' }),
91
96
  },
92
97
  },
93
- resolve: {
94
- conditions: ['style'],
95
- },
96
- };
97
- });
98
+ },
99
+ resolve: {
100
+ conditions: ['style'],
101
+ },
102
+ };
98
103
  },
99
104
  configureServer(server) {
100
105
  viteServer = server;
101
106
  server.watcher.on('add', setupCompilation);
102
107
  server.watcher.on('unlink', setupCompilation);
103
108
  },
104
- buildStart({ plugins }) {
105
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
106
- if (Array.isArray(plugins)) {
107
- cssPlugin = plugins.find((plugin) => plugin.name === 'vite:css');
108
- }
109
- setupCompilation();
110
- // Only store cache if in watch mode
111
- if (watchMode) {
112
- augmentHostWithCaching(host, sourceFileCache);
113
- }
114
- yield buildAndAnalyze();
115
- });
109
+ async buildStart({ plugins }) {
110
+ if (Array.isArray(plugins)) {
111
+ cssPlugin = plugins.find((plugin) => plugin.name === 'vite:css');
112
+ }
113
+ setupCompilation(userConfig);
114
+ // Only store cache if in watch mode
115
+ if (watchMode) {
116
+ augmentHostWithCaching(host, sourceFileCache);
117
+ }
118
+ await buildAndAnalyze();
116
119
  },
117
- handleHotUpdate(ctx) {
118
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
119
- // The `handleHotUpdate` hook may be called before the `buildStart`,
120
- // which sets the compilation. As a result, the `host` may not be available
121
- // yet for use, leading to build errors such as "cannot read properties of undefined"
122
- // (because `host` is undefined).
123
- if (!host) {
124
- return;
125
- }
126
- if (TS_EXT_REGEX.test(ctx.file)) {
127
- sourceFileCache.invalidate([ctx.file.replace(/\?(.*)/, '')]);
128
- yield buildAndAnalyze();
120
+ async handleHotUpdate(ctx) {
121
+ // The `handleHotUpdate` hook may be called before the `buildStart`,
122
+ // which sets the compilation. As a result, the `host` may not be available
123
+ // yet for use, leading to build errors such as "cannot read properties of undefined"
124
+ // (because `host` is undefined).
125
+ if (!host) {
126
+ return;
127
+ }
128
+ if (TS_EXT_REGEX.test(ctx.file)) {
129
+ sourceFileCache.invalidate([ctx.file.replace(/\?(.*)/, '')]);
130
+ await buildAndAnalyze();
131
+ }
132
+ if (/\.(html|htm|css|less|sass|scss)$/.test(ctx.file)) {
133
+ /**
134
+ * Check to see if this was a direct request
135
+ * for an external resource (styles, html).
136
+ */
137
+ const isDirect = ctx.modules.find((mod) => ctx.file === mod.file && mod.id?.includes('?direct'));
138
+ if (isDirect) {
139
+ return ctx.modules;
129
140
  }
130
- if (/\.(html|htm|css|less|sass|scss)$/.test(ctx.file)) {
131
- /**
132
- * Check to see if this was a direct request
133
- * for an external resource (styles, html).
134
- */
135
- const isDirect = ctx.modules.find((mod) => { var _a; return ctx.file === mod.file && ((_a = mod.id) === null || _a === void 0 ? void 0 : _a.includes('?direct')); });
136
- if (isDirect) {
137
- return ctx.modules;
138
- }
139
- const mods = [];
140
- ctx.modules.forEach((mod) => {
141
- mod.importers.forEach((imp) => {
142
- sourceFileCache.invalidate([imp.id]);
143
- ctx.server.moduleGraph.invalidateModule(imp);
144
- mods.push(imp);
145
- });
141
+ const mods = [];
142
+ ctx.modules.forEach((mod) => {
143
+ mod.importers.forEach((imp) => {
144
+ sourceFileCache.invalidate([imp.id]);
145
+ ctx.server.moduleGraph.invalidateModule(imp);
146
+ mods.push(imp);
146
147
  });
147
- yield buildAndAnalyze();
148
- return mods;
149
- }
150
- return ctx.modules;
151
- });
148
+ });
149
+ await buildAndAnalyze();
150
+ return mods;
151
+ }
152
+ return ctx.modules;
152
153
  },
153
- transform(code, id) {
154
- var _a, _b, _c;
155
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
156
- // Skip transforming node_modules
157
- if (id.includes('node_modules')) {
154
+ async transform(code, id) {
155
+ // Skip transforming node_modules
156
+ if (id.includes('node_modules')) {
157
+ return;
158
+ }
159
+ /**
160
+ * Check for options.transformFilter
161
+ */
162
+ if (options?.transformFilter) {
163
+ if (!(options?.transformFilter(code, id) ?? true)) {
158
164
  return;
159
165
  }
166
+ }
167
+ /**
168
+ * Check for .ts extenstions for inline script files being
169
+ * transformed (Astro).
170
+ *
171
+ * Example ID:
172
+ *
173
+ * /src/pages/index.astro?astro&type=script&index=0&lang.ts
174
+ */
175
+ if (id.includes('type=script')) {
176
+ return;
177
+ }
178
+ if (TS_EXT_REGEX.test(id)) {
179
+ if (id.includes('.ts?')) {
180
+ // Strip the query string off the ID
181
+ // in case of a dynamically loaded file
182
+ id = id.replace(/\?(.*)/, '');
183
+ }
160
184
  /**
161
- * Check for options.transformFilter
185
+ * Re-analyze on each transform
186
+ * for test(Vitest)
162
187
  */
163
- if (options === null || options === void 0 ? void 0 : options.transformFilter) {
164
- if (!((_a = options === null || options === void 0 ? void 0 : options.transformFilter(code, id)) !== null && _a !== void 0 ? _a : true)) {
165
- return;
188
+ if (isTest) {
189
+ const tsMod = viteServer?.moduleGraph.getModuleById(id);
190
+ if (tsMod) {
191
+ sourceFileCache.invalidate([id]);
192
+ await buildAndAnalyze();
166
193
  }
167
194
  }
168
- /**
169
- * Check for .ts extenstions for inline script files being
170
- * transformed (Astro).
171
- *
172
- * Example ID:
173
- *
174
- * /src/pages/index.astro?astro&type=script&index=0&lang.ts
175
- */
176
- if (id.includes('type=script')) {
177
- return;
195
+ let templateUrls = [];
196
+ let styleUrls = [];
197
+ if ((0, component_resolvers_1.hasTemplateUrl)(code)) {
198
+ templateUrls = templateUrlsResolver.resolve(code, id);
178
199
  }
179
- if (TS_EXT_REGEX.test(id)) {
180
- if (id.includes('.ts?')) {
181
- // Strip the query string off the ID
182
- // in case of a dynamically loaded file
183
- id = id.replace(/\?(.*)/, '');
184
- }
185
- /**
186
- * Re-analyze on each transform
187
- * for test(Vitest)
188
- */
189
- if (isTest) {
190
- const tsMod = viteServer === null || viteServer === void 0 ? void 0 : viteServer.moduleGraph.getModuleById(id);
191
- if (tsMod) {
192
- sourceFileCache.invalidate([id]);
193
- yield buildAndAnalyze();
194
- }
195
- }
196
- let templateUrls = [];
197
- let styleUrls = [];
198
- if ((0, component_resolvers_1.hasTemplateUrl)(code)) {
199
- templateUrls = templateUrlsResolver.resolve(code, id);
200
- }
201
- if ((0, component_resolvers_1.hasStyleUrls)(code)) {
202
- styleUrls = styleUrlsResolver.resolve(code, id);
203
- }
204
- if (watchMode) {
205
- for (const urlSet of [...templateUrls, ...styleUrls]) {
206
- // `urlSet` is a string where a relative path is joined with an
207
- // absolute path using the `|` symbol.
208
- // For example: `./app.component.html|/home/projects/analog/src/app/app.component.html`.
209
- const [, absoluteFileUrl] = urlSet.split('|');
210
- this.addWatchFile(absoluteFileUrl);
211
- }
212
- }
213
- const typescriptResult = yield fileEmitter(id);
214
- // return fileEmitter
215
- let data = (_b = typescriptResult === null || typescriptResult === void 0 ? void 0 : typescriptResult.content) !== null && _b !== void 0 ? _b : '';
216
- if (jit && data.includes('angular:jit:')) {
217
- data = data.replace(/angular:jit:style:inline;/g, 'virtual:angular:jit:style:inline;');
218
- templateUrls.forEach((templateUrlSet) => {
219
- const [templateFile, resolvedTemplateUrl] = templateUrlSet.split('|');
220
- data = data.replace(`angular:jit:template:file;${templateFile}`, `${resolvedTemplateUrl}?raw`);
221
- });
222
- styleUrls.forEach((styleUrlSet) => {
223
- const [styleFile, resolvedStyleUrl] = styleUrlSet.split('|');
224
- data = data.replace(`angular:jit:style:file;${styleFile}`, `${resolvedStyleUrl}?inline`);
225
- });
226
- }
227
- if (jit) {
228
- return {
229
- code: data.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, ''),
230
- map: {
231
- mappings: '',
232
- },
233
- };
234
- }
235
- const forceAsyncTransformation = /for\s+await\s*\(|async\s+function\s*\*/.test(data);
236
- const useInputSourcemap = (!isProd ? undefined : false);
237
- if (!forceAsyncTransformation && !isProd) {
238
- return {
239
- code: data.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, ''),
240
- map: {
241
- mappings: '',
242
- },
243
- };
200
+ if ((0, component_resolvers_1.hasStyleUrls)(code)) {
201
+ styleUrls = styleUrlsResolver.resolve(code, id);
202
+ }
203
+ if (watchMode) {
204
+ for (const urlSet of [...templateUrls, ...styleUrls]) {
205
+ // `urlSet` is a string where a relative path is joined with an
206
+ // absolute path using the `|` symbol.
207
+ // For example: `./app.component.html|/home/projects/analog/src/app/app.component.html`.
208
+ const [, absoluteFileUrl] = urlSet.split('|');
209
+ this.addWatchFile(absoluteFileUrl);
244
210
  }
245
- const babelResult = yield (0, core_1.transformAsync)(data, {
246
- filename: id,
247
- inputSourceMap: (useInputSourcemap
248
- ? undefined
249
- : false),
250
- sourceMaps: !isProd ? 'inline' : false,
251
- compact: false,
252
- configFile: false,
253
- babelrc: false,
254
- browserslistConfigFile: false,
255
- plugins: [],
256
- presets: [
257
- [
258
- devkit_1.angularApplicationPreset,
259
- {
260
- supportedBrowsers: pluginOptions.supportedBrowsers,
261
- forceAsyncTransformation,
262
- optimize: isProd && {},
263
- },
264
- ],
265
- ],
211
+ }
212
+ const typescriptResult = fileEmitter && (await fileEmitter(id));
213
+ // return fileEmitter
214
+ let data = typescriptResult?.content ?? '';
215
+ if (jit && data.includes('angular:jit:')) {
216
+ data = data.replace(/angular:jit:style:inline;/g, 'virtual:angular:jit:style:inline;');
217
+ templateUrls.forEach((templateUrlSet) => {
218
+ const [templateFile, resolvedTemplateUrl] = templateUrlSet.split('|');
219
+ data = data.replace(`angular:jit:template:file;${templateFile}`, `${resolvedTemplateUrl}?raw`);
220
+ });
221
+ styleUrls.forEach((styleUrlSet) => {
222
+ const [styleFile, resolvedStyleUrl] = styleUrlSet.split('|');
223
+ data = data.replace(`angular:jit:style:file;${styleFile}`, `${resolvedStyleUrl}?inline`);
266
224
  });
225
+ }
226
+ if (jit) {
227
+ return {
228
+ code: data.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, ''),
229
+ map: {
230
+ mappings: '',
231
+ },
232
+ };
233
+ }
234
+ const forceAsyncTransformation = /for\s+await\s*\(|async\s+function\s*\*/.test(data);
235
+ const useInputSourcemap = (!isProd ? undefined : false);
236
+ if (id.includes('.ng') &&
237
+ pluginOptions.supportNgFormat &&
238
+ fileEmitter) {
239
+ sourceFileCache.invalidate([`${id}.ts`]);
240
+ const ngFileResult = await fileEmitter(`${id}.ts`);
241
+ data = ngFileResult?.content || '';
242
+ }
243
+ if (!forceAsyncTransformation && !isProd) {
267
244
  return {
268
- code: (_c = babelResult === null || babelResult === void 0 ? void 0 : babelResult.code) !== null && _c !== void 0 ? _c : '',
269
- map: babelResult === null || babelResult === void 0 ? void 0 : babelResult.map,
245
+ code: data.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, ''),
246
+ map: {
247
+ mappings: '',
248
+ },
270
249
  };
271
250
  }
272
- return undefined;
273
- });
251
+ const babelResult = await (0, core_1.transformAsync)(data, {
252
+ filename: id,
253
+ inputSourceMap: (useInputSourcemap
254
+ ? undefined
255
+ : false),
256
+ sourceMaps: !isProd ? 'inline' : false,
257
+ compact: false,
258
+ configFile: false,
259
+ babelrc: false,
260
+ browserslistConfigFile: false,
261
+ plugins: [],
262
+ presets: [
263
+ [
264
+ devkit_1.angularApplicationPreset,
265
+ {
266
+ supportedBrowsers: pluginOptions.supportedBrowsers,
267
+ forceAsyncTransformation,
268
+ optimize: isProd && {},
269
+ },
270
+ ],
271
+ ],
272
+ });
273
+ return {
274
+ code: babelResult?.code ?? '',
275
+ map: babelResult?.map,
276
+ };
277
+ }
278
+ return undefined;
274
279
  },
275
280
  };
276
281
  }
@@ -286,7 +291,21 @@ function angular(options) {
286
291
  supportedBrowsers: pluginOptions.supportedBrowsers,
287
292
  }),
288
293
  ].filter(Boolean);
289
- function setupCompilation() {
294
+ function findNgFiles(config) {
295
+ if (!pluginOptions.supportNgFormat) {
296
+ return [];
297
+ }
298
+ const fg = require('fast-glob');
299
+ const root = (0, vite_1.normalizePath)(path.resolve(pluginOptions.workspaceRoot, config.root || '.'));
300
+ const ngFiles = fg
301
+ .sync([`${root}/**/*.ng`], {
302
+ dot: true,
303
+ })
304
+ .map((file) => `${file}.ts`);
305
+ return ngFiles;
306
+ }
307
+ function setupCompilation(config) {
308
+ const ngFiles = findNgFiles(config);
290
309
  const { options: tsCompilerOptions, rootNames: rn } = compilerCli.readConfiguration(pluginOptions.tsconfig, {
291
310
  suppressOutputPathCheck: true,
292
311
  outDir: undefined,
@@ -299,7 +318,13 @@ function angular(options) {
299
318
  enableResourceInlining: false,
300
319
  supportTestBed: false,
301
320
  });
302
- rootNames = rn;
321
+ if (pluginOptions.supportNgFormat) {
322
+ // Experimental Local Compilation is necessary
323
+ // for the Angular compiler to work with
324
+ // AOT and virtually compiled .ng files.
325
+ tsCompilerOptions.compilationMode = 'experimental-local';
326
+ }
327
+ rootNames = rn.concat(ngFiles);
303
328
  compilerOptions = tsCompilerOptions;
304
329
  host = ts.createIncrementalCompilerHost(compilerOptions);
305
330
  styleTransform = watchMode
@@ -308,6 +333,8 @@ function angular(options) {
308
333
  if (!jit) {
309
334
  (0, host_1.augmentHostWithResources)(host, styleTransform, {
310
335
  inlineStylesExtension: pluginOptions.inlineStylesExtension,
336
+ supportNgFormat: pluginOptions.supportNgFormat,
337
+ isProd: isProd,
311
338
  });
312
339
  }
313
340
  }
@@ -316,54 +343,52 @@ function angular(options) {
316
343
  * the source files and create a file emitter.
317
344
  * This is shared between an initial build and a hot update.
318
345
  */
319
- function buildAndAnalyze() {
320
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
321
- let builder;
322
- let typeScriptProgram;
323
- let angularCompiler;
324
- if (!jit) {
325
- // Create the Angular specific program that contains the Angular compiler
326
- const angularProgram = new compilerCli.NgtscProgram(rootNames, compilerOptions, host, nextProgram);
327
- angularCompiler = angularProgram.compiler;
328
- typeScriptProgram = angularProgram.getTsProgram();
329
- augmentProgramWithVersioning(typeScriptProgram);
330
- builder = builderProgram =
331
- ts.createEmitAndSemanticDiagnosticsBuilderProgram(typeScriptProgram, host, builderProgram);
332
- yield angularCompiler.analyzeAsync();
333
- nextProgram = angularProgram;
334
- }
335
- else {
336
- builder = builderProgram =
337
- ts.createEmitAndSemanticDiagnosticsBuilderProgram(rootNames, compilerOptions, host, nextProgram);
338
- typeScriptProgram = builder.getProgram();
339
- nextProgram = builderProgram;
340
- }
341
- if (!watchMode) {
342
- // When not in watch mode, the startup cost of the incremental analysis can be avoided by
343
- // using an abstract builder that only wraps a TypeScript program.
344
- builder = ts.createAbstractBuilder(typeScriptProgram, host);
345
- }
346
- const getTypeChecker = () => builder.getProgram().getTypeChecker();
347
- fileEmitter = createFileEmitter(builder, mergeTransformers({
348
- before: [
349
- replaceBootstrap(getTypeChecker),
350
- ...(jit
351
- ? [
352
- compilerCli.constructorParametersDownlevelTransform(builder.getProgram()),
353
- (0, devkit_1.createJitResourceTransformer)(getTypeChecker),
354
- ]
355
- : []),
356
- ...pluginOptions.advanced.tsTransformers.before,
357
- ],
358
- after: pluginOptions.advanced.tsTransformers.after,
359
- afterDeclarations: pluginOptions.advanced.tsTransformers.afterDeclarations,
360
- }, jit ? {} : angularCompiler.prepareEmit().transformers), () => []);
361
- });
346
+ async function buildAndAnalyze() {
347
+ let builder;
348
+ let typeScriptProgram;
349
+ let angularCompiler;
350
+ if (!jit) {
351
+ // Create the Angular specific program that contains the Angular compiler
352
+ const angularProgram = new compilerCli.NgtscProgram(rootNames, compilerOptions, host, nextProgram);
353
+ angularCompiler = angularProgram.compiler;
354
+ typeScriptProgram = angularProgram.getTsProgram();
355
+ augmentProgramWithVersioning(typeScriptProgram);
356
+ builder = builderProgram =
357
+ ts.createEmitAndSemanticDiagnosticsBuilderProgram(typeScriptProgram, host, builderProgram);
358
+ await angularCompiler.analyzeAsync();
359
+ nextProgram = angularProgram;
360
+ }
361
+ else {
362
+ builder = builderProgram =
363
+ ts.createEmitAndSemanticDiagnosticsBuilderProgram(rootNames, compilerOptions, host, nextProgram);
364
+ typeScriptProgram = builder.getProgram();
365
+ nextProgram = builderProgram;
366
+ }
367
+ if (!watchMode) {
368
+ // When not in watch mode, the startup cost of the incremental analysis can be avoided by
369
+ // using an abstract builder that only wraps a TypeScript program.
370
+ builder = ts.createAbstractBuilder(typeScriptProgram, host);
371
+ }
372
+ const getTypeChecker = () => builder.getProgram().getTypeChecker();
373
+ fileEmitter = createFileEmitter(builder, mergeTransformers({
374
+ before: [
375
+ replaceBootstrap(getTypeChecker),
376
+ ...(jit
377
+ ? [
378
+ compilerCli.constructorParametersDownlevelTransform(builder.getProgram()),
379
+ (0, devkit_1.createJitResourceTransformer)(getTypeChecker),
380
+ ]
381
+ : []),
382
+ ...pluginOptions.advanced.tsTransformers.before,
383
+ ],
384
+ after: pluginOptions.advanced.tsTransformers.after,
385
+ afterDeclarations: pluginOptions.advanced.tsTransformers.afterDeclarations,
386
+ }, jit ? {} : angularCompiler.prepareEmit().transformers), () => []);
362
387
  }
363
388
  }
364
389
  exports.angular = angular;
365
390
  function createFileEmitter(program, transformers = {}, onAfterEmit) {
366
- return (file) => tslib_1.__awaiter(this, void 0, void 0, function* () {
391
+ return async (file) => {
367
392
  const sourceFile = program.getSourceFile(file);
368
393
  if (!sourceFile) {
369
394
  return undefined;
@@ -374,9 +399,9 @@ function createFileEmitter(program, transformers = {}, onAfterEmit) {
374
399
  content = data;
375
400
  }
376
401
  }, undefined /* cancellationToken */, undefined /* emitOnlyDtsFiles */, transformers);
377
- onAfterEmit === null || onAfterEmit === void 0 ? void 0 : onAfterEmit(sourceFile);
402
+ onAfterEmit?.(sourceFile);
378
403
  return { content, dependencies: [] };
379
- });
404
+ };
380
405
  }
381
406
  exports.createFileEmitter = createFileEmitter;
382
407
  //# sourceMappingURL=angular-vite-plugin.js.map