@analogjs/vite-plugin-angular 3.0.0-beta.6 → 3.0.0-beta.60
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/migrations/migrate-setup-vitest/migrate-setup-vitest.js +3 -3
- package/migrations/migrate-setup-vitest/migrate-setup-vitest.js.map +1 -1
- package/ngp/LICENSE +21 -0
- package/ngp/api.mjs +46806 -0
- package/ngp/build.json +4 -0
- package/ngp/ng_analyze.cjs +601 -0
- package/ngp/ng_analyze_bg.wasm +0 -0
- package/package.json +5 -2
- package/src/index.d.ts +2 -0
- package/src/index.js +4 -3
- package/src/index.js.map +1 -1
- package/src/lib/analog-plugin-interop.js +2 -3
- package/src/lib/angular-build-context.d.ts +215 -0
- package/src/lib/angular-build-context.js +360 -0
- package/src/lib/angular-build-context.js.map +1 -0
- package/src/lib/angular-build-optimizer-plugin.d.ts +6 -4
- package/src/lib/angular-build-optimizer-plugin.js +25 -11
- package/src/lib/angular-build-optimizer-plugin.js.map +1 -1
- package/src/lib/angular-jit-plugin.d.ts +5 -3
- package/src/lib/angular-jit-plugin.js +16 -9
- package/src/lib/angular-jit-plugin.js.map +1 -1
- package/src/lib/angular-vite-plugin.d.ts +6 -109
- package/src/lib/angular-vite-plugin.js +9 -1251
- package/src/lib/angular-vite-plugin.js.map +1 -1
- package/src/lib/angular-vitest-plugin.d.ts +3 -2
- package/src/lib/angular-vitest-plugin.js +7 -5
- package/src/lib/angular-vitest-plugin.js.map +1 -1
- package/src/lib/companion-plugins.d.ts +7 -0
- package/src/lib/companion-plugins.js +37 -0
- package/src/lib/companion-plugins.js.map +1 -0
- package/src/lib/compilation-api/compilation-api-plugin.d.ts +11 -2
- package/src/lib/compilation-api/compilation-api-plugin.js +156 -285
- package/src/lib/compilation-api/compilation-api-plugin.js.map +1 -1
- package/src/lib/compilation-api/index.js +1 -2
- package/src/lib/compiler/angular-version.js +0 -1
- package/src/lib/compiler/class-field-lowering.js +1 -2
- package/src/lib/compiler/compile.d.ts +1 -1
- package/src/lib/compiler/compile.js +224 -151
- package/src/lib/compiler/compile.js.map +1 -1
- package/src/lib/compiler/constants.js +1 -2
- package/src/lib/compiler/debug.js +1 -2
- package/src/lib/compiler/defer.d.ts +2 -3
- package/src/lib/compiler/defer.js +13 -16
- package/src/lib/compiler/defer.js.map +1 -1
- package/src/lib/compiler/dts-reader.d.ts +6 -1
- package/src/lib/compiler/dts-reader.js +20 -12
- package/src/lib/compiler/dts-reader.js.map +1 -1
- package/src/lib/compiler/hmr.d.ts +4 -0
- package/src/lib/compiler/hmr.js +8 -4
- package/src/lib/compiler/hmr.js.map +1 -1
- package/src/lib/compiler/index.d.ts +2 -2
- package/src/lib/compiler/index.js +6 -6
- package/src/lib/compiler/jit-metadata.js +1 -2
- package/src/lib/compiler/jit-transform.js +37 -15
- package/src/lib/compiler/jit-transform.js.map +1 -1
- package/src/lib/compiler/js-emitter.d.ts +0 -3
- package/src/lib/compiler/js-emitter.js +4 -23
- package/src/lib/compiler/js-emitter.js.map +1 -1
- package/src/lib/compiler/metadata.js +2 -3
- package/src/lib/compiler/registry.d.ts +7 -1
- package/src/lib/compiler/registry.js +24 -5
- package/src/lib/compiler/registry.js.map +1 -1
- package/src/lib/compiler/resource-inliner.d.ts +8 -9
- package/src/lib/compiler/resource-inliner.js +11 -20
- package/src/lib/compiler/resource-inliner.js.map +1 -1
- package/src/lib/compiler/style-ast.d.ts +22 -1
- package/src/lib/compiler/style-ast.js +37 -20
- package/src/lib/compiler/style-ast.js.map +1 -1
- package/src/lib/compiler/type-elision.d.ts +4 -3
- package/src/lib/compiler/type-elision.js +11 -13
- package/src/lib/compiler/type-elision.js.map +1 -1
- package/src/lib/compiler/utils.d.ts +9 -7
- package/src/lib/compiler/utils.js +50 -11
- package/src/lib/compiler/utils.js.map +1 -1
- package/src/lib/compiler-plugin.js +1 -2
- package/src/lib/component-resolvers.js +1 -2
- package/src/lib/component-scanner.d.ts +74 -0
- package/src/lib/component-scanner.js +177 -0
- package/src/lib/component-scanner.js.map +1 -0
- package/src/lib/component-styles-plugin.d.ts +52 -0
- package/src/lib/component-styles-plugin.js +352 -0
- package/src/lib/component-styles-plugin.js.map +1 -0
- package/src/lib/compose-angular-plugins.d.ts +12 -0
- package/src/lib/compose-angular-plugins.js +30 -0
- package/src/lib/compose-angular-plugins.js.map +1 -0
- package/src/lib/dev-server-mode-plugin.d.ts +7 -0
- package/src/lib/dev-server-mode-plugin.js +22 -0
- package/src/lib/dev-server-mode-plugin.js.map +1 -0
- package/src/lib/encapsulation-plugin.js +1 -2
- package/src/lib/fast-compile-api.d.ts +62 -0
- package/src/lib/fast-compile-api.js +11 -0
- package/src/lib/fast-compile-api.js.map +1 -0
- package/src/lib/fast-compile-plugin.d.ts +6 -1
- package/src/lib/fast-compile-plugin.js +169 -110
- package/src/lib/fast-compile-plugin.js.map +1 -1
- package/src/lib/host.d.ts +3 -0
- package/src/lib/host.js +5 -4
- package/src/lib/host.js.map +1 -1
- package/src/lib/hot-update-batch.js +1 -2
- package/src/lib/live-reload-plugin.d.ts +6 -3
- package/src/lib/live-reload-plugin.js +14 -8
- package/src/lib/live-reload-plugin.js.map +1 -1
- package/src/lib/ngp-compile-api.d.ts +55 -0
- package/src/lib/ngp-compile-api.js +11 -0
- package/src/lib/ngp-compile-api.js.map +1 -0
- package/src/lib/ngp-compile-plugin.d.ts +4 -0
- package/src/lib/ngp-compile-plugin.js +672 -0
- package/src/lib/ngp-compile-plugin.js.map +1 -0
- package/src/lib/ngtsc-compile-plugin.d.ts +23 -0
- package/src/lib/ngtsc-compile-plugin.js +776 -0
- package/src/lib/ngtsc-compile-plugin.js.map +1 -0
- package/src/lib/nx-folder-plugin.d.ts +6 -5
- package/src/lib/nx-folder-plugin.js +13 -9
- package/src/lib/nx-folder-plugin.js.map +1 -1
- package/src/lib/plugins/file-replacements.plugin.d.ts +6 -0
- package/src/lib/plugins/file-replacements.plugin.js +2 -2
- package/src/lib/plugins/file-replacements.plugin.js.map +1 -1
- package/src/lib/router-plugin.d.ts +5 -1
- package/src/lib/router-plugin.js +14 -6
- package/src/lib/router-plugin.js.map +1 -1
- package/src/lib/style-preprocessor.js +1 -2
- package/src/lib/stylesheet-registry.js +1 -2
- package/src/lib/stylesheet-registry.js.map +1 -1
- package/src/lib/tools/src/builders/vite/vite-build.impl.js +2 -3
- package/src/lib/tools/src/builders/vite/vite-build.impl.js.map +1 -1
- package/src/lib/tools/src/builders/vite-dev-server/dev-server.impl.js +1 -2
- package/src/lib/tools/src/builders/vite-dev-server/dev-server.impl.js.map +1 -1
- package/src/lib/utils/compilation-shared.js +3 -4
- package/src/lib/utils/css-extension-resolver.js +1 -2
- package/src/lib/utils/css-preprocessor-workers.js +1 -2
- package/src/lib/utils/debug-harness.js +1 -2
- package/src/lib/utils/debug-log-file.js +1 -2
- package/src/lib/utils/debug.js +1 -2
- package/src/lib/utils/devkit.js +2 -3
- package/src/lib/utils/hmr-component-updates.d.ts +7 -0
- package/src/lib/utils/hmr-component-updates.js +86 -0
- package/src/lib/utils/hmr-component-updates.js.map +1 -0
- package/src/lib/utils/jit-inline-styles.js +1 -2
- package/src/lib/utils/lsp-type-checker.d.ts +75 -0
- package/src/lib/utils/lsp-type-checker.js +532 -0
- package/src/lib/utils/lsp-type-checker.js.map +1 -0
- package/src/lib/utils/ngp-host-fs.d.ts +43 -0
- package/src/lib/utils/ngp-host-fs.js +128 -0
- package/src/lib/utils/ngp-host-fs.js.map +1 -0
- package/src/lib/utils/plugin-config.d.ts +15 -8
- package/src/lib/utils/plugin-config.js +19 -7
- package/src/lib/utils/plugin-config.js.map +1 -1
- package/src/lib/utils/rolldown.js +1 -2
- package/src/lib/utils/safe-module-paths.js +1 -2
- package/src/lib/utils/stylesheet-warning.d.ts +2 -0
- package/src/lib/utils/stylesheet-warning.js +10 -0
- package/src/lib/utils/stylesheet-warning.js.map +1 -0
- package/src/lib/utils/transform-cache.js +1 -2
- package/src/lib/utils/transform-cache.js.map +1 -1
- package/src/lib/utils/tsconfig-resolver.js +2 -3
- package/src/lib/utils/virtual-ids.js +1 -2
- package/src/lib/utils/virtual-resources.js +1 -2
- package/src/lib/utils/virtual-resources.js.map +1 -1
- package/src/lib/virtual-modules-plugin.d.ts +11 -3
- package/src/lib/virtual-modules-plugin.js +70 -15
- package/src/lib/virtual-modules-plugin.js.map +1 -1
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
import { discoverAnalogIntegrations } from "./analog-plugin-interop.js";
|
|
2
|
+
import { createAngularBuildContext, isAngularBuildContext } from "./angular-build-context.js";
|
|
3
|
+
import { ComponentScanner } from "./component-scanner.js";
|
|
4
|
+
import { preprocessStylesheet } from "./stylesheet-registry.js";
|
|
5
|
+
import { isIgnoredHmrFile } from "./utils/compilation-shared.js";
|
|
6
|
+
import { TS_EXT_REGEX, createDepOptimizerConfig, getTsConfigPath, isProdMode } from "./utils/plugin-config.js";
|
|
7
|
+
import { releaseCssPreprocessorWorkers } from "./utils/css-preprocessor-workers.js";
|
|
8
|
+
import { angularFullVersion } from "./utils/devkit.js";
|
|
9
|
+
import { findHmrComponentUpdates } from "./utils/hmr-component-updates.js";
|
|
10
|
+
import { LspTypeChecker } from "./utils/lsp-type-checker.js";
|
|
11
|
+
import { canonicalPath, createNgpHostFs, fromWasmPath, toWasmPath } from "./utils/ngp-host-fs.js";
|
|
12
|
+
import { isRolldown } from "./utils/rolldown.js";
|
|
13
|
+
import { loadAngularVirtualModule, resolveAngularVirtualId } from "./virtual-modules-plugin.js";
|
|
14
|
+
import { existsSync, promises, realpathSync } from "node:fs";
|
|
15
|
+
import { createRequire } from "node:module";
|
|
16
|
+
import { dirname, resolve } from "node:path";
|
|
17
|
+
import * as vite from "vite";
|
|
18
|
+
import * as compilerCli from "@angular/compiler-cli";
|
|
19
|
+
import { glob } from "tinyglobby";
|
|
20
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
21
|
+
// 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;
|
|
26
|
+
function isFastCompileOptions(value) {
|
|
27
|
+
return typeof value === "object" && value !== null && !isAngularBuildContext(value) && "tsconfigGetter" in value && typeof value.tsconfigGetter === "function";
|
|
28
|
+
}
|
|
29
|
+
var BUNDLED_NGP = new URL("../../ngp/", "" + import.meta.url);
|
|
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) {
|
|
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.");
|
|
37
|
+
let modulePath = process.env["ANALOG_NGP_COMPILER"];
|
|
38
|
+
if (!modulePath) try {
|
|
39
|
+
modulePath = createRequire(tsconfig).resolve("@angular/ngp");
|
|
40
|
+
} catch {
|
|
41
|
+
if (angularFullVersion < 220200) throw new Error("[@analogjs/vite-plugin-angular]: compilation: 'ngp' requires Angular v22.2 or later.");
|
|
42
|
+
return {
|
|
43
|
+
api: await import(new URL("api.mjs", BUNDLED_NGP).href),
|
|
44
|
+
analyzerOptions: nativeBinding ? { nativeBinding } : {
|
|
45
|
+
backend: "wasm",
|
|
46
|
+
wasmBinding: fileURLToPath(new URL("ng_analyze.cjs", BUNDLED_NGP))
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
api: await import(pathToFileURL(resolve(modulePath)).href),
|
|
52
|
+
analyzerOptions: nativeBinding ? { nativeBinding } : needsHostFs ? { backend: "wasm" } : {}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function transpile(source, filePath, map) {
|
|
56
|
+
return vite.transformWithOxc ? vite.transformWithOxc(source, filePath, {
|
|
57
|
+
lang: "ts",
|
|
58
|
+
sourcemap: true
|
|
59
|
+
}, map) : vite.transformWithEsbuild(source, filePath, {
|
|
60
|
+
loader: "ts",
|
|
61
|
+
sourcemap: true
|
|
62
|
+
}, map);
|
|
63
|
+
}
|
|
64
|
+
function ngpCompilePlugin(optionsOrCtx) {
|
|
65
|
+
const sharedCtx = isAngularBuildContext(optionsOrCtx) ? optionsOrCtx : isFastCompileOptions(optionsOrCtx) ? void 0 : createAngularBuildContext({
|
|
66
|
+
jit: false,
|
|
67
|
+
...optionsOrCtx,
|
|
68
|
+
compilationBackend: "ngp"
|
|
69
|
+
});
|
|
70
|
+
const options = sharedCtx ? sharedCtx.options : optionsOrCtx;
|
|
71
|
+
if (options.jit || options.fastCompileMode === "partial") throw new Error("compilation: 'ngp' requires jit: false and fastCompileMode: 'full'.");
|
|
72
|
+
let configContext;
|
|
73
|
+
let resolvedConfig;
|
|
74
|
+
let transformFilter;
|
|
75
|
+
let componentRegistries = [];
|
|
76
|
+
let stylePreprocessor;
|
|
77
|
+
let watchMode = false;
|
|
78
|
+
let hmr = false;
|
|
79
|
+
let activeBuilds = 0;
|
|
80
|
+
let analyzer;
|
|
81
|
+
let compiler;
|
|
82
|
+
let typeChecker;
|
|
83
|
+
let api;
|
|
84
|
+
let toAnalyzerPath = (path) => path;
|
|
85
|
+
let fromAnalyzerPath = vite.normalizePath;
|
|
86
|
+
const filesByPath = /* @__PURE__ */ new Map();
|
|
87
|
+
const chunkContexts = /* @__PURE__ */ new Map();
|
|
88
|
+
const output = /* @__PURE__ */ new Map();
|
|
89
|
+
const emittedMaps = /* @__PURE__ */ new Map();
|
|
90
|
+
const sources = /* @__PURE__ */ new Map();
|
|
91
|
+
const dependents = /* @__PURE__ */ new Map();
|
|
92
|
+
const hmrUpdateCode = /* @__PURE__ */ new Map();
|
|
93
|
+
const classNames = sharedCtx ? sharedCtx.hmr.classNames : /* @__PURE__ */ new Map();
|
|
94
|
+
const fileEmitter = (file, className) => ({ hmrUpdateCode: hmrUpdateCode.get(`${file}@${className}`) });
|
|
95
|
+
sharedCtx?.hmr.setFileEmitter(fileEmitter);
|
|
96
|
+
const componentUpdates = {
|
|
97
|
+
classNames,
|
|
98
|
+
fileEmitter
|
|
99
|
+
};
|
|
100
|
+
let pending = Promise.resolve();
|
|
101
|
+
const excluded = [
|
|
102
|
+
/node_modules/,
|
|
103
|
+
/[?&]raw\b/,
|
|
104
|
+
/type=script/,
|
|
105
|
+
/@ng\/component/
|
|
106
|
+
];
|
|
107
|
+
const filter = vite.createFilter(TS_EXT_REGEX, excluded);
|
|
108
|
+
const sourceFilter = vite.createFilter(null, excluded);
|
|
109
|
+
const sourceFormats = [];
|
|
110
|
+
const scanner = new ComponentScanner();
|
|
111
|
+
const sourceModules = /* @__PURE__ */ new Map();
|
|
112
|
+
const removals = /* @__PURE__ */ new Map();
|
|
113
|
+
const analyzed = /* @__PURE__ */ new Map();
|
|
114
|
+
const includes = /* @__PURE__ */ new Map();
|
|
115
|
+
const moduleFormatOf = (file) => sourceFormats.find((format) => format.test(file) && includes.get(format)?.(file));
|
|
116
|
+
let devServer;
|
|
117
|
+
const reloweredByChange = /* @__PURE__ */ new Map();
|
|
118
|
+
const programFileOf = (file) => {
|
|
119
|
+
const programFile = `${file}.ts`;
|
|
120
|
+
return sourceModules.has(programFile) ? programFile : void 0;
|
|
121
|
+
};
|
|
122
|
+
const sourceFileOf = (file) => sourceModules.get(file)?.sourceFile ?? file;
|
|
123
|
+
function resolveTsConfigPath() {
|
|
124
|
+
if (sharedCtx) return sharedCtx.resolveTsConfigPath();
|
|
125
|
+
const ctx = configContext ?? {
|
|
126
|
+
root: resolvedConfig?.root || ".",
|
|
127
|
+
isProd: isProdMode(resolvedConfig?.mode),
|
|
128
|
+
isLib: !!resolvedConfig?.build?.lib
|
|
129
|
+
};
|
|
130
|
+
return getTsConfigPath(ctx.root, options.tsconfigGetter(), ctx.isProd, options.isTest, ctx.isLib, options.workspaceRoot);
|
|
131
|
+
}
|
|
132
|
+
const compilerApi = {
|
|
133
|
+
angularCompiler: true,
|
|
134
|
+
getTsConfigPath: resolveTsConfigPath,
|
|
135
|
+
getSourceMap: (id) => emittedMaps.get(vite.normalizePath(id)),
|
|
136
|
+
hasEmitted: (id) => filesByPath.has(vite.normalizePath(id)),
|
|
137
|
+
output,
|
|
138
|
+
ready: () => pending,
|
|
139
|
+
componentUpdates,
|
|
140
|
+
ngp: { registerSourceFormat(format) {
|
|
141
|
+
if (!sourceFormats.includes(format)) {
|
|
142
|
+
sourceFormats.push(format);
|
|
143
|
+
scanner.registerSourceFormat(format);
|
|
144
|
+
}
|
|
145
|
+
} }
|
|
146
|
+
};
|
|
147
|
+
/** Lowers a module of a source format. Returns whether its TypeScript changed. */
|
|
148
|
+
async function lowerSourceModule(sourceFile, source, format, resolve) {
|
|
149
|
+
const programFile = `${sourceFile}.ts`;
|
|
150
|
+
if (existsSync(programFile)) throw new Error(`[@analogjs/vite-plugin-angular]: ${programFile} has the name NGP compiles ${sourceFile} as. Rename one of them.`);
|
|
151
|
+
const removed = removals.get(programFile);
|
|
152
|
+
const lowered = await format.lower(source, sourceFile, {
|
|
153
|
+
scanImports: (specifiers) => scanner.scanImports(specifiers, sourceFile, resolve),
|
|
154
|
+
resolveSelector: (className) => scanner.lookupSelector(className, sharedCtx?.componentRegistries ?? componentRegistries)
|
|
155
|
+
});
|
|
156
|
+
if (removals.get(programFile) !== removed) return false;
|
|
157
|
+
const changed = sourceModules.get(programFile)?.lowered.code !== lowered.code;
|
|
158
|
+
sourceModules.set(programFile, {
|
|
159
|
+
sourceFile,
|
|
160
|
+
source,
|
|
161
|
+
format,
|
|
162
|
+
lowered
|
|
163
|
+
});
|
|
164
|
+
return changed;
|
|
165
|
+
}
|
|
166
|
+
/** Lowers every module of the registered source formats under `root`. */
|
|
167
|
+
async function lowerSourceModules(root, resolve) {
|
|
168
|
+
scanner.reset(root);
|
|
169
|
+
sourceModules.clear();
|
|
170
|
+
const sourceRoot = vite.normalizePath(realpathSync(root));
|
|
171
|
+
await Promise.all(sourceFormats.map(async (format) => {
|
|
172
|
+
includes.set(format, vite.createFilter(format.include, "**/node_modules/**", { resolve: sourceRoot }));
|
|
173
|
+
const files = await glob(format.include, {
|
|
174
|
+
cwd: sourceRoot,
|
|
175
|
+
absolute: true,
|
|
176
|
+
ignore: ["**/node_modules/**"]
|
|
177
|
+
});
|
|
178
|
+
await Promise.all(files.map(async (file) => {
|
|
179
|
+
const sourceFile = canonicalPath(file);
|
|
180
|
+
await lowerSourceModule(sourceFile, await promises.readFile(sourceFile, "utf-8"), format, resolve);
|
|
181
|
+
}));
|
|
182
|
+
}));
|
|
183
|
+
analyzed.clear();
|
|
184
|
+
for (const [programFile, { lowered }] of sourceModules) analyzed.set(programFile, lowered.code);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Lowers the source modules a change to `file` affects again: the module
|
|
188
|
+
* itself, and those that looked up selectors in it. Returns the program
|
|
189
|
+
* files whose TypeScript changed.
|
|
190
|
+
*/
|
|
191
|
+
async function relowerSourceModules(file, resolve) {
|
|
192
|
+
const affected = [file, ...scanner.importersByFile.get(file) ?? []].map(programFileOf).filter((programFile) => programFile !== void 0);
|
|
193
|
+
scanner.invalidate(file);
|
|
194
|
+
return (await Promise.all(affected.map(async (programFile) => {
|
|
195
|
+
const sourceModule = sourceModules.get(programFile);
|
|
196
|
+
if (!sourceModule) return [];
|
|
197
|
+
const { sourceFile, format } = sourceModule;
|
|
198
|
+
const source = await readSource(sourceFile);
|
|
199
|
+
if (source === void 0) return [];
|
|
200
|
+
return await lowerSourceModule(sourceFile, source, format, resolve) ? [programFile] : [];
|
|
201
|
+
}))).flat();
|
|
202
|
+
}
|
|
203
|
+
let sourceWork = Promise.resolve();
|
|
204
|
+
function withSourceLock(work) {
|
|
205
|
+
const run = sourceWork.catch(() => void 0).then(work);
|
|
206
|
+
sourceWork = run;
|
|
207
|
+
return run;
|
|
208
|
+
}
|
|
209
|
+
/** Sends the analyzer the lowered modules it doesn't have yet. */
|
|
210
|
+
async function syncProgramFiles() {
|
|
211
|
+
const updates = [...sourceModules].filter(([filePath, { lowered }]) => analyzed.get(filePath) !== lowered.code).map(([filePath, { lowered }]) => ({
|
|
212
|
+
filePath,
|
|
213
|
+
content: lowered.code
|
|
214
|
+
}));
|
|
215
|
+
if (updates.length === 0) return;
|
|
216
|
+
for (const { filePath, content } of updates) analyzed.set(filePath, content);
|
|
217
|
+
await enqueueIndex(() => analyzer.updateFileContent(updates.map(({ filePath, content }) => ({
|
|
218
|
+
filePath: toAnalyzerPath(filePath),
|
|
219
|
+
content
|
|
220
|
+
}))));
|
|
221
|
+
}
|
|
222
|
+
/** Reads a source file, or `undefined` if it was removed. */
|
|
223
|
+
function readSource(file) {
|
|
224
|
+
return promises.readFile(file, "utf-8").catch((error) => {
|
|
225
|
+
if (error.code === "ENOENT") return void 0;
|
|
226
|
+
throw error;
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
/** Lowers a module added after the build started, if NGP compiles it. */
|
|
230
|
+
async function addSourceModule(sourceFile, format, source, resolve) {
|
|
231
|
+
const programFile = `${sourceFile}.ts`;
|
|
232
|
+
await lowerSourceModule(sourceFile, source, format, resolve);
|
|
233
|
+
const sourceModule = sourceModules.get(programFile);
|
|
234
|
+
if (!sourceModule) return;
|
|
235
|
+
analyzed.set(programFile, sourceModule.lowered.code);
|
|
236
|
+
await enqueueIndex(() => analyzer.invalidateFiles([{
|
|
237
|
+
filePath: toAnalyzerPath(programFile),
|
|
238
|
+
updateType: FILE_CREATED
|
|
239
|
+
}]));
|
|
240
|
+
}
|
|
241
|
+
/** Drops a removed module, and any lowering of it still in progress. */
|
|
242
|
+
async function removeSourceModule(sourceFile) {
|
|
243
|
+
const programFile = `${sourceFile}.ts`;
|
|
244
|
+
removals.set(programFile, (removals.get(programFile) ?? 0) + 1);
|
|
245
|
+
analyzed.delete(programFile);
|
|
246
|
+
if (!sourceModules.delete(programFile)) return;
|
|
247
|
+
await enqueueIndex(() => analyzer.invalidateFiles([{
|
|
248
|
+
filePath: toAnalyzerPath(programFile),
|
|
249
|
+
updateType: FILE_DELETED
|
|
250
|
+
}]));
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Lowers a module for its transform, as fast compile does on every
|
|
254
|
+
* transform, so the selectors it looks up are current. A transform that
|
|
255
|
+
* started before a hot update can carry the previous code; it compiles the
|
|
256
|
+
* latest lowering instead.
|
|
257
|
+
*/
|
|
258
|
+
async function lowerForTransform(sourceFile, format, code, resolve) {
|
|
259
|
+
const programFile = `${sourceFile}.ts`;
|
|
260
|
+
if (!sourceModules.has(programFile)) await addSourceModule(sourceFile, format, code, resolve);
|
|
261
|
+
else if (code === await readSource(sourceFile)) await lowerSourceModule(sourceFile, code, format, resolve);
|
|
262
|
+
return sourceModules.get(programFile)?.lowered;
|
|
263
|
+
}
|
|
264
|
+
/** Maps a type error in a program file to the source module's location. */
|
|
265
|
+
function toSourceLocation(location) {
|
|
266
|
+
const sourceModule = sourceModules.get(vite.normalizePath(location.file));
|
|
267
|
+
const mapped = sourceModule?.lowered.toSourceLocation(location.line - 1, location.character - 1);
|
|
268
|
+
return mapped ? {
|
|
269
|
+
file: sourceModule.sourceFile,
|
|
270
|
+
line: mapped.line + 1,
|
|
271
|
+
character: mapped.column + 1
|
|
272
|
+
} : location;
|
|
273
|
+
}
|
|
274
|
+
sharedCtx?.registerCompiler({
|
|
275
|
+
name: "@analogjs/vite-plugin-angular-ngp",
|
|
276
|
+
getTsConfigPath: compilerApi.getTsConfigPath,
|
|
277
|
+
getSourceMap: compilerApi.getSourceMap,
|
|
278
|
+
hasEmitted: compilerApi.hasEmitted
|
|
279
|
+
});
|
|
280
|
+
async function prepareResources(file, watch) {
|
|
281
|
+
const hostFilePath = fromAnalyzerPath(file.filePath);
|
|
282
|
+
watch(hostFilePath);
|
|
283
|
+
const activeConfig = sharedCtx?.resolvedConfig ?? resolvedConfig;
|
|
284
|
+
const activeStylePreprocessor = sharedCtx?.stylePreprocessor ?? stylePreprocessor;
|
|
285
|
+
return {
|
|
286
|
+
...file,
|
|
287
|
+
classes: await Promise.all(file.classes.map(async (metadata, classIndex) => {
|
|
288
|
+
const component = metadata.component;
|
|
289
|
+
if (!component) return metadata;
|
|
290
|
+
if (component.templateUrl) watch(fromAnalyzerPath(component.templateUrl.resolvedPath));
|
|
291
|
+
for (const style of component.styleUrls ?? []) watch(fromAnalyzerPath(style.resolvedPath));
|
|
292
|
+
const preprocess = async (content, path, order, inline) => {
|
|
293
|
+
const preprocessed = preprocessStylesheet(content, path, activeStylePreprocessor, {
|
|
294
|
+
filename: path,
|
|
295
|
+
containingFile: hostFilePath,
|
|
296
|
+
resourceFile: inline ? void 0 : path,
|
|
297
|
+
className: metadata.className,
|
|
298
|
+
order,
|
|
299
|
+
inline
|
|
300
|
+
});
|
|
301
|
+
const result = await vite.preprocessCSS(preprocessed, path, activeConfig);
|
|
302
|
+
result.deps?.forEach(watch);
|
|
303
|
+
return result.code;
|
|
304
|
+
};
|
|
305
|
+
return {
|
|
306
|
+
...metadata,
|
|
307
|
+
component: {
|
|
308
|
+
...component,
|
|
309
|
+
styles: component.styles && await Promise.all(component.styles.map((style, index) => preprocess(style, `${hostFilePath}.inline-${classIndex}-${index}.${options.inlineStylesExtension}`, index, true))),
|
|
310
|
+
stylesFromUrls: component.stylesFromUrls && await Promise.all(component.stylesFromUrls.map((style, index) => preprocess(style, fromAnalyzerPath(component.styleUrls[index].resolvedPath), index, false)))
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
}))
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
async function indexChunks(update) {
|
|
317
|
+
if (update) {
|
|
318
|
+
const invalidated = await update();
|
|
319
|
+
for (const filePath of invalidated) {
|
|
320
|
+
output.delete(fromAnalyzerPath(filePath));
|
|
321
|
+
compiler.fileCache.delete(filePath);
|
|
322
|
+
}
|
|
323
|
+
chunkContexts.clear();
|
|
324
|
+
} else {
|
|
325
|
+
filesByPath.clear();
|
|
326
|
+
chunkContexts.clear();
|
|
327
|
+
output.clear();
|
|
328
|
+
const angularOptions = compilerCli.readConfiguration(resolveTsConfigPath()).options;
|
|
329
|
+
compiler = new api.HybridCompiler(analyzer, {
|
|
330
|
+
...angularOptions,
|
|
331
|
+
optimize: true,
|
|
332
|
+
enableHmr: hmr,
|
|
333
|
+
tcbConfig: api.buildTypeCheckingConfig(angularOptions)
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
const chunks = update ? compiler.analyzeOptimizedDelta() : compiler.analyzeOptimized();
|
|
337
|
+
for await (const chunk of chunks) {
|
|
338
|
+
for (const file of chunk.files) {
|
|
339
|
+
const normPath = fromAnalyzerPath(file.filePath);
|
|
340
|
+
filesByPath.set(normPath, {
|
|
341
|
+
file,
|
|
342
|
+
chunk
|
|
343
|
+
});
|
|
344
|
+
output.delete(normPath);
|
|
345
|
+
compiler.fileCache.delete(file.filePath);
|
|
346
|
+
}
|
|
347
|
+
for (const removed of chunk.removedFiles ?? []) {
|
|
348
|
+
const normPath = fromAnalyzerPath(removed);
|
|
349
|
+
filesByPath.delete(normPath);
|
|
350
|
+
output.delete(normPath);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
function enqueueIndex(update) {
|
|
355
|
+
pending = pending.catch(() => {}).then(() => indexChunks(update));
|
|
356
|
+
return pending;
|
|
357
|
+
}
|
|
358
|
+
function getChunkContext(chunk) {
|
|
359
|
+
let context = chunkContexts.get(chunk);
|
|
360
|
+
if (!context) {
|
|
361
|
+
context = compiler.prepareChunk(chunk);
|
|
362
|
+
chunkContexts.set(chunk, context);
|
|
363
|
+
}
|
|
364
|
+
return context;
|
|
365
|
+
}
|
|
366
|
+
async function checkTypes(filePath, sourceCode, tcbCode) {
|
|
367
|
+
typeChecker ??= new LspTypeChecker(resolveTsConfigPath(), options.typeChecker, {
|
|
368
|
+
inMemoryFiles: () => new Map([...sourceModules].map(([file, { lowered }]) => [file, lowered.code])),
|
|
369
|
+
mapLocation: toSourceLocation
|
|
370
|
+
});
|
|
371
|
+
typeChecker.queueFile(filePath, sourceCode, tcbCode);
|
|
372
|
+
if (!watchMode) return;
|
|
373
|
+
const typeErrors = await typeChecker.flushQueue(true);
|
|
374
|
+
if (typeErrors.length) throw new Error(typeErrors.join("\n"));
|
|
375
|
+
}
|
|
376
|
+
async function processFile(normPath, activeCompiler, watch) {
|
|
377
|
+
const { file: analyzed, chunk } = filesByPath.get(normPath);
|
|
378
|
+
const [file, context, content] = await Promise.all([
|
|
379
|
+
prepareResources(analyzed, (path) => {
|
|
380
|
+
const key = sourceFileOf(vite.normalizePath(path));
|
|
381
|
+
dependents.set(key, (dependents.get(key) ?? /* @__PURE__ */ new Set()).add(normPath));
|
|
382
|
+
watch(key);
|
|
383
|
+
}),
|
|
384
|
+
getChunkContext(chunk),
|
|
385
|
+
analyzer.getFileContent(analyzed.filePath)
|
|
386
|
+
]);
|
|
387
|
+
sources.set(normPath, content);
|
|
388
|
+
return {
|
|
389
|
+
file,
|
|
390
|
+
content,
|
|
391
|
+
result: activeCompiler.processFile.length >= 5 ? await activeCompiler.processFile(file.filePath, content, void 0, file, context) : await activeCompiler.processFile(file.filePath, content, file, context)
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
function compileFile(normPath, watch) {
|
|
395
|
+
if (!filesByPath.has(normPath)) return void 0;
|
|
396
|
+
let task = output.get(normPath);
|
|
397
|
+
if (!task) {
|
|
398
|
+
task = (async () => {
|
|
399
|
+
await pending;
|
|
400
|
+
if (!filesByPath.has(normPath)) throw new Error(`${sourceFileOf(normPath)} was removed.`);
|
|
401
|
+
const { file, content, result } = await processFile(normPath, compiler, watch);
|
|
402
|
+
const errors = [...file.diagnostics ?? [], ...result.diagnostics ?? []].filter((diagnostic) => diagnostic.category === 1);
|
|
403
|
+
if (errors.length) throw new Error(`${sourceFileOf(normPath)}: ${errors.map((error) => error.messageText).join("\n")}`);
|
|
404
|
+
if (options.disableTypeChecking !== true) await checkTypes(fromAnalyzerPath(file.filePath), content, result.tcb?.code);
|
|
405
|
+
return result;
|
|
406
|
+
})();
|
|
407
|
+
output.set(normPath, task);
|
|
408
|
+
}
|
|
409
|
+
return task;
|
|
410
|
+
}
|
|
411
|
+
function componentNames(normPath) {
|
|
412
|
+
return (filesByPath.get(normPath)?.file.classes ?? []).flatMap(({ className, component }) => component && className ? [className] : []);
|
|
413
|
+
}
|
|
414
|
+
async function findComponentUpdates(normPath, read) {
|
|
415
|
+
if (!TS_EXT_REGEX.test(normPath)) return [...dependents.get(normPath) ?? []].flatMap((file) => componentNames(file).map((className) => ({
|
|
416
|
+
file,
|
|
417
|
+
className
|
|
418
|
+
})));
|
|
419
|
+
const stale = sources.get(normPath);
|
|
420
|
+
if (stale === void 0) return [];
|
|
421
|
+
return (findHmrComponentUpdates(normPath, stale, await read(), new Set(componentNames(normPath))) ?? []).map((className) => ({
|
|
422
|
+
file: normPath,
|
|
423
|
+
className
|
|
424
|
+
}));
|
|
425
|
+
}
|
|
426
|
+
async function emitHmrUpdate(file, className) {
|
|
427
|
+
const { result } = await processFile(file, Object.assign(Object.create(Object.getPrototypeOf(compiler)), compiler, {
|
|
428
|
+
hmrUpdateTarget: className,
|
|
429
|
+
reuseProcessedFiles: false
|
|
430
|
+
}), () => {});
|
|
431
|
+
return result.hmrUpdateModule && (await transpile(result.hmrUpdateModule, file)).code;
|
|
432
|
+
}
|
|
433
|
+
async function sendComponentUpdates(updates, server) {
|
|
434
|
+
try {
|
|
435
|
+
for (const file of new Set(updates.map((update) => update.file))) await compileFile(file, () => {});
|
|
436
|
+
const codes = await Promise.all(updates.map(({ file, className }) => emitHmrUpdate(file, className)));
|
|
437
|
+
if (codes.some((code) => !code)) return false;
|
|
438
|
+
updates.forEach(({ file, className }, index) => {
|
|
439
|
+
const moduleFile = sourceModules.get(file)?.sourceFile ?? file;
|
|
440
|
+
hmrUpdateCode.set(`${moduleFile}@${className}`, codes[index]);
|
|
441
|
+
componentUpdates.classNames.set(moduleFile, className);
|
|
442
|
+
server.moduleGraph.getModulesByFile(moduleFile)?.forEach((mod) => server.moduleGraph.invalidateModule(mod));
|
|
443
|
+
server.ws.send("angular:component-update", {
|
|
444
|
+
id: encodeURIComponent(`${moduleFile}@${className}`),
|
|
445
|
+
timestamp: Date.now()
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
return true;
|
|
449
|
+
} catch {
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return {
|
|
454
|
+
name: "@analogjs/vite-plugin-angular-ngp",
|
|
455
|
+
enforce: "pre",
|
|
456
|
+
api: compilerApi,
|
|
457
|
+
config(config, { command, mode }) {
|
|
458
|
+
watchMode = command === "serve";
|
|
459
|
+
const isProd = isProdMode(config.mode);
|
|
460
|
+
if (sharedCtx) sharedCtx.initConfig(config, {
|
|
461
|
+
command,
|
|
462
|
+
mode
|
|
463
|
+
});
|
|
464
|
+
else configContext = {
|
|
465
|
+
root: config.root || ".",
|
|
466
|
+
isProd,
|
|
467
|
+
isLib: !!config.build?.lib
|
|
468
|
+
};
|
|
469
|
+
const depOptimizer = createDepOptimizerConfig({
|
|
470
|
+
tsconfig: resolveTsConfigPath(),
|
|
471
|
+
isProd,
|
|
472
|
+
jit: false,
|
|
473
|
+
watchMode,
|
|
474
|
+
isTest: options.isTest,
|
|
475
|
+
isAstroIntegration: options.isAstroIntegration
|
|
476
|
+
});
|
|
477
|
+
return {
|
|
478
|
+
...isRolldown() ? { oxc: {} } : { esbuild: false },
|
|
479
|
+
...depOptimizer
|
|
480
|
+
};
|
|
481
|
+
},
|
|
482
|
+
async configResolved(config) {
|
|
483
|
+
resolvedConfig = config;
|
|
484
|
+
hmr = watchMode && options.liveReload && !options.isTest && config.server.hmr !== false;
|
|
485
|
+
const integrations = sharedCtx ? await sharedCtx.resolveConfig(config) : await discoverAnalogIntegrations(config);
|
|
486
|
+
transformFilter = integrations.transformFilter;
|
|
487
|
+
componentRegistries = integrations.componentRegistries;
|
|
488
|
+
stylePreprocessor = integrations.stylePreprocessor;
|
|
489
|
+
},
|
|
490
|
+
configureServer(server) {
|
|
491
|
+
const changeTypes = {
|
|
492
|
+
add: 1,
|
|
493
|
+
change: 2,
|
|
494
|
+
unlink: 3
|
|
495
|
+
};
|
|
496
|
+
devServer = server;
|
|
497
|
+
server.watcher.on("all", (event, path) => {
|
|
498
|
+
const type = changeTypes[event];
|
|
499
|
+
if (type) typeChecker?.notifyFileChanged(path, type);
|
|
500
|
+
});
|
|
501
|
+
server.httpServer?.on("close", () => {
|
|
502
|
+
typeChecker?.close();
|
|
503
|
+
typeChecker = void 0;
|
|
504
|
+
analyzer?.close();
|
|
505
|
+
analyzer = void 0;
|
|
506
|
+
compiler = void 0;
|
|
507
|
+
});
|
|
508
|
+
},
|
|
509
|
+
async watchChange(id, { event }) {
|
|
510
|
+
if (!analyzer || sourceFormats.length === 0) return;
|
|
511
|
+
const file = canonicalPath(id);
|
|
512
|
+
const resolve = (spec, importer) => this.resolve(spec, importer, { skipSelf: true });
|
|
513
|
+
await withSourceLock(async () => {
|
|
514
|
+
scanner.invalidate(file);
|
|
515
|
+
const format = moduleFormatOf(file);
|
|
516
|
+
if (event === "update") reloweredByChange.set(file, await relowerSourceModules(file, resolve));
|
|
517
|
+
else if (event === "delete") await removeSourceModule(file);
|
|
518
|
+
else if (event === "create" && format && !sourceModules.has(`${file}.ts`)) {
|
|
519
|
+
const source = await readSource(file);
|
|
520
|
+
if (source !== void 0) await addSourceModule(file, format, source, resolve);
|
|
521
|
+
}
|
|
522
|
+
await syncProgramFiles();
|
|
523
|
+
});
|
|
524
|
+
for (const importer of scanner.importersByFile.get(file) ?? []) devServer?.moduleGraph.getModulesByFile(importer)?.forEach((mod) => devServer.moduleGraph.invalidateModule(mod));
|
|
525
|
+
},
|
|
526
|
+
async buildStart() {
|
|
527
|
+
activeBuilds++;
|
|
528
|
+
if (!analyzer) {
|
|
529
|
+
const tsconfig = resolveTsConfigPath();
|
|
530
|
+
const hasSourceFormats = sourceFormats.length > 0;
|
|
531
|
+
const ngp = await loadNgp(tsconfig, hasSourceFormats);
|
|
532
|
+
const isWasm = ngp.analyzerOptions.backend === "wasm";
|
|
533
|
+
toAnalyzerPath = isWasm ? toWasmPath : (path) => path;
|
|
534
|
+
fromAnalyzerPath = isWasm ? fromWasmPath : vite.normalizePath;
|
|
535
|
+
api = ngp.api;
|
|
536
|
+
if (hasSourceFormats) await lowerSourceModules(dirname(tsconfig), (specifier, importer) => this.resolve(specifier, importer, { skipSelf: true }));
|
|
537
|
+
analyzer = await api.createAnalyzer(toAnalyzerPath(tsconfig), {
|
|
538
|
+
...ngp.analyzerOptions,
|
|
539
|
+
enableHmr: hmr,
|
|
540
|
+
hostFs: hasSourceFormats || isWasm ? createNgpHostFs(hasSourceFormats ? (path) => sourceModules.get(path)?.lowered.code : void 0) : void 0
|
|
541
|
+
});
|
|
542
|
+
pending = indexChunks();
|
|
543
|
+
}
|
|
544
|
+
await pending;
|
|
545
|
+
for (const path of dependents.keys()) this.addWatchFile(path);
|
|
546
|
+
},
|
|
547
|
+
resolveId(id, importer) {
|
|
548
|
+
return resolveAngularVirtualId(id, importer, {
|
|
549
|
+
jit: false,
|
|
550
|
+
resolvedConfig: sharedCtx?.resolvedConfig ?? resolvedConfig
|
|
551
|
+
});
|
|
552
|
+
},
|
|
553
|
+
async load(id) {
|
|
554
|
+
return loadAngularVirtualModule(this, id, sharedCtx?.resolvedConfig ?? resolvedConfig);
|
|
555
|
+
},
|
|
556
|
+
async transform(code, id) {
|
|
557
|
+
const activeTransformFilter = sharedCtx?.transformFilter ?? transformFilter;
|
|
558
|
+
const filePath = vite.normalizePath(id.split("?")[0]);
|
|
559
|
+
const sourceFormat = scanner.sourceFormatOf(filePath);
|
|
560
|
+
if (!(sourceFormat ? sourceFilter(id) : filter(id)) || activeTransformFilter?.(code, id) === false) return;
|
|
561
|
+
await pending;
|
|
562
|
+
let lowered;
|
|
563
|
+
let compiledFile = filePath;
|
|
564
|
+
if (sourceFormat) {
|
|
565
|
+
const sourceFile = canonicalPath(filePath);
|
|
566
|
+
const format = moduleFormatOf(sourceFile);
|
|
567
|
+
if (!format) this.error(`${filePath} is outside ${dirname(resolveTsConfigPath())}, where '${sourceFormat.name}' modules are compiled from.`);
|
|
568
|
+
lowered = await withSourceLock(async () => {
|
|
569
|
+
const result = await lowerForTransform(sourceFile, format, code, (spec, importer) => this.resolve(spec, importer, { skipSelf: true }));
|
|
570
|
+
await syncProgramFiles();
|
|
571
|
+
return result;
|
|
572
|
+
});
|
|
573
|
+
if (!lowered) this.error(`${filePath} was removed while it was compiled.`);
|
|
574
|
+
if (lowered.error) this.error({
|
|
575
|
+
message: lowered.error.message,
|
|
576
|
+
id: filePath,
|
|
577
|
+
loc: lowered.error.loc && {
|
|
578
|
+
file: filePath,
|
|
579
|
+
...lowered.error.loc
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
compiledFile = `${sourceFile}.ts`;
|
|
583
|
+
for (const dependency of scanner.dependenciesOf(sourceFile)) this.addWatchFile(dependency);
|
|
584
|
+
}
|
|
585
|
+
const compiledCode = lowered ? lowered.code : code;
|
|
586
|
+
const watch = (path) => this.addWatchFile(path);
|
|
587
|
+
let result;
|
|
588
|
+
try {
|
|
589
|
+
result = await compileFile(compiledFile, watch);
|
|
590
|
+
} catch (error) {
|
|
591
|
+
if (!sources.has(compiledFile) || sources.get(compiledFile) === compiledCode) throw error;
|
|
592
|
+
}
|
|
593
|
+
if (filesByPath.has(compiledFile) && sources.get(compiledFile) !== compiledCode) {
|
|
594
|
+
if (lowered) analyzed.set(compiledFile, compiledCode);
|
|
595
|
+
await enqueueIndex(() => analyzer.updateFileContent([{
|
|
596
|
+
filePath: toAnalyzerPath(compiledFile),
|
|
597
|
+
content: compiledCode
|
|
598
|
+
}]));
|
|
599
|
+
result = await compileFile(compiledFile, watch);
|
|
600
|
+
} 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\`.`);
|
|
601
|
+
else if (!result && options.disableTypeChecking !== true) await checkTypes(filePath, code);
|
|
602
|
+
let source = result?.magicString.toString() ?? code;
|
|
603
|
+
if (result) {
|
|
604
|
+
const analyzerFile = filesByPath.get(compiledFile).file.filePath;
|
|
605
|
+
if (analyzerFile !== filePath) source = source.replaceAll(encodeURIComponent(`${analyzerFile}@`), encodeURIComponent(`${filePath}@`));
|
|
606
|
+
}
|
|
607
|
+
const map = result?.magicString.generateMap({
|
|
608
|
+
source: compiledFile,
|
|
609
|
+
includeContent: true,
|
|
610
|
+
hires: true
|
|
611
|
+
});
|
|
612
|
+
const transpiled = await transpile(source, compiledFile, map);
|
|
613
|
+
const outputMap = lowered && transpiled.map ? lowered.mapToSource(JSON.stringify(transpiled.map)) : transpiled.map;
|
|
614
|
+
if (outputMap) emittedMaps.set(filePath, typeof outputMap === "string" ? outputMap : JSON.stringify(outputMap));
|
|
615
|
+
return {
|
|
616
|
+
code: transpiled.code,
|
|
617
|
+
map: outputMap
|
|
618
|
+
};
|
|
619
|
+
},
|
|
620
|
+
async handleHotUpdate(ctx) {
|
|
621
|
+
if (sharedCtx ? sharedCtx.hmr.isIgnoredFile(ctx.file) : isIgnoredHmrFile(ctx.file)) return [];
|
|
622
|
+
const normPath = vite.normalizePath(ctx.file);
|
|
623
|
+
const sourceFile = canonicalPath(ctx.file);
|
|
624
|
+
const programFile = programFileOf(sourceFile);
|
|
625
|
+
const updates = await withSourceLock(async () => {
|
|
626
|
+
const relowered = reloweredByChange.get(sourceFile) ?? [];
|
|
627
|
+
reloweredByChange.delete(sourceFile);
|
|
628
|
+
const lowered = programFile ? sourceModules.get(programFile)?.lowered : void 0;
|
|
629
|
+
const componentUpdates = hmr && !lowered?.error && relowered.every((file) => file === programFile) ? await findComponentUpdates(programFile ?? normPath, lowered ? () => lowered.code : ctx.read) : [];
|
|
630
|
+
if (!programFile) await enqueueIndex(() => analyzer.invalidateFiles([{
|
|
631
|
+
filePath: toAnalyzerPath(ctx.file),
|
|
632
|
+
updateType: FILE_CHANGED
|
|
633
|
+
}]));
|
|
634
|
+
await syncProgramFiles();
|
|
635
|
+
return componentUpdates;
|
|
636
|
+
});
|
|
637
|
+
if (updates.length && await sendComponentUpdates(updates, ctx.server)) return [];
|
|
638
|
+
hmrUpdateCode.clear();
|
|
639
|
+
componentUpdates.classNames.clear();
|
|
640
|
+
ctx.server.moduleGraph.invalidateAll();
|
|
641
|
+
ctx.server.ws.send({ type: "full-reload" });
|
|
642
|
+
return [];
|
|
643
|
+
},
|
|
644
|
+
async buildEnd(error) {
|
|
645
|
+
if (error || watchMode || !typeChecker) return;
|
|
646
|
+
const typeErrors = await typeChecker.flushQueue();
|
|
647
|
+
if (typeErrors.length) throw new Error(typeErrors.join("\n"));
|
|
648
|
+
},
|
|
649
|
+
async closeBundle() {
|
|
650
|
+
activeBuilds = Math.max(0, activeBuilds - 1);
|
|
651
|
+
if (watchMode || activeBuilds > 0) return;
|
|
652
|
+
await pending.catch(() => {});
|
|
653
|
+
typeChecker?.close();
|
|
654
|
+
typeChecker = void 0;
|
|
655
|
+
analyzer?.close();
|
|
656
|
+
analyzer = void 0;
|
|
657
|
+
compiler = void 0;
|
|
658
|
+
filesByPath.clear();
|
|
659
|
+
chunkContexts.clear();
|
|
660
|
+
output.clear();
|
|
661
|
+
emittedMaps.clear();
|
|
662
|
+
sources.clear();
|
|
663
|
+
dependents.clear();
|
|
664
|
+
sourceModules.clear();
|
|
665
|
+
analyzed.clear();
|
|
666
|
+
releaseCssPreprocessorWorkers();
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
export { ngpCompilePlugin };
|
|
671
|
+
|
|
672
|
+
//# sourceMappingURL=ngp-compile-plugin.js.map
|