@analogjs/vite-plugin-angular 3.0.0-beta.46 → 3.0.0-beta.48
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/ngp/api.mjs +3439 -2681
- package/ngp/build.json +1 -1
- package/ngp/ng_analyze.cjs +98 -1
- package/ngp/ng_analyze_bg.wasm +0 -0
- package/package.json +1 -1
- package/src/index.d.ts +2 -0
- package/src/index.js +3 -1
- package/src/index.js.map +1 -1
- package/src/lib/compiler/utils.d.ts +0 -5
- package/src/lib/compiler/utils.js +11 -0
- package/src/lib/compiler/utils.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-api.d.ts +62 -0
- package/src/lib/fast-compile-api.js +12 -0
- package/src/lib/fast-compile-api.js.map +1 -0
- package/src/lib/fast-compile-plugin.js +39 -109
- 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 +290 -42
- 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
|
@@ -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
|
})();
|
|
@@ -246,7 +418,10 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
246
418
|
}));
|
|
247
419
|
}
|
|
248
420
|
async function emitHmrUpdate(file, className) {
|
|
249
|
-
const { result } = await processFile(file, Object.assign(Object.create(Object.getPrototypeOf(compiler)), compiler, {
|
|
421
|
+
const { result } = await processFile(file, Object.assign(Object.create(Object.getPrototypeOf(compiler)), compiler, {
|
|
422
|
+
hmrUpdateTarget: className,
|
|
423
|
+
reuseProcessedFiles: false
|
|
424
|
+
}), () => {});
|
|
250
425
|
return result.hmrUpdateModule && (await transpile(result.hmrUpdateModule, file)).code;
|
|
251
426
|
}
|
|
252
427
|
async function sendComponentUpdates(updates, server) {
|
|
@@ -255,11 +430,13 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
255
430
|
const codes = await Promise.all(updates.map(({ file, className }) => emitHmrUpdate(file, className)));
|
|
256
431
|
if (codes.some((code) => !code)) return false;
|
|
257
432
|
updates.forEach(({ file, className }, index) => {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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));
|
|
261
438
|
server.ws.send("angular:component-update", {
|
|
262
|
-
id: encodeURIComponent(`${filesByPath.get(file).file.filePath}@${className}`),
|
|
439
|
+
id: encodeURIComponent(`${sourceFile ?? filesByPath.get(file).file.filePath}@${className}`),
|
|
263
440
|
timestamp: Date.now()
|
|
264
441
|
});
|
|
265
442
|
});
|
|
@@ -302,6 +479,7 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
302
479
|
hmr = watchMode && options.liveReload && !options.isTest && config.server.hmr !== false;
|
|
303
480
|
const integrations = sharedCtx ? await sharedCtx.resolveConfig(config) : await discoverAnalogIntegrations(config);
|
|
304
481
|
transformFilter = integrations.transformFilter;
|
|
482
|
+
componentRegistries = integrations.componentRegistries;
|
|
305
483
|
stylePreprocessor = integrations.stylePreprocessor;
|
|
306
484
|
},
|
|
307
485
|
configureServer(server) {
|
|
@@ -310,6 +488,7 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
310
488
|
change: 2,
|
|
311
489
|
unlink: 3
|
|
312
490
|
};
|
|
491
|
+
devServer = server;
|
|
313
492
|
server.watcher.on("all", (event, path) => {
|
|
314
493
|
const type = changeTypes[event];
|
|
315
494
|
if (type) typeChecker?.notifyFileChanged(path, type);
|
|
@@ -322,15 +501,35 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
322
501
|
compiler = void 0;
|
|
323
502
|
});
|
|
324
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
|
+
},
|
|
325
521
|
async buildStart() {
|
|
326
522
|
activeBuilds++;
|
|
327
523
|
if (!analyzer) {
|
|
328
524
|
const tsconfig = resolveTsConfigPath();
|
|
329
|
-
const
|
|
525
|
+
const hasSourceFormats = sourceFormats.length > 0;
|
|
526
|
+
const ngp = await loadNgp(tsconfig, hasSourceFormats);
|
|
330
527
|
api = ngp.api;
|
|
528
|
+
if (hasSourceFormats) await lowerSourceModules(dirname(tsconfig), (specifier, importer) => this.resolve(specifier, importer, { skipSelf: true }));
|
|
331
529
|
analyzer = await api.createAnalyzer(tsconfig, {
|
|
332
530
|
...ngp.analyzerOptions,
|
|
333
|
-
enableHmr: hmr
|
|
531
|
+
enableHmr: hmr,
|
|
532
|
+
hostFs: hasSourceFormats ? createNgpHostFs((path) => sourceModules.get(path)?.lowered.code) : void 0
|
|
334
533
|
});
|
|
335
534
|
pending = indexChunks();
|
|
336
535
|
}
|
|
@@ -348,35 +547,82 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
348
547
|
},
|
|
349
548
|
async transform(code, id) {
|
|
350
549
|
const activeTransformFilter = sharedCtx?.transformFilter ?? transformFilter;
|
|
351
|
-
if (!filter(id) || activeTransformFilter?.(code, id) === false) return;
|
|
352
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;
|
|
353
553
|
await pending;
|
|
354
|
-
let
|
|
355
|
-
|
|
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);
|
|
356
587
|
await enqueueIndex(() => analyzer.updateFileContent([{
|
|
357
|
-
filePath,
|
|
358
|
-
content:
|
|
588
|
+
filePath: compiledFile,
|
|
589
|
+
content: compiledCode
|
|
359
590
|
}]));
|
|
360
|
-
result = await compileFile(
|
|
361
|
-
} else if (!result &&
|
|
362
|
-
|
|
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}@`));
|
|
363
596
|
const map = result?.magicString.generateMap({
|
|
364
|
-
source:
|
|
597
|
+
source: compiledFile,
|
|
365
598
|
includeContent: true,
|
|
366
599
|
hires: true
|
|
367
600
|
});
|
|
368
|
-
const transpiled = await transpile(source,
|
|
369
|
-
|
|
370
|
-
|
|
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
|
+
};
|
|
371
608
|
},
|
|
372
609
|
async handleHotUpdate(ctx) {
|
|
373
610
|
if (sharedCtx ? sharedCtx.hmr.isIgnoredFile(ctx.file) : isIgnoredHmrFile(ctx.file)) return [];
|
|
374
611
|
const normPath = vite.normalizePath(ctx.file);
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
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
|
+
});
|
|
380
626
|
if (updates.length && await sendComponentUpdates(updates, ctx.server)) return [];
|
|
381
627
|
hmrUpdateCode.clear();
|
|
382
628
|
componentUpdates.classNames.clear();
|
|
@@ -404,6 +650,8 @@ function ngpCompilePlugin(optionsOrCtx) {
|
|
|
404
650
|
emittedMaps.clear();
|
|
405
651
|
sources.clear();
|
|
406
652
|
dependents.clear();
|
|
653
|
+
sourceModules.clear();
|
|
654
|
+
analyzed.clear();
|
|
407
655
|
releaseCssPreprocessorWorkers();
|
|
408
656
|
}
|
|
409
657
|
};
|