heximon 0.26.0 → 0.27.0
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/README.md +10 -0
- package/dist/bin.mjs +1815 -1631
- package/dist/index.d.mts +10 -0
- package/dist/index.mjs +1 -1
- package/dist/{skills-command-Czsv_c74.mjs → skills-command-BiuBtTbZ.mjs} +6 -1
- package/package.json +18 -17
package/dist/bin.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { _ as StdoutRedirect, d as RegistryAccessStatus, f as CommandOutput, g as CliEntry, i as createCommand, m as PromptRefusedError, n as ProjectRoot, p as CommandFailure, r as CreateDefaults, t as skillsCommand, u as RegistryAccess, v as CliDiagnosticCode, x as PromptCancelledError, y as CliDiagnostics } from "./skills-command-
|
|
2
|
+
import { _ as StdoutRedirect, d as RegistryAccessStatus, f as CommandOutput, g as CliEntry, i as createCommand, m as PromptRefusedError, n as ProjectRoot, p as CommandFailure, r as CreateDefaults, t as skillsCommand, u as RegistryAccess, v as CliDiagnosticCode, x as PromptCancelledError, y as CliDiagnostics } from "./skills-command-BiuBtTbZ.mjs";
|
|
3
3
|
import { createRequire, register } from "node:module";
|
|
4
4
|
import { cancel, confirm, intro, isCancel, log, outro, select, spinner } from "@clack/prompts";
|
|
5
5
|
import process from "node:process";
|
|
@@ -46,7 +46,12 @@ const CheckDiagnosticCode = {
|
|
|
46
46
|
* `--startup` in a Nuxt app whose target record (`.heximon/nuxt-target.json`) is missing, has a format
|
|
47
47
|
* version that the command does not read, cannot be read, or is older than a Nuxt config file of the app.
|
|
48
48
|
*/
|
|
49
|
-
NuxtTargetUnavailable: "check.nuxt-target-unavailable"
|
|
49
|
+
NuxtTargetUnavailable: "check.nuxt-target-unavailable",
|
|
50
|
+
/**
|
|
51
|
+
* An info: a Nitro or Nuxt host compiled for the Node default, because the command could not apply its Nitro
|
|
52
|
+
* preset and the sidecar declares no `platform`.
|
|
53
|
+
*/
|
|
54
|
+
HostTargetDefault: "check.host-target-default"
|
|
50
55
|
};
|
|
51
56
|
/** Where the deploy target of a `heximon check --startup` compile comes from. */
|
|
52
57
|
const StartupTargetSource = {
|
|
@@ -190,1897 +195,2051 @@ var ConfigSidecar = class ConfigSidecar {
|
|
|
190
195
|
}
|
|
191
196
|
};
|
|
192
197
|
//#endregion
|
|
193
|
-
//#region src/
|
|
198
|
+
//#region src/nitro-static-config.ts
|
|
194
199
|
/**
|
|
195
|
-
* Reads
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
200
|
+
* Reads the Nitro options that choose the graph of a build (`preset`, `serverDir`, `scanDirs`) from the source of
|
|
201
|
+
* the config files, without running them. A config file is project code: it can read the environment, call a
|
|
202
|
+
* service, or write a file. So the files are parsed with oxc, and the reader ACCEPTS only a shape that it can
|
|
203
|
+
* prove. Everything else is "computed", and the command then refuses or asks for an explicit input.
|
|
204
|
+
*
|
|
205
|
+
* Accepted in `nitro.config.*`: `export default {…}`, or `export default defineNitroConfig({…})` with
|
|
206
|
+
* `defineNitroConfig` imported from `nitro` or `nitro/config`, or either one through ONE top-level `const` that is
|
|
207
|
+
* never assigned again. The object has no spread. A `$production` or `$env.production` overlay applies; the other
|
|
208
|
+
* environments do not, because the command always compiles for production.
|
|
209
|
+
*
|
|
210
|
+
* Accepted in `vite.config.*`: ONE call of the Nitro plugin of `nitro/vite` (a named or a namespace import), as a
|
|
211
|
+
* direct element of the `plugins` array literal of the exported config (`export default {…}`, or `defineConfig`
|
|
212
|
+
* from `vite` over one, or either through one `const`). No ternary, no `&&`, no spread, no helper, no function.
|
|
213
|
+
*
|
|
214
|
+
* The precedence is Nitro's own: the options of the Vite config, then `NITRO_PRESET` / `SERVER_PRESET`, then the
|
|
215
|
+
* Nitro config file.
|
|
199
216
|
*/
|
|
200
|
-
var
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
217
|
+
var NitroStaticConfig = class NitroStaticConfig {
|
|
218
|
+
static nitroConfigNames = [
|
|
219
|
+
"nitro.config.ts",
|
|
220
|
+
"nitro.config.mts",
|
|
221
|
+
"nitro.config.js",
|
|
222
|
+
"nitro.config.mjs"
|
|
223
|
+
];
|
|
224
|
+
static viteConfigNames = [
|
|
225
|
+
"vite.config.ts",
|
|
226
|
+
"vite.config.mts",
|
|
227
|
+
"vite.config.js",
|
|
228
|
+
"vite.config.mjs"
|
|
229
|
+
];
|
|
230
|
+
/** Nuxt creates its own Nitro, which a static read of these files cannot describe. */
|
|
231
|
+
static nuxtConfigNames = [
|
|
232
|
+
"nuxt.config.ts",
|
|
233
|
+
"nuxt.config.mts",
|
|
234
|
+
"nuxt.config.js",
|
|
235
|
+
"nuxt.config.mjs"
|
|
236
|
+
];
|
|
237
|
+
/** The package names a Nuxt app depends on: the release, or the nightly. */
|
|
238
|
+
static nuxtPackages = ["nuxt", "nuxt-nightly"];
|
|
239
|
+
/** The extensions c12 tries for a config file, scripts and data formats. */
|
|
240
|
+
static configExtensions = [
|
|
241
|
+
".js",
|
|
242
|
+
".ts",
|
|
243
|
+
".mjs",
|
|
244
|
+
".cjs",
|
|
245
|
+
".mts",
|
|
246
|
+
".cts",
|
|
247
|
+
".json",
|
|
248
|
+
".jsonc",
|
|
249
|
+
".json5",
|
|
250
|
+
".yaml",
|
|
251
|
+
".yml",
|
|
252
|
+
".toml"
|
|
253
|
+
];
|
|
254
|
+
/** The exact name of the rc file c12 reads for Nitro. */
|
|
255
|
+
static resourceFileName = ".nitrorc";
|
|
256
|
+
/** The expression nodes that only wrap another expression. */
|
|
257
|
+
static wrapperTypes = [
|
|
258
|
+
"TSAsExpression",
|
|
259
|
+
"TSSatisfiesExpression",
|
|
260
|
+
"TSNonNullExpression",
|
|
261
|
+
"TSTypeAssertion",
|
|
262
|
+
"ParenthesizedExpression"
|
|
263
|
+
];
|
|
264
|
+
/** The module that exports the Nitro Vite plugin. */
|
|
265
|
+
static nitroVitePackage = "nitro/vite";
|
|
266
|
+
/** The key of the Nitro config that holds the options of the Heximon compile. */
|
|
267
|
+
static heximonSection = "heximon";
|
|
268
|
+
/** The helper that returns its Nitro config unchanged, and where it is imported from. */
|
|
269
|
+
static nitroConfigHelper = {
|
|
270
|
+
name: "defineNitroConfig",
|
|
271
|
+
packages: ["nitro", "nitro/config"]
|
|
272
|
+
};
|
|
273
|
+
/** The helper that returns its Vite config unchanged, and where it is imported from. */
|
|
274
|
+
static viteConfigHelper = {
|
|
275
|
+
name: "defineConfig",
|
|
276
|
+
packages: ["vite", "vite-plus"]
|
|
277
|
+
};
|
|
211
278
|
/**
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
* @param rootDir - The app root.
|
|
215
|
-
* @returns The target, or why the record cannot be used.
|
|
279
|
+
* @param rootDir - The project root.
|
|
280
|
+
* @param environment - The environment to read `NITRO_PRESET` from; the process environment by default.
|
|
216
281
|
*/
|
|
217
|
-
static read(rootDir) {
|
|
218
|
-
const
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
const
|
|
232
|
-
if (
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
const
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
282
|
+
static read(rootDir, environment = process.env) {
|
|
283
|
+
const find = (names) => names.map((name) => join(rootDir, name)).find((path) => existsSync(path));
|
|
284
|
+
const notAHost = {
|
|
285
|
+
isNitroHost: false,
|
|
286
|
+
preset: { kind: "none" },
|
|
287
|
+
serverDir: { kind: "none" },
|
|
288
|
+
scanDirs: { kind: "none" },
|
|
289
|
+
sourceDirectories: { kind: "none" }
|
|
290
|
+
};
|
|
291
|
+
if (NitroStaticConfig.isNuxtProject(rootDir)) return notAHost;
|
|
292
|
+
const configFiles = NitroStaticConfig.configFilesOf(rootDir);
|
|
293
|
+
const nitroFile = configFiles.length === 1 && NitroStaticConfig.nitroConfigNames.some((name) => join(rootDir, name) === configFiles[0]) ? configFiles[0] : void 0;
|
|
294
|
+
const viteFile = find(NitroStaticConfig.viteConfigNames);
|
|
295
|
+
const vite = viteFile === void 0 ? void 0 : NitroStaticConfig.readVite(viteFile);
|
|
296
|
+
const hasNitroSource = configFiles.length > 0 || vite?.usesNitroPlugin === true;
|
|
297
|
+
if (!hasNitroSource && !NitroStaticConfig.dependsOnNitro(rootDir)) return notAHost;
|
|
298
|
+
const viteSources = vite?.sources ?? [];
|
|
299
|
+
const fileSources = nitroFile === void 0 ? [] : NitroStaticConfig.readNitroConfig(nitroFile);
|
|
300
|
+
const hiddenSources = NitroStaticConfig.hiddenSources(rootDir, hasNitroSource, nitroFile === void 0 ? configFiles : []);
|
|
301
|
+
const all = [
|
|
302
|
+
...viteSources,
|
|
303
|
+
...fileSources,
|
|
304
|
+
...hiddenSources
|
|
305
|
+
];
|
|
306
|
+
const fromVite = NitroStaticConfig.scalar("preset", viteSources, true);
|
|
307
|
+
const fromEnvironment = environment["NITRO_PRESET"] || environment["SERVER_PRESET"];
|
|
308
|
+
const found = fromVite.kind !== "none" ? fromVite : fromEnvironment ? {
|
|
309
|
+
kind: "literal",
|
|
310
|
+
value: fromEnvironment
|
|
311
|
+
} : NitroStaticConfig.scalar("preset", [...fileSources, ...hiddenSources], true);
|
|
312
|
+
const unreadable = all.find((source) => source.kind === "unproven");
|
|
239
313
|
return {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
314
|
+
isNitroHost: true,
|
|
315
|
+
preset: unreadable !== void 0 && found.kind !== "computed" ? {
|
|
316
|
+
kind: "computed",
|
|
317
|
+
file: unreadable.file,
|
|
318
|
+
reason: unreadable.reason
|
|
319
|
+
} : NitroStaticConfig.withHidden(found, all, ["static", "extends"], ["defaultPreset"]),
|
|
320
|
+
serverDir: NitroStaticConfig.withHidden(NitroStaticConfig.scalar("serverDir", all), all, [], ["extends"]),
|
|
321
|
+
scanDirs: NitroStaticConfig.withHiddenList(NitroStaticConfig.list("scanDirs", all), all),
|
|
322
|
+
sourceDirectories: NitroStaticConfig.withHiddenList(NitroStaticConfig.list("sourceDirectories", NitroStaticConfig.sectionSources(NitroStaticConfig.heximonSection, all)), all)
|
|
247
323
|
};
|
|
248
324
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
/** The absolute source directories to scan, or `undefined` for the compiler's own `<rootDir>/src`. */
|
|
274
|
-
sourceDirectories;
|
|
275
|
-
/** The absolute `.heximon/` directory the compile anchors its generated paths at. */
|
|
276
|
-
outputDirectory;
|
|
277
|
-
/** The compiler plugins the sidecar declares, merged with the platform strategy's own. */
|
|
278
|
-
plugins;
|
|
279
|
-
strategy;
|
|
280
|
-
/** The `$env` name the sidecar was read for, which the member sidecars of the workspace scan share. */
|
|
281
|
-
environment;
|
|
282
|
-
/** The runtime config file the app compile checks and bakes in, or `undefined` when there is none. */
|
|
283
|
-
runtimeConfig;
|
|
284
|
-
/** The module aliases of a host build, which {@link compile} resolves, or `undefined` for none. */
|
|
285
|
-
aliases;
|
|
286
|
-
/** The output-relative files that another step writes into {@link outputDirectory}, which {@link write} keeps. */
|
|
287
|
-
retainedOutput;
|
|
288
|
-
constructor(options) {
|
|
289
|
-
this.rootDir = options.rootDir;
|
|
290
|
-
this.compiler = options.compiler;
|
|
291
|
-
this.config = options.config;
|
|
292
|
-
this.runtimeConfig = options.runtimeConfig;
|
|
293
|
-
this.sourceDirectories = options.sourceDirectories;
|
|
294
|
-
this.plugins = options.plugins;
|
|
295
|
-
this.strategy = options.strategy;
|
|
296
|
-
this.environment = options.environment;
|
|
297
|
-
this.aliases = options.aliases;
|
|
298
|
-
this.outputDirectory = join(options.rootDir, ProjectCompileSession.outputDirectoryName);
|
|
299
|
-
this.retainedOutput = [NuxtTargetFile.fileName, ...options.retainedOutput ?? []];
|
|
325
|
+
/**
|
|
326
|
+
* The literal source directories of a Nitro host, or the first of `serverDir`, `scanDirs` and
|
|
327
|
+
* `heximon.sourceDirectories` that is computed. The build scans the union of the three, so the
|
|
328
|
+
* `heximon.sourceDirectories` join `scanDirs`.
|
|
329
|
+
*
|
|
330
|
+
* @param result - What {@link read} returned.
|
|
331
|
+
*/
|
|
332
|
+
static sourceOptionsOf(result) {
|
|
333
|
+
for (const [option, value] of [
|
|
334
|
+
["serverDir", result.serverDir],
|
|
335
|
+
["scanDirs", result.scanDirs],
|
|
336
|
+
["heximon.sourceDirectories", result.sourceDirectories]
|
|
337
|
+
]) if (value.kind === "computed") return {
|
|
338
|
+
kind: "computed",
|
|
339
|
+
option,
|
|
340
|
+
file: value.file,
|
|
341
|
+
reason: value.reason
|
|
342
|
+
};
|
|
343
|
+
const scanDirs = [...result.scanDirs.kind === "literal" ? result.scanDirs.value : [], ...result.sourceDirectories.kind === "literal" ? result.sourceDirectories.value : []];
|
|
344
|
+
return {
|
|
345
|
+
kind: "literal",
|
|
346
|
+
...result.serverDir.kind === "literal" ? { serverDir: result.serverDir.value } : {},
|
|
347
|
+
...result.scanDirs.kind === "literal" || result.sourceDirectories.kind === "literal" ? { scanDirs } : {}
|
|
348
|
+
};
|
|
300
349
|
}
|
|
301
350
|
/**
|
|
302
|
-
*
|
|
351
|
+
* Whether the project is a Nuxt app: it has a `nuxt.config.*` file, or package.json lists `nuxt` (or
|
|
352
|
+
* `nuxt-nightly`) as a dependency. Nuxt creates its own Nitro, so {@link read} does not report such a project
|
|
353
|
+
* as a Nitro host, although it deploys through Nitro.
|
|
303
354
|
*
|
|
304
|
-
* @param
|
|
305
|
-
* @returns The session the caller compiles through.
|
|
306
|
-
* @throws CommandFailure with `cli.config-unreadable` when the sidecar throws.
|
|
355
|
+
* @param rootDir - The project root.
|
|
307
356
|
*/
|
|
308
|
-
static
|
|
309
|
-
const
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
runtimeConfig: await RuntimeConfigLoader.load(options.rootDir, config, options.environment),
|
|
316
|
-
sourceDirectories: options.sourceDirectories,
|
|
317
|
-
plugins: NodeCompileSeam.mergePlugins(config?.plugins ?? [], strategy),
|
|
318
|
-
strategy,
|
|
319
|
-
environment: options.environment
|
|
320
|
-
});
|
|
357
|
+
static isNuxtProject(rootDir) {
|
|
358
|
+
const manifest = NitroStaticConfig.manifestOf(rootDir);
|
|
359
|
+
const dependencies = manifest === void 0 || manifest === "invalid" ? {} : {
|
|
360
|
+
...manifest.dependencies,
|
|
361
|
+
...manifest.devDependencies
|
|
362
|
+
};
|
|
363
|
+
return NitroStaticConfig.nuxtConfigNames.some((name) => existsSync(join(rootDir, name))) || NitroStaticConfig.nuxtPackages.some((name) => name in dependencies);
|
|
321
364
|
}
|
|
322
365
|
/**
|
|
323
|
-
*
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
* @param options - The project root, the host inputs, the module aliases, and the source directories to scan
|
|
328
|
-
* instead of the host's, when the caller has some.
|
|
329
|
-
* @returns The session the caller compiles through.
|
|
366
|
+
* Sources of options that the project has but the reader cannot read: a `.nitrorc` file (in the project or the
|
|
367
|
+
* home directory), the `nitro` key of `package.json`, and a dependency on `nitro` when no config file or
|
|
368
|
+
* provable plugin call exists.
|
|
330
369
|
*/
|
|
331
|
-
static
|
|
332
|
-
const
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
config: inputs.config,
|
|
337
|
-
runtimeConfig: inputs.runtimeConfig,
|
|
338
|
-
sourceDirectories: options.sourceDirectories ?? inputs.sourceDirectories,
|
|
339
|
-
plugins: inputs.plugins,
|
|
340
|
-
strategy: inputs.strategy,
|
|
341
|
-
environment: "production",
|
|
342
|
-
aliases: options.aliases,
|
|
343
|
-
...inputs.retainedOutput === void 0 ? {} : { retainedOutput: inputs.retainedOutput }
|
|
370
|
+
static hiddenSources(rootDir, hasNitroSource, unreadFiles) {
|
|
371
|
+
const unproven = (reason) => ({
|
|
372
|
+
kind: "unproven",
|
|
373
|
+
file: join(rootDir, "package.json"),
|
|
374
|
+
reason
|
|
344
375
|
});
|
|
376
|
+
const sources = [];
|
|
377
|
+
const resourceFile = [rootDir, homedir()].map((directory) => join(directory, NitroStaticConfig.resourceFileName)).find((path) => existsSync(path));
|
|
378
|
+
if (resourceFile !== void 0) sources.push(unproven(`the file ${resourceFile} sets Nitro options`));
|
|
379
|
+
if (unreadFiles.length > 0) sources.push(unproven(`the file ${unreadFiles[0]} sets Nitro options in a form the command does not read`));
|
|
380
|
+
const manifest = NitroStaticConfig.manifestOf(rootDir);
|
|
381
|
+
if (manifest === "invalid") sources.push(unproven("package.json is not valid JSON"));
|
|
382
|
+
else if (manifest !== void 0) {
|
|
383
|
+
if (manifest.nitro !== void 0) sources.push(unproven("the `nitro` key of package.json sets Nitro options"));
|
|
384
|
+
if (!hasNitroSource && NitroStaticConfig.dependsOnNitro(rootDir)) sources.push(unproven("the project depends on nitro, but no Nitro config file or Nitro Vite plugin call was found"));
|
|
385
|
+
}
|
|
386
|
+
return sources;
|
|
345
387
|
}
|
|
346
388
|
/**
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
* @param options - The baseline verification pass, and the cross-service topology the fan-out
|
|
351
|
-
* diagnostics read.
|
|
352
|
-
* @returns The compile output, diagnostics included.
|
|
389
|
+
* Every file or directory that the config loader of Nitro (c12) would take as its main config: the name
|
|
390
|
+
* `nitro.config` in the project, in `.config/`, and `.config/nitro`, bare, with each supported extension, and as
|
|
391
|
+
* a directory with an `index` file. The project's own script forms are among them.
|
|
353
392
|
*/
|
|
354
|
-
|
|
355
|
-
return
|
|
356
|
-
rootDir
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
...this.sourceDirectories === void 0 ? {} : { sourceDirectories: this.sourceDirectories },
|
|
361
|
-
...this.aliases === void 0 ? {} : {
|
|
362
|
-
aliases: this.aliases.prefixes,
|
|
363
|
-
exactAliases: this.aliases.exact
|
|
364
|
-
},
|
|
365
|
-
...this.config === void 0 ? {} : { config: this.config },
|
|
366
|
-
...this.config?.wiringMode === void 0 ? {} : { wiringMode: this.config.wiringMode },
|
|
367
|
-
...this.runtimeConfig === void 0 ? {} : { runtimeConfig: this.runtimeConfig },
|
|
368
|
-
...this.config?.root === void 0 ? {} : { apps: { main: this.config.root } },
|
|
369
|
-
...typeVerdicts ? { typeVerdicts: true } : {},
|
|
370
|
-
...options.verifyBaseline === true ? { verifyBaseline: true } : {},
|
|
371
|
-
...options.workspaceSubscribers === void 0 ? {} : { workspaceSubscribers: options.workspaceSubscribers }
|
|
372
|
-
});
|
|
393
|
+
static configFilesOf(rootDir) {
|
|
394
|
+
return [
|
|
395
|
+
join(rootDir, "nitro.config"),
|
|
396
|
+
join(rootDir, ".config", "nitro.config"),
|
|
397
|
+
join(rootDir, ".config", "nitro")
|
|
398
|
+
].flatMap((base) => ["", "/index"].flatMap((suffix) => ["", ...NitroStaticConfig.configExtensions].map((extension) => `${base}${suffix}${extension}`))).filter((path) => existsSync(path));
|
|
373
399
|
}
|
|
374
|
-
/**
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
*
|
|
379
|
-
* @returns The topology, or one error diagnostic in place of the error the read threw.
|
|
380
|
-
*/
|
|
381
|
-
async readWorkspaceTopology() {
|
|
400
|
+
/** The parsed package.json of the project, `undefined` when it has none, `"invalid"` when it is not JSON. */
|
|
401
|
+
static manifestOf(rootDir) {
|
|
402
|
+
const file = join(rootDir, "package.json");
|
|
403
|
+
if (!existsSync(file)) return;
|
|
382
404
|
try {
|
|
383
|
-
return
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
};
|
|
387
|
-
} catch (error) {
|
|
388
|
-
return {
|
|
389
|
-
topology: void 0,
|
|
390
|
-
diagnostics: [this.topologyFailure(error)]
|
|
391
|
-
};
|
|
405
|
+
return JSON.parse(readFileSync(file, "utf8"));
|
|
406
|
+
} catch {
|
|
407
|
+
return "invalid";
|
|
392
408
|
}
|
|
393
409
|
}
|
|
410
|
+
/** Whether package.json lists `nitro` as a dependency or a dev dependency. */
|
|
411
|
+
static dependsOnNitro(rootDir) {
|
|
412
|
+
const manifest = NitroStaticConfig.manifestOf(rootDir);
|
|
413
|
+
return manifest !== void 0 && manifest !== "invalid" && ("nitro" in (manifest.dependencies ?? {}) || "nitro" in (manifest.devDependencies ?? {}));
|
|
414
|
+
}
|
|
394
415
|
/**
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
* @param files - The files of the compile output.
|
|
416
|
+
* Apply the keys that change a value without showing in it. A key in `always` (for the preset: `static`, which
|
|
417
|
+
* turns a preset into its static variant) makes the value unknown. A key in `whenUnset` (`defaultPreset`,
|
|
418
|
+
* `extends`: the layers sit below every other source) matters only when no literal was found.
|
|
399
419
|
*/
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
420
|
+
static withHidden(value, sources, always, whenUnset) {
|
|
421
|
+
const hidden = (keys) => {
|
|
422
|
+
for (const source of sources) {
|
|
423
|
+
const key = source.kind === "object" ? keys.find((name) => source.entries.has(name)) : void 0;
|
|
424
|
+
if (source.kind === "object" && key !== void 0) return {
|
|
425
|
+
file: source.file,
|
|
426
|
+
key
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
const alwaysHidden = hidden(always);
|
|
431
|
+
const unsetHidden = value.kind === "none" ? hidden(whenUnset) : void 0;
|
|
432
|
+
const reason = alwaysHidden ?? unsetHidden;
|
|
433
|
+
return reason === void 0 ? value : {
|
|
434
|
+
kind: "computed",
|
|
435
|
+
file: reason.file,
|
|
436
|
+
reason: `\`${reason.key}\` can change the value`
|
|
437
|
+
};
|
|
406
438
|
}
|
|
407
|
-
/**
|
|
408
|
-
|
|
409
|
-
|
|
439
|
+
/** A list gets every `extends` layer on top of it, so a layer makes it unknown. */
|
|
440
|
+
static withHiddenList(value, sources) {
|
|
441
|
+
const layered = sources.find((source) => source.kind === "object" && source.entries.has("extends"));
|
|
442
|
+
return layered === void 0 || layered.kind !== "object" ? value : {
|
|
443
|
+
kind: "computed",
|
|
444
|
+
file: layered.file,
|
|
445
|
+
reason: "`extends` can add values"
|
|
446
|
+
};
|
|
410
447
|
}
|
|
411
448
|
/**
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
*
|
|
416
|
-
* @param outputDirectory - The directory the generated files will be written to. It must sit directly
|
|
417
|
-
* under the project root, because the generated imports of the app source are relative to it.
|
|
418
|
-
* @param aliases - The module aliases of the project (prefix → absolute path), when it has some.
|
|
419
|
-
* @returns The compile output. Nothing is written.
|
|
449
|
+
* The option sources of a Nitro config file, highest priority first: the production overlays, then the
|
|
450
|
+
* object itself. An `extends` or `defaultPreset` key brings values that the source does not show, so it adds
|
|
451
|
+
* an unproven source below the object.
|
|
420
452
|
*/
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
453
|
+
static readNitroConfig(file) {
|
|
454
|
+
const { program, imports } = NitroStaticConfig.parse(file);
|
|
455
|
+
const resolved = NitroStaticConfig.exportedObject(program, imports, NitroStaticConfig.nitroConfigHelper);
|
|
456
|
+
if (resolved.kind === "unproven") return [{
|
|
457
|
+
kind: "unproven",
|
|
458
|
+
file,
|
|
459
|
+
reason: resolved.reason
|
|
460
|
+
}];
|
|
461
|
+
const base = NitroStaticConfig.prove(resolved.node, file);
|
|
462
|
+
if (base.kind === "unproven") return [base];
|
|
463
|
+
const overlay = /* @__PURE__ */ new Map();
|
|
464
|
+
const sources = [];
|
|
465
|
+
const overlays = [base.entries.get("$production")];
|
|
466
|
+
const environments = base.entries.get("$env");
|
|
467
|
+
if (environments !== void 0) {
|
|
468
|
+
const proven = NitroStaticConfig.prove(environments, file);
|
|
469
|
+
if (proven.kind === "unproven") sources.push(proven);
|
|
470
|
+
else overlays.push(proven.entries.get("production"));
|
|
471
|
+
}
|
|
472
|
+
for (const node of overlays) {
|
|
473
|
+
if (node === void 0) continue;
|
|
474
|
+
const proven = NitroStaticConfig.prove(node, file);
|
|
475
|
+
if (proven.kind === "unproven") sources.push(proven);
|
|
476
|
+
else for (const [key, value] of proven.entries) overlay.set(key, value);
|
|
477
|
+
}
|
|
478
|
+
if (overlay.size > 0) sources.push({
|
|
479
|
+
kind: "object",
|
|
480
|
+
file,
|
|
481
|
+
entries: overlay
|
|
442
482
|
});
|
|
483
|
+
sources.push(base);
|
|
484
|
+
return sources;
|
|
443
485
|
}
|
|
444
486
|
/**
|
|
445
|
-
*
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
* while this reads plugins from the config sidecar and falls back to the committed `exports` map.
|
|
449
|
-
*
|
|
450
|
-
* Two things make the app-mode compile wrong here, not merely noisy. It enforces every `requires` hop-by-hop
|
|
451
|
-
* against a host binding, while a library leaves that seam open on purpose (the published factory reads a
|
|
452
|
-
* required token as a constructor input); and it looks for one root module, while a multi-root package
|
|
453
|
-
* publishes several. Both surface as errors a `vp pack` of the same source never raises.
|
|
454
|
-
*
|
|
455
|
-
* @param options - `pruneVerdicts` also removes a verdict mirror an EARLIER app-mode run wrote. Library
|
|
456
|
-
* mode emits none to replace it, so nothing would ever overwrite those app-mode-only errors — only
|
|
457
|
-
* `heximon check`, which owns the mirror, asks for it. Every other verb stays write-free.
|
|
458
|
-
* @returns The compile's diagnostics, files and graph projections; a throwing emit becomes one diagnostic.
|
|
487
|
+
* What the Vite config says: whether it imports the Nitro plugin, and the option sources it gives. Once the
|
|
488
|
+
* plugin is imported, anything the reader cannot prove makes the sources unproven; the project is still a
|
|
489
|
+
* Nitro host.
|
|
459
490
|
*/
|
|
460
|
-
|
|
461
|
-
const
|
|
462
|
-
const
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
rootDir: this.rootDir,
|
|
469
|
-
plugins: this.plugins,
|
|
470
|
-
...sourceDirectory === void 0 ? {} : { sourceDirectory },
|
|
471
|
-
...this.config === void 0 ? {} : { config: this.config }
|
|
472
|
-
});
|
|
473
|
-
return {
|
|
474
|
-
diagnostics: emitted.diagnostics,
|
|
475
|
-
files: emitted.files,
|
|
476
|
-
moduleGraph: emitted.moduleGraph,
|
|
477
|
-
boundaryNodes: emitted.boundaryNodes,
|
|
478
|
-
...prunedVerdicts === void 0 ? {} : { prunedVerdicts }
|
|
479
|
-
};
|
|
480
|
-
} catch (error) {
|
|
481
|
-
return {
|
|
482
|
-
diagnostics: [{
|
|
483
|
-
severity: DiagnosticSeverity.Error,
|
|
484
|
-
message: `The library compile of this package failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
485
|
-
location: this.rootDir
|
|
486
|
-
}],
|
|
487
|
-
files: [],
|
|
488
|
-
moduleGraph: void 0,
|
|
489
|
-
boundaryNodes: [],
|
|
490
|
-
...prunedVerdicts === void 0 ? {} : { prunedVerdicts }
|
|
491
|
-
};
|
|
491
|
+
static readVite(file) {
|
|
492
|
+
const { program, imports } = NitroStaticConfig.parse(file);
|
|
493
|
+
const named = /* @__PURE__ */ new Set();
|
|
494
|
+
const namespaces = /* @__PURE__ */ new Set();
|
|
495
|
+
for (const entry of imports) {
|
|
496
|
+
if (entry.request !== NitroStaticConfig.nitroVitePackage || entry.isTypeOnly) continue;
|
|
497
|
+
if (entry.importKind === ImportKind.Namespace) namespaces.add(entry.localName);
|
|
498
|
+
else if (entry.importKind === ImportKind.Named && entry.importedName === "nitro") named.add(entry.localName);
|
|
492
499
|
}
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
500
|
+
if (named.size === 0 && namespaces.size === 0) return {
|
|
501
|
+
usesNitroPlugin: false,
|
|
502
|
+
sources: []
|
|
503
|
+
};
|
|
504
|
+
const unproven = (reason) => ({
|
|
505
|
+
usesNitroPlugin: true,
|
|
506
|
+
sources: [{
|
|
507
|
+
kind: "unproven",
|
|
508
|
+
file,
|
|
509
|
+
reason
|
|
510
|
+
}]
|
|
511
|
+
});
|
|
512
|
+
const importStarts = new Set(program.descendants("ImportDeclaration").flatMap((declaration) => declaration.descendants(AstNodeType.Identifier)).map((identifier) => identifier.start));
|
|
513
|
+
const calls = program.descendants(AstNodeType.CallExpression).flatMap((call) => {
|
|
514
|
+
const callee = call.child("callee");
|
|
515
|
+
if (callee.is(AstNodeType.Identifier) && named.has(callee.name ?? "")) return [{
|
|
516
|
+
call,
|
|
517
|
+
referenceStart: callee.start
|
|
518
|
+
}];
|
|
519
|
+
if (callee.is(AstNodeType.MemberExpression) && !callee.flagAt("computed") && callee.child("property").name === "nitro" && callee.child("object").is(AstNodeType.Identifier) && namespaces.has(callee.child("object").name ?? "")) return [{
|
|
520
|
+
call,
|
|
521
|
+
referenceStart: callee.child("object").start
|
|
522
|
+
}];
|
|
523
|
+
return [];
|
|
524
|
+
});
|
|
525
|
+
const callStarts = new Set(calls.map((entry) => entry.referenceStart));
|
|
526
|
+
const nameStarts = NitroStaticConfig.nameStarts(program);
|
|
527
|
+
if (program.descendants(AstNodeType.Identifier).filter((identifier) => (named.has(identifier.name ?? "") || namespaces.has(identifier.name ?? "")) && !importStarts.has(identifier.start) && !nameStarts.has(identifier.start) && !callStarts.has(identifier.start)).length > 0) return unproven("the Nitro Vite plugin is used other than by a direct call");
|
|
528
|
+
if (calls.length !== 1) return unproven(`the Nitro Vite plugin is called ${calls.length} times, not once`);
|
|
529
|
+
const resolved = NitroStaticConfig.exportedObject(program, imports, NitroStaticConfig.viteConfigHelper);
|
|
530
|
+
if (resolved.kind === "unproven") return unproven(resolved.reason);
|
|
531
|
+
const config = NitroStaticConfig.prove(resolved.node, file);
|
|
532
|
+
if (config.kind === "unproven") return {
|
|
533
|
+
usesNitroPlugin: true,
|
|
534
|
+
sources: [config]
|
|
535
|
+
};
|
|
536
|
+
const plugins = NitroStaticConfig.unwrap(config.entries.get("plugins"));
|
|
537
|
+
const call = calls[0].call;
|
|
538
|
+
if (!(plugins?.is(AstNodeType.ArrayExpression) === true && plugins.children("elements").some((element) => NitroStaticConfig.unwrap(element)?.start === call.start))) return unproven("the Nitro Vite plugin is not a direct element of the `plugins` array");
|
|
539
|
+
const sources = [];
|
|
540
|
+
const argument = NitroStaticConfig.unwrap(call.children("arguments")[0]);
|
|
541
|
+
if (argument !== void 0) {
|
|
542
|
+
const options = NitroStaticConfig.prove(argument, file);
|
|
543
|
+
sources.push(options);
|
|
544
|
+
const nested = options.kind === "object" ? options.entries.get("config") : void 0;
|
|
545
|
+
if (nested !== void 0) sources.push(NitroStaticConfig.prove(nested, file));
|
|
546
|
+
}
|
|
547
|
+
const nitroKey = config.entries.get("nitro");
|
|
548
|
+
if (nitroKey !== void 0) sources.push(NitroStaticConfig.prove(nitroKey, file));
|
|
510
549
|
return {
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
...this.sourceDirectories === void 0 ? {} : { sourceDirectories: this.sourceDirectories }
|
|
550
|
+
usesNitroPlugin: true,
|
|
551
|
+
sources
|
|
514
552
|
};
|
|
515
553
|
}
|
|
516
554
|
/**
|
|
517
|
-
* The
|
|
518
|
-
*
|
|
555
|
+
* The object that `export default` gives: a literal, a call of the verified identity helper over one, or a
|
|
556
|
+
* single top-level `const` that holds either and is never assigned again.
|
|
519
557
|
*/
|
|
520
|
-
|
|
521
|
-
const
|
|
522
|
-
const
|
|
523
|
-
const
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
558
|
+
static exportedObject(program, imports, helper) {
|
|
559
|
+
const helperNames = new Set(imports.filter((entry) => entry.importKind === ImportKind.Named && entry.importedName === helper.name && helper.packages.includes(entry.request) && !entry.isTypeOnly).map((entry) => entry.localName));
|
|
560
|
+
const declaration = program.children("body").find((statement) => statement.is(AstNodeType.ExportDefaultDeclaration))?.child("declaration");
|
|
561
|
+
const objectOf = (node, viaConstant) => {
|
|
562
|
+
const expression = NitroStaticConfig.unwrap(node);
|
|
563
|
+
if (expression === void 0) return {
|
|
564
|
+
kind: "unproven",
|
|
565
|
+
reason: "the config has no default export"
|
|
566
|
+
};
|
|
567
|
+
if (expression.is(AstNodeType.ObjectExpression)) return {
|
|
568
|
+
kind: "found",
|
|
569
|
+
node: expression
|
|
570
|
+
};
|
|
571
|
+
if (expression.is(AstNodeType.CallExpression)) {
|
|
572
|
+
const callee = expression.child("callee");
|
|
573
|
+
return callee.is(AstNodeType.Identifier) && helperNames.has(callee.name ?? "") && expression.children("arguments").length === 1 ? objectOf(expression.children("arguments")[0], viaConstant) : {
|
|
574
|
+
kind: "unproven",
|
|
575
|
+
reason: `the config is a call of something other than ${helper.name} from ${helper.packages.join(" or ")}`
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
if (expression.is(AstNodeType.Identifier) && !viaConstant) {
|
|
579
|
+
const initializer = NitroStaticConfig.constantOf(program, expression.name ?? "", expression.start);
|
|
580
|
+
return initializer === void 0 ? {
|
|
581
|
+
kind: "unproven",
|
|
582
|
+
reason: `\`${expression.name}\` is not a top-level const that nothing else uses`
|
|
583
|
+
} : objectOf(initializer, true);
|
|
584
|
+
}
|
|
585
|
+
return {
|
|
586
|
+
kind: "unproven",
|
|
587
|
+
reason: "the default export is not an object the reader can prove"
|
|
588
|
+
};
|
|
530
589
|
};
|
|
590
|
+
return objectOf(declaration, false);
|
|
531
591
|
}
|
|
532
|
-
};
|
|
533
|
-
//#endregion
|
|
534
|
-
//#region src/source-directories.ts
|
|
535
|
-
/**
|
|
536
|
-
* Resolves which directories the host-agnostic compile commands (`check`, `graph`, `routes`) scan for Heximon
|
|
537
|
-
* source. The compiler's own default is `<rootDir>/src`; a Nitro/Nuxt host instead keeps its modules under
|
|
538
|
-
* `server/` and passes that directory to the compiler from its own build. The standalone CLI is just another
|
|
539
|
-
* host, so it detects that layout and passes `server/` too — otherwise its compile finds no modules.
|
|
540
|
-
*/
|
|
541
|
-
var SourceDirectories = class SourceDirectories {
|
|
542
|
-
/** Host-framework config basenames whose presence marks a project that roots its modules under `server/`. */
|
|
543
|
-
static serverHostConfigNames = [
|
|
544
|
-
"nuxt.config.ts",
|
|
545
|
-
"nuxt.config.mts",
|
|
546
|
-
"nuxt.config.js",
|
|
547
|
-
"nuxt.config.mjs",
|
|
548
|
-
"nitro.config.ts",
|
|
549
|
-
"nitro.config.mts",
|
|
550
|
-
"nitro.config.js",
|
|
551
|
-
"nitro.config.mjs"
|
|
552
|
-
];
|
|
553
592
|
/**
|
|
554
|
-
* The
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
*
|
|
558
|
-
* @param rootDir - The absolute project root.
|
|
559
|
-
* @param sourceOption - The raw `--src` argument, when the caller passed one.
|
|
560
|
-
* @returns Absolute directories, or `undefined` for the compiler default.
|
|
593
|
+
* The initializer of `const <name> = …` at the top level, when the name has no use except the one at
|
|
594
|
+
* `exportStart` (the default export, or the single wrapper call around it). A member assignment
|
|
595
|
+
* (`config.preset = …`), a call that gets the name (`Object.assign(config, …)`), or a spread of it is another
|
|
596
|
+
* use, and it makes the value unknown.
|
|
561
597
|
*/
|
|
562
|
-
static
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
598
|
+
static constantOf(program, name, exportStart) {
|
|
599
|
+
const initializers = [];
|
|
600
|
+
for (const statement of program.children("body")) {
|
|
601
|
+
const declaration = statement.is(AstNodeType.ExportNamedDeclaration) ? statement.child("declaration") : statement;
|
|
602
|
+
if (declaration.stringAt("kind") !== "const") continue;
|
|
603
|
+
for (const declarator of declaration.children("declarations")) if (declarator.child("id").name === name) initializers.push({
|
|
604
|
+
init: declarator.child("init"),
|
|
605
|
+
idStart: declarator.child("id").start
|
|
606
|
+
});
|
|
566
607
|
}
|
|
567
|
-
if (
|
|
568
|
-
|
|
608
|
+
if (initializers.length !== 1) return;
|
|
609
|
+
const names = NitroStaticConfig.nameStarts(program);
|
|
610
|
+
return program.descendants(AstNodeType.Identifier).filter((identifier) => identifier.name === name && identifier.start !== initializers[0].idStart && identifier.start !== exportStart && !names.has(identifier.start)).length === 0 ? initializers[0].init : void 0;
|
|
569
611
|
}
|
|
570
|
-
/**
|
|
571
|
-
static
|
|
572
|
-
return
|
|
612
|
+
/** The starts of identifiers that are names, not references: a property key and a member name. */
|
|
613
|
+
static nameStarts(program) {
|
|
614
|
+
return /* @__PURE__ */ new Set([...program.descendants(AstNodeType.Property).filter((property) => !property.flagAt("computed") && !property.flagAt("shorthand")).map((property) => property.child("key").start), ...program.descendants(AstNodeType.MemberExpression).filter((member) => !member.flagAt("computed")).map((member) => member.child("property").start)]);
|
|
573
615
|
}
|
|
574
|
-
/**
|
|
575
|
-
static
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
616
|
+
/** The options of one object, or the reason that the reader cannot prove it. */
|
|
617
|
+
static prove(node, file) {
|
|
618
|
+
const object = NitroStaticConfig.unwrap(node);
|
|
619
|
+
if (object === void 0 || !object.is(AstNodeType.ObjectExpression)) return {
|
|
620
|
+
kind: "unproven",
|
|
621
|
+
file,
|
|
622
|
+
reason: "an options value is not an object literal"
|
|
623
|
+
};
|
|
624
|
+
const entries = /* @__PURE__ */ new Map();
|
|
625
|
+
for (const property of object.children("properties")) {
|
|
626
|
+
if (!property.is(AstNodeType.Property)) return {
|
|
627
|
+
kind: "unproven",
|
|
628
|
+
file,
|
|
629
|
+
reason: "an options object has a spread"
|
|
630
|
+
};
|
|
631
|
+
const key = property.child("key");
|
|
632
|
+
const name = key.name ?? key.stringAt("value");
|
|
633
|
+
if (property.flagAt("computed") || property.flagAt("method") || property.stringAt("kind") !== "init" || name === void 0) return {
|
|
634
|
+
kind: "unproven",
|
|
635
|
+
file,
|
|
636
|
+
reason: "an options object has a computed key, a getter or a method"
|
|
637
|
+
};
|
|
638
|
+
entries.set(name, property.child("value"));
|
|
580
639
|
}
|
|
640
|
+
return {
|
|
641
|
+
kind: "object",
|
|
642
|
+
file,
|
|
643
|
+
entries
|
|
644
|
+
};
|
|
581
645
|
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
var StartupBaseline = class StartupBaseline {
|
|
590
|
-
/** The command that writes the file. */
|
|
591
|
-
static updateCommand = "heximon check --startup --update";
|
|
592
|
-
/**
|
|
593
|
-
* Compares the start reports of a compile with the committed file.
|
|
594
|
-
*
|
|
595
|
-
* @param rootDir - The app root that holds the committed file.
|
|
596
|
-
* @param reports - The start reports of the compile.
|
|
597
|
-
*/
|
|
598
|
-
static check(rootDir, reports) {
|
|
599
|
-
const target = join(rootDir, StartupBaselineFile.fileName);
|
|
600
|
-
const committed = StartupBaseline.readCommitted(target);
|
|
601
|
-
const cliCodes = /* @__PURE__ */ new Map();
|
|
602
|
-
if (committed === void 0 || committed.apps === void 0) {
|
|
603
|
-
const missing = committed === void 0;
|
|
604
|
-
const diagnostic = {
|
|
605
|
-
severity: DiagnosticSeverity.Error,
|
|
606
|
-
message: missing ? `there is no committed ${StartupBaselineFile.fileName}, so the startup check has nothing to compare with.` : `${StartupBaselineFile.fileName} is not a version ${StartupBaselineFile.version} startup baseline, so the startup check cannot read it.`,
|
|
607
|
-
hint: `Run \`${StartupBaseline.updateCommand}\` and commit the file.`
|
|
646
|
+
/** A string option: the first source that sets it decides. */
|
|
647
|
+
static scalar(key, sources, emptyIsAbsent = false) {
|
|
648
|
+
for (const source of sources) {
|
|
649
|
+
if (source.kind === "unproven") return {
|
|
650
|
+
kind: "computed",
|
|
651
|
+
file: source.file,
|
|
652
|
+
reason: source.reason
|
|
608
653
|
};
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
654
|
+
const node = source.entries.get(key);
|
|
655
|
+
if (node === void 0) continue;
|
|
656
|
+
const value = NitroStaticConfig.stringOf(node);
|
|
657
|
+
if (emptyIsAbsent && value === "") continue;
|
|
658
|
+
return value === void 0 ? {
|
|
659
|
+
kind: "computed",
|
|
660
|
+
file: source.file,
|
|
661
|
+
reason: `\`${key}\` is not a string literal`
|
|
662
|
+
} : {
|
|
663
|
+
kind: "literal",
|
|
664
|
+
value
|
|
620
665
|
};
|
|
621
666
|
}
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
return
|
|
667
|
+
return { kind: "none" };
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* The options of one nested section (such as `heximon`) of each source, in the same order. A section that is
|
|
671
|
+
* not an object literal the reader can prove is unproven; a source without the section adds nothing.
|
|
672
|
+
*/
|
|
673
|
+
static sectionSources(key, sources) {
|
|
674
|
+
return sources.flatMap((source) => {
|
|
675
|
+
if (source.kind === "unproven") return [source];
|
|
676
|
+
const section = source.entries.get(key);
|
|
677
|
+
return section === void 0 ? [] : [NitroStaticConfig.prove(section, source.file)];
|
|
631
678
|
});
|
|
679
|
+
}
|
|
680
|
+
/** A list option: every source adds to it, so one unproven source makes it unknown. */
|
|
681
|
+
static list(key, sources) {
|
|
682
|
+
const values = [];
|
|
683
|
+
let found = false;
|
|
684
|
+
for (const source of sources) {
|
|
685
|
+
if (source.kind === "unproven") return {
|
|
686
|
+
kind: "computed",
|
|
687
|
+
file: source.file,
|
|
688
|
+
reason: source.reason
|
|
689
|
+
};
|
|
690
|
+
const node = NitroStaticConfig.unwrap(source.entries.get(key));
|
|
691
|
+
if (node === void 0) continue;
|
|
692
|
+
const strings = (node.is(AstNodeType.ArrayExpression) ? node.children("elements") : []).map((element) => NitroStaticConfig.stringOf(element));
|
|
693
|
+
if (!node.is(AstNodeType.ArrayExpression) || strings.some((value) => value === void 0)) return {
|
|
694
|
+
kind: "computed",
|
|
695
|
+
file: source.file,
|
|
696
|
+
reason: `\`${key}\` is not an array of string literals`
|
|
697
|
+
};
|
|
698
|
+
found = true;
|
|
699
|
+
values.push(...strings);
|
|
700
|
+
}
|
|
701
|
+
return found ? {
|
|
702
|
+
kind: "literal",
|
|
703
|
+
value: values
|
|
704
|
+
} : { kind: "none" };
|
|
705
|
+
}
|
|
706
|
+
/** The text of a string literal or of a template literal with no placeholder; else `undefined`. */
|
|
707
|
+
static stringOf(node) {
|
|
708
|
+
const value = NitroStaticConfig.unwrap(node);
|
|
709
|
+
if (value === void 0) return;
|
|
710
|
+
if (typeof value.literalValue === "string") return value.literalValue;
|
|
711
|
+
if (value.is(AstNodeType.TemplateLiteral) && !value.hasChildren("expressions")) return value.children("quasis")[0]?.child("value").stringAt("cooked");
|
|
712
|
+
}
|
|
713
|
+
/** The node without its wrappers (`as`, `satisfies`, `!`, parentheses); `undefined` when absent. */
|
|
714
|
+
static unwrap(node) {
|
|
715
|
+
let current = node;
|
|
716
|
+
while (current !== void 0 && current.exists() && NitroStaticConfig.wrapperTypes.includes(current.type ?? "")) current = current.child("expression");
|
|
717
|
+
return current === void 0 || !current.exists() ? void 0 : current;
|
|
718
|
+
}
|
|
719
|
+
static parse(file) {
|
|
720
|
+
const parsed = new SourceParser().parse(file, readFileSync(file, "utf8"));
|
|
632
721
|
return {
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
path: target,
|
|
636
|
-
committed: true,
|
|
637
|
-
growth: comparison.growth,
|
|
638
|
-
notes: comparison.notes
|
|
639
|
-
},
|
|
640
|
-
diagnostics,
|
|
641
|
-
cliCodes,
|
|
642
|
-
notes: StartupBaseline.noteLines(comparison.notes)
|
|
722
|
+
program: parsed.program,
|
|
723
|
+
imports: parsed.imports
|
|
643
724
|
};
|
|
644
725
|
}
|
|
726
|
+
};
|
|
727
|
+
//#endregion
|
|
728
|
+
//#region src/nuxt-target-file.ts
|
|
729
|
+
/**
|
|
730
|
+
* Reads `.heximon/nuxt-target.json`, which `@heximon/nuxt` writes on each production Nuxt run (a build, or
|
|
731
|
+
* `nuxt prepare`): the Nitro preset and the source directories of that run. The command never runs the Nuxt
|
|
732
|
+
* config, so the record is the only source of the deploy target of a Nuxt app. It shows the last run: a record
|
|
733
|
+
* older than one of the Nuxt config files that the run loaded is refused.
|
|
734
|
+
*/
|
|
735
|
+
var NuxtTargetFile = class NuxtTargetFile {
|
|
736
|
+
/** The format version that this reader knows. `@heximon/nuxt` writes the same number. */
|
|
737
|
+
static formatVersion = 1;
|
|
738
|
+
/** The file name of the record, in the `.heximon` output directory of the app. */
|
|
739
|
+
static fileName = "nuxt-target.json";
|
|
740
|
+
/** The path of the record, relative to the app root. */
|
|
741
|
+
static relativePath = `.heximon/${NuxtTargetFile.fileName}`;
|
|
742
|
+
/** The types directory of Nuxt when the record names none: the default `buildDir`. */
|
|
743
|
+
static defaultTypesDirectory = ".nuxt";
|
|
744
|
+
/** The tsconfig files that Nuxt generates in its types directory, weakest first: the server one wins. */
|
|
745
|
+
static generatedTsconfigNames = ["tsconfig.json", "tsconfig.server.json"];
|
|
645
746
|
/**
|
|
646
|
-
*
|
|
747
|
+
* Read the record of a Nuxt app.
|
|
647
748
|
*
|
|
648
|
-
* @param rootDir - The app root
|
|
749
|
+
* @param rootDir - The app root.
|
|
750
|
+
* @returns The target, or why the record cannot be used.
|
|
649
751
|
*/
|
|
650
|
-
static
|
|
651
|
-
const
|
|
652
|
-
|
|
752
|
+
static read(rootDir) {
|
|
753
|
+
const file = join(rootDir, NuxtTargetFile.relativePath);
|
|
754
|
+
const unavailable = (message) => ({
|
|
755
|
+
kind: "unavailable",
|
|
756
|
+
file,
|
|
757
|
+
message
|
|
758
|
+
});
|
|
759
|
+
if (!existsSync(file)) return unavailable(`This Nuxt app has no record of its deploy target (${NuxtTargetFile.relativePath}).`);
|
|
760
|
+
let record;
|
|
761
|
+
try {
|
|
762
|
+
record = JSON.parse(readFileSync(file, "utf8"));
|
|
763
|
+
} catch {
|
|
764
|
+
return unavailable(`The record of the deploy target (${file}) is not valid JSON.`);
|
|
765
|
+
}
|
|
766
|
+
const content = typeof record === "object" && record !== null ? record : {};
|
|
767
|
+
if (content.version !== NuxtTargetFile.formatVersion) return unavailable(`The record of the deploy target (${file}) has the format version ${JSON.stringify(content.version)}, and this command reads version ${NuxtTargetFile.formatVersion}.`);
|
|
768
|
+
const isStringList = (value) => Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
769
|
+
if (typeof content.preset !== "string" || content.preset.length === 0 || !isStringList(content.sourceDirectories) || !isStringList(content.configFiles) || content.typesDir !== void 0 && typeof content.typesDir !== "string") return unavailable(`The record of the deploy target (${file}) does not have the expected fields.`);
|
|
770
|
+
const typesDirectory = typeof content.typesDir === "string" ? content.typesDir : NuxtTargetFile.defaultTypesDirectory;
|
|
771
|
+
const recordedAt = statSync(file).mtimeMs;
|
|
772
|
+
const changed = content.configFiles.map((path) => resolve(rootDir, path)).find((path) => !existsSync(path) || statSync(path).mtimeMs > recordedAt);
|
|
773
|
+
if (changed !== void 0) return unavailable(`The record of the deploy target (${file}) is older than the Nuxt config file ${changed}, or that file is gone, so the record can hold an old target.`);
|
|
774
|
+
return {
|
|
775
|
+
kind: "found",
|
|
776
|
+
target: {
|
|
777
|
+
file,
|
|
778
|
+
preset: content.preset,
|
|
779
|
+
sourceDirectories: content.sourceDirectories.map((path) => resolve(rootDir, path)),
|
|
780
|
+
generatedTsconfigFiles: NuxtTargetFile.generatedTsconfigNames.map((name) => join(typesDirectory, name))
|
|
781
|
+
}
|
|
782
|
+
};
|
|
653
783
|
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
784
|
+
};
|
|
785
|
+
//#endregion
|
|
786
|
+
//#region src/host-target-strategy.ts
|
|
787
|
+
/**
|
|
788
|
+
* Resolves the deploy strategy of a compile. A Nitro or Nuxt host takes it from its Nitro preset, as its build
|
|
789
|
+
* does: `@heximon/nitro` maps the preset to the strategy of the vendor. The preset of a Nitro host comes from the
|
|
790
|
+
* text of its config files ({@link NitroStaticConfig}), and the preset of a Nuxt app from the record of its last
|
|
791
|
+
* production run ({@link NuxtTargetFile}). When the command cannot apply the preset, the sidecar `platform`
|
|
792
|
+
* decides; without one, the compile uses the Node default and gets a note. Any other project uses the sidecar.
|
|
793
|
+
*/
|
|
794
|
+
var HostTargetStrategy = class HostTargetStrategy {
|
|
795
|
+
/** The package that maps a Nitro preset to its strategy. */
|
|
796
|
+
static nitroPackage = "@heximon/nitro";
|
|
797
|
+
/** The entry of {@link nitroPackage} with the strategy function. */
|
|
798
|
+
static nitroModule = `${HostTargetStrategy.nitroPackage}/module`;
|
|
799
|
+
/** The package of a Nuxt app that depends on {@link nitroPackage}, which pnpm does not hoist to the app. */
|
|
800
|
+
static nuxtPackage = "@heximon/nuxt";
|
|
801
|
+
/** The preset of the Node default. A request without a preset would take the provider of the environment. */
|
|
802
|
+
static nodePreset = "node-server";
|
|
803
|
+
/** The fix that every note names last. */
|
|
804
|
+
static platformFix = "declare `platform` in heximon.config.ts (for example `platform: new CloudflareWorkersStrategy()`)";
|
|
805
|
+
/**
|
|
806
|
+
* Resolve the strategy of a project.
|
|
657
807
|
*
|
|
658
|
-
* @param rootDir - The
|
|
659
|
-
* @param
|
|
660
|
-
* @
|
|
661
|
-
* @
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
const
|
|
666
|
-
|
|
667
|
-
const
|
|
668
|
-
|
|
808
|
+
* @param rootDir - The absolute project root.
|
|
809
|
+
* @param config - The loaded sidecar, or `undefined` when the project has none.
|
|
810
|
+
* @returns The strategy, and the note when a host compiles for the Node default.
|
|
811
|
+
* @throws CommandFailure with {@link CliDiagnosticCode.HostStrategyUnavailable} when the preset names a vendor
|
|
812
|
+
* whose platform package does not load. The build of the host fails on the same error.
|
|
813
|
+
*/
|
|
814
|
+
static async resolve(rootDir, config) {
|
|
815
|
+
const hostPreset = HostTargetStrategy.presetOf(rootDir);
|
|
816
|
+
if (hostPreset === void 0) return { strategy: NodeCompileSeam.resolveStrategy(config?.platform) };
|
|
817
|
+
const resolver = await HostTargetStrategy.resolverOf(rootDir);
|
|
818
|
+
let unapplied;
|
|
819
|
+
if (hostPreset.kind === "known" && resolver.kind === "loaded") {
|
|
820
|
+
const strategy = await HostTargetStrategy.strategyOf(resolver, rootDir, hostPreset.preset);
|
|
821
|
+
if (strategy !== void 0) return { strategy };
|
|
822
|
+
unapplied = {
|
|
823
|
+
kind: "unapplied",
|
|
824
|
+
reason: `The Nitro preset '${hostPreset.preset ?? ""}' is not a built-in preset of Nitro, and the command does not load a custom preset.`,
|
|
825
|
+
fix: "Set the Nitro preset to the name of a built-in preset"
|
|
826
|
+
};
|
|
827
|
+
} else unapplied = hostPreset.kind === "unapplied" ? hostPreset : resolver;
|
|
828
|
+
if (config?.platform !== void 0) return { strategy: config.platform };
|
|
669
829
|
return {
|
|
670
|
-
|
|
671
|
-
|
|
830
|
+
strategy: (resolver.kind === "loaded" ? await HostTargetStrategy.strategyOf(resolver, rootDir, HostTargetStrategy.nodePreset) : void 0) ?? NodeCompileSeam.resolveStrategy(void 0),
|
|
831
|
+
note: {
|
|
832
|
+
severity: DiagnosticSeverity.Info,
|
|
833
|
+
message: `The compile used the Node default, not the deploy target of the build: ${unapplied.reason} A diagnostic that depends on the target can be wrong for the build.`,
|
|
834
|
+
hint: `${unapplied.fix}, or ${HostTargetStrategy.platformFix}.`,
|
|
835
|
+
location: rootDir
|
|
836
|
+
}
|
|
672
837
|
};
|
|
673
838
|
}
|
|
674
|
-
/** The
|
|
675
|
-
static
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
839
|
+
/** The preset of a Nitro or Nuxt host, or `undefined` for a project that is neither. */
|
|
840
|
+
static presetOf(rootDir) {
|
|
841
|
+
if (NitroStaticConfig.isNuxtProject(rootDir)) {
|
|
842
|
+
const reading = NuxtTargetFile.read(rootDir);
|
|
843
|
+
return reading.kind === "found" ? {
|
|
844
|
+
kind: "known",
|
|
845
|
+
preset: reading.target.preset
|
|
846
|
+
} : {
|
|
847
|
+
kind: "unapplied",
|
|
848
|
+
reason: `${reading.message} The command does not run the Nuxt config.`,
|
|
849
|
+
fix: "Run `nuxi prepare` (or build the app) to write the record"
|
|
850
|
+
};
|
|
681
851
|
}
|
|
852
|
+
const nitro = NitroStaticConfig.read(rootDir);
|
|
853
|
+
if (!nitro.isNitroHost) return;
|
|
854
|
+
if (nitro.preset.kind === "computed") return {
|
|
855
|
+
kind: "unapplied",
|
|
856
|
+
reason: `The Nitro preset is computed (${nitro.preset.reason}, in ${nitro.preset.file}), and the command does not run your config files.`,
|
|
857
|
+
fix: "Set `preset` in the Nitro config to a string, or set NITRO_PRESET"
|
|
858
|
+
};
|
|
859
|
+
return {
|
|
860
|
+
kind: "known",
|
|
861
|
+
preset: nitro.preset.kind === "literal" ? nitro.preset.value : void 0
|
|
862
|
+
};
|
|
682
863
|
}
|
|
683
|
-
/**
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
static readCommitted(target) {
|
|
702
|
-
let content;
|
|
864
|
+
/**
|
|
865
|
+
* Load the preset resolver of `@heximon/nitro` from the project: its own copy, or the copy that its
|
|
866
|
+
* `@heximon/nuxt` depends on.
|
|
867
|
+
*/
|
|
868
|
+
static async resolverOf(rootDir) {
|
|
869
|
+
const projectRequire = createRequire(join(rootDir, "package.json"));
|
|
870
|
+
const resolutions = [() => projectRequire.resolve(HostTargetStrategy.nitroModule), () => createRequire(projectRequire.resolve(HostTargetStrategy.nuxtPackage)).resolve(HostTargetStrategy.nitroModule)];
|
|
871
|
+
let entry;
|
|
872
|
+
for (const resolution of resolutions) try {
|
|
873
|
+
entry = resolution();
|
|
874
|
+
break;
|
|
875
|
+
} catch {}
|
|
876
|
+
if (entry === void 0) return {
|
|
877
|
+
kind: "unapplied",
|
|
878
|
+
reason: `The project has no ${HostTargetStrategy.nitroPackage} to map the Nitro preset to its target.`,
|
|
879
|
+
fix: `Install ${HostTargetStrategy.nitroPackage} in the project`
|
|
880
|
+
};
|
|
881
|
+
let resolve;
|
|
703
882
|
try {
|
|
704
|
-
|
|
705
|
-
} catch {
|
|
706
|
-
return
|
|
883
|
+
resolve = (await import(pathToFileURL(entry).href)).resolveNitroPresetStrategy;
|
|
884
|
+
} catch (error) {
|
|
885
|
+
return {
|
|
886
|
+
kind: "unapplied",
|
|
887
|
+
reason: `${HostTargetStrategy.nitroModule} of the project could not be loaded (${error instanceof Error ? error.message : String(error)}).`,
|
|
888
|
+
fix: `Fix the install of ${HostTargetStrategy.nitroPackage}`
|
|
889
|
+
};
|
|
707
890
|
}
|
|
708
|
-
|
|
891
|
+
return resolve === void 0 ? {
|
|
892
|
+
kind: "unapplied",
|
|
893
|
+
reason: `The ${HostTargetStrategy.nitroPackage} of the project is older than this command, so it cannot map the Nitro preset to its target.`,
|
|
894
|
+
fix: `Update ${HostTargetStrategy.nitroPackage} to the version of this command`
|
|
895
|
+
} : {
|
|
896
|
+
kind: "loaded",
|
|
897
|
+
resolve
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* The strategy of a preset, or `undefined` for a custom preset. The vendor platform package loads from the
|
|
902
|
+
* project root, as in the build.
|
|
903
|
+
*/
|
|
904
|
+
static async strategyOf(resolver, rootDir, preset) {
|
|
709
905
|
try {
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
906
|
+
return await resolver.resolve({
|
|
907
|
+
rootDirectory: rootDir,
|
|
908
|
+
preset
|
|
909
|
+
});
|
|
910
|
+
} catch (error) {
|
|
911
|
+
throw CommandFailure.of(CliDiagnosticCode.HostStrategyUnavailable, `The strategy of ${preset === void 0 ? "the default Nitro preset" : `the Nitro preset '${preset}'`} could not be loaded, so the command cannot compile for the deploy target of the build: ${error instanceof Error ? error.message : String(error)}`, "Fix the install of the platform package. The build of the app fails on the same error.");
|
|
713
912
|
}
|
|
714
|
-
return {
|
|
715
|
-
content,
|
|
716
|
-
document,
|
|
717
|
-
apps: StartupBaselineFile.parse(document)
|
|
718
|
-
};
|
|
719
913
|
}
|
|
720
914
|
};
|
|
721
915
|
//#endregion
|
|
722
|
-
//#region src/
|
|
916
|
+
//#region src/project-compile-session.ts
|
|
723
917
|
/**
|
|
724
|
-
* The
|
|
725
|
-
*
|
|
918
|
+
* The from-disk compile prologue every inspection command shares (`check`, `graph`, `routes`, `workflows`):
|
|
919
|
+
* resolve the source directories, load the `heximon.config.ts` sidecar for the requested `$env`, derive the
|
|
920
|
+
* platform compile strategy, and merge the plugin set the compile runs with.
|
|
726
921
|
*
|
|
727
|
-
* The
|
|
728
|
-
*
|
|
729
|
-
*
|
|
730
|
-
*
|
|
731
|
-
* package reach. From these files, the code of a package is every file that they import, with a static import or
|
|
732
|
-
* with `import()`, up to the loader chunk of another package.
|
|
922
|
+
* The `.env` cascade is deliberately NOT loaded here. Only `heximon routes` loads it (so a booted app and its
|
|
923
|
+
* config sidecar read the environment `vp dev` would give them); the compile-only verbs must not pick up a
|
|
924
|
+
* developer's `.env` just by inspecting a project. A caller that wants it loads it before calling
|
|
925
|
+
* {@link open}, so the sidecar read still sees it.
|
|
733
926
|
*/
|
|
734
|
-
var
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
/**
|
|
738
|
-
static
|
|
739
|
-
/** The
|
|
740
|
-
|
|
741
|
-
/** The
|
|
742
|
-
|
|
743
|
-
/** The
|
|
744
|
-
|
|
745
|
-
/** The
|
|
746
|
-
|
|
747
|
-
/** The
|
|
748
|
-
|
|
749
|
-
/**
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
*/
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
this.
|
|
765
|
-
this.
|
|
766
|
-
this.
|
|
927
|
+
var ProjectCompileSession = class ProjectCompileSession {
|
|
928
|
+
/** The gitignored directory the compile anchors its generated paths at. */
|
|
929
|
+
static outputDirectoryName = ".heximon";
|
|
930
|
+
/** Why a check reports a topology failure: the build of the same project fails on it too. */
|
|
931
|
+
static topologyFailureHint = "The sidecar declares `crossService`, so the build reads the cross-service subscription topology of the workspace and fails on the same error.";
|
|
932
|
+
/** The absolute project root the compile runs against. */
|
|
933
|
+
rootDir;
|
|
934
|
+
/** The warm compiler — reused by a caller that also prunes or writes type verdicts. */
|
|
935
|
+
compiler;
|
|
936
|
+
/** The resolved sidecar, or `undefined` when the project ships none. */
|
|
937
|
+
config;
|
|
938
|
+
/** The absolute source directories to scan, or `undefined` for the compiler's own `<rootDir>/src`. */
|
|
939
|
+
sourceDirectories;
|
|
940
|
+
/** The absolute `.heximon/` directory the compile anchors its generated paths at. */
|
|
941
|
+
outputDirectory;
|
|
942
|
+
/** The compiler plugins the sidecar declares, merged with the platform strategy's own. */
|
|
943
|
+
plugins;
|
|
944
|
+
strategy;
|
|
945
|
+
/** The `$env` name the sidecar was read for, which the member sidecars of the workspace scan share. */
|
|
946
|
+
environment;
|
|
947
|
+
/** The runtime config file the app compile checks and bakes in, or `undefined` when there is none. */
|
|
948
|
+
runtimeConfig;
|
|
949
|
+
/** The module aliases of a host build, which {@link compile} resolves, or `undefined` for none. */
|
|
950
|
+
aliases;
|
|
951
|
+
/** The output-relative files that another step writes into {@link outputDirectory}, which {@link write} keeps. */
|
|
952
|
+
retainedOutput;
|
|
953
|
+
/** Why a Nitro or Nuxt host compiles for the Node default, or `undefined` when it does not. */
|
|
954
|
+
hostTargetNote;
|
|
955
|
+
constructor(options) {
|
|
956
|
+
this.rootDir = options.rootDir;
|
|
957
|
+
this.compiler = options.compiler;
|
|
958
|
+
this.config = options.config;
|
|
959
|
+
this.runtimeConfig = options.runtimeConfig;
|
|
960
|
+
this.sourceDirectories = options.sourceDirectories;
|
|
961
|
+
this.plugins = options.plugins;
|
|
962
|
+
this.strategy = options.strategy;
|
|
963
|
+
this.environment = options.environment;
|
|
964
|
+
this.aliases = options.aliases;
|
|
965
|
+
this.hostTargetNote = options.hostTargetNote;
|
|
966
|
+
this.outputDirectory = join(options.rootDir, ProjectCompileSession.outputDirectoryName);
|
|
967
|
+
this.retainedOutput = [NuxtTargetFile.fileName, ...options.retainedOutput ?? []];
|
|
767
968
|
}
|
|
768
969
|
/**
|
|
769
|
-
*
|
|
970
|
+
* Resolve one project's compile inputs.
|
|
770
971
|
*
|
|
771
|
-
* @param
|
|
972
|
+
* @param options - The project root, the `$env` name, and the resolved source directories.
|
|
973
|
+
* @returns The session the caller compiles through.
|
|
974
|
+
* @throws CommandFailure with `cli.config-unreadable` when the sidecar throws, or with
|
|
975
|
+
* `cli.host-strategy-unavailable` when the strategy of a host preset does not load.
|
|
772
976
|
*/
|
|
773
|
-
static
|
|
774
|
-
const
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
977
|
+
static async open(options) {
|
|
978
|
+
const config = await ConfigSidecar.load(options.rootDir, options.environment);
|
|
979
|
+
const target = options.hostTarget === true ? await HostTargetStrategy.resolve(options.rootDir, config) : {
|
|
980
|
+
strategy: NodeCompileSeam.resolveStrategy(config?.platform),
|
|
981
|
+
note: void 0
|
|
982
|
+
};
|
|
983
|
+
const strategy = target.strategy;
|
|
984
|
+
return new ProjectCompileSession({
|
|
985
|
+
rootDir: options.rootDir,
|
|
986
|
+
compiler: new ProjectCompiler(),
|
|
987
|
+
config,
|
|
988
|
+
runtimeConfig: await RuntimeConfigLoader.load(options.rootDir, config, options.environment),
|
|
989
|
+
sourceDirectories: options.sourceDirectories,
|
|
990
|
+
plugins: NodeCompileSeam.mergePlugins(config?.plugins ?? [], strategy),
|
|
991
|
+
strategy,
|
|
992
|
+
environment: options.environment,
|
|
993
|
+
...target.note === void 0 ? {} : { hostTargetNote: target.note }
|
|
994
|
+
});
|
|
780
995
|
}
|
|
781
996
|
/**
|
|
782
|
-
*
|
|
783
|
-
*
|
|
784
|
-
*
|
|
785
|
-
* through `import()`, as that chunk loads on the first use of that package.
|
|
997
|
+
* A session that compiles with the production inputs of a Nitro or Nuxt host: the strategy of its preset, the
|
|
998
|
+
* Nitro host plugin, and the production sidecar. {@link open} reads only the sidecar, so it compiles a Nitro app
|
|
999
|
+
* for Node whatever the preset is.
|
|
786
1000
|
*
|
|
787
|
-
* @param
|
|
788
|
-
*
|
|
1001
|
+
* @param options - The project root, the host inputs, the module aliases, and the source directories to scan
|
|
1002
|
+
* instead of the host's, when the caller has some.
|
|
1003
|
+
* @returns The session the caller compiles through.
|
|
789
1004
|
*/
|
|
790
|
-
|
|
791
|
-
|
|
1005
|
+
static forNitroHost(options) {
|
|
1006
|
+
const inputs = options.inputs;
|
|
1007
|
+
return new ProjectCompileSession({
|
|
1008
|
+
rootDir: options.rootDir,
|
|
1009
|
+
compiler: new ProjectCompiler(),
|
|
1010
|
+
config: inputs.config,
|
|
1011
|
+
runtimeConfig: inputs.runtimeConfig,
|
|
1012
|
+
sourceDirectories: options.sourceDirectories ?? inputs.sourceDirectories,
|
|
1013
|
+
plugins: inputs.plugins,
|
|
1014
|
+
strategy: inputs.strategy,
|
|
1015
|
+
environment: "production",
|
|
1016
|
+
aliases: options.aliases,
|
|
1017
|
+
...inputs.retainedOutput === void 0 ? {} : { retainedOutput: inputs.retainedOutput }
|
|
1018
|
+
});
|
|
792
1019
|
}
|
|
793
1020
|
/**
|
|
794
|
-
*
|
|
795
|
-
* packages ({@link codeOf}), also where it passes through the static start.
|
|
1021
|
+
* Run the app-mode compile.
|
|
796
1022
|
*
|
|
797
|
-
* @param
|
|
1023
|
+
* @param typeVerdicts - Whether to emit the editor-verdict typegen file (only `heximon check` writes it).
|
|
1024
|
+
* @param options - The baseline verification pass, and the cross-service topology the fan-out
|
|
1025
|
+
* diagnostics read.
|
|
1026
|
+
* @returns The compile output, diagnostics included.
|
|
798
1027
|
*/
|
|
799
|
-
|
|
800
|
-
return
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
1028
|
+
compile(typeVerdicts = false, options = {}) {
|
|
1029
|
+
return this.compiler.compile({
|
|
1030
|
+
rootDir: this.rootDir,
|
|
1031
|
+
outputDirectory: this.outputDirectory,
|
|
1032
|
+
plugins: this.plugins,
|
|
1033
|
+
strategy: this.strategy,
|
|
1034
|
+
...this.sourceDirectories === void 0 ? {} : { sourceDirectories: this.sourceDirectories },
|
|
1035
|
+
...this.aliases === void 0 ? {} : {
|
|
1036
|
+
aliases: this.aliases.prefixes,
|
|
1037
|
+
exactAliases: this.aliases.exact
|
|
1038
|
+
},
|
|
1039
|
+
...this.config === void 0 ? {} : { config: this.config },
|
|
1040
|
+
...this.config?.wiringMode === void 0 ? {} : { wiringMode: this.config.wiringMode },
|
|
1041
|
+
...this.runtimeConfig === void 0 ? {} : { runtimeConfig: this.runtimeConfig },
|
|
1042
|
+
...this.config?.root === void 0 ? {} : { apps: { main: this.config.root } },
|
|
1043
|
+
...typeVerdicts ? { typeVerdicts: true } : {},
|
|
1044
|
+
...options.verifyBaseline === true ? { verifyBaseline: true } : {},
|
|
1045
|
+
...options.workspaceSubscribers === void 0 ? {} : { workspaceSubscribers: options.workspaceSubscribers }
|
|
1046
|
+
});
|
|
805
1047
|
}
|
|
806
1048
|
/**
|
|
807
|
-
*
|
|
808
|
-
*
|
|
1049
|
+
* Read the cross-service subscription topology for {@link compile}: only when the sidecar declares
|
|
1050
|
+
* `crossService`, with the plugins and the `$env` name of this session, over the production graph that
|
|
1051
|
+
* {@link compile} builds.
|
|
809
1052
|
*
|
|
810
|
-
* @
|
|
1053
|
+
* @returns The topology, or one error diagnostic in place of the error the read threw.
|
|
811
1054
|
*/
|
|
812
|
-
|
|
813
|
-
|
|
1055
|
+
async readWorkspaceTopology() {
|
|
1056
|
+
try {
|
|
1057
|
+
return {
|
|
1058
|
+
topology: await WorkspaceTopologyReader.resolveFromConfig(this.rootDir, this.plugins, this.config, this.environment, CompileEnvironment.Production),
|
|
1059
|
+
diagnostics: []
|
|
1060
|
+
};
|
|
1061
|
+
} catch (error) {
|
|
1062
|
+
return {
|
|
1063
|
+
topology: void 0,
|
|
1064
|
+
diagnostics: [this.topologyFailure(error)]
|
|
1065
|
+
};
|
|
1066
|
+
}
|
|
814
1067
|
}
|
|
815
1068
|
/**
|
|
816
|
-
*
|
|
1069
|
+
* Write the generated files into {@link outputDirectory}, and prune the stale ones. The files that another
|
|
1070
|
+
* step writes there, such as the deploy target record of `@heximon/nuxt`, stay.
|
|
817
1071
|
*
|
|
818
|
-
* @param files - The files.
|
|
1072
|
+
* @param files - The files of the compile output.
|
|
819
1073
|
*/
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
for (const file of files) bytes += this.bytesByFile.get(file) ?? 0;
|
|
823
|
-
return {
|
|
824
|
-
files: files.size,
|
|
825
|
-
bytes
|
|
826
|
-
};
|
|
1074
|
+
write(files) {
|
|
1075
|
+
this.compiler.write(this.outputDirectory, files, { retain: this.retainedOutput });
|
|
827
1076
|
}
|
|
828
|
-
/**
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
* package. Other bytes, such as a runtime helper of the bundler, belong to no package.
|
|
832
|
-
*
|
|
833
|
-
* @param files - The files.
|
|
834
|
-
* @returns The bytes of each package, by package name.
|
|
835
|
-
*/
|
|
836
|
-
packageBytes(files) {
|
|
837
|
-
const bytesByPackage = /* @__PURE__ */ new Map();
|
|
838
|
-
for (const file of files) for (const [name, bytes] of Object.entries(this.packagesOfFile(file))) bytesByPackage.set(name, (bytesByPackage.get(name) ?? 0) + bytes);
|
|
839
|
-
return bytesByPackage;
|
|
1077
|
+
/** The platform the session compiles for, or `undefined` for the zero-config Node default. */
|
|
1078
|
+
platformName() {
|
|
1079
|
+
return this.strategy.platformName;
|
|
840
1080
|
}
|
|
841
1081
|
/**
|
|
842
|
-
*
|
|
843
|
-
*
|
|
1082
|
+
* Why a Nitro or Nuxt host compiles for the Node default and not for the deploy target of its build.
|
|
1083
|
+
*
|
|
1084
|
+
* @returns One info diagnostic, or `undefined` when the session compiles for the target of the build.
|
|
844
1085
|
*/
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
const pending = seeds.filter((file) => !loaded.has(file));
|
|
848
|
-
for (let file = pending.pop(); file !== void 0; file = pending.pop()) {
|
|
849
|
-
if (reached.has(file)) continue;
|
|
850
|
-
reached.add(file);
|
|
851
|
-
const targets = [...StartupBuiltCode.importsOf(this.measured.staticImports, file), ...boundary === void 0 ? [] : StartupBuiltCode.importsOf(this.measured.dynamicImports, file).filter((target) => !boundary.has(target))];
|
|
852
|
-
for (const target of targets) if (!loaded.has(target) && !reached.has(target)) pending.push(target);
|
|
853
|
-
}
|
|
854
|
-
return reached;
|
|
1086
|
+
targetNote() {
|
|
1087
|
+
return this.hostTargetNote;
|
|
855
1088
|
}
|
|
856
|
-
/** The
|
|
857
|
-
|
|
858
|
-
return
|
|
859
|
-
}
|
|
860
|
-
/** The loader chunks of the packages that are not in `identities`. */
|
|
861
|
-
loaderChunksOutside(identities) {
|
|
862
|
-
return new Set([...this.loaderChunks].filter(([identity]) => !identities.has(identity)).flatMap(([, chunks]) => chunks));
|
|
1089
|
+
/** The runtime class the session compiles for: `node` or `edge`. */
|
|
1090
|
+
targetClass() {
|
|
1091
|
+
return this.strategy.targetClass;
|
|
863
1092
|
}
|
|
864
1093
|
/**
|
|
865
|
-
*
|
|
866
|
-
*
|
|
867
|
-
*
|
|
1094
|
+
* Run the compile `vp build` runs, into a chosen directory: the production graph with the sidecar's wiring
|
|
1095
|
+
* mode, diagnostic dials and cross-service topology. {@link compile} leaves out the first two, and reads the
|
|
1096
|
+
* topology only when its caller passes it.
|
|
1097
|
+
*
|
|
1098
|
+
* @param outputDirectory - The directory the generated files will be written to. It must sit directly
|
|
1099
|
+
* under the project root, because the generated imports of the app source are relative to it.
|
|
1100
|
+
* @param aliases - The module aliases of the project (prefix → absolute path), when it has some.
|
|
1101
|
+
* @returns The compile output. Nothing is written.
|
|
868
1102
|
*/
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
1103
|
+
async compileProduction(outputDirectory, aliases = {
|
|
1104
|
+
exact: {},
|
|
1105
|
+
prefixes: {}
|
|
1106
|
+
}) {
|
|
1107
|
+
const workspaceSubscribers = await WorkspaceTopologyReader.resolveFromConfig(this.rootDir, this.plugins, this.config, this.environment);
|
|
1108
|
+
return this.compiler.compile({
|
|
1109
|
+
rootDir: this.rootDir,
|
|
1110
|
+
outputDirectory,
|
|
1111
|
+
plugins: this.plugins,
|
|
1112
|
+
strategy: this.strategy,
|
|
1113
|
+
environment: CompileEnvironment.Production,
|
|
1114
|
+
aliases: aliases.prefixes,
|
|
1115
|
+
exactAliases: aliases.exact,
|
|
1116
|
+
...this.sourceDirectories === void 0 ? {} : { sourceDirectories: this.sourceDirectories },
|
|
1117
|
+
...this.config === void 0 ? {} : { config: this.config },
|
|
1118
|
+
...this.config?.wiringMode === void 0 ? {} : { wiringMode: this.config.wiringMode },
|
|
1119
|
+
...this.config?.strictDiagnostics === void 0 ? {} : { strictDiagnostics: this.config.strictDiagnostics },
|
|
1120
|
+
...this.config?.suppressDiagnostics === void 0 ? {} : { suppressDiagnostics: this.config.suppressDiagnostics },
|
|
1121
|
+
...this.runtimeConfig === void 0 ? {} : { runtimeConfig: this.runtimeConfig },
|
|
1122
|
+
...this.config?.root === void 0 ? {} : { apps: { main: this.config.root } },
|
|
1123
|
+
...workspaceSubscribers === void 0 ? {} : { workspaceSubscribers }
|
|
1124
|
+
});
|
|
885
1125
|
}
|
|
886
1126
|
/**
|
|
887
|
-
*
|
|
888
|
-
*
|
|
1127
|
+
* Run the library-mode compile — a published module package goes through the same {@link LibraryEmitter}
|
|
1128
|
+
* its own `vp pack` build uses, rather than being compiled as an app. Not a byte-for-byte replay of that
|
|
1129
|
+
* build: the pack host supplies its own plugin set, wiring mode and `pack.entry`-derived publication set,
|
|
1130
|
+
* while this reads plugins from the config sidecar and falls back to the committed `exports` map.
|
|
1131
|
+
*
|
|
1132
|
+
* Two things make the app-mode compile wrong here, not merely noisy. It enforces every `requires` hop-by-hop
|
|
1133
|
+
* against a host binding, while a library leaves that seam open on purpose (the published factory reads a
|
|
1134
|
+
* required token as a constructor input); and it looks for one root module, while a multi-root package
|
|
1135
|
+
* publishes several. Both surface as errors a `vp pack` of the same source never raises.
|
|
1136
|
+
*
|
|
1137
|
+
* @param options - `pruneVerdicts` also removes a verdict mirror an EARLIER app-mode run wrote. Library
|
|
1138
|
+
* mode emits none to replace it, so nothing would ever overwrite those app-mode-only errors — only
|
|
1139
|
+
* `heximon check`, which owns the mirror, asks for it. Every other verb stays write-free.
|
|
1140
|
+
* @returns The compile's diagnostics, files and graph projections; a throwing emit becomes one diagnostic.
|
|
889
1141
|
*/
|
|
890
|
-
|
|
891
|
-
const
|
|
892
|
-
const
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
const
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
1142
|
+
compileLibrary(options = {}) {
|
|
1143
|
+
const sourceDirectory = this.sourceDirectories?.[0] ?? (this.config?.sourceDirectory === void 0 ? void 0 : resolve(this.rootDir, this.config.sourceDirectory));
|
|
1144
|
+
const prunedVerdicts = options.pruneVerdicts === true ? this.compiler.pruneTypeVerdicts({
|
|
1145
|
+
rootDir: this.rootDir,
|
|
1146
|
+
...sourceDirectory === void 0 ? {} : { sourceDirectories: [sourceDirectory] }
|
|
1147
|
+
}) : void 0;
|
|
1148
|
+
try {
|
|
1149
|
+
const emitted = new LibraryEmitter(this.compiler).compile({
|
|
1150
|
+
rootDir: this.rootDir,
|
|
1151
|
+
plugins: this.plugins,
|
|
1152
|
+
...sourceDirectory === void 0 ? {} : { sourceDirectory },
|
|
1153
|
+
...this.config === void 0 ? {} : { config: this.config }
|
|
1154
|
+
});
|
|
1155
|
+
return {
|
|
1156
|
+
diagnostics: emitted.diagnostics,
|
|
1157
|
+
files: emitted.files,
|
|
1158
|
+
moduleGraph: emitted.moduleGraph,
|
|
1159
|
+
boundaryNodes: emitted.boundaryNodes,
|
|
1160
|
+
...prunedVerdicts === void 0 ? {} : { prunedVerdicts }
|
|
1161
|
+
};
|
|
1162
|
+
} catch (error) {
|
|
1163
|
+
return {
|
|
1164
|
+
diagnostics: [{
|
|
1165
|
+
severity: DiagnosticSeverity.Error,
|
|
1166
|
+
message: `The library compile of this package failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1167
|
+
location: this.rootDir
|
|
1168
|
+
}],
|
|
1169
|
+
files: [],
|
|
1170
|
+
moduleGraph: void 0,
|
|
1171
|
+
boundaryNodes: [],
|
|
1172
|
+
...prunedVerdicts === void 0 ? {} : { prunedVerdicts }
|
|
1173
|
+
};
|
|
910
1174
|
}
|
|
911
|
-
const name = StartupBuiltCode.installedPackageOf(file);
|
|
912
|
-
return name === void 0 ? {} : { [name]: fileBytes };
|
|
913
|
-
}
|
|
914
|
-
/** The package of a file in a `node_modules` directory of the output, or `undefined`. */
|
|
915
|
-
static installedPackageOf(file) {
|
|
916
|
-
const index = file.lastIndexOf(StartupBuiltCode.packageDirectory);
|
|
917
|
-
if (index === -1 || index > 0 && file[index - 1] !== "/") return;
|
|
918
|
-
const segments = file.slice(index + StartupBuiltCode.packageDirectory.length).split("/");
|
|
919
|
-
const length = segments[0]?.startsWith("@") === true ? 2 : 1;
|
|
920
|
-
return segments.length > length ? segments.slice(0, length).join("/") : void 0;
|
|
921
1175
|
}
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
*
|
|
927
|
-
*
|
|
928
|
-
*/
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
places;
|
|
936
|
-
/** The identities of the composed packages of the report. */
|
|
937
|
-
packages;
|
|
938
|
-
/** @param report - The start report of the app. */
|
|
939
|
-
constructor(report) {
|
|
940
|
-
this.places = new Set(report.entries.map((entry) => `${entry.kind} ${entry.key}`));
|
|
941
|
-
this.packages = /* @__PURE__ */ new Set([
|
|
942
|
-
...report.start.map((entry) => entry.package),
|
|
943
|
-
...report.entries.flatMap((entry) => entry.packages),
|
|
944
|
-
...(report.loads?.asyncRegistries ?? []).flatMap((registry) => [registry.owner, ...registry.packages.map((entry) => entry.package)])
|
|
945
|
-
]);
|
|
1176
|
+
/**
|
|
1177
|
+
* One error diagnostic for each library the last compile consumed whose source changed since its last
|
|
1178
|
+
* pack. The compile read old wiring for it, so the verdicts of that compile cannot be trusted. Each
|
|
1179
|
+
* message is the `heximon doctor` line for the library.
|
|
1180
|
+
*
|
|
1181
|
+
* @returns The diagnostics, or an empty list when every consumed library is current.
|
|
1182
|
+
*/
|
|
1183
|
+
async staleLibraryDiagnostics() {
|
|
1184
|
+
return (await new LibraryFreshness().findStale(this.compiler.consumedBoundaryPackages())).map((library) => ({
|
|
1185
|
+
severity: DiagnosticSeverity.Error,
|
|
1186
|
+
message: LibraryFreshness.staleLine(library.specifier, library.report),
|
|
1187
|
+
location: library.report.packageDirectory
|
|
1188
|
+
}));
|
|
946
1189
|
}
|
|
947
|
-
/** The
|
|
948
|
-
|
|
1190
|
+
/** The `rootDir` + `sourceDirectories` pair the type-verdict write path keys on, plus the runtime config file. */
|
|
1191
|
+
typeVerdictTarget() {
|
|
949
1192
|
return {
|
|
950
|
-
|
|
951
|
-
|
|
1193
|
+
rootDir: this.rootDir,
|
|
1194
|
+
...this.runtimeConfig === void 0 ? {} : { runtimeConfigFile: this.runtimeConfig.file },
|
|
1195
|
+
...this.sourceDirectories === void 0 ? {} : { sourceDirectories: this.sourceDirectories }
|
|
952
1196
|
};
|
|
953
1197
|
}
|
|
954
|
-
/**
|
|
955
|
-
|
|
1198
|
+
/**
|
|
1199
|
+
* The diagnostic for a topology read that threw. The reader puts the code of a known failure on the error
|
|
1200
|
+
* and at the start of its message; the diagnostic carries the code once, in its own field.
|
|
1201
|
+
*/
|
|
1202
|
+
topologyFailure(error) {
|
|
1203
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1204
|
+
const code = error?.code;
|
|
1205
|
+
const knownCode = Object.values(DiagnosticCode).find((candidate) => candidate === code);
|
|
956
1206
|
return {
|
|
957
|
-
|
|
958
|
-
|
|
1207
|
+
severity: DiagnosticSeverity.Error,
|
|
1208
|
+
message: knownCode !== void 0 && message.startsWith(`[${knownCode}] `) ? message.slice(knownCode.length + 3) : message,
|
|
1209
|
+
location: this.rootDir,
|
|
1210
|
+
hint: ProjectCompileSession.topologyFailureHint,
|
|
1211
|
+
...knownCode === void 0 ? {} : { code: knownCode }
|
|
959
1212
|
};
|
|
960
1213
|
}
|
|
1214
|
+
};
|
|
1215
|
+
//#endregion
|
|
1216
|
+
//#region src/source-directories.ts
|
|
1217
|
+
/**
|
|
1218
|
+
* Resolves which directories the host-agnostic compile commands (`check`, `graph`, `routes`) scan for Heximon
|
|
1219
|
+
* source. The compiler's own default is `<rootDir>/src`; a Nitro/Nuxt host instead keeps its modules under
|
|
1220
|
+
* `server/` and passes that directory to the compiler from its own build. The standalone CLI is just another
|
|
1221
|
+
* host, so it detects that layout and passes `server/` too — otherwise its compile finds no modules.
|
|
1222
|
+
*/
|
|
1223
|
+
var SourceDirectories = class SourceDirectories {
|
|
1224
|
+
/** Host-framework config basenames whose presence marks a project that roots its modules under `server/`. */
|
|
1225
|
+
static serverHostConfigNames = [
|
|
1226
|
+
"nuxt.config.ts",
|
|
1227
|
+
"nuxt.config.mts",
|
|
1228
|
+
"nuxt.config.js",
|
|
1229
|
+
"nuxt.config.mjs",
|
|
1230
|
+
"nitro.config.ts",
|
|
1231
|
+
"nitro.config.mts",
|
|
1232
|
+
"nitro.config.js",
|
|
1233
|
+
"nitro.config.mjs"
|
|
1234
|
+
];
|
|
961
1235
|
/**
|
|
962
|
-
*
|
|
1236
|
+
* The absolute source directories to scan, or `undefined` to accept the compiler's own `<rootDir>/src`
|
|
1237
|
+
* default. An explicit `--src` (comma-separated) always wins; otherwise, when the project has no `src/` but
|
|
1238
|
+
* is a Nitro/Nuxt host carrying a `server/` directory, that directory is scanned.
|
|
963
1239
|
*
|
|
964
|
-
* @param
|
|
1240
|
+
* @param rootDir - The absolute project root.
|
|
1241
|
+
* @param sourceOption - The raw `--src` argument, when the caller passed one.
|
|
1242
|
+
* @returns Absolute directories, or `undefined` for the compiler default.
|
|
965
1243
|
*/
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
...reason.tokens === void 0 ? {} : { tokens: reason.tokens },
|
|
971
|
-
text: this.textOf(reason)
|
|
972
|
-
};
|
|
973
|
-
}
|
|
974
|
-
/** The text of one reason. */
|
|
975
|
-
textOf(reason) {
|
|
976
|
-
const { source } = reason;
|
|
977
|
-
switch (reason.kind) {
|
|
978
|
-
case StartReasonKind.Dependency: return this.dependencyText(reason);
|
|
979
|
-
case StartReasonKind.SealGroup: return `seal group of the sync registry ${source ?? "(registry not recorded)"}`;
|
|
980
|
-
case StartReasonKind.ContributesTo: return `contributor to the registry ${source ?? "(registry not recorded)"}`;
|
|
981
|
-
case StartReasonKind.ContributionCannotWait: return `contribution section that cannot wait${source === void 0 ? "" : `: ${source}`}`;
|
|
982
|
-
case StartReasonKind.RegistrarOptions: return `registrar options of the plugin ${source ?? "(plugin not recorded)"}`;
|
|
983
|
-
case StartReasonKind.PluginNeedsAtStart: return `a compiler plugin needs it at the start${source === void 0 ? "" : `: ${source}`}`;
|
|
984
|
-
case StartReasonKind.LazyRead: return `Lazy<T> read${source === void 0 ? "" : `: ${source}`}`;
|
|
985
|
-
case StartReasonKind.EagerProvider: return `eager provider${source === void 0 ? "" : ` ${source}`}`;
|
|
986
|
-
case StartReasonKind.BootHook: return "boot hook";
|
|
987
|
-
case StartReasonKind.ConfigSection: return "config section";
|
|
988
|
-
case StartReasonKind.DispatchInventory: return "DispatchInventory read";
|
|
989
|
-
case StartReasonKind.NotDeferred: return "the target does not defer packages";
|
|
990
|
-
default: return source === void 0 ? String(reason.kind) : `${String(reason.kind)}: ${source}`;
|
|
1244
|
+
static resolve(rootDir, sourceOption) {
|
|
1245
|
+
if (sourceOption !== void 0 && sourceOption.trim() !== "") {
|
|
1246
|
+
const directories = sourceOption.split(",").map((entry) => entry.trim()).filter((entry) => entry.length > 0).map((entry) => resolve(rootDir, entry));
|
|
1247
|
+
if (directories.length > 0) return directories;
|
|
991
1248
|
}
|
|
1249
|
+
if (SourceDirectories.isDirectory(join(rootDir, "src"))) return;
|
|
1250
|
+
if (SourceDirectories.isServerHost(rootDir) && SourceDirectories.isDirectory(join(rootDir, "server"))) return [join(rootDir, "server")];
|
|
992
1251
|
}
|
|
993
|
-
/**
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1252
|
+
/** Whether the root carries a Nitro/Nuxt host config, which roots Heximon modules under `server/`. */
|
|
1253
|
+
static isServerHost(rootDir) {
|
|
1254
|
+
return SourceDirectories.serverHostConfigNames.some((name) => existsSync(join(rootDir, name)));
|
|
1255
|
+
}
|
|
1256
|
+
/** Whether `path` exists and is a directory. */
|
|
1257
|
+
static isDirectory(path) {
|
|
1258
|
+
try {
|
|
1259
|
+
return statSync(path).isDirectory();
|
|
1260
|
+
} catch {
|
|
1261
|
+
return false;
|
|
1262
|
+
}
|
|
1000
1263
|
}
|
|
1001
1264
|
};
|
|
1002
1265
|
//#endregion
|
|
1003
|
-
//#region src/startup-
|
|
1266
|
+
//#region src/startup-baseline.ts
|
|
1004
1267
|
/**
|
|
1005
|
-
* The
|
|
1006
|
-
*
|
|
1007
|
-
* `heximon.startup.json`. Without build output, the check skips it.
|
|
1268
|
+
* The command side of the committed startup baseline: it reads and writes `heximon.startup.json`, and turns a
|
|
1269
|
+
* comparison into the errors and notes of `heximon check --startup`.
|
|
1008
1270
|
*/
|
|
1009
|
-
var
|
|
1271
|
+
var StartupBaseline = class StartupBaseline {
|
|
1010
1272
|
/** The command that writes the file. */
|
|
1011
1273
|
static updateCommand = "heximon check --startup --update";
|
|
1012
|
-
/** How many requests that the walk could not follow a note names. */
|
|
1013
|
-
static namedGapCount = 5;
|
|
1014
|
-
/**
|
|
1015
|
-
* Measure the server code of the last build.
|
|
1016
|
-
*
|
|
1017
|
-
* @param rootDir - The app root.
|
|
1018
|
-
* @returns The measurement, or why there is none.
|
|
1019
|
-
*/
|
|
1020
|
-
static measure(rootDir) {
|
|
1021
|
-
const reading = ServerBuildRecord.read(rootDir);
|
|
1022
|
-
const skip = (reason, message) => ({
|
|
1023
|
-
kind: "skipped",
|
|
1024
|
-
result: {
|
|
1025
|
-
action: "skipped",
|
|
1026
|
-
reason,
|
|
1027
|
-
record: reading.file
|
|
1028
|
-
},
|
|
1029
|
-
message: `${message} The size check of the server code is skipped.`
|
|
1030
|
-
});
|
|
1031
|
-
if (reading.kind === "absent") return skip("no-build-output", `There is no build output: the build writes ${ServerBuildRecord.relativePath}, and the file is absent. Build the app before the check to check the size of its server code.`);
|
|
1032
|
-
if (reading.kind === "unreadable") return skip("record-unreadable", `${reading.message} Build the app again.`);
|
|
1033
|
-
const names = Object.keys(reading.entries).sort();
|
|
1034
|
-
const missing = names.map((name) => reading.entries[name].path).find((path) => !StartupSizeCheck.isFile(path) || !existsSync(reading.outputDirectory));
|
|
1035
|
-
if (missing !== void 0) return skip("entry-missing", `The build record names the server entry ${relative(rootDir, missing)}, and the file does not exist. Build the app again.`);
|
|
1036
|
-
const entries = names.map((name) => {
|
|
1037
|
-
const entry = reading.entries[name];
|
|
1038
|
-
const graph = ServerCodeWalk.graph(rootDir, reading.outputDirectory, entry.path, entry.target);
|
|
1039
|
-
return {
|
|
1040
|
-
name,
|
|
1041
|
-
measurement: graph.measurement,
|
|
1042
|
-
staticImports: graph.staticImports,
|
|
1043
|
-
dynamicImports: graph.dynamicImports
|
|
1044
|
-
};
|
|
1045
|
-
});
|
|
1046
|
-
return {
|
|
1047
|
-
kind: "measured",
|
|
1048
|
-
entries,
|
|
1049
|
-
section: Object.fromEntries(entries.map(({ name, measurement }) => [name, StartupSizeBudget.recordOf(measurement)])),
|
|
1050
|
-
...reading.chunks === void 0 ? {} : { chunks: reading.chunks },
|
|
1051
|
-
...reading.generated === void 0 ? {} : { generated: reading.generated }
|
|
1052
|
-
};
|
|
1053
|
-
}
|
|
1054
1274
|
/**
|
|
1055
|
-
*
|
|
1275
|
+
* Compares the start reports of a compile with the committed file.
|
|
1056
1276
|
*
|
|
1057
|
-
* @param
|
|
1058
|
-
*
|
|
1059
|
-
* @param measured - The measurement.
|
|
1277
|
+
* @param rootDir - The app root that holds the committed file.
|
|
1278
|
+
* @param reports - The start reports of the compile.
|
|
1060
1279
|
*/
|
|
1061
|
-
static
|
|
1062
|
-
const
|
|
1063
|
-
const
|
|
1280
|
+
static check(rootDir, reports) {
|
|
1281
|
+
const target = join(rootDir, StartupBaselineFile.fileName);
|
|
1282
|
+
const committed = StartupBaseline.readCommitted(target);
|
|
1064
1283
|
const cliCodes = /* @__PURE__ */ new Map();
|
|
1065
|
-
if (committed === void 0)
|
|
1066
|
-
|
|
1067
|
-
action: "compare",
|
|
1068
|
-
entries: StartupSizeCheck.entriesOf(measured, void 0),
|
|
1069
|
-
committed: false,
|
|
1070
|
-
reason: "no-baseline"
|
|
1071
|
-
},
|
|
1072
|
-
diagnostics: [],
|
|
1073
|
-
cliCodes,
|
|
1074
|
-
notes
|
|
1075
|
-
};
|
|
1076
|
-
const baseline = StartupSizeBudget.parse(committed);
|
|
1077
|
-
if (baseline === null || baseline === void 0) {
|
|
1284
|
+
if (committed === void 0 || committed.apps === void 0) {
|
|
1285
|
+
const missing = committed === void 0;
|
|
1078
1286
|
const diagnostic = {
|
|
1079
1287
|
severity: DiagnosticSeverity.Error,
|
|
1080
|
-
message:
|
|
1081
|
-
hint: `Run \`${
|
|
1288
|
+
message: missing ? `there is no committed ${StartupBaselineFile.fileName}, so the startup check has nothing to compare with.` : `${StartupBaselineFile.fileName} is not a version ${StartupBaselineFile.version} startup baseline, so the startup check cannot read it.`,
|
|
1289
|
+
hint: `Run \`${StartupBaseline.updateCommand}\` and commit the file.`
|
|
1082
1290
|
};
|
|
1083
|
-
cliCodes.set(diagnostic,
|
|
1291
|
+
cliCodes.set(diagnostic, missing ? CheckDiagnosticCode.StartupBaselineMissing : CheckDiagnosticCode.StartupBaselineUnreadable);
|
|
1084
1292
|
return {
|
|
1085
1293
|
result: {
|
|
1086
1294
|
action: "compare",
|
|
1087
|
-
|
|
1295
|
+
path: target,
|
|
1088
1296
|
committed: false,
|
|
1089
|
-
reason:
|
|
1297
|
+
reason: missing ? "missing" : "unreadable"
|
|
1090
1298
|
},
|
|
1091
1299
|
diagnostics: [diagnostic],
|
|
1092
1300
|
cliCodes,
|
|
1093
|
-
notes
|
|
1301
|
+
notes: []
|
|
1094
1302
|
};
|
|
1095
1303
|
}
|
|
1096
|
-
const comparison =
|
|
1304
|
+
const comparison = StartupBaselineFile.compare(committed.apps, reports);
|
|
1097
1305
|
const diagnostics = comparison.growth.map((change) => {
|
|
1098
|
-
const isAdded = change.kind === StartupSizeChangeKind.EntryAdded;
|
|
1099
1306
|
const diagnostic = {
|
|
1100
1307
|
severity: DiagnosticSeverity.Error,
|
|
1101
|
-
message:
|
|
1102
|
-
hint:
|
|
1308
|
+
message: StartupBaseline.growthMessage(change),
|
|
1309
|
+
hint: `Make the package wait for its first use (see the reasons), or accept the growth: run \`${StartupBaseline.updateCommand}\` and commit the file.`
|
|
1103
1310
|
};
|
|
1104
|
-
cliCodes.set(diagnostic,
|
|
1311
|
+
cliCodes.set(diagnostic, CheckDiagnosticCode.StartupGrowth);
|
|
1105
1312
|
return diagnostic;
|
|
1106
1313
|
});
|
|
1107
1314
|
return {
|
|
1108
1315
|
result: {
|
|
1109
1316
|
action: "compare",
|
|
1110
|
-
|
|
1317
|
+
path: target,
|
|
1111
1318
|
committed: true,
|
|
1112
1319
|
growth: comparison.growth,
|
|
1113
1320
|
notes: comparison.notes
|
|
1114
1321
|
},
|
|
1115
1322
|
diagnostics,
|
|
1116
1323
|
cliCodes,
|
|
1117
|
-
notes:
|
|
1324
|
+
notes: StartupBaseline.noteLines(comparison.notes)
|
|
1118
1325
|
};
|
|
1119
1326
|
}
|
|
1120
1327
|
/**
|
|
1121
|
-
* The
|
|
1328
|
+
* The committed file, parsed, when it is a startup baseline that this version reads.
|
|
1122
1329
|
*
|
|
1123
|
-
* @param
|
|
1124
|
-
* @param written - Whether the file was written or confirmed.
|
|
1330
|
+
* @param rootDir - The app root that holds the committed file.
|
|
1125
1331
|
*/
|
|
1126
|
-
static
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
action: "update",
|
|
1130
|
-
entries: StartupSizeCheck.entriesOf(measured, void 0),
|
|
1131
|
-
written
|
|
1132
|
-
},
|
|
1133
|
-
notes: StartupSizeCheck.measurementLines(measured)
|
|
1134
|
-
};
|
|
1332
|
+
static committedDocument(rootDir) {
|
|
1333
|
+
const committed = StartupBaseline.readCommitted(join(rootDir, StartupBaselineFile.fileName));
|
|
1334
|
+
return committed?.apps === void 0 ? void 0 : committed.document;
|
|
1135
1335
|
}
|
|
1136
1336
|
/**
|
|
1137
|
-
*
|
|
1337
|
+
* Writes the file for the start reports of a compile when it differs from the committed one. The other
|
|
1338
|
+
* top-level sections of the committed file stay as they are.
|
|
1138
1339
|
*
|
|
1139
|
-
* @param
|
|
1340
|
+
* @param rootDir - The app root that holds the committed file.
|
|
1341
|
+
* @param reports - The start reports of the compile.
|
|
1342
|
+
* @param size - The size of the built server code. Without it, the committed size section stays.
|
|
1343
|
+
* @returns The path, and whether the content changed.
|
|
1140
1344
|
*/
|
|
1141
|
-
static
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
name,
|
|
1152
|
-
entry: measurement.entry,
|
|
1153
|
-
measured: {
|
|
1154
|
-
start: measurement.start,
|
|
1155
|
-
total: measurement.total
|
|
1156
|
-
},
|
|
1157
|
-
...recorded === void 0 ? {} : { baseline: {
|
|
1158
|
-
start: recorded.start,
|
|
1159
|
-
total: recorded.total
|
|
1160
|
-
} },
|
|
1161
|
-
gaps: measurement.gaps,
|
|
1162
|
-
unparsed: measurement.unparsed
|
|
1163
|
-
};
|
|
1164
|
-
});
|
|
1165
|
-
}
|
|
1166
|
-
/** The summary and gap lines of each measured entry. */
|
|
1167
|
-
static measurementLines(measured) {
|
|
1168
|
-
return measured.entries.flatMap(({ measurement }) => [StartupSizeCheck.summaryLine(measurement), ...StartupSizeCheck.gapLines(measurement)]);
|
|
1169
|
-
}
|
|
1170
|
-
/** The line with the measures. */
|
|
1171
|
-
static summaryLine(measurement) {
|
|
1172
|
-
const size = (value) => `${value.files} file${value.files === 1 ? "" : "s"}, ${StartupSizeCheck.formatBytes(value.bytes)}`;
|
|
1173
|
-
return `Server code of ${measurement.entry}: the static start closure has ${size(measurement.start)}; the total server code has ${size(measurement.total)}.`;
|
|
1174
|
-
}
|
|
1175
|
-
/** The lines about requests that the walk could not follow, and files that do not parse. */
|
|
1176
|
-
static gapLines(measurement) {
|
|
1177
|
-
const lines = [];
|
|
1178
|
-
const gaps = measurement.gaps;
|
|
1179
|
-
if (gaps.length > 0) {
|
|
1180
|
-
const named = gaps.slice(0, StartupSizeCheck.namedGapCount).map((gap) => `${gap.importer} → ${gap.request}`).join("; ");
|
|
1181
|
-
lines.push(`The size walk could not follow ${gaps.length} request${gaps.length === 1 ? "" : "s"} (${named}${gaps.length > StartupSizeCheck.namedGapCount ? "; …" : ""}), so the measures can be too small.`);
|
|
1182
|
-
}
|
|
1183
|
-
if (measurement.unparsed.length > 0) lines.push(`The size walk could not parse ${measurement.unparsed.join(", ")}, so the imports of ${measurement.unparsed.length === 1 ? "that file are" : "those files are"} not counted.`);
|
|
1184
|
-
return lines;
|
|
1345
|
+
static update(rootDir, reports, size) {
|
|
1346
|
+
const target = join(rootDir, StartupBaselineFile.fileName);
|
|
1347
|
+
const committed = StartupBaseline.readCommitted(target);
|
|
1348
|
+
const content = StartupBaselineFile.compose(reports, committed?.document, size === void 0 ? {} : { [StartupSizeBudget.sectionKey]: size });
|
|
1349
|
+
const changed = committed?.content !== content;
|
|
1350
|
+
if (changed) writeFileSync(target, content);
|
|
1351
|
+
return {
|
|
1352
|
+
target,
|
|
1353
|
+
changed
|
|
1354
|
+
};
|
|
1185
1355
|
}
|
|
1186
1356
|
/** The error text of one growth. */
|
|
1187
|
-
static growthMessage(change
|
|
1188
|
-
const measure = change.measure ?? StartupSizeMeasure.StartBytes;
|
|
1189
|
-
const format = (value) => typeof value === "number" ? StartupSizeCheck.formatValue(measure, value) : String(value);
|
|
1190
|
-
const files = (change.files ?? []).map((file) => `${file.path} (${file.isNew ? `${StartupSizeCheck.formatBytes(file.bytes)}, new` : `+${StartupSizeCheck.formatBytes(file.growth)}`})`).join(", ");
|
|
1191
|
-
return `${StartupSizeCheck.measureName(measure)} of ${entry} is now ${format(change.current)}, and ${StartupBaselineFile.fileName} records ${format(change.committed)} with a slack of ${format(change.slack ?? 0)}.${files.length > 0 ? ` Largest new or grown files: ${files}.` : ""}`;
|
|
1192
|
-
}
|
|
1193
|
-
/** The note lines of the changes that do not fail. */
|
|
1194
|
-
static noteLines(notes, section) {
|
|
1357
|
+
static growthMessage(change) {
|
|
1195
1358
|
const file = StartupBaselineFile.fileName;
|
|
1359
|
+
switch (change.kind) {
|
|
1360
|
+
case StartupChangeKind.NewApp: return `app '${change.app}' has no record in ${file}, so every package of its start is new.`;
|
|
1361
|
+
case StartupChangeKind.EntryGrowth: return `app '${change.app}': the first use of '${change.place}' now loads '${change.package}', and ${file} does not list it for this entry.`;
|
|
1362
|
+
default: return `app '${change.app}': '${change.package}' now loads at the start, and ${file} does not list it there. Reasons: ${(change.reasons ?? []).join("; ")}.`;
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
/** The note lines: one per change that does not fail, and one line that says the file can be smaller. */
|
|
1366
|
+
static noteLines(notes) {
|
|
1196
1367
|
const lines = notes.map((change) => {
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1368
|
+
switch (change.kind) {
|
|
1369
|
+
case StartupChangeKind.StartShrink: return `app '${change.app}': '${change.package}' no longer loads at the start.`;
|
|
1370
|
+
case StartupChangeKind.EntryShrink: return `app '${change.app}': the first use of '${change.place}' no longer loads '${change.package}'.`;
|
|
1371
|
+
case StartupChangeKind.NewEntry: return `app '${change.app}': '${change.place}' is a new entry.`;
|
|
1372
|
+
case StartupChangeKind.RemovedEntry: return `app '${change.app}': '${change.place}' is no longer an entry.`;
|
|
1373
|
+
case StartupChangeKind.RemovedApp: return `app '${change.app}' is no longer built.`;
|
|
1374
|
+
case StartupChangeKind.TargetChanged: return `app '${change.app}': the deploy target changed whether it defers packages.`;
|
|
1375
|
+
default: return `app '${change.app}': '${change.package}' loads at the start for other reasons: ${(change.reasons ?? []).join("; ")}.`;
|
|
1376
|
+
}
|
|
1201
1377
|
});
|
|
1202
1378
|
if (lines.length === 0) return [];
|
|
1203
|
-
|
|
1379
|
+
const shrinks = notes.some((change) => change.kind === StartupChangeKind.StartShrink || change.kind === StartupChangeKind.EntryShrink);
|
|
1380
|
+
return [...lines, shrinks ? `The app loads less than ${StartupBaselineFile.fileName} records. Run \`${StartupBaseline.updateCommand}\` to make the baseline smaller.` : `Run \`${StartupBaseline.updateCommand}\` to record these changes.`];
|
|
1204
1381
|
}
|
|
1205
|
-
/** The
|
|
1206
|
-
static
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1382
|
+
/** The committed file's text, its parsed form, and its app records, or `undefined` when the file is absent. */
|
|
1383
|
+
static readCommitted(target) {
|
|
1384
|
+
let content;
|
|
1385
|
+
try {
|
|
1386
|
+
content = readFileSync(target, "utf8");
|
|
1387
|
+
} catch {
|
|
1388
|
+
return;
|
|
1211
1389
|
}
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
return
|
|
1390
|
+
let document;
|
|
1391
|
+
try {
|
|
1392
|
+
document = JSON.parse(content);
|
|
1393
|
+
} catch {
|
|
1394
|
+
document = void 0;
|
|
1395
|
+
}
|
|
1396
|
+
return {
|
|
1397
|
+
content,
|
|
1398
|
+
document,
|
|
1399
|
+
apps: StartupBaselineFile.parse(document)
|
|
1400
|
+
};
|
|
1219
1401
|
}
|
|
1220
1402
|
};
|
|
1221
1403
|
//#endregion
|
|
1222
|
-
//#region src/startup-
|
|
1404
|
+
//#region src/startup-built-code.ts
|
|
1223
1405
|
/**
|
|
1224
|
-
* The
|
|
1225
|
-
* that
|
|
1226
|
-
*
|
|
1227
|
-
*
|
|
1406
|
+
* The built server code of one entry, as the size walk read it, with the packages of each file. It finds the
|
|
1407
|
+
* files that hold the code of a package, and the files that some files load when they run.
|
|
1408
|
+
*
|
|
1409
|
+
* The code of a package starts at the chunk of its generated loader, when the start report names the loader and
|
|
1410
|
+
* the build record names the chunk. So two roots of one npm package each get their own files. Otherwise a file
|
|
1411
|
+
* holds the code of a package when the build record gives the package bytes in the file, or when the file is in
|
|
1412
|
+
* the `node_modules` directory of the package in the output, without the files that only the other roots of the
|
|
1413
|
+
* package reach. From these files, the code of a package is every file that they import, with a static import or
|
|
1414
|
+
* with `import()`, up to the loader chunk of another package.
|
|
1228
1415
|
*/
|
|
1229
|
-
var
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
/**
|
|
1233
|
-
static
|
|
1234
|
-
/**
|
|
1235
|
-
|
|
1236
|
-
/**
|
|
1237
|
-
|
|
1416
|
+
var StartupBuiltCode = class StartupBuiltCode {
|
|
1417
|
+
measured;
|
|
1418
|
+
chunks;
|
|
1419
|
+
/** The directory of the installed packages in a path of the output. */
|
|
1420
|
+
static packageDirectory = "node_modules/";
|
|
1421
|
+
/** The bytes of each file that the walk reached, by path in the output directory. */
|
|
1422
|
+
bytesByFile;
|
|
1423
|
+
/** The files that hold the code of each package, by package name. */
|
|
1424
|
+
filesByPackage;
|
|
1425
|
+
/** The chunks that hold the loader of each package with a known loader, by package identity. */
|
|
1426
|
+
loaderChunks;
|
|
1427
|
+
/** The files that the first load of each package with a known loader reaches, by package identity. */
|
|
1428
|
+
loaderClosures = /* @__PURE__ */ new Map();
|
|
1429
|
+
/** The static start closure of the walk. */
|
|
1430
|
+
staticStart;
|
|
1238
1431
|
/**
|
|
1239
|
-
* The
|
|
1432
|
+
* @param measured - The measurement of the entry, with the imports of each file.
|
|
1433
|
+
* @param chunks - The packages of each chunk of the build, from the build record.
|
|
1434
|
+
* @param generated - The generated wiring files of each chunk of the build, from the build record.
|
|
1435
|
+
* @param loaders - The generated loader file of each package, by identity, from the start report.
|
|
1436
|
+
*/
|
|
1437
|
+
constructor(measured, chunks, generated = {}, loaders = {}) {
|
|
1438
|
+
this.measured = measured;
|
|
1439
|
+
this.chunks = chunks;
|
|
1440
|
+
const filesByPackage = /* @__PURE__ */ new Map();
|
|
1441
|
+
this.bytesByFile = new Map(measured.measurement.files.map((file) => [file.path, file.bytes]));
|
|
1442
|
+
for (const file of measured.measurement.files) {
|
|
1443
|
+
const names = new Set(Object.keys(this.packagesOfFile(file.path)));
|
|
1444
|
+
for (const name of names) filesByPackage.set(name, [...filesByPackage.get(name) ?? [], file.path]);
|
|
1445
|
+
}
|
|
1446
|
+
this.filesByPackage = filesByPackage;
|
|
1447
|
+
this.loaderChunks = this.loaderChunksOf(generated, loaders);
|
|
1448
|
+
this.staticStart = this.reach(this.staticStartFiles(), /* @__PURE__ */ new Set(), void 0);
|
|
1449
|
+
}
|
|
1450
|
+
/**
|
|
1451
|
+
* The package name of a package identity: `@acme/orders/admin#OrdersModule` gives `@acme/orders`.
|
|
1240
1452
|
*
|
|
1241
|
-
* @param
|
|
1242
|
-
* @param measuring - The measurement of the build output by the size check.
|
|
1243
|
-
* @param top - The number of module sets and async registries to show for each app. All when absent.
|
|
1453
|
+
* @param identity - The identity, as `<specifier>#<module class>`.
|
|
1244
1454
|
*/
|
|
1245
|
-
static
|
|
1246
|
-
const
|
|
1247
|
-
|
|
1248
|
-
const chunks = serverEntry === void 0 ? void 0 : measured?.chunks;
|
|
1249
|
-
const bytesUnavailable = measuring.kind === "skipped" ? measuring.result.reason : chunks === void 0 ? "no-chunk-record" : void 0;
|
|
1250
|
-
const apps = reports.map((report) => StartupEntriesReport.appReport(report, serverEntry === void 0 || chunks === void 0 ? void 0 : new StartupBuiltCode(serverEntry, chunks, measured?.generated, report.loads?.loaders), top));
|
|
1251
|
-
const notice = measuring.kind === "skipped" ? `${measuring.message.replace(" The size check of the server code is skipped.", "")} The entries report has no bytes.` : chunks === void 0 ? `The build record ${ServerBuildRecord.relativePath} does not have the packages of its chunks, so the entries report has no bytes. Build the app again with this version of Heximon.` : void 0;
|
|
1252
|
-
return {
|
|
1253
|
-
result: {
|
|
1254
|
-
...serverEntry === void 0 ? {} : { serverEntry: serverEntry.measurement.entry },
|
|
1255
|
-
...bytesUnavailable === void 0 ? {} : { bytesUnavailable },
|
|
1256
|
-
apps
|
|
1257
|
-
},
|
|
1258
|
-
lines: [...notice === void 0 ? [] : [notice], ...apps.map((app) => StartupEntriesReport.appText(app, serverEntry?.measurement.entry))]
|
|
1259
|
-
};
|
|
1455
|
+
static packageNameOf(identity) {
|
|
1456
|
+
const specifier = identity.slice(0, identity.lastIndexOf("#") === -1 ? void 0 : identity.lastIndexOf("#"));
|
|
1457
|
+
return specifier.split("/").slice(0, specifier.startsWith("@") ? 2 : 1).join("/");
|
|
1260
1458
|
}
|
|
1261
|
-
/** The
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
const groupsByKey = /* @__PURE__ */ new Map();
|
|
1265
|
-
for (const entry of report.entries) {
|
|
1266
|
-
const key = entry.packages.join("\n");
|
|
1267
|
-
const group = groupsByKey.get(key) ?? {
|
|
1268
|
-
packages: entry.packages,
|
|
1269
|
-
places: []
|
|
1270
|
-
};
|
|
1271
|
-
group.places.push(StartupEntriesReport.placeOf(entry.kind, entry.key));
|
|
1272
|
-
groupsByKey.set(key, group);
|
|
1273
|
-
}
|
|
1274
|
-
const startLoaded = code?.startFiles(report.start.map((entry) => entry.package));
|
|
1275
|
-
const asyncRegistries = report.loads?.asyncRegistries ?? [];
|
|
1276
|
-
const sortedGroups = [...groupsByKey.values()].map((group) => StartupEntriesReport.groupReport(group, new StartupEntryReasons(report), reasonsByPlace.get(group.places[0]) ?? /* @__PURE__ */ new Map(), asyncRegistries, code, startLoaded)).sort((first, second) => (second.added?.bytes ?? 0) - (first.added?.bytes ?? 0) || second.entries.length - first.entries.length || StartupEntriesReport.compareText(first.example, second.example));
|
|
1277
|
-
const registries = asyncRegistries.map((registry) => StartupEntriesReport.asyncRegistryReport(registry, code, startLoaded)).sort((first, second) => (second.added?.bytes ?? 0) - (first.added?.bytes ?? 0) || StartupEntriesReport.compareText(first.owner, second.owner) || StartupEntriesReport.compareText(first.registry, second.registry));
|
|
1278
|
-
return {
|
|
1279
|
-
app: report.app,
|
|
1280
|
-
defersPackages: report.defersPackages,
|
|
1281
|
-
entries: report.entries.length,
|
|
1282
|
-
groupCount: sortedGroups.length,
|
|
1283
|
-
...code === void 0 || startLoaded === void 0 ? {} : {
|
|
1284
|
-
start: code.sizeOf(startLoaded),
|
|
1285
|
-
...StartupEntriesReport.byPackageNameOf(code, report.start.map((entry) => entry.package), "startByPackageName"),
|
|
1286
|
-
byPackageNameOnly: !code.hasLoaders()
|
|
1287
|
-
},
|
|
1288
|
-
groups: top === void 0 ? sortedGroups : sortedGroups.slice(0, top),
|
|
1289
|
-
summary: StartupEntriesReport.summaryOf(report, sortedGroups, code, startLoaded),
|
|
1290
|
-
asyncRegistryCount: registries.length,
|
|
1291
|
-
asyncRegistries: top === void 0 ? registries : registries.slice(0, top)
|
|
1292
|
-
};
|
|
1459
|
+
/** The files that the entry loads when it runs: the static start closure of the walk. */
|
|
1460
|
+
staticStartFiles() {
|
|
1461
|
+
return this.measured.measurement.files.filter((file) => file.isStart).map((file) => file.path);
|
|
1293
1462
|
}
|
|
1294
|
-
/**
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
const identities = identitiesByName.get(name);
|
|
1306
|
-
if (identities === void 0) return {
|
|
1307
|
-
module: name,
|
|
1308
|
-
bytes,
|
|
1309
|
-
reasons: [StartupEntryReasons.imported()]
|
|
1310
|
-
};
|
|
1311
|
-
const described = [...new Map(identities.flatMap((identity) => reasons.get(identity) ?? []).map((reason) => reasonNames.describe(reason)).map((named) => [named.text, named])).values()];
|
|
1312
|
-
return {
|
|
1313
|
-
module: identities.join(", "),
|
|
1314
|
-
bytes,
|
|
1315
|
-
reasons: described.length === 0 ? [StartupEntryReasons.notRecorded()] : described
|
|
1316
|
-
};
|
|
1317
|
-
}).sort((first, second) => second.bytes - first.bytes || StartupEntriesReport.compareText(first.module, second.module)).slice(0, StartupEntriesReport.namedModuleCount);
|
|
1318
|
-
const sealPackages = /* @__PURE__ */ new Map();
|
|
1319
|
-
for (const identity of group.packages) for (const reason of reasons.get(identity) ?? []) if (reason.kind === StartReasonKind.SealGroup && reason.source !== void 0) sealPackages.set(reason.source, [...sealPackages.get(reason.source) ?? [], identity]);
|
|
1320
|
-
const sealGroups = [...sealPackages].sort(([first], [second]) => StartupEntriesReport.compareText(first, second)).map(([registry, packages]) => ({
|
|
1321
|
-
registry,
|
|
1322
|
-
packages,
|
|
1323
|
-
...addedFiles === void 0 ? {} : { bytes: [...new Set(packages.map((identity) => StartupBuiltCode.packageNameOf(identity)))].reduce((sum, name) => sum + (bytesByPackage.get(name) ?? 0), 0) }
|
|
1324
|
-
}));
|
|
1325
|
-
const loadedWithEntry = startLoaded === void 0 || addedFiles === void 0 ? void 0 : /* @__PURE__ */ new Set([...startLoaded, ...addedFiles]);
|
|
1326
|
-
return {
|
|
1327
|
-
packages: group.packages,
|
|
1328
|
-
entries: places,
|
|
1329
|
-
example: places[0],
|
|
1330
|
-
...code === void 0 || addedFiles === void 0 ? {} : { added: code.sizeOf(addedFiles) },
|
|
1331
|
-
modules,
|
|
1332
|
-
sealGroups,
|
|
1333
|
-
onRead: asyncRegistries.filter((registry) => group.packages.includes(registry.owner)).map((registry) => ({
|
|
1334
|
-
registry: registry.registry,
|
|
1335
|
-
owner: registry.owner,
|
|
1336
|
-
...code === void 0 || loadedWithEntry === void 0 ? {} : { added: code.sizeOf(code.codeOf(registry.packages.map((loaded) => loaded.package), loadedWithEntry)) }
|
|
1337
|
-
})),
|
|
1338
|
-
...code === void 0 ? {} : StartupEntriesReport.byPackageNameOf(code, group.packages, "byPackageName")
|
|
1339
|
-
};
|
|
1463
|
+
/**
|
|
1464
|
+
* The files that the first load of some packages reaches, without the files that are loaded already: from the
|
|
1465
|
+
* chunk of each loader, or the files of the npm package ({@link byPackageName}), every file that they import
|
|
1466
|
+
* with a static import or with `import()`. The walk does not go into the loader chunk of another package
|
|
1467
|
+
* through `import()`, as that chunk loads on the first use of that package.
|
|
1468
|
+
*
|
|
1469
|
+
* @param identities - The packages, by identity.
|
|
1470
|
+
* @param loaded - The files that are loaded already. The walk does not pass through them.
|
|
1471
|
+
*/
|
|
1472
|
+
codeOf(identities, loaded = /* @__PURE__ */ new Set()) {
|
|
1473
|
+
return this.reach(this.seedsOf(identities), loaded, this.loaderChunksOutside(new Set(identities)));
|
|
1340
1474
|
}
|
|
1341
|
-
/**
|
|
1342
|
-
static
|
|
1343
|
-
|
|
1344
|
-
|
|
1475
|
+
/**
|
|
1476
|
+
* The files that the app loads before it is ready: the static start closure, and the code of the start
|
|
1477
|
+
* packages ({@link codeOf}), also where it passes through the static start.
|
|
1478
|
+
*
|
|
1479
|
+
* @param identities - The start packages, by identity.
|
|
1480
|
+
*/
|
|
1481
|
+
startFiles(identities) {
|
|
1482
|
+
return /* @__PURE__ */ new Set([...this.staticStart, ...this.codeOf(identities)]);
|
|
1483
|
+
}
|
|
1484
|
+
/** Whether the code finds the files of some package by its loader chunk. */
|
|
1485
|
+
hasLoaders() {
|
|
1486
|
+
return this.loaderChunks.size > 0;
|
|
1345
1487
|
}
|
|
1346
1488
|
/**
|
|
1347
|
-
* The
|
|
1348
|
-
*
|
|
1489
|
+
* The packages whose files {@link codeOf} finds by npm package name, because the build record names no
|
|
1490
|
+
* chunk of a loader of theirs. Their bytes are less exact.
|
|
1491
|
+
*
|
|
1492
|
+
* @param identities - The packages, by identity.
|
|
1349
1493
|
*/
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
let added;
|
|
1353
|
-
if (code !== void 0 && startLoaded !== void 0) {
|
|
1354
|
-
const loaded = /* @__PURE__ */ new Set([...startLoaded, ...code.codeOf([registry.owner, ...registry.ownerPackages], startLoaded)]);
|
|
1355
|
-
added = code.sizeOf(code.codeOf(packages, loaded));
|
|
1356
|
-
}
|
|
1357
|
-
return {
|
|
1358
|
-
registry: registry.registry,
|
|
1359
|
-
owner: registry.owner,
|
|
1360
|
-
inputKey: registry.inputKey,
|
|
1361
|
-
contributors: registry.contributors,
|
|
1362
|
-
packages,
|
|
1363
|
-
...added === void 0 ? {} : { added },
|
|
1364
|
-
...code === void 0 ? {} : StartupEntriesReport.byPackageNameOf(code, packages, "byPackageName")
|
|
1365
|
-
};
|
|
1494
|
+
byPackageName(identities) {
|
|
1495
|
+
return identities.filter((identity) => !this.loaderChunks.has(identity));
|
|
1366
1496
|
}
|
|
1367
|
-
/**
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
entries: sets.length,
|
|
1376
|
-
packages: shared,
|
|
1377
|
-
...code === void 0 || startLoaded === void 0 ? {} : { added: code.sizeOf(code.codeOf(shared, startLoaded)) }
|
|
1378
|
-
}];
|
|
1379
|
-
});
|
|
1380
|
-
if (code === void 0 || report.entries.length === 0) return { everyEntry };
|
|
1381
|
-
const entryBytes = groups.flatMap((group) => group.entries.map(() => group.added?.bytes ?? 0)).sort((first, second) => first - second);
|
|
1382
|
-
const p75 = StartupEntriesReport.percentile(entryBytes, .75);
|
|
1383
|
-
const heavyGroups = groups.filter((group) => {
|
|
1384
|
-
const bytes = group.added?.bytes ?? 0;
|
|
1385
|
-
return bytes > 0 && bytes >= p75;
|
|
1386
|
-
});
|
|
1387
|
-
const heavyCounts = /* @__PURE__ */ new Map();
|
|
1388
|
-
for (const group of heavyGroups) for (const identity of group.packages) heavyCounts.set(identity, (heavyCounts.get(identity) ?? 0) + group.entries.length);
|
|
1497
|
+
/**
|
|
1498
|
+
* How many files a set has, and their bytes.
|
|
1499
|
+
*
|
|
1500
|
+
* @param files - The files.
|
|
1501
|
+
*/
|
|
1502
|
+
sizeOf(files) {
|
|
1503
|
+
let bytes = 0;
|
|
1504
|
+
for (const file of files) bytes += this.bytesByFile.get(file) ?? 0;
|
|
1389
1505
|
return {
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
p75,
|
|
1393
|
-
max: entryBytes.at(-1) ?? 0
|
|
1394
|
-
},
|
|
1395
|
-
everyEntry,
|
|
1396
|
-
heavy: {
|
|
1397
|
-
entries: heavyGroups.reduce((sum, group) => sum + group.entries.length, 0),
|
|
1398
|
-
modules: [...heavyCounts].sort(([firstModule, firstCount], [secondModule, secondCount]) => secondCount - firstCount || StartupEntriesReport.compareText(firstModule, secondModule)).slice(0, StartupEntriesReport.namedHeavyModuleCount).map(([module, entries]) => ({
|
|
1399
|
-
module,
|
|
1400
|
-
entries
|
|
1401
|
-
}))
|
|
1402
|
-
}
|
|
1506
|
+
files: files.size,
|
|
1507
|
+
bytes
|
|
1403
1508
|
};
|
|
1404
1509
|
}
|
|
1405
|
-
/**
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
}
|
|
1418
|
-
const { summary } = app;
|
|
1419
|
-
if (summary.bytes !== void 0) lines.push(`Per entry: median ${StartupSizeCheck.formatBytes(summary.bytes.median)}, p75 ${StartupSizeCheck.formatBytes(summary.bytes.p75)}, max ${StartupSizeCheck.formatBytes(summary.bytes.max)}.`);
|
|
1420
|
-
for (const every of summary.everyEntry) lines.push(`Every '${every.kind}' entry (${every.entries}) loads ${StartupEntriesReport.count(every.packages.length, "package", "packages")}` + (every.added === void 0 ? "" : ` (${StartupSizeCheck.formatBytes(every.added.bytes)})`) + `: ${StartupEntriesReport.listText(every.packages)}.`);
|
|
1421
|
-
if (summary.heavy !== void 0 && summary.heavy.modules.length > 0) lines.push(`The ${StartupEntriesReport.count(summary.heavy.entries, "heavy entry", "heavy entries")} (p75 or more) load these packages most: ${summary.heavy.modules.map((module) => `${module.module} (${module.entries})`).join(", ")}.`);
|
|
1422
|
-
return lines.join("\n") + StartupEntriesReport.asyncRegistriesText(app);
|
|
1423
|
-
}
|
|
1424
|
-
/** The lines of the async registries of one app, with a newline before them, or nothing. */
|
|
1425
|
-
static asyncRegistriesText(app) {
|
|
1426
|
-
if (app.asyncRegistryCount === 0) return "";
|
|
1427
|
-
return `\n${[`Async registries: the first load() call of each one loads its contributors. No entry counts this code${app.asyncRegistries.length < app.asyncRegistryCount ? ` (the ${app.asyncRegistries.length} largest of ${app.asyncRegistryCount}, --top)` : ""}.`, ...app.asyncRegistries.map((registry) => `- '${registry.registry}' of ${registry.owner}: ${StartupEntriesReport.count(registry.contributors.length, "contributor", "contributors")} (${StartupEntriesReport.listText(registry.contributors, StartupEntriesReport.namedContributorCount)}), ${StartupEntriesReport.count(registry.packages.length, "package", "packages")} beyond the start` + (registry.added === void 0 ? "." : `, ${StartupEntriesReport.sizeText(registry.added)}.`))].join("\n")}`;
|
|
1428
|
-
}
|
|
1429
|
-
/** Files and bytes for a person to read. */
|
|
1430
|
-
static sizeText(size) {
|
|
1431
|
-
return `${StartupEntriesReport.count(size.files, "file", "files")}, ${StartupSizeCheck.formatBytes(size.bytes)}`;
|
|
1510
|
+
/**
|
|
1511
|
+
* About how many bytes of some files belong to each package. A chunk gives each of its packages a part of its
|
|
1512
|
+
* bytes, in the ratio of their rendered code. A file of a package's `node_modules` directory belongs to the
|
|
1513
|
+
* package. Other bytes, such as a runtime helper of the bundler, belong to no package.
|
|
1514
|
+
*
|
|
1515
|
+
* @param files - The files.
|
|
1516
|
+
* @returns The bytes of each package, by package name.
|
|
1517
|
+
*/
|
|
1518
|
+
packageBytes(files) {
|
|
1519
|
+
const bytesByPackage = /* @__PURE__ */ new Map();
|
|
1520
|
+
for (const file of files) for (const [name, bytes] of Object.entries(this.packagesOfFile(file))) bytesByPackage.set(name, (bytesByPackage.get(name) ?? 0) + bytes);
|
|
1521
|
+
return bytesByPackage;
|
|
1432
1522
|
}
|
|
1433
|
-
/**
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1523
|
+
/**
|
|
1524
|
+
* Some files and every file that they import, without the files that are loaded already. With `boundary`, the
|
|
1525
|
+
* walk follows `import()` too, but not into a file of `boundary`.
|
|
1526
|
+
*/
|
|
1527
|
+
reach(seeds, loaded, boundary) {
|
|
1528
|
+
const reached = /* @__PURE__ */ new Set();
|
|
1529
|
+
const pending = seeds.filter((file) => !loaded.has(file));
|
|
1530
|
+
for (let file = pending.pop(); file !== void 0; file = pending.pop()) {
|
|
1531
|
+
if (reached.has(file)) continue;
|
|
1532
|
+
reached.add(file);
|
|
1533
|
+
const targets = [...StartupBuiltCode.importsOf(this.measured.staticImports, file), ...boundary === void 0 ? [] : StartupBuiltCode.importsOf(this.measured.dynamicImports, file).filter((target) => !boundary.has(target))];
|
|
1534
|
+
for (const target of targets) if (!loaded.has(target) && !reached.has(target)) pending.push(target);
|
|
1535
|
+
}
|
|
1536
|
+
return reached;
|
|
1438
1537
|
}
|
|
1439
|
-
/**
|
|
1440
|
-
|
|
1441
|
-
return
|
|
1538
|
+
/** The chunk of the loader of each package, or the files of its npm package. */
|
|
1539
|
+
seedsOf(identities) {
|
|
1540
|
+
return [...new Set(identities.flatMap((identity) => this.loaderChunks.get(identity) ?? this.packageNameFilesOf(identity)))];
|
|
1442
1541
|
}
|
|
1443
|
-
/**
|
|
1444
|
-
|
|
1445
|
-
return
|
|
1542
|
+
/** The loader chunks of the packages that are not in `identities`. */
|
|
1543
|
+
loaderChunksOutside(identities) {
|
|
1544
|
+
return new Set([...this.loaderChunks].filter(([identity]) => !identities.has(identity)).flatMap(([, chunks]) => chunks));
|
|
1446
1545
|
}
|
|
1447
|
-
/**
|
|
1448
|
-
|
|
1449
|
-
|
|
1546
|
+
/**
|
|
1547
|
+
* The files of the npm package of a root, without the files that only the other roots of the package reach
|
|
1548
|
+
* beyond the static start: those load on the first use of the other roots. A file that the root reaches from
|
|
1549
|
+
* its own files stays, as the walk cannot show that only the other roots load it.
|
|
1550
|
+
*/
|
|
1551
|
+
packageNameFilesOf(identity) {
|
|
1552
|
+
const name = StartupBuiltCode.packageNameOf(identity);
|
|
1553
|
+
const files = this.filesByPackage.get(name) ?? [];
|
|
1554
|
+
const others = [...this.loaderChunks].filter(([other]) => other !== identity && StartupBuiltCode.packageNameOf(other) === name);
|
|
1555
|
+
if (others.length === 0) return files;
|
|
1556
|
+
const otherFiles = new Set(others.flatMap(([other, chunks]) => {
|
|
1557
|
+
const closure = this.loaderClosures.get(other) ?? this.reach(chunks, this.staticStart, this.loaderChunksOutside(/* @__PURE__ */ new Set([other])));
|
|
1558
|
+
this.loaderClosures.set(other, closure);
|
|
1559
|
+
return [...closure];
|
|
1560
|
+
}));
|
|
1561
|
+
const ownFiles = this.reach(files.filter((file) => !otherFiles.has(file)), /* @__PURE__ */ new Set(), this.loaderChunksOutside(/* @__PURE__ */ new Set([identity])));
|
|
1562
|
+
return files.filter((file) => !otherFiles.has(file) || ownFiles.has(file));
|
|
1450
1563
|
}
|
|
1451
|
-
|
|
1452
|
-
|
|
1564
|
+
/** The targets of one file in an import table of the walk. */
|
|
1565
|
+
static importsOf(imports, file) {
|
|
1566
|
+
return Object.hasOwn(imports, file) ? imports[file] ?? [] : [];
|
|
1453
1567
|
}
|
|
1454
|
-
};
|
|
1455
|
-
//#endregion
|
|
1456
|
-
//#region src/project-modules.ts
|
|
1457
|
-
/**
|
|
1458
|
-
* Imports packages the way the generated wiring imports them: through a small module written into the
|
|
1459
|
-
* compile output directory, which sits in the project. The specifiers inside it resolve from the project, so
|
|
1460
|
-
* the command gets the copy of a package that the wiring gets. A package the command imported from its own
|
|
1461
|
-
* install would be a second set of class objects, and `app.from(CoreModule)` would not know them.
|
|
1462
|
-
*/
|
|
1463
|
-
var ProjectModules = class ProjectModules {
|
|
1464
1568
|
/**
|
|
1465
|
-
*
|
|
1466
|
-
*
|
|
1467
|
-
* @param directory - A directory inside the project, with write access (the compile output directory).
|
|
1468
|
-
* @param fileName - The name of the module to write, unique among the calls for one directory.
|
|
1469
|
-
* @param source - The module source: `export … from "<specifier>"` lines.
|
|
1470
|
-
* @returns The namespace of the written module.
|
|
1569
|
+
* The chunks that hold the loader file of each package with a loader. A loader whose chunk the walk did not
|
|
1570
|
+
* reach, or is in the static start closure, or holds the loader of another package, gives no chunks.
|
|
1471
1571
|
*/
|
|
1472
|
-
|
|
1473
|
-
const
|
|
1474
|
-
|
|
1475
|
-
|
|
1572
|
+
loaderChunksOf(generated, loaders) {
|
|
1573
|
+
const loaderFiles = new Set(Object.values(loaders));
|
|
1574
|
+
const startFiles = new Set(this.staticStartFiles());
|
|
1575
|
+
const chunksByLoader = /* @__PURE__ */ new Map();
|
|
1576
|
+
for (const [chunk, files] of Object.entries(generated)) for (const file of files) if (loaderFiles.has(file)) chunksByLoader.set(file, [...chunksByLoader.get(file) ?? [], chunk]);
|
|
1577
|
+
const chunks = /* @__PURE__ */ new Map();
|
|
1578
|
+
for (const [identity, loader] of Object.entries(loaders)) {
|
|
1579
|
+
const holders = chunksByLoader.get(loader) ?? [];
|
|
1580
|
+
const isOwnChunk = holders.every((chunk) => this.bytesByFile.has(chunk) && !startFiles.has(chunk) && (Object.hasOwn(generated, chunk) ? generated[chunk] ?? [] : []).every((file) => file === loader || !loaderFiles.has(file)));
|
|
1581
|
+
if (holders.length > 0 && isOwnChunk) chunks.set(identity, holders);
|
|
1582
|
+
}
|
|
1583
|
+
return chunks;
|
|
1476
1584
|
}
|
|
1477
|
-
/** The
|
|
1478
|
-
|
|
1479
|
-
|
|
1585
|
+
/** The bytes of one file that belong to each package, by package name. */
|
|
1586
|
+
packagesOfFile(file) {
|
|
1587
|
+
const fileBytes = this.bytesByFile.get(file) ?? 0;
|
|
1588
|
+
const rendered = Object.hasOwn(this.chunks, file) ? this.chunks[file] : void 0;
|
|
1589
|
+
if (rendered !== void 0) {
|
|
1590
|
+
const total = Object.values(rendered).reduce((sum, bytes) => sum + bytes, 0);
|
|
1591
|
+
return total === 0 ? {} : Object.fromEntries(Object.entries(rendered).map(([name, bytes]) => [name, Math.round(fileBytes * bytes / total)]));
|
|
1592
|
+
}
|
|
1593
|
+
const name = StartupBuiltCode.installedPackageOf(file);
|
|
1594
|
+
return name === void 0 ? {} : { [name]: fileBytes };
|
|
1595
|
+
}
|
|
1596
|
+
/** The package of a file in a `node_modules` directory of the output, or `undefined`. */
|
|
1597
|
+
static installedPackageOf(file) {
|
|
1598
|
+
const index = file.lastIndexOf(StartupBuiltCode.packageDirectory);
|
|
1599
|
+
if (index === -1 || index > 0 && file[index - 1] !== "/") return;
|
|
1600
|
+
const segments = file.slice(index + StartupBuiltCode.packageDirectory.length).split("/");
|
|
1601
|
+
const length = segments[0]?.startsWith("@") === true ? 2 : 1;
|
|
1602
|
+
return segments.length > length ? segments.slice(0, length).join("/") : void 0;
|
|
1480
1603
|
}
|
|
1481
1604
|
};
|
|
1482
1605
|
//#endregion
|
|
1483
|
-
//#region src/
|
|
1606
|
+
//#region src/startup-entry-reasons.ts
|
|
1484
1607
|
/**
|
|
1485
|
-
* The
|
|
1486
|
-
*
|
|
1487
|
-
* sidecar alone would build another graph. `@heximon/nitro` of the project makes the inputs from the preset
|
|
1488
|
-
* name. Both packages load from the project, so the plugins, the strategy and the compiler are one copy. No
|
|
1489
|
-
* Nitro or Vite config runs.
|
|
1608
|
+
* The reasons of the entries report, named by the kind that the compile records. A reason that the compile
|
|
1609
|
+
* does not record is "reason not recorded": the report does not guess a kind for it.
|
|
1490
1610
|
*/
|
|
1491
|
-
var
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
/**
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
const modules = await ProjectModules.import(directory, "project-nitro.js", "export { resolveNitroPresetInputs } from \"@heximon/nitro/module\";\n");
|
|
1509
|
-
return new NitroHost(await modules.resolveNitroPresetInputs({
|
|
1510
|
-
rootDirectory: rootDir,
|
|
1511
|
-
preset: request.preset,
|
|
1512
|
-
aliases: request.aliases.prefixes,
|
|
1513
|
-
...request.serverDir === void 0 ? {} : { serverDir: request.serverDir },
|
|
1514
|
-
...request.scanDirs === void 0 ? {} : { scanDirs: request.scanDirs },
|
|
1515
|
-
...request.platformFromSidecar === true ? { platformFromSidecar: true } : {}
|
|
1516
|
-
}), request.aliases);
|
|
1611
|
+
var StartupEntryReasons = class StartupEntryReasons {
|
|
1612
|
+
/** The kind of code that a loaded file imports, and that is not a composed package. */
|
|
1613
|
+
static importKind = "import";
|
|
1614
|
+
/** The kind of a module whose reason the compile does not record. */
|
|
1615
|
+
static notRecordedKind = "not-recorded";
|
|
1616
|
+
/** The entries of the report, as `<kind> <key>`. */
|
|
1617
|
+
places;
|
|
1618
|
+
/** The identities of the composed packages of the report. */
|
|
1619
|
+
packages;
|
|
1620
|
+
/** @param report - The start report of the app. */
|
|
1621
|
+
constructor(report) {
|
|
1622
|
+
this.places = new Set(report.entries.map((entry) => `${entry.kind} ${entry.key}`));
|
|
1623
|
+
this.packages = /* @__PURE__ */ new Set([
|
|
1624
|
+
...report.start.map((entry) => entry.package),
|
|
1625
|
+
...report.entries.flatMap((entry) => entry.packages),
|
|
1626
|
+
...(report.loads?.asyncRegistries ?? []).flatMap((registry) => [registry.owner, ...registry.packages.map((entry) => entry.package)])
|
|
1627
|
+
]);
|
|
1517
1628
|
}
|
|
1518
|
-
/**
|
|
1519
|
-
|
|
1520
|
-
return
|
|
1629
|
+
/** The reason of code that a loaded file imports, and that is not a composed package. */
|
|
1630
|
+
static imported() {
|
|
1631
|
+
return {
|
|
1632
|
+
kind: StartupEntryReasons.importKind,
|
|
1633
|
+
text: "import of a loaded file, not a composed package"
|
|
1634
|
+
};
|
|
1521
1635
|
}
|
|
1522
|
-
/** The
|
|
1523
|
-
|
|
1524
|
-
return
|
|
1636
|
+
/** The reason of a module whose reason the compile does not record. */
|
|
1637
|
+
static notRecorded() {
|
|
1638
|
+
return {
|
|
1639
|
+
kind: StartupEntryReasons.notRecordedKind,
|
|
1640
|
+
text: "reason not recorded"
|
|
1641
|
+
};
|
|
1525
1642
|
}
|
|
1526
1643
|
/**
|
|
1527
|
-
*
|
|
1644
|
+
* One reason of the start report, with its text.
|
|
1528
1645
|
*
|
|
1529
|
-
* @param
|
|
1530
|
-
* @param outputDirectory - Where the generated files will be written.
|
|
1531
|
-
* @param sourceDirectories - The source directories to scan instead of the host's, when the caller has some.
|
|
1646
|
+
* @param reason - The reason, as the compile records it.
|
|
1532
1647
|
*/
|
|
1533
|
-
|
|
1534
|
-
const context = this.context;
|
|
1535
|
-
const config = context.config;
|
|
1536
|
-
const compiler = new ProjectCompiler();
|
|
1648
|
+
describe(reason) {
|
|
1537
1649
|
return {
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
aliases: this.aliases.prefixes,
|
|
1543
|
-
exactAliases: this.aliases.exact,
|
|
1544
|
-
plugins: context.plugins,
|
|
1545
|
-
strategy: context.strategy,
|
|
1546
|
-
...context.wiringMode === void 0 ? {} : { wiringMode: context.wiringMode },
|
|
1547
|
-
...config === void 0 ? {} : { config },
|
|
1548
|
-
...context.runtimeConfig === void 0 ? {} : { runtimeConfig: context.runtimeConfig },
|
|
1549
|
-
...config?.root === void 0 ? {} : { apps: { main: config.root } },
|
|
1550
|
-
...context.workspaceSubscribers === void 0 ? {} : { workspaceSubscribers: context.workspaceSubscribers }
|
|
1551
|
-
}),
|
|
1552
|
-
compiler,
|
|
1553
|
-
platformName: context.strategy.platformName
|
|
1650
|
+
kind: reason.kind,
|
|
1651
|
+
...reason.source === void 0 ? {} : { source: reason.source },
|
|
1652
|
+
...reason.tokens === void 0 ? {} : { tokens: reason.tokens },
|
|
1653
|
+
text: this.textOf(reason)
|
|
1554
1654
|
};
|
|
1555
1655
|
}
|
|
1656
|
+
/** The text of one reason. */
|
|
1657
|
+
textOf(reason) {
|
|
1658
|
+
const { source } = reason;
|
|
1659
|
+
switch (reason.kind) {
|
|
1660
|
+
case StartReasonKind.Dependency: return this.dependencyText(reason);
|
|
1661
|
+
case StartReasonKind.SealGroup: return `seal group of the sync registry ${source ?? "(registry not recorded)"}`;
|
|
1662
|
+
case StartReasonKind.ContributesTo: return `contributor to the registry ${source ?? "(registry not recorded)"}`;
|
|
1663
|
+
case StartReasonKind.ContributionCannotWait: return `contribution section that cannot wait${source === void 0 ? "" : `: ${source}`}`;
|
|
1664
|
+
case StartReasonKind.RegistrarOptions: return `registrar options of the plugin ${source ?? "(plugin not recorded)"}`;
|
|
1665
|
+
case StartReasonKind.PluginNeedsAtStart: return `a compiler plugin needs it at the start${source === void 0 ? "" : `: ${source}`}`;
|
|
1666
|
+
case StartReasonKind.LazyRead: return `Lazy<T> read${source === void 0 ? "" : `: ${source}`}`;
|
|
1667
|
+
case StartReasonKind.EagerProvider: return `eager provider${source === void 0 ? "" : ` ${source}`}`;
|
|
1668
|
+
case StartReasonKind.BootHook: return "boot hook";
|
|
1669
|
+
case StartReasonKind.ConfigSection: return "config section";
|
|
1670
|
+
case StartReasonKind.DispatchInventory: return "DispatchInventory read";
|
|
1671
|
+
case StartReasonKind.NotDeferred: return "the target does not defer packages";
|
|
1672
|
+
default: return source === void 0 ? String(reason.kind) : `${String(reason.kind)}: ${source}`;
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
/** The text of a dependency: on the first use of an entry, or a wait of a package with its DI tokens. */
|
|
1676
|
+
dependencyText(reason) {
|
|
1677
|
+
const { source, tokens } = reason;
|
|
1678
|
+
if (source === void 0) return "dependency (source not recorded)";
|
|
1679
|
+
if (this.places.has(source)) return `first use of the entry '${source}'`;
|
|
1680
|
+
if (!this.packages.has(source)) return `dependency of ${source}`;
|
|
1681
|
+
return tokens === void 0 || tokens.length === 0 ? `dependency of ${source} (token not recorded)` : `DI dependency of ${source}, ${tokens.length === 1 ? "token" : "tokens"} ${tokens.join(", ")}`;
|
|
1682
|
+
}
|
|
1556
1683
|
};
|
|
1557
1684
|
//#endregion
|
|
1558
|
-
//#region src/
|
|
1685
|
+
//#region src/startup-size-check.ts
|
|
1559
1686
|
/**
|
|
1560
|
-
*
|
|
1561
|
-
*
|
|
1562
|
-
*
|
|
1563
|
-
* prove. Everything else is "computed", and the command then refuses or asks for an explicit input.
|
|
1564
|
-
*
|
|
1565
|
-
* Accepted in `nitro.config.*`: `export default {…}`, or `export default defineNitroConfig({…})` with
|
|
1566
|
-
* `defineNitroConfig` imported from `nitro` or `nitro/config`, or either one through ONE top-level `const` that is
|
|
1567
|
-
* never assigned again. The object has no spread. A `$production` or `$env.production` overlay applies; the other
|
|
1568
|
-
* environments do not, because the command always compiles for production.
|
|
1569
|
-
*
|
|
1570
|
-
* Accepted in `vite.config.*`: ONE call of the Nitro plugin of `nitro/vite` (a named or a namespace import), as a
|
|
1571
|
-
* direct element of the `plugins` array literal of the exported config (`export default {…}`, or `defineConfig`
|
|
1572
|
-
* from `vite` over one, or either through one `const`). No ternary, no `&&`, no spread, no helper, no function.
|
|
1573
|
-
*
|
|
1574
|
-
* The precedence is Nitro's own: the options of the Vite config, then `NITRO_PRESET` / `SERVER_PRESET`, then the
|
|
1575
|
-
* Nitro config file.
|
|
1687
|
+
* The size half of `heximon check --startup`: it measures the built server code from the entry that the last
|
|
1688
|
+
* build recorded ({@link ServerBuildRecord}), and compares the measures with the `size` section of
|
|
1689
|
+
* `heximon.startup.json`. Without build output, the check skips it.
|
|
1576
1690
|
*/
|
|
1577
|
-
var
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
"nitro.config.mjs"
|
|
1583
|
-
];
|
|
1584
|
-
static viteConfigNames = [
|
|
1585
|
-
"vite.config.ts",
|
|
1586
|
-
"vite.config.mts",
|
|
1587
|
-
"vite.config.js",
|
|
1588
|
-
"vite.config.mjs"
|
|
1589
|
-
];
|
|
1590
|
-
/** Nuxt creates its own Nitro, which a static read of these files cannot describe. */
|
|
1591
|
-
static nuxtConfigNames = [
|
|
1592
|
-
"nuxt.config.ts",
|
|
1593
|
-
"nuxt.config.mts",
|
|
1594
|
-
"nuxt.config.js",
|
|
1595
|
-
"nuxt.config.mjs"
|
|
1596
|
-
];
|
|
1597
|
-
/** The package names a Nuxt app depends on: the release, or the nightly. */
|
|
1598
|
-
static nuxtPackages = ["nuxt", "nuxt-nightly"];
|
|
1599
|
-
/** The extensions c12 tries for a config file, scripts and data formats. */
|
|
1600
|
-
static configExtensions = [
|
|
1601
|
-
".js",
|
|
1602
|
-
".ts",
|
|
1603
|
-
".mjs",
|
|
1604
|
-
".cjs",
|
|
1605
|
-
".mts",
|
|
1606
|
-
".cts",
|
|
1607
|
-
".json",
|
|
1608
|
-
".jsonc",
|
|
1609
|
-
".json5",
|
|
1610
|
-
".yaml",
|
|
1611
|
-
".yml",
|
|
1612
|
-
".toml"
|
|
1613
|
-
];
|
|
1614
|
-
/** The exact name of the rc file c12 reads for Nitro. */
|
|
1615
|
-
static resourceFileName = ".nitrorc";
|
|
1616
|
-
/** The expression nodes that only wrap another expression. */
|
|
1617
|
-
static wrapperTypes = [
|
|
1618
|
-
"TSAsExpression",
|
|
1619
|
-
"TSSatisfiesExpression",
|
|
1620
|
-
"TSNonNullExpression",
|
|
1621
|
-
"TSTypeAssertion",
|
|
1622
|
-
"ParenthesizedExpression"
|
|
1623
|
-
];
|
|
1624
|
-
/** The module that exports the Nitro Vite plugin. */
|
|
1625
|
-
static nitroVitePackage = "nitro/vite";
|
|
1626
|
-
/** The key of the Nitro config that holds the options of the Heximon compile. */
|
|
1627
|
-
static heximonSection = "heximon";
|
|
1628
|
-
/** The helper that returns its Nitro config unchanged, and where it is imported from. */
|
|
1629
|
-
static nitroConfigHelper = {
|
|
1630
|
-
name: "defineNitroConfig",
|
|
1631
|
-
packages: ["nitro", "nitro/config"]
|
|
1632
|
-
};
|
|
1633
|
-
/** The helper that returns its Vite config unchanged, and where it is imported from. */
|
|
1634
|
-
static viteConfigHelper = {
|
|
1635
|
-
name: "defineConfig",
|
|
1636
|
-
packages: ["vite", "vite-plus"]
|
|
1637
|
-
};
|
|
1691
|
+
var StartupSizeCheck = class StartupSizeCheck {
|
|
1692
|
+
/** The command that writes the file. */
|
|
1693
|
+
static updateCommand = "heximon check --startup --update";
|
|
1694
|
+
/** How many requests that the walk could not follow a note names. */
|
|
1695
|
+
static namedGapCount = 5;
|
|
1638
1696
|
/**
|
|
1639
|
-
*
|
|
1640
|
-
*
|
|
1697
|
+
* Measure the server code of the last build.
|
|
1698
|
+
*
|
|
1699
|
+
* @param rootDir - The app root.
|
|
1700
|
+
* @returns The measurement, or why there is none.
|
|
1641
1701
|
*/
|
|
1642
|
-
static
|
|
1643
|
-
const
|
|
1644
|
-
const
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1702
|
+
static measure(rootDir) {
|
|
1703
|
+
const reading = ServerBuildRecord.read(rootDir);
|
|
1704
|
+
const skip = (reason, message) => ({
|
|
1705
|
+
kind: "skipped",
|
|
1706
|
+
result: {
|
|
1707
|
+
action: "skipped",
|
|
1708
|
+
reason,
|
|
1709
|
+
record: reading.file
|
|
1710
|
+
},
|
|
1711
|
+
message: `${message} The size check of the server code is skipped.`
|
|
1712
|
+
});
|
|
1713
|
+
if (reading.kind === "absent") return skip("no-build-output", `There is no build output: the build writes ${ServerBuildRecord.relativePath}, and the file is absent. Build the app before the check to check the size of its server code.`);
|
|
1714
|
+
if (reading.kind === "unreadable") return skip("record-unreadable", `${reading.message} Build the app again.`);
|
|
1715
|
+
const names = Object.keys(reading.entries).sort();
|
|
1716
|
+
const missing = names.map((name) => reading.entries[name].path).find((path) => !StartupSizeCheck.isFile(path) || !existsSync(reading.outputDirectory));
|
|
1717
|
+
if (missing !== void 0) return skip("entry-missing", `The build record names the server entry ${relative(rootDir, missing)}, and the file does not exist. Build the app again.`);
|
|
1718
|
+
const entries = names.map((name) => {
|
|
1719
|
+
const entry = reading.entries[name];
|
|
1720
|
+
const graph = ServerCodeWalk.graph(rootDir, reading.outputDirectory, entry.path, entry.target);
|
|
1721
|
+
return {
|
|
1722
|
+
name,
|
|
1723
|
+
measurement: graph.measurement,
|
|
1724
|
+
staticImports: graph.staticImports,
|
|
1725
|
+
dynamicImports: graph.dynamicImports
|
|
1726
|
+
};
|
|
1727
|
+
});
|
|
1728
|
+
return {
|
|
1729
|
+
kind: "measured",
|
|
1730
|
+
entries,
|
|
1731
|
+
section: Object.fromEntries(entries.map(({ name, measurement }) => [name, StartupSizeBudget.recordOf(measurement)])),
|
|
1732
|
+
...reading.chunks === void 0 ? {} : { chunks: reading.chunks },
|
|
1733
|
+
...reading.generated === void 0 ? {} : { generated: reading.generated }
|
|
1650
1734
|
};
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
const
|
|
1661
|
-
const
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1735
|
+
}
|
|
1736
|
+
/**
|
|
1737
|
+
* Compare a measurement with the committed file.
|
|
1738
|
+
*
|
|
1739
|
+
* @param committed - The committed file, parsed, or `undefined` when there is no usable file. The startup
|
|
1740
|
+
* check reports that case, so the size check adds no error.
|
|
1741
|
+
* @param measured - The measurement.
|
|
1742
|
+
*/
|
|
1743
|
+
static compare(committed, measured) {
|
|
1744
|
+
const file = StartupBaselineFile.fileName;
|
|
1745
|
+
const notes = StartupSizeCheck.measurementLines(measured);
|
|
1746
|
+
const cliCodes = /* @__PURE__ */ new Map();
|
|
1747
|
+
if (committed === void 0) return {
|
|
1748
|
+
result: {
|
|
1749
|
+
action: "compare",
|
|
1750
|
+
entries: StartupSizeCheck.entriesOf(measured, void 0),
|
|
1751
|
+
committed: false,
|
|
1752
|
+
reason: "no-baseline"
|
|
1753
|
+
},
|
|
1754
|
+
diagnostics: [],
|
|
1755
|
+
cliCodes,
|
|
1756
|
+
notes
|
|
1757
|
+
};
|
|
1758
|
+
const baseline = StartupSizeBudget.parse(committed);
|
|
1759
|
+
if (baseline === null || baseline === void 0) {
|
|
1760
|
+
const diagnostic = {
|
|
1761
|
+
severity: DiagnosticSeverity.Error,
|
|
1762
|
+
message: baseline === null ? `${file} has no size section, and the build output exists (${measured.entries.map(({ measurement }) => measurement.entry).join(", ")}), so the size check has nothing to compare with.` : `The size section of ${file} has another shape, so the size check cannot read it.`,
|
|
1763
|
+
hint: `Run \`${StartupSizeCheck.updateCommand}\` after the build and commit the file.`
|
|
1764
|
+
};
|
|
1765
|
+
cliCodes.set(diagnostic, baseline === null ? CheckDiagnosticCode.StartupSizeUnrecorded : CheckDiagnosticCode.StartupBaselineUnreadable);
|
|
1766
|
+
return {
|
|
1767
|
+
result: {
|
|
1768
|
+
action: "compare",
|
|
1769
|
+
entries: StartupSizeCheck.entriesOf(measured, void 0),
|
|
1770
|
+
committed: false,
|
|
1771
|
+
reason: baseline === null ? "unrecorded" : "unreadable"
|
|
1772
|
+
},
|
|
1773
|
+
diagnostics: [diagnostic],
|
|
1774
|
+
cliCodes,
|
|
1775
|
+
notes
|
|
1776
|
+
};
|
|
1777
|
+
}
|
|
1778
|
+
const comparison = StartupSizeBudget.compare(baseline, measured.section);
|
|
1779
|
+
const diagnostics = comparison.growth.map((change) => {
|
|
1780
|
+
const isAdded = change.kind === StartupSizeChangeKind.EntryAdded;
|
|
1781
|
+
const diagnostic = {
|
|
1782
|
+
severity: DiagnosticSeverity.Error,
|
|
1783
|
+
message: isAdded ? `The build has the server entry '${change.name}' (${String(change.current)}), and the size section of ${file} does not record it, so the size check has nothing to compare it with.` : StartupSizeCheck.growthMessage(change, measured.section[change.name]?.entry ?? change.name),
|
|
1784
|
+
hint: isAdded ? `Run \`${StartupSizeCheck.updateCommand}\` after the build and commit the file.` : `Find what the new files bring into the server code (a static import that can be an \`import()\`, or a new package), or accept the growth: run \`${StartupSizeCheck.updateCommand}\` after the build and commit the file.`
|
|
1785
|
+
};
|
|
1786
|
+
cliCodes.set(diagnostic, isAdded ? CheckDiagnosticCode.StartupSizeUnrecorded : CheckDiagnosticCode.StartupSizeGrowth);
|
|
1787
|
+
return diagnostic;
|
|
1788
|
+
});
|
|
1673
1789
|
return {
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1790
|
+
result: {
|
|
1791
|
+
action: "compare",
|
|
1792
|
+
entries: StartupSizeCheck.entriesOf(measured, baseline),
|
|
1793
|
+
committed: true,
|
|
1794
|
+
growth: comparison.growth,
|
|
1795
|
+
notes: comparison.notes
|
|
1796
|
+
},
|
|
1797
|
+
diagnostics,
|
|
1798
|
+
cliCodes,
|
|
1799
|
+
notes: [...notes, ...StartupSizeCheck.noteLines(comparison.notes, measured.section)]
|
|
1683
1800
|
};
|
|
1684
1801
|
}
|
|
1685
1802
|
/**
|
|
1686
|
-
* The
|
|
1687
|
-
* `heximon.sourceDirectories` that is computed. The build scans the union of the three, so the
|
|
1688
|
-
* `heximon.sourceDirectories` join `scanDirs`.
|
|
1803
|
+
* The outcome of `--startup --update` for a measurement.
|
|
1689
1804
|
*
|
|
1690
|
-
* @param
|
|
1805
|
+
* @param measured - The measurement.
|
|
1806
|
+
* @param written - Whether the file was written or confirmed.
|
|
1691
1807
|
*/
|
|
1692
|
-
static
|
|
1693
|
-
for (const [option, value] of [
|
|
1694
|
-
["serverDir", result.serverDir],
|
|
1695
|
-
["scanDirs", result.scanDirs],
|
|
1696
|
-
["heximon.sourceDirectories", result.sourceDirectories]
|
|
1697
|
-
]) if (value.kind === "computed") return {
|
|
1698
|
-
kind: "computed",
|
|
1699
|
-
option,
|
|
1700
|
-
file: value.file,
|
|
1701
|
-
reason: value.reason
|
|
1702
|
-
};
|
|
1703
|
-
const scanDirs = [...result.scanDirs.kind === "literal" ? result.scanDirs.value : [], ...result.sourceDirectories.kind === "literal" ? result.sourceDirectories.value : []];
|
|
1808
|
+
static updated(measured, written) {
|
|
1704
1809
|
return {
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1810
|
+
result: {
|
|
1811
|
+
action: "update",
|
|
1812
|
+
entries: StartupSizeCheck.entriesOf(measured, void 0),
|
|
1813
|
+
written
|
|
1814
|
+
},
|
|
1815
|
+
notes: StartupSizeCheck.measurementLines(measured)
|
|
1708
1816
|
};
|
|
1709
1817
|
}
|
|
1710
1818
|
/**
|
|
1711
|
-
*
|
|
1712
|
-
* `nuxt-nightly`) as a dependency. Nuxt creates its own Nitro, so {@link read} does not report such a project
|
|
1713
|
-
* as a Nitro host, although it deploys through Nitro.
|
|
1819
|
+
* A number of bytes for a person to read: bytes, KiB with one decimal, or MiB with two.
|
|
1714
1820
|
*
|
|
1715
|
-
* @param
|
|
1821
|
+
* @param bytes - The number of bytes.
|
|
1716
1822
|
*/
|
|
1717
|
-
static
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
...manifest.devDependencies
|
|
1722
|
-
};
|
|
1723
|
-
return NitroStaticConfig.nuxtConfigNames.some((name) => existsSync(join(rootDir, name))) || NitroStaticConfig.nuxtPackages.some((name) => name in dependencies);
|
|
1823
|
+
static formatBytes(bytes) {
|
|
1824
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
1825
|
+
if (bytes < 1048576) return `${(bytes / 1024).toFixed(1)} KiB`;
|
|
1826
|
+
return `${(bytes / 1048576).toFixed(2)} MiB`;
|
|
1724
1827
|
}
|
|
1725
|
-
/**
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1828
|
+
/** The entries of a result, each with the measures that the committed section records for its name. */
|
|
1829
|
+
static entriesOf(measured, baseline) {
|
|
1830
|
+
return measured.entries.map(({ name, measurement }) => {
|
|
1831
|
+
const recorded = baseline !== void 0 && Object.hasOwn(baseline, name) ? baseline[name] : void 0;
|
|
1832
|
+
return {
|
|
1833
|
+
name,
|
|
1834
|
+
entry: measurement.entry,
|
|
1835
|
+
measured: {
|
|
1836
|
+
start: measurement.start,
|
|
1837
|
+
total: measurement.total
|
|
1838
|
+
},
|
|
1839
|
+
...recorded === void 0 ? {} : { baseline: {
|
|
1840
|
+
start: recorded.start,
|
|
1841
|
+
total: recorded.total
|
|
1842
|
+
} },
|
|
1843
|
+
gaps: measurement.gaps,
|
|
1844
|
+
unparsed: measurement.unparsed
|
|
1845
|
+
};
|
|
1735
1846
|
});
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1847
|
+
}
|
|
1848
|
+
/** The summary and gap lines of each measured entry. */
|
|
1849
|
+
static measurementLines(measured) {
|
|
1850
|
+
return measured.entries.flatMap(({ measurement }) => [StartupSizeCheck.summaryLine(measurement), ...StartupSizeCheck.gapLines(measurement)]);
|
|
1851
|
+
}
|
|
1852
|
+
/** The line with the measures. */
|
|
1853
|
+
static summaryLine(measurement) {
|
|
1854
|
+
const size = (value) => `${value.files} file${value.files === 1 ? "" : "s"}, ${StartupSizeCheck.formatBytes(value.bytes)}`;
|
|
1855
|
+
return `Server code of ${measurement.entry}: the static start closure has ${size(measurement.start)}; the total server code has ${size(measurement.total)}.`;
|
|
1856
|
+
}
|
|
1857
|
+
/** The lines about requests that the walk could not follow, and files that do not parse. */
|
|
1858
|
+
static gapLines(measurement) {
|
|
1859
|
+
const lines = [];
|
|
1860
|
+
const gaps = measurement.gaps;
|
|
1861
|
+
if (gaps.length > 0) {
|
|
1862
|
+
const named = gaps.slice(0, StartupSizeCheck.namedGapCount).map((gap) => `${gap.importer} → ${gap.request}`).join("; ");
|
|
1863
|
+
lines.push(`The size walk could not follow ${gaps.length} request${gaps.length === 1 ? "" : "s"} (${named}${gaps.length > StartupSizeCheck.namedGapCount ? "; …" : ""}), so the measures can be too small.`);
|
|
1745
1864
|
}
|
|
1746
|
-
|
|
1865
|
+
if (measurement.unparsed.length > 0) lines.push(`The size walk could not parse ${measurement.unparsed.join(", ")}, so the imports of ${measurement.unparsed.length === 1 ? "that file are" : "those files are"} not counted.`);
|
|
1866
|
+
return lines;
|
|
1747
1867
|
}
|
|
1748
|
-
/**
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
return [
|
|
1755
|
-
join(rootDir, "nitro.config"),
|
|
1756
|
-
join(rootDir, ".config", "nitro.config"),
|
|
1757
|
-
join(rootDir, ".config", "nitro")
|
|
1758
|
-
].flatMap((base) => ["", "/index"].flatMap((suffix) => ["", ...NitroStaticConfig.configExtensions].map((extension) => `${base}${suffix}${extension}`))).filter((path) => existsSync(path));
|
|
1868
|
+
/** The error text of one growth. */
|
|
1869
|
+
static growthMessage(change, entry) {
|
|
1870
|
+
const measure = change.measure ?? StartupSizeMeasure.StartBytes;
|
|
1871
|
+
const format = (value) => typeof value === "number" ? StartupSizeCheck.formatValue(measure, value) : String(value);
|
|
1872
|
+
const files = (change.files ?? []).map((file) => `${file.path} (${file.isNew ? `${StartupSizeCheck.formatBytes(file.bytes)}, new` : `+${StartupSizeCheck.formatBytes(file.growth)}`})`).join(", ");
|
|
1873
|
+
return `${StartupSizeCheck.measureName(measure)} of ${entry} is now ${format(change.current)}, and ${StartupBaselineFile.fileName} records ${format(change.committed)} with a slack of ${format(change.slack ?? 0)}.${files.length > 0 ? ` Largest new or grown files: ${files}.` : ""}`;
|
|
1759
1874
|
}
|
|
1760
|
-
/** The
|
|
1761
|
-
static
|
|
1762
|
-
const file =
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
return
|
|
1766
|
-
|
|
1767
|
-
return
|
|
1875
|
+
/** The note lines of the changes that do not fail. */
|
|
1876
|
+
static noteLines(notes, section) {
|
|
1877
|
+
const file = StartupBaselineFile.fileName;
|
|
1878
|
+
const lines = notes.map((change) => {
|
|
1879
|
+
if (change.kind === StartupSizeChangeKind.EntryChanged) return `The server entry '${change.name}' is now ${String(change.current)}, and ${file} records ${String(change.committed)}.`;
|
|
1880
|
+
if (change.kind === StartupSizeChangeKind.EntryRemoved) return `The build no longer has the server entry '${change.name}' (${String(change.committed)}) that ${file} records.`;
|
|
1881
|
+
const measure = change.measure ?? StartupSizeMeasure.StartBytes;
|
|
1882
|
+
return `${StartupSizeCheck.measureName(measure)} of ${section[change.name]?.entry ?? change.name} is now ${StartupSizeCheck.formatValue(measure, Number(change.current))}, less than the ${StartupSizeCheck.formatValue(measure, Number(change.committed))} that ${file} records.`;
|
|
1883
|
+
});
|
|
1884
|
+
if (lines.length === 0) return [];
|
|
1885
|
+
return [...lines, `Run \`${StartupSizeCheck.updateCommand}\` after the build to record the new size of the server code.`];
|
|
1886
|
+
}
|
|
1887
|
+
/** The name of a measure, at the start of a sentence. */
|
|
1888
|
+
static measureName(measure) {
|
|
1889
|
+
switch (measure) {
|
|
1890
|
+
case StartupSizeMeasure.StartFiles: return "The file count of the static start closure";
|
|
1891
|
+
case StartupSizeMeasure.TotalBytes: return "The total server code";
|
|
1892
|
+
default: return "The static start closure";
|
|
1768
1893
|
}
|
|
1769
1894
|
}
|
|
1770
|
-
/**
|
|
1771
|
-
static
|
|
1772
|
-
|
|
1773
|
-
return manifest !== void 0 && manifest !== "invalid" && ("nitro" in (manifest.dependencies ?? {}) || "nitro" in (manifest.devDependencies ?? {}));
|
|
1895
|
+
/** A value of a measure for a person to read. */
|
|
1896
|
+
static formatValue(measure, value) {
|
|
1897
|
+
return measure === StartupSizeMeasure.StartFiles ? `${value} file${value === 1 ? "" : "s"}` : StartupSizeCheck.formatBytes(value);
|
|
1774
1898
|
}
|
|
1899
|
+
static isFile(path) {
|
|
1900
|
+
return existsSync(path) && statSync(path).isFile();
|
|
1901
|
+
}
|
|
1902
|
+
};
|
|
1903
|
+
//#endregion
|
|
1904
|
+
//#region src/startup-entries-report.ts
|
|
1905
|
+
/**
|
|
1906
|
+
* The entries report of `heximon check --startup --entries`: for each app, the entries grouped by the packages
|
|
1907
|
+
* that their first use loads, and the built code that each group adds after the start. It walks the code with
|
|
1908
|
+
* the size walk of the check ({@link StartupSizeCheck}), and finds the code of each package from the build
|
|
1909
|
+
* record. It is a report: it fails nothing and writes nothing.
|
|
1910
|
+
*/
|
|
1911
|
+
var StartupEntriesReport = class StartupEntriesReport {
|
|
1912
|
+
/** How many modules each group names. */
|
|
1913
|
+
static namedModuleCount = 5;
|
|
1914
|
+
/** How many modules of the heavy entries the summary names. */
|
|
1915
|
+
static namedHeavyModuleCount = 5;
|
|
1916
|
+
/** How many packages of a list a line names before it stops. */
|
|
1917
|
+
static namedPackageCount = 8;
|
|
1918
|
+
/** How many contributors of an async registry a line names before it stops. */
|
|
1919
|
+
static namedContributorCount = 5;
|
|
1775
1920
|
/**
|
|
1776
|
-
*
|
|
1777
|
-
*
|
|
1778
|
-
*
|
|
1921
|
+
* The report of the start reports of a compile.
|
|
1922
|
+
*
|
|
1923
|
+
* @param reports - The start report of each app.
|
|
1924
|
+
* @param measuring - The measurement of the build output by the size check.
|
|
1925
|
+
* @param top - The number of module sets and async registries to show for each app. All when absent.
|
|
1779
1926
|
*/
|
|
1780
|
-
static
|
|
1781
|
-
const
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1927
|
+
static report(reports, measuring, top) {
|
|
1928
|
+
const measured = measuring.kind === "measured" ? measuring : void 0;
|
|
1929
|
+
const serverEntry = measured?.entries.find((entry) => entry.name === ServerBuildRecord.mainEntryName) ?? measured?.entries[0];
|
|
1930
|
+
const chunks = serverEntry === void 0 ? void 0 : measured?.chunks;
|
|
1931
|
+
const bytesUnavailable = measuring.kind === "skipped" ? measuring.result.reason : chunks === void 0 ? "no-chunk-record" : void 0;
|
|
1932
|
+
const apps = reports.map((report) => StartupEntriesReport.appReport(report, serverEntry === void 0 || chunks === void 0 ? void 0 : new StartupBuiltCode(serverEntry, chunks, measured?.generated, report.loads?.loaders), top));
|
|
1933
|
+
const notice = measuring.kind === "skipped" ? `${measuring.message.replace(" The size check of the server code is skipped.", "")} The entries report has no bytes.` : chunks === void 0 ? `The build record ${ServerBuildRecord.relativePath} does not have the packages of its chunks, so the entries report has no bytes. Build the app again with this version of Heximon.` : void 0;
|
|
1934
|
+
return {
|
|
1935
|
+
result: {
|
|
1936
|
+
...serverEntry === void 0 ? {} : { serverEntry: serverEntry.measurement.entry },
|
|
1937
|
+
...bytesUnavailable === void 0 ? {} : { bytesUnavailable },
|
|
1938
|
+
apps
|
|
1939
|
+
},
|
|
1940
|
+
lines: [...notice === void 0 ? [] : [notice], ...apps.map((app) => StartupEntriesReport.appText(app, serverEntry?.measurement.entry))]
|
|
1789
1941
|
};
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1942
|
+
}
|
|
1943
|
+
/** The report of one app. */
|
|
1944
|
+
static appReport(report, code, top) {
|
|
1945
|
+
const reasonsByPlace = new Map((report.loads?.entries ?? []).map((entry) => [StartupEntriesReport.placeOf(entry.kind, entry.key), new Map(entry.packages.map((loaded) => [loaded.package, loaded.reasons]))]));
|
|
1946
|
+
const groupsByKey = /* @__PURE__ */ new Map();
|
|
1947
|
+
for (const entry of report.entries) {
|
|
1948
|
+
const key = entry.packages.join("\n");
|
|
1949
|
+
const group = groupsByKey.get(key) ?? {
|
|
1950
|
+
packages: entry.packages,
|
|
1951
|
+
places: []
|
|
1952
|
+
};
|
|
1953
|
+
group.places.push(StartupEntriesReport.placeOf(entry.kind, entry.key));
|
|
1954
|
+
groupsByKey.set(key, group);
|
|
1955
|
+
}
|
|
1956
|
+
const startLoaded = code?.startFiles(report.start.map((entry) => entry.package));
|
|
1957
|
+
const asyncRegistries = report.loads?.asyncRegistries ?? [];
|
|
1958
|
+
const sortedGroups = [...groupsByKey.values()].map((group) => StartupEntriesReport.groupReport(group, new StartupEntryReasons(report), reasonsByPlace.get(group.places[0]) ?? /* @__PURE__ */ new Map(), asyncRegistries, code, startLoaded)).sort((first, second) => (second.added?.bytes ?? 0) - (first.added?.bytes ?? 0) || second.entries.length - first.entries.length || StartupEntriesReport.compareText(first.example, second.example));
|
|
1959
|
+
const registries = asyncRegistries.map((registry) => StartupEntriesReport.asyncRegistryReport(registry, code, startLoaded)).sort((first, second) => (second.added?.bytes ?? 0) - (first.added?.bytes ?? 0) || StartupEntriesReport.compareText(first.owner, second.owner) || StartupEntriesReport.compareText(first.registry, second.registry));
|
|
1960
|
+
return {
|
|
1961
|
+
app: report.app,
|
|
1962
|
+
defersPackages: report.defersPackages,
|
|
1963
|
+
entries: report.entries.length,
|
|
1964
|
+
groupCount: sortedGroups.length,
|
|
1965
|
+
...code === void 0 || startLoaded === void 0 ? {} : {
|
|
1966
|
+
start: code.sizeOf(startLoaded),
|
|
1967
|
+
...StartupEntriesReport.byPackageNameOf(code, report.start.map((entry) => entry.package), "startByPackageName"),
|
|
1968
|
+
byPackageNameOnly: !code.hasLoaders()
|
|
1969
|
+
},
|
|
1970
|
+
groups: top === void 0 ? sortedGroups : sortedGroups.slice(0, top),
|
|
1971
|
+
summary: StartupEntriesReport.summaryOf(report, sortedGroups, code, startLoaded),
|
|
1972
|
+
asyncRegistryCount: registries.length,
|
|
1973
|
+
asyncRegistries: top === void 0 ? registries : registries.slice(0, top)
|
|
1797
1974
|
};
|
|
1798
1975
|
}
|
|
1799
|
-
/**
|
|
1800
|
-
static
|
|
1801
|
-
const
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1976
|
+
/** The report of one group of entries. */
|
|
1977
|
+
static groupReport(group, reasonNames, reasons, asyncRegistries, code, startLoaded) {
|
|
1978
|
+
const places = [...group.places].sort((first, second) => StartupEntriesReport.compareText(first, second));
|
|
1979
|
+
const addedFiles = code === void 0 || startLoaded === void 0 ? void 0 : code.codeOf(group.packages, startLoaded);
|
|
1980
|
+
const bytesByPackage = code === void 0 || addedFiles === void 0 ? /* @__PURE__ */ new Map() : code.packageBytes(addedFiles);
|
|
1981
|
+
const identitiesByName = /* @__PURE__ */ new Map();
|
|
1982
|
+
for (const identity of group.packages) {
|
|
1983
|
+
const name = StartupBuiltCode.packageNameOf(identity);
|
|
1984
|
+
identitiesByName.set(name, [...identitiesByName.get(name) ?? [], identity]);
|
|
1985
|
+
}
|
|
1986
|
+
const modules = [...bytesByPackage].filter(([, bytes]) => bytes > 0).map(([name, bytes]) => {
|
|
1987
|
+
const identities = identitiesByName.get(name);
|
|
1988
|
+
if (identities === void 0) return {
|
|
1989
|
+
module: name,
|
|
1990
|
+
bytes,
|
|
1991
|
+
reasons: [StartupEntryReasons.imported()]
|
|
1992
|
+
};
|
|
1993
|
+
const described = [...new Map(identities.flatMap((identity) => reasons.get(identity) ?? []).map((reason) => reasonNames.describe(reason)).map((named) => [named.text, named])).values()];
|
|
1994
|
+
return {
|
|
1995
|
+
module: identities.join(", "),
|
|
1996
|
+
bytes,
|
|
1997
|
+
reasons: described.length === 0 ? [StartupEntryReasons.notRecorded()] : described
|
|
1998
|
+
};
|
|
1999
|
+
}).sort((first, second) => second.bytes - first.bytes || StartupEntriesReport.compareText(first.module, second.module)).slice(0, StartupEntriesReport.namedModuleCount);
|
|
2000
|
+
const sealPackages = /* @__PURE__ */ new Map();
|
|
2001
|
+
for (const identity of group.packages) for (const reason of reasons.get(identity) ?? []) if (reason.kind === StartReasonKind.SealGroup && reason.source !== void 0) sealPackages.set(reason.source, [...sealPackages.get(reason.source) ?? [], identity]);
|
|
2002
|
+
const sealGroups = [...sealPackages].sort(([first], [second]) => StartupEntriesReport.compareText(first, second)).map(([registry, packages]) => ({
|
|
2003
|
+
registry,
|
|
2004
|
+
packages,
|
|
2005
|
+
...addedFiles === void 0 ? {} : { bytes: [...new Set(packages.map((identity) => StartupBuiltCode.packageNameOf(identity)))].reduce((sum, name) => sum + (bytesByPackage.get(name) ?? 0), 0) }
|
|
2006
|
+
}));
|
|
2007
|
+
const loadedWithEntry = startLoaded === void 0 || addedFiles === void 0 ? void 0 : /* @__PURE__ */ new Set([...startLoaded, ...addedFiles]);
|
|
2008
|
+
return {
|
|
2009
|
+
packages: group.packages,
|
|
2010
|
+
entries: places,
|
|
2011
|
+
example: places[0],
|
|
2012
|
+
...code === void 0 || addedFiles === void 0 ? {} : { added: code.sizeOf(addedFiles) },
|
|
2013
|
+
modules,
|
|
2014
|
+
sealGroups,
|
|
2015
|
+
onRead: asyncRegistries.filter((registry) => group.packages.includes(registry.owner)).map((registry) => ({
|
|
2016
|
+
registry: registry.registry,
|
|
2017
|
+
owner: registry.owner,
|
|
2018
|
+
...code === void 0 || loadedWithEntry === void 0 ? {} : { added: code.sizeOf(code.codeOf(registry.packages.map((loaded) => loaded.package), loadedWithEntry)) }
|
|
2019
|
+
})),
|
|
2020
|
+
...code === void 0 ? {} : StartupEntriesReport.byPackageNameOf(code, group.packages, "byPackageName")
|
|
1806
2021
|
};
|
|
1807
2022
|
}
|
|
1808
|
-
/**
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
*/
|
|
1813
|
-
static readNitroConfig(file) {
|
|
1814
|
-
const { program, imports } = NitroStaticConfig.parse(file);
|
|
1815
|
-
const resolved = NitroStaticConfig.exportedObject(program, imports, NitroStaticConfig.nitroConfigHelper);
|
|
1816
|
-
if (resolved.kind === "unproven") return [{
|
|
1817
|
-
kind: "unproven",
|
|
1818
|
-
file,
|
|
1819
|
-
reason: resolved.reason
|
|
1820
|
-
}];
|
|
1821
|
-
const base = NitroStaticConfig.prove(resolved.node, file);
|
|
1822
|
-
if (base.kind === "unproven") return [base];
|
|
1823
|
-
const overlay = /* @__PURE__ */ new Map();
|
|
1824
|
-
const sources = [];
|
|
1825
|
-
const overlays = [base.entries.get("$production")];
|
|
1826
|
-
const environments = base.entries.get("$env");
|
|
1827
|
-
if (environments !== void 0) {
|
|
1828
|
-
const proven = NitroStaticConfig.prove(environments, file);
|
|
1829
|
-
if (proven.kind === "unproven") sources.push(proven);
|
|
1830
|
-
else overlays.push(proven.entries.get("production"));
|
|
1831
|
-
}
|
|
1832
|
-
for (const node of overlays) {
|
|
1833
|
-
if (node === void 0) continue;
|
|
1834
|
-
const proven = NitroStaticConfig.prove(node, file);
|
|
1835
|
-
if (proven.kind === "unproven") sources.push(proven);
|
|
1836
|
-
else for (const [key, value] of proven.entries) overlay.set(key, value);
|
|
1837
|
-
}
|
|
1838
|
-
if (overlay.size > 0) sources.push({
|
|
1839
|
-
kind: "object",
|
|
1840
|
-
file,
|
|
1841
|
-
entries: overlay
|
|
1842
|
-
});
|
|
1843
|
-
sources.push(base);
|
|
1844
|
-
return sources;
|
|
2023
|
+
/** A field that names the packages whose files `code` finds by npm package name, or no field. */
|
|
2024
|
+
static byPackageNameOf(code, identities, field) {
|
|
2025
|
+
const byPackageName = code.byPackageName(identities);
|
|
2026
|
+
return byPackageName.length === 0 ? {} : { [field]: byPackageName };
|
|
1845
2027
|
}
|
|
1846
2028
|
/**
|
|
1847
|
-
*
|
|
1848
|
-
*
|
|
1849
|
-
* Nitro host.
|
|
2029
|
+
* The report of one async registry: the code of its first `load()` call beyond the start and the code that
|
|
2030
|
+
* loads with the owner.
|
|
1850
2031
|
*/
|
|
1851
|
-
static
|
|
1852
|
-
const
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
if (entry.importKind === ImportKind.Namespace) namespaces.add(entry.localName);
|
|
1858
|
-
else if (entry.importKind === ImportKind.Named && entry.importedName === "nitro") named.add(entry.localName);
|
|
2032
|
+
static asyncRegistryReport(registry, code, startLoaded) {
|
|
2033
|
+
const packages = registry.packages.map((loaded) => loaded.package);
|
|
2034
|
+
let added;
|
|
2035
|
+
if (code !== void 0 && startLoaded !== void 0) {
|
|
2036
|
+
const loaded = /* @__PURE__ */ new Set([...startLoaded, ...code.codeOf([registry.owner, ...registry.ownerPackages], startLoaded)]);
|
|
2037
|
+
added = code.sizeOf(code.codeOf(packages, loaded));
|
|
1859
2038
|
}
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
2039
|
+
return {
|
|
2040
|
+
registry: registry.registry,
|
|
2041
|
+
owner: registry.owner,
|
|
2042
|
+
inputKey: registry.inputKey,
|
|
2043
|
+
contributors: registry.contributors,
|
|
2044
|
+
packages,
|
|
2045
|
+
...added === void 0 ? {} : { added },
|
|
2046
|
+
...code === void 0 ? {} : StartupEntriesReport.byPackageNameOf(code, packages, "byPackageName")
|
|
1863
2047
|
};
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
call,
|
|
1877
|
-
referenceStart: callee.start
|
|
1878
|
-
}];
|
|
1879
|
-
if (callee.is(AstNodeType.MemberExpression) && !callee.flagAt("computed") && callee.child("property").name === "nitro" && callee.child("object").is(AstNodeType.Identifier) && namespaces.has(callee.child("object").name ?? "")) return [{
|
|
1880
|
-
call,
|
|
1881
|
-
referenceStart: callee.child("object").start
|
|
2048
|
+
}
|
|
2049
|
+
/** The summary of the entries of one app, over every group. */
|
|
2050
|
+
static summaryOf(report, groups, code, startLoaded) {
|
|
2051
|
+
const kinds = /* @__PURE__ */ new Map();
|
|
2052
|
+
for (const entry of report.entries) kinds.set(entry.kind, [...kinds.get(entry.kind) ?? [], entry.packages]);
|
|
2053
|
+
const everyEntry = [...kinds].filter(([, sets]) => sets.length > 1).sort(([first], [second]) => StartupEntriesReport.compareText(first, second)).flatMap(([kind, sets]) => {
|
|
2054
|
+
const shared = sets[0].filter((identity) => sets.every((set) => set.includes(identity)));
|
|
2055
|
+
return shared.length === 0 ? [] : [{
|
|
2056
|
+
kind,
|
|
2057
|
+
entries: sets.length,
|
|
2058
|
+
packages: shared,
|
|
2059
|
+
...code === void 0 || startLoaded === void 0 ? {} : { added: code.sizeOf(code.codeOf(shared, startLoaded)) }
|
|
1882
2060
|
}];
|
|
1883
|
-
return [];
|
|
1884
2061
|
});
|
|
1885
|
-
|
|
1886
|
-
const
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
sources: [config]
|
|
1895
|
-
};
|
|
1896
|
-
const plugins = NitroStaticConfig.unwrap(config.entries.get("plugins"));
|
|
1897
|
-
const call = calls[0].call;
|
|
1898
|
-
if (!(plugins?.is(AstNodeType.ArrayExpression) === true && plugins.children("elements").some((element) => NitroStaticConfig.unwrap(element)?.start === call.start))) return unproven("the Nitro Vite plugin is not a direct element of the `plugins` array");
|
|
1899
|
-
const sources = [];
|
|
1900
|
-
const argument = NitroStaticConfig.unwrap(call.children("arguments")[0]);
|
|
1901
|
-
if (argument !== void 0) {
|
|
1902
|
-
const options = NitroStaticConfig.prove(argument, file);
|
|
1903
|
-
sources.push(options);
|
|
1904
|
-
const nested = options.kind === "object" ? options.entries.get("config") : void 0;
|
|
1905
|
-
if (nested !== void 0) sources.push(NitroStaticConfig.prove(nested, file));
|
|
1906
|
-
}
|
|
1907
|
-
const nitroKey = config.entries.get("nitro");
|
|
1908
|
-
if (nitroKey !== void 0) sources.push(NitroStaticConfig.prove(nitroKey, file));
|
|
2062
|
+
if (code === void 0 || report.entries.length === 0) return { everyEntry };
|
|
2063
|
+
const entryBytes = groups.flatMap((group) => group.entries.map(() => group.added?.bytes ?? 0)).sort((first, second) => first - second);
|
|
2064
|
+
const p75 = StartupEntriesReport.percentile(entryBytes, .75);
|
|
2065
|
+
const heavyGroups = groups.filter((group) => {
|
|
2066
|
+
const bytes = group.added?.bytes ?? 0;
|
|
2067
|
+
return bytes > 0 && bytes >= p75;
|
|
2068
|
+
});
|
|
2069
|
+
const heavyCounts = /* @__PURE__ */ new Map();
|
|
2070
|
+
for (const group of heavyGroups) for (const identity of group.packages) heavyCounts.set(identity, (heavyCounts.get(identity) ?? 0) + group.entries.length);
|
|
1909
2071
|
return {
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
const expression = NitroStaticConfig.unwrap(node);
|
|
1923
|
-
if (expression === void 0) return {
|
|
1924
|
-
kind: "unproven",
|
|
1925
|
-
reason: "the config has no default export"
|
|
1926
|
-
};
|
|
1927
|
-
if (expression.is(AstNodeType.ObjectExpression)) return {
|
|
1928
|
-
kind: "found",
|
|
1929
|
-
node: expression
|
|
1930
|
-
};
|
|
1931
|
-
if (expression.is(AstNodeType.CallExpression)) {
|
|
1932
|
-
const callee = expression.child("callee");
|
|
1933
|
-
return callee.is(AstNodeType.Identifier) && helperNames.has(callee.name ?? "") && expression.children("arguments").length === 1 ? objectOf(expression.children("arguments")[0], viaConstant) : {
|
|
1934
|
-
kind: "unproven",
|
|
1935
|
-
reason: `the config is a call of something other than ${helper.name} from ${helper.packages.join(" or ")}`
|
|
1936
|
-
};
|
|
1937
|
-
}
|
|
1938
|
-
if (expression.is(AstNodeType.Identifier) && !viaConstant) {
|
|
1939
|
-
const initializer = NitroStaticConfig.constantOf(program, expression.name ?? "", expression.start);
|
|
1940
|
-
return initializer === void 0 ? {
|
|
1941
|
-
kind: "unproven",
|
|
1942
|
-
reason: `\`${expression.name}\` is not a top-level const that nothing else uses`
|
|
1943
|
-
} : objectOf(initializer, true);
|
|
2072
|
+
bytes: {
|
|
2073
|
+
median: StartupEntriesReport.percentile(entryBytes, .5),
|
|
2074
|
+
p75,
|
|
2075
|
+
max: entryBytes.at(-1) ?? 0
|
|
2076
|
+
},
|
|
2077
|
+
everyEntry,
|
|
2078
|
+
heavy: {
|
|
2079
|
+
entries: heavyGroups.reduce((sum, group) => sum + group.entries.length, 0),
|
|
2080
|
+
modules: [...heavyCounts].sort(([firstModule, firstCount], [secondModule, secondCount]) => secondCount - firstCount || StartupEntriesReport.compareText(firstModule, secondModule)).slice(0, StartupEntriesReport.namedHeavyModuleCount).map(([module, entries]) => ({
|
|
2081
|
+
module,
|
|
2082
|
+
entries
|
|
2083
|
+
}))
|
|
1944
2084
|
}
|
|
1945
|
-
return {
|
|
1946
|
-
kind: "unproven",
|
|
1947
|
-
reason: "the default export is not an object the reader can prove"
|
|
1948
|
-
};
|
|
1949
2085
|
};
|
|
1950
|
-
return objectOf(declaration, false);
|
|
1951
2086
|
}
|
|
2087
|
+
/** The text of the report of one app. */
|
|
2088
|
+
static appText(app, serverEntry) {
|
|
2089
|
+
if (app.entries === 0) return `App '${app.app}' has no entries: the first use of each entry loads no package beyond the start.` + StartupEntriesReport.asyncRegistriesText(app);
|
|
2090
|
+
const lines = [`Entries of app '${app.app}': ${StartupEntriesReport.count(app.entries, "entry", "entries")} in ${StartupEntriesReport.count(app.groupCount, "module set", "module sets")}.` + (app.start === void 0 || serverEntry === void 0 ? "" : ` Each row gives the code that the first use of an entry loads after the start (${StartupEntriesReport.sizeText(app.start)}), in the build of ${serverEntry}.`)];
|
|
2091
|
+
if (app.byPackageNameOnly === true) lines.push("The build record names no loader chunk of a package, so the report finds the code of each package by its npm package name. Two roots of one npm package then share their code.");
|
|
2092
|
+
if (app.groups.length < app.groupCount) lines.push(`The ${app.groups.length} largest of the ${app.groupCount} module sets (--top):`);
|
|
2093
|
+
for (const [index, group] of app.groups.entries()) {
|
|
2094
|
+
lines.push(`${index + 1}. ${StartupEntriesReport.count(group.entries.length, "entry", "entries")}, for example '${group.example}'` + (group.added === void 0 ? "." : `: ${StartupEntriesReport.sizeText(group.added)}.`), ` Module set: ${group.packages.length === 0 ? "no package beyond the start" : StartupEntriesReport.listText(group.packages)}.`);
|
|
2095
|
+
for (const module of group.modules) lines.push(` - ${module.module}: ${StartupSizeCheck.formatBytes(module.bytes)} (${module.reasons.map((reason) => reason.text).join("; ")})`);
|
|
2096
|
+
for (const sealGroup of group.sealGroups) lines.push(` - The seal group of ${sealGroup.registry} loads ${StartupEntriesReport.count(sealGroup.packages.length, "package", "packages")}` + (sealGroup.bytes === void 0 ? "" : ` (${StartupSizeCheck.formatBytes(sealGroup.bytes)})`) + " with these entries. Extend AsyncContributions<T> for the registry, so that its contributors load on its first load() call.");
|
|
2097
|
+
if (app.byPackageNameOnly !== true && group.byPackageName !== void 0) lines.push(` - Found by npm package name, as the build record names no loader chunk: ${StartupEntriesReport.listText(group.byPackageName)}.`);
|
|
2098
|
+
for (const onRead of group.onRead) lines.push(` - On read: a load() call of '${onRead.registry}' of ${onRead.owner} can add ` + (onRead.added === void 0 ? "the code of its contributors." : `${StartupEntriesReport.sizeText(onRead.added)}. The row above does not count it.`));
|
|
2099
|
+
}
|
|
2100
|
+
const { summary } = app;
|
|
2101
|
+
if (summary.bytes !== void 0) lines.push(`Per entry: median ${StartupSizeCheck.formatBytes(summary.bytes.median)}, p75 ${StartupSizeCheck.formatBytes(summary.bytes.p75)}, max ${StartupSizeCheck.formatBytes(summary.bytes.max)}.`);
|
|
2102
|
+
for (const every of summary.everyEntry) lines.push(`Every '${every.kind}' entry (${every.entries}) loads ${StartupEntriesReport.count(every.packages.length, "package", "packages")}` + (every.added === void 0 ? "" : ` (${StartupSizeCheck.formatBytes(every.added.bytes)})`) + `: ${StartupEntriesReport.listText(every.packages)}.`);
|
|
2103
|
+
if (summary.heavy !== void 0 && summary.heavy.modules.length > 0) lines.push(`The ${StartupEntriesReport.count(summary.heavy.entries, "heavy entry", "heavy entries")} (p75 or more) load these packages most: ${summary.heavy.modules.map((module) => `${module.module} (${module.entries})`).join(", ")}.`);
|
|
2104
|
+
return lines.join("\n") + StartupEntriesReport.asyncRegistriesText(app);
|
|
2105
|
+
}
|
|
2106
|
+
/** The lines of the async registries of one app, with a newline before them, or nothing. */
|
|
2107
|
+
static asyncRegistriesText(app) {
|
|
2108
|
+
if (app.asyncRegistryCount === 0) return "";
|
|
2109
|
+
return `\n${[`Async registries: the first load() call of each one loads its contributors. No entry counts this code${app.asyncRegistries.length < app.asyncRegistryCount ? ` (the ${app.asyncRegistries.length} largest of ${app.asyncRegistryCount}, --top)` : ""}.`, ...app.asyncRegistries.map((registry) => `- '${registry.registry}' of ${registry.owner}: ${StartupEntriesReport.count(registry.contributors.length, "contributor", "contributors")} (${StartupEntriesReport.listText(registry.contributors, StartupEntriesReport.namedContributorCount)}), ${StartupEntriesReport.count(registry.packages.length, "package", "packages")} beyond the start` + (registry.added === void 0 ? "." : `, ${StartupEntriesReport.sizeText(registry.added)}.`))].join("\n")}`;
|
|
2110
|
+
}
|
|
2111
|
+
/** Files and bytes for a person to read. */
|
|
2112
|
+
static sizeText(size) {
|
|
2113
|
+
return `${StartupEntriesReport.count(size.files, "file", "files")}, ${StartupSizeCheck.formatBytes(size.bytes)}`;
|
|
2114
|
+
}
|
|
2115
|
+
/** A list of names, with the count of the rest when it is long. */
|
|
2116
|
+
static listText(names, limit = StartupEntriesReport.namedPackageCount) {
|
|
2117
|
+
const named = names.slice(0, limit).join(", ");
|
|
2118
|
+
const rest = names.length - limit;
|
|
2119
|
+
return rest > 0 ? `${named} and ${rest} more` : named;
|
|
2120
|
+
}
|
|
2121
|
+
/** A count with the noun in the singular or the plural. */
|
|
2122
|
+
static count(value, singular, plural) {
|
|
2123
|
+
return `${value} ${value === 1 ? singular : plural}`;
|
|
2124
|
+
}
|
|
2125
|
+
/** An entry as `<kind> <key>`, as `heximon.startup.json` names it. */
|
|
2126
|
+
static placeOf(kind, key) {
|
|
2127
|
+
return `${kind} ${key}`;
|
|
2128
|
+
}
|
|
2129
|
+
/** The value at a fraction of sorted values, by the nearest rank. */
|
|
2130
|
+
static percentile(sorted, fraction) {
|
|
2131
|
+
return sorted[Math.max(0, Math.ceil(fraction * sorted.length) - 1)] ?? 0;
|
|
2132
|
+
}
|
|
2133
|
+
static compareText(first, second) {
|
|
2134
|
+
return first < second ? -1 : first > second ? 1 : 0;
|
|
2135
|
+
}
|
|
2136
|
+
};
|
|
2137
|
+
//#endregion
|
|
2138
|
+
//#region src/project-modules.ts
|
|
2139
|
+
/**
|
|
2140
|
+
* Imports packages the way the generated wiring imports them: through a small module written into the
|
|
2141
|
+
* compile output directory, which sits in the project. The specifiers inside it resolve from the project, so
|
|
2142
|
+
* the command gets the copy of a package that the wiring gets. A package the command imported from its own
|
|
2143
|
+
* install would be a second set of class objects, and `app.from(CoreModule)` would not know them.
|
|
2144
|
+
*/
|
|
2145
|
+
var ProjectModules = class ProjectModules {
|
|
1952
2146
|
/**
|
|
1953
|
-
*
|
|
1954
|
-
*
|
|
1955
|
-
*
|
|
1956
|
-
*
|
|
2147
|
+
* Import `specifiers` of the project.
|
|
2148
|
+
*
|
|
2149
|
+
* @param directory - A directory inside the project, with write access (the compile output directory).
|
|
2150
|
+
* @param fileName - The name of the module to write, unique among the calls for one directory.
|
|
2151
|
+
* @param source - The module source: `export … from "<specifier>"` lines.
|
|
2152
|
+
* @returns The namespace of the written module.
|
|
1957
2153
|
*/
|
|
1958
|
-
static
|
|
1959
|
-
const
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
if (declaration.stringAt("kind") !== "const") continue;
|
|
1963
|
-
for (const declarator of declaration.children("declarations")) if (declarator.child("id").name === name) initializers.push({
|
|
1964
|
-
init: declarator.child("init"),
|
|
1965
|
-
idStart: declarator.child("id").start
|
|
1966
|
-
});
|
|
1967
|
-
}
|
|
1968
|
-
if (initializers.length !== 1) return;
|
|
1969
|
-
const names = NitroStaticConfig.nameStarts(program);
|
|
1970
|
-
return program.descendants(AstNodeType.Identifier).filter((identifier) => identifier.name === name && identifier.start !== initializers[0].idStart && identifier.start !== exportStart && !names.has(identifier.start)).length === 0 ? initializers[0].init : void 0;
|
|
1971
|
-
}
|
|
1972
|
-
/** The starts of identifiers that are names, not references: a property key and a member name. */
|
|
1973
|
-
static nameStarts(program) {
|
|
1974
|
-
return /* @__PURE__ */ new Set([...program.descendants(AstNodeType.Property).filter((property) => !property.flagAt("computed") && !property.flagAt("shorthand")).map((property) => property.child("key").start), ...program.descendants(AstNodeType.MemberExpression).filter((member) => !member.flagAt("computed")).map((member) => member.child("property").start)]);
|
|
2154
|
+
static async import(directory, fileName, source) {
|
|
2155
|
+
const file = join(directory, fileName);
|
|
2156
|
+
writeFileSync(file, source);
|
|
2157
|
+
return await import(pathToFileURL(file).href);
|
|
1975
2158
|
}
|
|
1976
|
-
/** The
|
|
1977
|
-
static
|
|
1978
|
-
|
|
1979
|
-
if (object === void 0 || !object.is(AstNodeType.ObjectExpression)) return {
|
|
1980
|
-
kind: "unproven",
|
|
1981
|
-
file,
|
|
1982
|
-
reason: "an options value is not an object literal"
|
|
1983
|
-
};
|
|
1984
|
-
const entries = /* @__PURE__ */ new Map();
|
|
1985
|
-
for (const property of object.children("properties")) {
|
|
1986
|
-
if (!property.is(AstNodeType.Property)) return {
|
|
1987
|
-
kind: "unproven",
|
|
1988
|
-
file,
|
|
1989
|
-
reason: "an options object has a spread"
|
|
1990
|
-
};
|
|
1991
|
-
const key = property.child("key");
|
|
1992
|
-
const name = key.name ?? key.stringAt("value");
|
|
1993
|
-
if (property.flagAt("computed") || property.flagAt("method") || property.stringAt("kind") !== "init" || name === void 0) return {
|
|
1994
|
-
kind: "unproven",
|
|
1995
|
-
file,
|
|
1996
|
-
reason: "an options object has a computed key, a getter or a method"
|
|
1997
|
-
};
|
|
1998
|
-
entries.set(name, property.child("value"));
|
|
1999
|
-
}
|
|
2000
|
-
return {
|
|
2001
|
-
kind: "object",
|
|
2002
|
-
file,
|
|
2003
|
-
entries
|
|
2004
|
-
};
|
|
2159
|
+
/** The runtime of the project: everything the command asks of `@heximon/runtime`. */
|
|
2160
|
+
static runtime(directory) {
|
|
2161
|
+
return ProjectModules.import(directory, "project-runtime.js", "export * from \"@heximon/runtime\";\n");
|
|
2005
2162
|
}
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
} : {
|
|
2023
|
-
kind: "literal",
|
|
2024
|
-
value
|
|
2025
|
-
};
|
|
2026
|
-
}
|
|
2027
|
-
return { kind: "none" };
|
|
2163
|
+
};
|
|
2164
|
+
//#endregion
|
|
2165
|
+
//#region src/nitro-host.ts
|
|
2166
|
+
/**
|
|
2167
|
+
* The production inputs of a Nitro host project, for the preset that was read from its config files. The
|
|
2168
|
+
* preset picks the deploy platform, and the compile adds the Nitro host plugin, so a compile that reads the
|
|
2169
|
+
* sidecar alone would build another graph. `@heximon/nitro` of the project makes the inputs from the preset
|
|
2170
|
+
* name. Both packages load from the project, so the plugins, the strategy and the compiler are one copy. No
|
|
2171
|
+
* Nitro or Vite config runs.
|
|
2172
|
+
*/
|
|
2173
|
+
var NitroHost = class NitroHost {
|
|
2174
|
+
context;
|
|
2175
|
+
aliases;
|
|
2176
|
+
constructor(context, aliases) {
|
|
2177
|
+
this.context = context;
|
|
2178
|
+
this.aliases = aliases;
|
|
2028
2179
|
}
|
|
2029
2180
|
/**
|
|
2030
|
-
*
|
|
2031
|
-
*
|
|
2181
|
+
* Resolve the inputs of the Nitro host.
|
|
2182
|
+
*
|
|
2183
|
+
* @param rootDir - The project root.
|
|
2184
|
+
* @param directory - A directory inside the project to write the import module into.
|
|
2185
|
+
* @param request - The preset (or the choice to take the platform from the sidecar), the literal source
|
|
2186
|
+
* directories of the Nitro config, and the module aliases of the project (the tsconfig `paths`).
|
|
2187
|
+
* @throws {Error} When `@heximon/nitro` is not installed in the project.
|
|
2032
2188
|
*/
|
|
2033
|
-
static
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2189
|
+
static async open(rootDir, directory, request) {
|
|
2190
|
+
const modules = await ProjectModules.import(directory, "project-nitro.js", "export { resolveNitroPresetInputs } from \"@heximon/nitro/module\";\n");
|
|
2191
|
+
return new NitroHost(await modules.resolveNitroPresetInputs({
|
|
2192
|
+
rootDirectory: rootDir,
|
|
2193
|
+
preset: request.preset,
|
|
2194
|
+
aliases: request.aliases.prefixes,
|
|
2195
|
+
...request.serverDir === void 0 ? {} : { serverDir: request.serverDir },
|
|
2196
|
+
...request.scanDirs === void 0 ? {} : { scanDirs: request.scanDirs },
|
|
2197
|
+
...request.platformFromSidecar === true ? { platformFromSidecar: true } : {}
|
|
2198
|
+
}), request.aliases);
|
|
2039
2199
|
}
|
|
2040
|
-
/**
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
let found = false;
|
|
2044
|
-
for (const source of sources) {
|
|
2045
|
-
if (source.kind === "unproven") return {
|
|
2046
|
-
kind: "computed",
|
|
2047
|
-
file: source.file,
|
|
2048
|
-
reason: source.reason
|
|
2049
|
-
};
|
|
2050
|
-
const node = NitroStaticConfig.unwrap(source.entries.get(key));
|
|
2051
|
-
if (node === void 0) continue;
|
|
2052
|
-
const strings = (node.is(AstNodeType.ArrayExpression) ? node.children("elements") : []).map((element) => NitroStaticConfig.stringOf(element));
|
|
2053
|
-
if (!node.is(AstNodeType.ArrayExpression) || strings.some((value) => value === void 0)) return {
|
|
2054
|
-
kind: "computed",
|
|
2055
|
-
file: source.file,
|
|
2056
|
-
reason: `\`${key}\` is not an array of string literals`
|
|
2057
|
-
};
|
|
2058
|
-
found = true;
|
|
2059
|
-
values.push(...strings);
|
|
2060
|
-
}
|
|
2061
|
-
return found ? {
|
|
2062
|
-
kind: "literal",
|
|
2063
|
-
value: values
|
|
2064
|
-
} : { kind: "none" };
|
|
2200
|
+
/** Whether the sidecar declares the deploy platform. */
|
|
2201
|
+
hasSidecarPlatform() {
|
|
2202
|
+
return this.context.config?.platform !== void 0;
|
|
2065
2203
|
}
|
|
2066
|
-
/**
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2204
|
+
/**
|
|
2205
|
+
* Why the preset is a custom preset (a path, a package, or a name that Nitro does not know), or `undefined` for
|
|
2206
|
+
* a built-in preset. The strategy of a custom preset comes from the sidecar `platform`, as for a computed one.
|
|
2207
|
+
*/
|
|
2208
|
+
customPresetReason() {
|
|
2209
|
+
return this.context.customPreset;
|
|
2072
2210
|
}
|
|
2073
|
-
/** The
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
while (current !== void 0 && current.exists() && NitroStaticConfig.wrapperTypes.includes(current.type ?? "")) current = current.child("expression");
|
|
2077
|
-
return current === void 0 || !current.exists() ? void 0 : current;
|
|
2211
|
+
/** The production compile inputs of the host: the strategy, the plugins, the sidecar, the source directories. */
|
|
2212
|
+
inputs() {
|
|
2213
|
+
return this.context;
|
|
2078
2214
|
}
|
|
2079
|
-
|
|
2080
|
-
|
|
2215
|
+
/**
|
|
2216
|
+
* Run the compile the host's production build runs.
|
|
2217
|
+
*
|
|
2218
|
+
* @param rootDir - The project root.
|
|
2219
|
+
* @param outputDirectory - Where the generated files will be written.
|
|
2220
|
+
* @param sourceDirectories - The source directories to scan instead of the host's, when the caller has some.
|
|
2221
|
+
*/
|
|
2222
|
+
compile(rootDir, outputDirectory, sourceDirectories) {
|
|
2223
|
+
const context = this.context;
|
|
2224
|
+
const config = context.config;
|
|
2225
|
+
const compiler = new ProjectCompiler();
|
|
2081
2226
|
return {
|
|
2082
|
-
|
|
2083
|
-
|
|
2227
|
+
output: compiler.compile({
|
|
2228
|
+
rootDir,
|
|
2229
|
+
outputDirectory,
|
|
2230
|
+
sourceDirectories: sourceDirectories ?? context.sourceDirectories,
|
|
2231
|
+
aliases: this.aliases.prefixes,
|
|
2232
|
+
exactAliases: this.aliases.exact,
|
|
2233
|
+
plugins: context.plugins,
|
|
2234
|
+
strategy: context.strategy,
|
|
2235
|
+
...context.wiringMode === void 0 ? {} : { wiringMode: context.wiringMode },
|
|
2236
|
+
...config === void 0 ? {} : { config },
|
|
2237
|
+
...context.runtimeConfig === void 0 ? {} : { runtimeConfig: context.runtimeConfig },
|
|
2238
|
+
...config?.root === void 0 ? {} : { apps: { main: config.root } },
|
|
2239
|
+
...context.workspaceSubscribers === void 0 ? {} : { workspaceSubscribers: context.workspaceSubscribers }
|
|
2240
|
+
}),
|
|
2241
|
+
compiler,
|
|
2242
|
+
platformName: context.strategy.platformName
|
|
2084
2243
|
};
|
|
2085
2244
|
}
|
|
2086
2245
|
};
|
|
@@ -2120,19 +2279,21 @@ var StartupTarget = class StartupTarget {
|
|
|
2120
2279
|
const preset = hostTarget.preset;
|
|
2121
2280
|
const aliases = TsconfigAliases.read(rootDir, hostTarget.generatedTsconfigFiles);
|
|
2122
2281
|
const host = await StartupTarget.openHost(rootDir, preset, aliases, explicit === void 0 ? hostTarget.sourceOptions : { scanDirs: explicit });
|
|
2123
|
-
|
|
2282
|
+
const customPreset = host.customPresetReason();
|
|
2283
|
+
const isPresetUnread = preset.kind === "computed" || customPreset !== void 0;
|
|
2284
|
+
if (isPresetUnread && !host.hasSidecarPlatform()) throw CommandFailure.of(CheckDiagnosticCode.NitroConfigComputed, preset.kind === "computed" ? `The Nitro preset of this Nitro host is computed (${preset.reason}, in ${preset.file}), and the command does not run your config files, so it cannot tell the deploy target of the build. The start report depends on that target. Nothing was checked.` : `The Nitro preset of this app is a custom preset (${customPreset}), and the command does not load a custom preset, so it cannot tell the deploy target of the build. The start report depends on that target. Nothing was checked.`, "Declare `platform` in heximon.config.ts (for example `platform: new CloudflareWorkersStrategy()`), " + (preset.kind === "computed" ? "or set `preset` in the Nitro config to a string." : "or set the Nitro preset to the name of a built-in preset."));
|
|
2124
2285
|
const session = ProjectCompileSession.forNitroHost({
|
|
2125
2286
|
rootDir,
|
|
2126
2287
|
inputs: host.inputs(),
|
|
2127
2288
|
aliases,
|
|
2128
2289
|
...explicit === void 0 ? {} : { sourceDirectories: explicit }
|
|
2129
2290
|
});
|
|
2130
|
-
const source =
|
|
2291
|
+
const source = isPresetUnread ? StartupTargetSource.Sidecar : hostTarget.source;
|
|
2131
2292
|
return {
|
|
2132
2293
|
session,
|
|
2133
2294
|
target: {
|
|
2134
2295
|
...StartupTarget.targetOf(session, source),
|
|
2135
|
-
...preset.kind === "literal" ? { preset: preset.value } : {}
|
|
2296
|
+
...preset.kind === "literal" && !isPresetUnread ? { preset: preset.value } : {}
|
|
2136
2297
|
}
|
|
2137
2298
|
};
|
|
2138
2299
|
}
|
|
@@ -2633,7 +2794,10 @@ const checkCommand = defineCommand({
|
|
|
2633
2794
|
...sessionOptions,
|
|
2634
2795
|
isSourceExplicit: sourceOption !== void 0 && sourceOption.trim() !== ""
|
|
2635
2796
|
}) : {
|
|
2636
|
-
session: await ProjectCompileSession.open(
|
|
2797
|
+
session: await ProjectCompileSession.open({
|
|
2798
|
+
...sessionOptions,
|
|
2799
|
+
hostTarget: mode === CompileMode.App
|
|
2800
|
+
}),
|
|
2637
2801
|
target: void 0
|
|
2638
2802
|
};
|
|
2639
2803
|
const session = opened.session;
|
|
@@ -2660,6 +2824,7 @@ const checkCommand = defineCommand({
|
|
|
2660
2824
|
workspaceSubscribers: topology.topology
|
|
2661
2825
|
};
|
|
2662
2826
|
const output = session.compile(true, appCompileOptions);
|
|
2827
|
+
const targetNote = session.targetNote();
|
|
2663
2828
|
const staleLibraries = await session.staleLibraryDiagnostics();
|
|
2664
2829
|
if (staleLibraries.length > 0) checked = {
|
|
2665
2830
|
diagnostics: staleLibraries,
|
|
@@ -2677,15 +2842,18 @@ const checkCommand = defineCommand({
|
|
|
2677
2842
|
await session.compiler.formatOnDisk(rootDir, session.outputDirectory, output.files);
|
|
2678
2843
|
}
|
|
2679
2844
|
const dropped = verifyBaseline ? UpgradeBaseline.droppedSections(rootDir, output.files, output.diagnostics, session.config?.strictDiagnostics ?? []) : void 0;
|
|
2845
|
+
const cliCodes = new Map(dropped?.cliCodes ?? []);
|
|
2846
|
+
if (targetNote !== void 0) cliCodes.set(targetNote, CheckDiagnosticCode.HostTargetDefault);
|
|
2680
2847
|
checked = {
|
|
2681
2848
|
diagnostics: [
|
|
2849
|
+
...targetNote === void 0 ? [] : [targetNote],
|
|
2682
2850
|
...topology.diagnostics,
|
|
2683
2851
|
...output.diagnostics,
|
|
2684
2852
|
...dropped?.diagnostics ?? []
|
|
2685
2853
|
],
|
|
2686
2854
|
files: output.files,
|
|
2687
2855
|
startReports: output.startReports ?? [],
|
|
2688
|
-
...
|
|
2856
|
+
...cliCodes.size === 0 ? {} : { cliCodes },
|
|
2689
2857
|
...emitted ? { emittedTo: session.outputDirectory } : {}
|
|
2690
2858
|
};
|
|
2691
2859
|
}
|
|
@@ -4968,7 +5136,8 @@ const graphCommand = defineCommand({
|
|
|
4968
5136
|
const session = await ProjectCompileSession.open({
|
|
4969
5137
|
rootDir,
|
|
4970
5138
|
environment: args.env,
|
|
4971
|
-
...sourceDirectories === void 0 ? {} : { sourceDirectories }
|
|
5139
|
+
...sourceDirectories === void 0 ? {} : { sourceDirectories },
|
|
5140
|
+
hostTarget: resolved.mode === CompileMode.App
|
|
4972
5141
|
});
|
|
4973
5142
|
output = resolved.mode === CompileMode.Library ? session.compileLibrary() : session.compile();
|
|
4974
5143
|
} catch (error) {
|
|
@@ -6141,7 +6310,8 @@ var RoutesCommand = class RoutesCommand {
|
|
|
6141
6310
|
const session = await ProjectCompileSession.open({
|
|
6142
6311
|
rootDir,
|
|
6143
6312
|
environment: args.env,
|
|
6144
|
-
...sourceDirectories === void 0 ? {} : { sourceDirectories }
|
|
6313
|
+
...sourceDirectories === void 0 ? {} : { sourceDirectories },
|
|
6314
|
+
hostTarget: mode === CompileMode.App
|
|
6145
6315
|
});
|
|
6146
6316
|
compiled = mode === CompileMode.Library ? session.compileLibrary() : session.compile();
|
|
6147
6317
|
} catch (error) {
|
|
@@ -6921,7 +7091,8 @@ var TaskRunner = class TaskRunner {
|
|
|
6921
7091
|
aliases: this.aliases,
|
|
6922
7092
|
...sourceDirectories
|
|
6923
7093
|
}));
|
|
6924
|
-
|
|
7094
|
+
const customPreset = host.customPresetReason();
|
|
7095
|
+
if ((preset.kind === "computed" || customPreset !== void 0) && !host.hasSidecarPlatform()) throw new TaskRunFailure(TaskDiagnosticCode.NitroConfigComputed, (preset.kind === "computed" ? `The Nitro preset is computed (${preset.reason}, in ${preset.file}), and the command does not run your config files, so it cannot read the platform: ` : `The Nitro preset is a custom preset (${customPreset}), and the command does not load a custom preset, so it cannot read the platform: `) + "declare `platform` in heximon.config.ts (for example `platform: new CloudflareWorkersStrategy()`), " + (preset.kind === "computed" ? "or set NITRO_PRESET." : "or set the Nitro preset to the name of a built-in preset.") + " Nothing was booted.");
|
|
6925
7096
|
const compiled = host.compile(rootDir, outputDirectory, this.options.isSourceExplicit === true ? this.options.sourceDirectories : void 0);
|
|
6926
7097
|
({output, compiler} = compiled);
|
|
6927
7098
|
this.platformName = compiled.platformName;
|
|
@@ -7025,7 +7196,7 @@ var TaskRunner = class TaskRunner {
|
|
|
7025
7196
|
throw TaskRunner.failureOf(TaskDiagnosticCode.StartFailed, `The task ${this.options.taskName} did not start`, error);
|
|
7026
7197
|
}
|
|
7027
7198
|
try {
|
|
7028
|
-
return await task.run(event);
|
|
7199
|
+
return await this.runThroughWorkHooks(runtime, target.task.className, () => task.run(event));
|
|
7029
7200
|
} catch (error) {
|
|
7030
7201
|
throw TaskRunner.failureOf(TaskDiagnosticCode.TaskFailed, `The task ${this.options.taskName} failed`, error, TaskRefusal);
|
|
7031
7202
|
}
|
|
@@ -7040,6 +7211,18 @@ var TaskRunner = class TaskRunner {
|
|
|
7040
7211
|
throw new TaskRunFailure(TaskDiagnosticCode.ResultNotJson, `The task ran, but its result is not JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
7041
7212
|
}
|
|
7042
7213
|
}
|
|
7214
|
+
/**
|
|
7215
|
+
* Run the task inside the app's work hooks, as `TaskManager` does, so tracing records a `task` span. A
|
|
7216
|
+
* project whose runtime has no work hooks runs the task as it is.
|
|
7217
|
+
*/
|
|
7218
|
+
runThroughWorkHooks(runtime, taskName, run) {
|
|
7219
|
+
const workHooks = runtime.WorkHooks;
|
|
7220
|
+
const hooks = workHooks?.hooksFor(this.context);
|
|
7221
|
+
return workHooks === void 0 || hooks === void 0 ? run() : workHooks.runWith(hooks, {
|
|
7222
|
+
kind: runtime.WorkKind.Task,
|
|
7223
|
+
name: taskName
|
|
7224
|
+
}, run);
|
|
7225
|
+
}
|
|
7043
7226
|
/** The seeds the app declares for every background frame, or none. */
|
|
7044
7227
|
async seedsOf(runtime) {
|
|
7045
7228
|
const resolvable = this.app;
|
|
@@ -7657,7 +7840,8 @@ const workflowsCommand = defineCommand({
|
|
|
7657
7840
|
const session = await ProjectCompileSession.open({
|
|
7658
7841
|
rootDir,
|
|
7659
7842
|
environment: args.env,
|
|
7660
|
-
...sourceDirectories === void 0 ? {} : { sourceDirectories }
|
|
7843
|
+
...sourceDirectories === void 0 ? {} : { sourceDirectories },
|
|
7844
|
+
hostTarget: true
|
|
7661
7845
|
});
|
|
7662
7846
|
const output = session.compile();
|
|
7663
7847
|
const errors = output.diagnostics.filter((diagnostic) => diagnostic.severity === DiagnosticSeverity.Error);
|