@analogjs/vite-plugin-angular 3.0.0-beta.47 → 3.0.0-beta.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.d.ts +1 -0
- package/src/index.js +2 -1
- package/src/index.js.map +1 -1
- package/src/lib/angular-build-optimizer-plugin.js +14 -4
- package/src/lib/angular-build-optimizer-plugin.js.map +1 -1
- package/src/lib/component-scanner.d.ts +74 -0
- package/src/lib/component-scanner.js +178 -0
- package/src/lib/component-scanner.js.map +1 -0
- package/src/lib/fast-compile-plugin.js +26 -165
- package/src/lib/fast-compile-plugin.js.map +1 -1
- package/src/lib/ngp-compile-api.d.ts +55 -0
- package/src/lib/ngp-compile-api.js +12 -0
- package/src/lib/ngp-compile-api.js.map +1 -0
- package/src/lib/ngp-compile-plugin.js +286 -41
- package/src/lib/ngp-compile-plugin.js.map +1 -1
- package/src/lib/utils/lsp-type-checker.d.ts +22 -2
- package/src/lib/utils/lsp-type-checker.js +39 -10
- package/src/lib/utils/lsp-type-checker.js.map +1 -1
- package/src/lib/utils/ngp-host-fs.d.ts +32 -0
- package/src/lib/utils/ngp-host-fs.js +104 -0
- package/src/lib/utils/ngp-host-fs.js.map +1 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region packages/vite-plugin-angular/src/lib/ngp-compile-api.ts
|
|
2
|
+
/** Finds the NGP compile plugin's API among resolved Vite plugins. */
|
|
3
|
+
function findNgpCompileApi(plugins) {
|
|
4
|
+
for (const plugin of plugins) {
|
|
5
|
+
const api = plugin.api;
|
|
6
|
+
if (api?.ngp) return api.ngp;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { findNgpCompileApi };
|
|
11
|
+
|
|
12
|
+
//# sourceMappingURL=ngp-compile-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ngp-compile-api.js","names":[],"sources":["../../../src/lib/ngp-compile-api.ts"],"sourcesContent":["import type {\n FastCompileSourceFormat,\n SourceMapInput,\n} from './fast-compile-api.js';\n\n/**\n * The NGP compile plugin's API (`compilation: 'ngp'`), for Vite plugins that\n * compile other source formats into Angular components.\n *\n * NGP analyzes the whole program when the build starts, so a format's modules\n * are lowered to TypeScript up front and NGP reads each one as a TypeScript\n * file named after it (`counter.tsrx` as `counter.tsrx.ts`). Imports of the\n * module resolve to that file, and the lowered code is type-checked with it.\n */\nexport interface NgpCompileApi {\n registerSourceFormat(format: NgpSourceFormat): void;\n}\n\nexport interface NgpSourceFormat extends FastCompileSourceFormat {\n /** Glob of the format's modules, relative to the tsconfig's directory. */\n include: string;\n /** Lowers a module to the TypeScript NGP compiles in its place. */\n lower(\n code: string,\n path: string,\n context: NgpLowerContext,\n ): Promise<NgpLoweredModule>;\n}\n\nexport interface NgpLowerContext {\n /**\n * Scans the modules behind `specifiers`, imported by the module being\n * lowered, so `resolveSelector` knows their selectors.\n */\n scanImports(specifiers: string[]): Promise<void>;\n /** The element selector of a component or directive class, if known. */\n resolveSelector(className: string): string | null;\n}\n\nexport interface NgpLoweredModule {\n code: string;\n /** Why the module couldn't be lowered, reported when it's transformed. */\n error?: { message: string; loc?: { line: number; column: number } };\n /** Maps a source map of `code` back to the module's source. */\n mapToSource(map: SourceMapInput): { [key: string]: unknown };\n /**\n * Maps a zero-based line and column in `code` to the module's source, or\n * `null` for generated code.\n */\n toSourceLocation(\n line: number,\n column: number,\n ): { line: number; column: number } | null;\n}\n\n/** Finds the NGP compile plugin's API among resolved Vite plugins. */\nexport function findNgpCompileApi(\n plugins: readonly { api?: unknown }[],\n): NgpCompileApi | undefined {\n for (const plugin of plugins) {\n const api = plugin.api as { ngp?: NgpCompileApi } | undefined;\n if (api?.ngp) {\n return api.ngp;\n }\n }\n return undefined;\n}\n"],"mappings":";;AAwDA,SAAgB,kBACd,SAC2B;CAC3B,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,MAAM,OAAO;EACnB,IAAI,KAAK,KACP,OAAO,IAAI;CAEf;AAEF"}
|
|
@@ -7,20 +7,33 @@ import { TS_EXT_REGEX, createDepOptimizerConfig, getTsConfigPath, isProdMode } f
|
|
|
7
7
|
import { createAngularBuildContext, isAngularBuildContext } from "./angular-build-context.js";
|
|
8
8
|
import { releaseCssPreprocessorWorkers } from "./utils/css-preprocessor-workers.js";
|
|
9
9
|
import { loadAngularVirtualModule, resolveAngularVirtualId } from "./virtual-modules-plugin.js";
|
|
10
|
+
import { ComponentScanner } from "./component-scanner.js";
|
|
10
11
|
import { findHmrComponentUpdates } from "./utils/hmr-component-updates.js";
|
|
11
12
|
import { LspTypeChecker } from "./utils/lsp-type-checker.js";
|
|
13
|
+
import { canonicalPath, createNgpHostFs } from "./utils/ngp-host-fs.js";
|
|
14
|
+
import { existsSync, promises, realpathSync } from "node:fs";
|
|
12
15
|
import { createRequire } from "node:module";
|
|
13
|
-
import { resolve } from "node:path";
|
|
16
|
+
import { dirname, resolve } from "node:path";
|
|
14
17
|
import * as vite from "vite";
|
|
15
18
|
import * as compilerCli from "@angular/compiler-cli";
|
|
19
|
+
import { glob } from "tinyglobby";
|
|
16
20
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
17
21
|
//#region packages/vite-plugin-angular/src/lib/ngp-compile-plugin.ts
|
|
22
|
+
/** NGP's `FileUpdateType`. */
|
|
23
|
+
var FILE_CREATED = 0;
|
|
24
|
+
var FILE_DELETED = 1;
|
|
25
|
+
var FILE_CHANGED = 2;
|
|
18
26
|
function isFastCompileOptions(value) {
|
|
19
27
|
return typeof value === "object" && value !== null && !isAngularBuildContext(value) && "tsconfigGetter" in value && typeof value.tsconfigGetter === "function";
|
|
20
28
|
}
|
|
21
29
|
var BUNDLED_NGP = new URL("../../ngp/", "" + import.meta.url);
|
|
22
|
-
|
|
30
|
+
/**
|
|
31
|
+
* @param needsHostFs Whether source formats are registered, which only the
|
|
32
|
+
* WebAssembly backend can read through `hostFs`.
|
|
33
|
+
*/
|
|
34
|
+
async function loadNgp(tsconfig, needsHostFs) {
|
|
23
35
|
const nativeBinding = process.env["ANALOG_NGP_NATIVE_BINDING"];
|
|
36
|
+
if (nativeBinding && needsHostFs) throw new Error("[@analogjs/vite-plugin-angular]: Source formats such as TSRX can't be compiled with a native NGP analyzer build. Unset ANALOG_NGP_NATIVE_BINDING to use NGP's default WebAssembly analyzer.");
|
|
24
37
|
let modulePath = process.env["ANALOG_NGP_COMPILER"];
|
|
25
38
|
if (!modulePath) try {
|
|
26
39
|
modulePath = createRequire(tsconfig).resolve("@angular/ngp");
|
|
@@ -36,7 +49,7 @@ async function loadNgp(tsconfig) {
|
|
|
36
49
|
}
|
|
37
50
|
return {
|
|
38
51
|
api: await import(pathToFileURL(resolve(modulePath)).href),
|
|
39
|
-
analyzerOptions: nativeBinding ? { nativeBinding } : {}
|
|
52
|
+
analyzerOptions: nativeBinding ? { nativeBinding } : needsHostFs ? { backend: "wasm" } : {}
|
|
40
53
|
};
|
|
41
54
|
}
|
|
42
55
|
function transpile(source, filePath, map) {
|
|
@@ -59,6 +72,7 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
59
72
|
let configContext;
|
|
60
73
|
let resolvedConfig;
|
|
61
74
|
let transformFilter;
|
|
75
|
+
let componentRegistries = [];
|
|
62
76
|
let stylePreprocessor;
|
|
63
77
|
let watchMode = false;
|
|
64
78
|
let hmr = false;
|
|
@@ -82,12 +96,28 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
82
96
|
fileEmitter
|
|
83
97
|
};
|
|
84
98
|
let pending = Promise.resolve();
|
|
85
|
-
const
|
|
99
|
+
const excluded = [
|
|
86
100
|
/node_modules/,
|
|
87
101
|
/[?&]raw\b/,
|
|
88
102
|
/type=script/,
|
|
89
103
|
/@ng\/component/
|
|
90
|
-
]
|
|
104
|
+
];
|
|
105
|
+
const filter = vite.createFilter(TS_EXT_REGEX, excluded);
|
|
106
|
+
const sourceFilter = vite.createFilter(null, excluded);
|
|
107
|
+
const sourceFormats = [];
|
|
108
|
+
const scanner = new ComponentScanner();
|
|
109
|
+
const sourceModules = /* @__PURE__ */ new Map();
|
|
110
|
+
const removals = /* @__PURE__ */ new Map();
|
|
111
|
+
const analyzed = /* @__PURE__ */ new Map();
|
|
112
|
+
const includes = /* @__PURE__ */ new Map();
|
|
113
|
+
const moduleFormatOf = (file) => sourceFormats.find((format) => format.test(file) && includes.get(format)?.(file));
|
|
114
|
+
let devServer;
|
|
115
|
+
const reloweredByChange = /* @__PURE__ */ new Map();
|
|
116
|
+
const programFileOf = (file) => {
|
|
117
|
+
const programFile = `${file}.ts`;
|
|
118
|
+
return sourceModules.has(programFile) ? programFile : void 0;
|
|
119
|
+
};
|
|
120
|
+
const sourceFileOf = (file) => sourceModules.get(file)?.sourceFile ?? file;
|
|
91
121
|
function resolveTsConfigPath() {
|
|
92
122
|
if (sharedCtx) return sharedCtx.resolveTsConfigPath();
|
|
93
123
|
const ctx = configContext ?? {
|
|
@@ -104,8 +134,138 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
104
134
|
hasEmitted: (id) => filesByPath.has(vite.normalizePath(id)),
|
|
105
135
|
output,
|
|
106
136
|
ready: () => pending,
|
|
107
|
-
componentUpdates
|
|
137
|
+
componentUpdates,
|
|
138
|
+
ngp: { registerSourceFormat(format) {
|
|
139
|
+
if (!sourceFormats.includes(format)) {
|
|
140
|
+
sourceFormats.push(format);
|
|
141
|
+
scanner.registerSourceFormat(format);
|
|
142
|
+
}
|
|
143
|
+
} }
|
|
108
144
|
};
|
|
145
|
+
/** Lowers a module of a source format. Returns whether its TypeScript changed. */
|
|
146
|
+
async function lowerSourceModule(sourceFile, source, format, resolve) {
|
|
147
|
+
const programFile = `${sourceFile}.ts`;
|
|
148
|
+
if (existsSync(programFile)) throw new Error(`[@analogjs/vite-plugin-angular]: ${programFile} has the name NGP compiles ${sourceFile} as. Rename one of them.`);
|
|
149
|
+
const removed = removals.get(programFile);
|
|
150
|
+
const lowered = await format.lower(source, sourceFile, {
|
|
151
|
+
scanImports: (specifiers) => scanner.scanImports(specifiers, sourceFile, resolve),
|
|
152
|
+
resolveSelector: (className) => scanner.lookupSelector(className, sharedCtx?.componentRegistries ?? componentRegistries)
|
|
153
|
+
});
|
|
154
|
+
if (removals.get(programFile) !== removed) return false;
|
|
155
|
+
const changed = sourceModules.get(programFile)?.lowered.code !== lowered.code;
|
|
156
|
+
sourceModules.set(programFile, {
|
|
157
|
+
sourceFile,
|
|
158
|
+
source,
|
|
159
|
+
format,
|
|
160
|
+
lowered
|
|
161
|
+
});
|
|
162
|
+
return changed;
|
|
163
|
+
}
|
|
164
|
+
/** Lowers every module of the registered source formats under `root`. */
|
|
165
|
+
async function lowerSourceModules(root, resolve) {
|
|
166
|
+
scanner.reset(root);
|
|
167
|
+
sourceModules.clear();
|
|
168
|
+
const sourceRoot = vite.normalizePath(realpathSync(root));
|
|
169
|
+
await Promise.all(sourceFormats.map(async (format) => {
|
|
170
|
+
includes.set(format, vite.createFilter(format.include, "**/node_modules/**", { resolve: sourceRoot }));
|
|
171
|
+
const files = await glob(format.include, {
|
|
172
|
+
cwd: sourceRoot,
|
|
173
|
+
absolute: true,
|
|
174
|
+
ignore: ["**/node_modules/**"]
|
|
175
|
+
});
|
|
176
|
+
await Promise.all(files.map(async (file) => {
|
|
177
|
+
const sourceFile = canonicalPath(file);
|
|
178
|
+
await lowerSourceModule(sourceFile, await promises.readFile(sourceFile, "utf-8"), format, resolve);
|
|
179
|
+
}));
|
|
180
|
+
}));
|
|
181
|
+
analyzed.clear();
|
|
182
|
+
for (const [programFile, { lowered }] of sourceModules) analyzed.set(programFile, lowered.code);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Lowers the source modules a change to `file` affects again: the module
|
|
186
|
+
* itself, and those that looked up selectors in it. Returns the program
|
|
187
|
+
* files whose TypeScript changed.
|
|
188
|
+
*/
|
|
189
|
+
async function relowerSourceModules(file, resolve) {
|
|
190
|
+
const affected = [file, ...scanner.importersByFile.get(file) ?? []].map(programFileOf).filter((programFile) => programFile !== void 0);
|
|
191
|
+
scanner.invalidate(file);
|
|
192
|
+
return (await Promise.all(affected.map(async (programFile) => {
|
|
193
|
+
const sourceModule = sourceModules.get(programFile);
|
|
194
|
+
if (!sourceModule) return [];
|
|
195
|
+
const { sourceFile, format } = sourceModule;
|
|
196
|
+
const source = await readSource(sourceFile);
|
|
197
|
+
if (source === void 0) return [];
|
|
198
|
+
return await lowerSourceModule(sourceFile, source, format, resolve) ? [programFile] : [];
|
|
199
|
+
}))).flat();
|
|
200
|
+
}
|
|
201
|
+
let sourceWork = Promise.resolve();
|
|
202
|
+
function withSourceLock(work) {
|
|
203
|
+
const run = sourceWork.catch(() => void 0).then(work);
|
|
204
|
+
sourceWork = run;
|
|
205
|
+
return run;
|
|
206
|
+
}
|
|
207
|
+
/** Sends the analyzer the lowered modules it doesn't have yet. */
|
|
208
|
+
async function syncProgramFiles() {
|
|
209
|
+
const updates = [...sourceModules].filter(([filePath, { lowered }]) => analyzed.get(filePath) !== lowered.code).map(([filePath, { lowered }]) => ({
|
|
210
|
+
filePath,
|
|
211
|
+
content: lowered.code
|
|
212
|
+
}));
|
|
213
|
+
if (updates.length === 0) return;
|
|
214
|
+
for (const { filePath, content } of updates) analyzed.set(filePath, content);
|
|
215
|
+
await enqueueIndex(() => analyzer.updateFileContent(updates));
|
|
216
|
+
}
|
|
217
|
+
/** Reads a source file, or `undefined` if it was removed. */
|
|
218
|
+
function readSource(file) {
|
|
219
|
+
return promises.readFile(file, "utf-8").catch((error) => {
|
|
220
|
+
if (error.code === "ENOENT") return void 0;
|
|
221
|
+
throw error;
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
/** Lowers a module added after the build started, if NGP compiles it. */
|
|
225
|
+
async function addSourceModule(sourceFile, format, source, resolve) {
|
|
226
|
+
const programFile = `${sourceFile}.ts`;
|
|
227
|
+
await lowerSourceModule(sourceFile, source, format, resolve);
|
|
228
|
+
const sourceModule = sourceModules.get(programFile);
|
|
229
|
+
if (!sourceModule) return;
|
|
230
|
+
analyzed.set(programFile, sourceModule.lowered.code);
|
|
231
|
+
await enqueueIndex(() => analyzer.invalidateFiles([{
|
|
232
|
+
filePath: programFile,
|
|
233
|
+
updateType: FILE_CREATED
|
|
234
|
+
}]));
|
|
235
|
+
}
|
|
236
|
+
/** Drops a removed module, and any lowering of it still in progress. */
|
|
237
|
+
async function removeSourceModule(sourceFile) {
|
|
238
|
+
const programFile = `${sourceFile}.ts`;
|
|
239
|
+
removals.set(programFile, (removals.get(programFile) ?? 0) + 1);
|
|
240
|
+
analyzed.delete(programFile);
|
|
241
|
+
if (!sourceModules.delete(programFile)) return;
|
|
242
|
+
await enqueueIndex(() => analyzer.invalidateFiles([{
|
|
243
|
+
filePath: programFile,
|
|
244
|
+
updateType: FILE_DELETED
|
|
245
|
+
}]));
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Lowers a module for its transform, as fast compile does on every
|
|
249
|
+
* transform, so the selectors it looks up are current. A transform that
|
|
250
|
+
* started before a hot update can carry the previous code; it compiles the
|
|
251
|
+
* latest lowering instead.
|
|
252
|
+
*/
|
|
253
|
+
async function lowerForTransform(sourceFile, format, code, resolve) {
|
|
254
|
+
const programFile = `${sourceFile}.ts`;
|
|
255
|
+
if (!sourceModules.has(programFile)) await addSourceModule(sourceFile, format, code, resolve);
|
|
256
|
+
else if (code === await readSource(sourceFile)) await lowerSourceModule(sourceFile, code, format, resolve);
|
|
257
|
+
return sourceModules.get(programFile)?.lowered;
|
|
258
|
+
}
|
|
259
|
+
/** Maps a type error in a program file to the source module's location. */
|
|
260
|
+
function toSourceLocation(location) {
|
|
261
|
+
const sourceModule = sourceModules.get(vite.normalizePath(location.file));
|
|
262
|
+
const mapped = sourceModule?.lowered.toSourceLocation(location.line - 1, location.character - 1);
|
|
263
|
+
return mapped ? {
|
|
264
|
+
file: sourceModule.sourceFile,
|
|
265
|
+
line: mapped.line + 1,
|
|
266
|
+
character: mapped.column + 1
|
|
267
|
+
} : location;
|
|
268
|
+
}
|
|
109
269
|
sharedCtx?.registerCompiler({
|
|
110
270
|
name: "@analogjs/vite-plugin-angular-ngp",
|
|
111
271
|
getTsConfigPath: compilerApi.getTsConfigPath,
|
|
@@ -168,14 +328,21 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
168
328
|
});
|
|
169
329
|
}
|
|
170
330
|
const chunks = update ? compiler.analyzeOptimizedDelta() : compiler.analyzeOptimized();
|
|
171
|
-
for await (const chunk of chunks)
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
331
|
+
for await (const chunk of chunks) {
|
|
332
|
+
for (const file of chunk.files) {
|
|
333
|
+
const normPath = vite.normalizePath(file.filePath);
|
|
334
|
+
filesByPath.set(normPath, {
|
|
335
|
+
file,
|
|
336
|
+
chunk
|
|
337
|
+
});
|
|
338
|
+
output.delete(normPath);
|
|
339
|
+
compiler.fileCache.delete(file.filePath);
|
|
340
|
+
}
|
|
341
|
+
for (const removed of chunk.removedFiles ?? []) {
|
|
342
|
+
const normPath = vite.normalizePath(removed);
|
|
343
|
+
filesByPath.delete(normPath);
|
|
344
|
+
output.delete(normPath);
|
|
345
|
+
}
|
|
179
346
|
}
|
|
180
347
|
}
|
|
181
348
|
function enqueueIndex(update) {
|
|
@@ -191,7 +358,10 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
191
358
|
return context;
|
|
192
359
|
}
|
|
193
360
|
async function checkTypes(filePath, sourceCode, tcbCode) {
|
|
194
|
-
typeChecker ??= new LspTypeChecker(resolveTsConfigPath(), options.typeChecker
|
|
361
|
+
typeChecker ??= new LspTypeChecker(resolveTsConfigPath(), options.typeChecker, {
|
|
362
|
+
inMemoryFiles: () => new Map([...sourceModules].map(([file, { lowered }]) => [file, lowered.code])),
|
|
363
|
+
mapLocation: toSourceLocation
|
|
364
|
+
});
|
|
195
365
|
typeChecker.queueFile(filePath, sourceCode, tcbCode);
|
|
196
366
|
if (!watchMode) return;
|
|
197
367
|
const typeErrors = await typeChecker.flushQueue(true);
|
|
@@ -201,9 +371,9 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
201
371
|
const { file: analyzed, chunk } = filesByPath.get(normPath);
|
|
202
372
|
const [file, context, content] = await Promise.all([
|
|
203
373
|
prepareResources(analyzed, (path) => {
|
|
204
|
-
const key = vite.normalizePath(path);
|
|
374
|
+
const key = sourceFileOf(vite.normalizePath(path));
|
|
205
375
|
dependents.set(key, (dependents.get(key) ?? /* @__PURE__ */ new Set()).add(normPath));
|
|
206
|
-
watch(
|
|
376
|
+
watch(key);
|
|
207
377
|
}),
|
|
208
378
|
getChunkContext(chunk),
|
|
209
379
|
analyzer.getFileContent(analyzed.filePath)
|
|
@@ -220,9 +390,11 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
220
390
|
let task = output.get(normPath);
|
|
221
391
|
if (!task) {
|
|
222
392
|
task = (async () => {
|
|
393
|
+
await pending;
|
|
394
|
+
if (!filesByPath.has(normPath)) throw new Error(`${sourceFileOf(normPath)} was removed.`);
|
|
223
395
|
const { file, content, result } = await processFile(normPath, compiler, watch);
|
|
224
396
|
const errors = [...file.diagnostics ?? [], ...result.diagnostics ?? []].filter((diagnostic) => diagnostic.category === 1);
|
|
225
|
-
if (errors.length) throw new Error(`${
|
|
397
|
+
if (errors.length) throw new Error(`${sourceFileOf(normPath)}: ${errors.map((error) => error.messageText).join("\n")}`);
|
|
226
398
|
if (options.disableTypeChecking !== true) await checkTypes(file.filePath, content, result.tcb?.code);
|
|
227
399
|
return result;
|
|
228
400
|
})();
|
|
@@ -258,11 +430,13 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
258
430
|
const codes = await Promise.all(updates.map(({ file, className }) => emitHmrUpdate(file, className)));
|
|
259
431
|
if (codes.some((code) => !code)) return false;
|
|
260
432
|
updates.forEach(({ file, className }, index) => {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
433
|
+
const sourceFile = sourceModules.get(file)?.sourceFile;
|
|
434
|
+
const moduleFile = sourceFile ?? file;
|
|
435
|
+
hmrUpdateCode.set(`${moduleFile}@${className}`, codes[index]);
|
|
436
|
+
componentUpdates.classNames.set(moduleFile, className);
|
|
437
|
+
server.moduleGraph.getModulesByFile(moduleFile)?.forEach((mod) => server.moduleGraph.invalidateModule(mod));
|
|
264
438
|
server.ws.send("angular:component-update", {
|
|
265
|
-
id: encodeURIComponent(`${filesByPath.get(file).file.filePath}@${className}`),
|
|
439
|
+
id: encodeURIComponent(`${sourceFile ?? filesByPath.get(file).file.filePath}@${className}`),
|
|
266
440
|
timestamp: Date.now()
|
|
267
441
|
});
|
|
268
442
|
});
|
|
@@ -305,6 +479,7 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
305
479
|
hmr = watchMode && options.liveReload && !options.isTest && config.server.hmr !== false;
|
|
306
480
|
const integrations = sharedCtx ? await sharedCtx.resolveConfig(config) : await discoverAnalogIntegrations(config);
|
|
307
481
|
transformFilter = integrations.transformFilter;
|
|
482
|
+
componentRegistries = integrations.componentRegistries;
|
|
308
483
|
stylePreprocessor = integrations.stylePreprocessor;
|
|
309
484
|
},
|
|
310
485
|
configureServer(server) {
|
|
@@ -313,6 +488,7 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
313
488
|
change: 2,
|
|
314
489
|
unlink: 3
|
|
315
490
|
};
|
|
491
|
+
devServer = server;
|
|
316
492
|
server.watcher.on("all", (event, path) => {
|
|
317
493
|
const type = changeTypes[event];
|
|
318
494
|
if (type) typeChecker?.notifyFileChanged(path, type);
|
|
@@ -325,15 +501,35 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
325
501
|
compiler = void 0;
|
|
326
502
|
});
|
|
327
503
|
},
|
|
504
|
+
async watchChange(id, { event }) {
|
|
505
|
+
if (!analyzer || sourceFormats.length === 0) return;
|
|
506
|
+
const file = canonicalPath(id);
|
|
507
|
+
const resolve = (spec, importer) => this.resolve(spec, importer, { skipSelf: true });
|
|
508
|
+
await withSourceLock(async () => {
|
|
509
|
+
scanner.invalidate(file);
|
|
510
|
+
const format = moduleFormatOf(file);
|
|
511
|
+
if (event === "update") reloweredByChange.set(file, await relowerSourceModules(file, resolve));
|
|
512
|
+
else if (event === "delete") await removeSourceModule(file);
|
|
513
|
+
else if (event === "create" && format && !sourceModules.has(`${file}.ts`)) {
|
|
514
|
+
const source = await readSource(file);
|
|
515
|
+
if (source !== void 0) await addSourceModule(file, format, source, resolve);
|
|
516
|
+
}
|
|
517
|
+
await syncProgramFiles();
|
|
518
|
+
});
|
|
519
|
+
for (const importer of scanner.importersByFile.get(file) ?? []) devServer?.moduleGraph.getModulesByFile(importer)?.forEach((mod) => devServer.moduleGraph.invalidateModule(mod));
|
|
520
|
+
},
|
|
328
521
|
async buildStart() {
|
|
329
522
|
activeBuilds++;
|
|
330
523
|
if (!analyzer) {
|
|
331
524
|
const tsconfig = resolveTsConfigPath();
|
|
332
|
-
const
|
|
525
|
+
const hasSourceFormats = sourceFormats.length > 0;
|
|
526
|
+
const ngp = await loadNgp(tsconfig, hasSourceFormats);
|
|
333
527
|
api = ngp.api;
|
|
528
|
+
if (hasSourceFormats) await lowerSourceModules(dirname(tsconfig), (specifier, importer) => this.resolve(specifier, importer, { skipSelf: true }));
|
|
334
529
|
analyzer = await api.createAnalyzer(tsconfig, {
|
|
335
530
|
...ngp.analyzerOptions,
|
|
336
|
-
enableHmr: hmr
|
|
531
|
+
enableHmr: hmr,
|
|
532
|
+
hostFs: hasSourceFormats ? createNgpHostFs((path) => sourceModules.get(path)?.lowered.code) : void 0
|
|
337
533
|
});
|
|
338
534
|
pending = indexChunks();
|
|
339
535
|
}
|
|
@@ -351,35 +547,82 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
351
547
|
},
|
|
352
548
|
async transform(code, id) {
|
|
353
549
|
const activeTransformFilter = sharedCtx?.transformFilter ?? transformFilter;
|
|
354
|
-
if (!filter(id) || activeTransformFilter?.(code, id) === false) return;
|
|
355
550
|
const filePath = vite.normalizePath(id.split("?")[0]);
|
|
551
|
+
const sourceFormat = scanner.sourceFormatOf(filePath);
|
|
552
|
+
if (!(sourceFormat ? sourceFilter(id) : filter(id)) || activeTransformFilter?.(code, id) === false) return;
|
|
356
553
|
await pending;
|
|
357
|
-
let
|
|
358
|
-
|
|
554
|
+
let lowered;
|
|
555
|
+
let compiledFile = filePath;
|
|
556
|
+
if (sourceFormat) {
|
|
557
|
+
const sourceFile = canonicalPath(filePath);
|
|
558
|
+
const format = moduleFormatOf(sourceFile);
|
|
559
|
+
if (!format) this.error(`${filePath} is outside ${dirname(resolveTsConfigPath())}, where '${sourceFormat.name}' modules are compiled from.`);
|
|
560
|
+
lowered = await withSourceLock(async () => {
|
|
561
|
+
const result = await lowerForTransform(sourceFile, format, code, (spec, importer) => this.resolve(spec, importer, { skipSelf: true }));
|
|
562
|
+
await syncProgramFiles();
|
|
563
|
+
return result;
|
|
564
|
+
});
|
|
565
|
+
if (!lowered) this.error(`${filePath} was removed while it was compiled.`);
|
|
566
|
+
if (lowered.error) this.error({
|
|
567
|
+
message: lowered.error.message,
|
|
568
|
+
id: filePath,
|
|
569
|
+
loc: lowered.error.loc && {
|
|
570
|
+
file: filePath,
|
|
571
|
+
...lowered.error.loc
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
compiledFile = `${sourceFile}.ts`;
|
|
575
|
+
for (const dependency of scanner.dependenciesOf(sourceFile)) this.addWatchFile(dependency);
|
|
576
|
+
}
|
|
577
|
+
const compiledCode = lowered ? lowered.code : code;
|
|
578
|
+
const watch = (path) => this.addWatchFile(path);
|
|
579
|
+
let result;
|
|
580
|
+
try {
|
|
581
|
+
result = await compileFile(compiledFile, watch);
|
|
582
|
+
} catch (error) {
|
|
583
|
+
if (!sources.has(compiledFile) || sources.get(compiledFile) === compiledCode) throw error;
|
|
584
|
+
}
|
|
585
|
+
if (filesByPath.has(compiledFile) && sources.get(compiledFile) !== compiledCode) {
|
|
586
|
+
if (lowered) analyzed.set(compiledFile, compiledCode);
|
|
359
587
|
await enqueueIndex(() => analyzer.updateFileContent([{
|
|
360
|
-
filePath,
|
|
361
|
-
content:
|
|
588
|
+
filePath: compiledFile,
|
|
589
|
+
content: compiledCode
|
|
362
590
|
}]));
|
|
363
|
-
result = await compileFile(
|
|
364
|
-
} else if (!result &&
|
|
365
|
-
|
|
591
|
+
result = await compileFile(compiledFile, watch);
|
|
592
|
+
} else if (!result && lowered) this.error(`${filePath} is not part of the TypeScript program of ${resolveTsConfigPath()}. Import it, or add it to the tsconfig's \`include\`.`);
|
|
593
|
+
else if (!result && options.disableTypeChecking !== true) await checkTypes(filePath, code);
|
|
594
|
+
let source = result?.magicString.toString() ?? code;
|
|
595
|
+
if (lowered && result) source = source.replaceAll(encodeURIComponent(`${filesByPath.get(compiledFile).file.filePath}@`), encodeURIComponent(`${filePath}@`));
|
|
366
596
|
const map = result?.magicString.generateMap({
|
|
367
|
-
source:
|
|
597
|
+
source: compiledFile,
|
|
368
598
|
includeContent: true,
|
|
369
599
|
hires: true
|
|
370
600
|
});
|
|
371
|
-
const transpiled = await transpile(source,
|
|
372
|
-
|
|
373
|
-
|
|
601
|
+
const transpiled = await transpile(source, compiledFile, map);
|
|
602
|
+
const outputMap = lowered && transpiled.map ? lowered.mapToSource(JSON.stringify(transpiled.map)) : transpiled.map;
|
|
603
|
+
if (outputMap) emittedMaps.set(filePath, typeof outputMap === "string" ? outputMap : JSON.stringify(outputMap));
|
|
604
|
+
return {
|
|
605
|
+
code: transpiled.code,
|
|
606
|
+
map: outputMap
|
|
607
|
+
};
|
|
374
608
|
},
|
|
375
609
|
async handleHotUpdate(ctx) {
|
|
376
610
|
if (sharedCtx ? sharedCtx.hmr.isIgnoredFile(ctx.file) : isIgnoredHmrFile(ctx.file)) return [];
|
|
377
611
|
const normPath = vite.normalizePath(ctx.file);
|
|
378
|
-
const
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
612
|
+
const sourceFile = canonicalPath(ctx.file);
|
|
613
|
+
const programFile = programFileOf(sourceFile);
|
|
614
|
+
const updates = await withSourceLock(async () => {
|
|
615
|
+
const relowered = reloweredByChange.get(sourceFile) ?? [];
|
|
616
|
+
reloweredByChange.delete(sourceFile);
|
|
617
|
+
const lowered = programFile ? sourceModules.get(programFile)?.lowered : void 0;
|
|
618
|
+
const componentUpdates = hmr && !lowered?.error && relowered.every((file) => file === programFile) ? await findComponentUpdates(programFile ?? normPath, lowered ? () => lowered.code : ctx.read) : [];
|
|
619
|
+
if (!programFile) await enqueueIndex(() => analyzer.invalidateFiles([{
|
|
620
|
+
filePath: ctx.file,
|
|
621
|
+
updateType: FILE_CHANGED
|
|
622
|
+
}]));
|
|
623
|
+
await syncProgramFiles();
|
|
624
|
+
return componentUpdates;
|
|
625
|
+
});
|
|
383
626
|
if (updates.length && await sendComponentUpdates(updates, ctx.server)) return [];
|
|
384
627
|
hmrUpdateCode.clear();
|
|
385
628
|
componentUpdates.classNames.clear();
|
|
@@ -407,6 +650,8 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
407
650
|
emittedMaps.clear();
|
|
408
651
|
sources.clear();
|
|
409
652
|
dependents.clear();
|
|
653
|
+
sourceModules.clear();
|
|
654
|
+
analyzed.clear();
|
|
410
655
|
releaseCssPreprocessorWorkers();
|
|
411
656
|
}
|
|
412
657
|
};
|