zitejs 0.10.0-start.14 → 0.10.0-start.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,24 @@
1
1
  import { type Problem } from './start/problem.js';
2
- type Step = {
3
- name: string;
2
+ /**
3
+ * A step's result. `unchecked` is why it couldn't answer at all (killed, out of
4
+ * memory, a tool missing): nothing for the app to fix, so it carries no output.
5
+ */
6
+ type StepResult = {
4
7
  ok: boolean;
5
8
  output?: string;
9
+ unchecked?: string;
6
10
  };
11
+ type Step = {
12
+ name: string;
13
+ } & StepResult;
7
14
  /** One app's check, as `--json` reports it. */
8
15
  export type AppCheck = {
9
16
  path: string;
10
17
  runtime: 'classic' | 'start';
18
+ /** False when something in it needs fixing. */
11
19
  ok: boolean;
20
+ /** Set when a step couldn't run to an answer, so the app wasn't fully checked. */
21
+ unchecked?: true;
12
22
  steps: Step[];
13
23
  problems: Problem[];
14
24
  };
@@ -23,11 +33,13 @@ export declare class Reporter {
23
33
  readonly apps: AppCheck[];
24
34
  constructor(json: boolean);
25
35
  app(path: string, runtime: AppCheck['runtime'], heading: boolean): AppCheck;
26
- step(app: AppCheck, name: string, result: {
27
- ok: boolean;
28
- output?: string;
29
- }): boolean;
36
+ step(app: AppCheck, name: string, result: StepResult): boolean;
30
37
  problems(app: AppCheck, problems: Problem[]): void;
31
38
  }
39
+ /**
40
+ * Runs a tool to its answer, or says why it gave none: a kill (a signal, or an
41
+ * exit of 128 and up with nothing printed) is a sandbox limit, not the app's.
42
+ */
43
+ export declare function runTool(file: string, args: string[], cwd: string): StepResult;
32
44
  export declare function runCheck(args?: string[]): Promise<void>;
33
45
  export {};
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.Reporter = exports.withStdoutOnStderr = void 0;
37
+ exports.runTool = runTool;
37
38
  exports.runCheck = runCheck;
38
39
  const child_process_1 = require("child_process");
39
40
  const fs_1 = require("fs");
@@ -124,6 +125,13 @@ class Reporter {
124
125
  return app;
125
126
  }
126
127
  step(app, name, result) {
128
+ if (result.unchecked) {
129
+ app.steps.push({ name, ok: false, unchecked: result.unchecked });
130
+ app.unchecked = true;
131
+ if (!this.json)
132
+ console.log(` ${name} ... unchecked: ${result.unchecked}`);
133
+ return false;
134
+ }
127
135
  const output = result.output?.trim();
128
136
  app.steps.push({ name, ok: result.ok, ...(output && !result.ok && { output }) });
129
137
  if (!result.ok)
@@ -173,28 +181,53 @@ const isInternalApp = (appPath) => {
173
181
  }
174
182
  };
175
183
  /**
176
- * A Start app: `vite build` first, since it regenerates the route tree tsc
177
- * reads, then tsc, then describe, which reports what the platform's build
178
- * refuses, read the same way, then the source hints.
184
+ * Runs a tool to its answer, or says why it gave none: a kill (a signal, or an
185
+ * exit of 128 and up with nothing printed) is a sandbox limit, not the app's.
186
+ */
187
+ function runTool(file, args, cwd) {
188
+ const result = (0, child_process_1.spawnSync)(file, args, { cwd, encoding: 'utf-8', stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 64 * 1024 * 1024 });
189
+ const output = (result.stderr ?? '') + (result.stdout ?? '');
190
+ if (result.error)
191
+ return { ok: false, unchecked: `couldn't run: ${result.error.message}` };
192
+ if (result.signal)
193
+ return { ok: false, unchecked: `killed by ${result.signal}` };
194
+ if (result.status !== null && result.status >= 128 && !output.trim()) {
195
+ return { ok: false, unchecked: `exited ${result.status} with no output` };
196
+ }
197
+ return { ok: result.status === 0, output };
198
+ }
199
+ const isMissingModule = (err) => err?.code === 'MODULE_NOT_FOUND';
200
+ /**
201
+ * A Start app, in about a second: its route tree regenerated as `vite build`
202
+ * writes it, a tsgo typecheck, which reads that tree, then the source rules.
203
+ * Neither a build nor describe: the platform's build is where access is decided
204
+ * and enforced, and these only say early what it would refuse.
179
205
  */
180
206
  async function checkStartApp(reporter, app, appPath) {
181
- if (!reporter.step(app, 'vite build', run('npx', ['vite', 'build'], appPath)))
182
- return;
183
- // --pretty for `file:line:col - error` lines, the shape the platform re-roots.
184
- reporter.step(app, 'tsc --noEmit', run('npx', ['tsc', '--noEmit', '--pretty', '-p', 'tsconfig.app.json'], appPath));
185
- const { describedProblems } = await Promise.resolve().then(() => __importStar(require('./start/describeRules.js')));
186
- const { sourceProblems } = await Promise.resolve().then(() => __importStar(require('./start/sourceRules.js')));
207
+ const { regenerateRouteTree } = await Promise.resolve().then(() => __importStar(require('./start/routeTree.js')));
208
+ let routeTree = { ok: true };
187
209
  try {
188
- const { describeBuiltApp } = await Promise.resolve().then(() => __importStar(require('../start/describeBuilt.js')));
189
- const description = (await describeBuiltApp(appPath));
190
- const problems = describedProblems(description, { internal: isInternalApp(appPath) });
191
- reporter.step(app, 'describe', { ok: !problems.some(p => p.severity === 'error') });
192
- reporter.problems(app, problems);
210
+ await regenerateRouteTree(appPath);
193
211
  }
194
212
  catch (err) {
195
- reporter.step(app, 'describe', { ok: false, output: err instanceof Error ? err.message : String(err) });
213
+ const message = err instanceof Error ? err.message : String(err);
214
+ routeTree = isMissingModule(err)
215
+ ? { ok: false, unchecked: `the app's TanStack Start isn't installed: ${message}` }
216
+ : { ok: false, output: message };
196
217
  }
197
- const hints = sourceProblems(readAppSources(appPath));
218
+ if (reporter.step(app, 'route tree', routeTree)) {
219
+ const { findTsgo } = await Promise.resolve().then(() => __importStar(require('./start/tsgo.js')));
220
+ const tsgo = findTsgo(appPath);
221
+ reporter.step(app, 'tsgo --noEmit', !(0, fs_1.existsSync)((0, path_1.join)(appPath, 'tsconfig.app.json'))
222
+ ? { ok: false, output: `no ${(0, path_1.join)(appPath, 'tsconfig.app.json')}` }
223
+ : !tsgo
224
+ ? { ok: false, unchecked: "tsgo isn't installed (npm install -g @typescript/native-preview)" }
225
+ : // --pretty for `file:line:col - error` lines, the shape the platform re-roots. Two checkers, not
226
+ // four, hold half the type graph: a large app fits the sandbox's memory.
227
+ runTool(process.execPath, [tsgo, '--noEmit', '--pretty', '-p', 'tsconfig.app.json', '--checkers', '2'], appPath));
228
+ }
229
+ const { sourceProblems } = await Promise.resolve().then(() => __importStar(require('./start/sourceRules.js')));
230
+ const hints = sourceProblems(readAppSources(appPath), { internal: isInternalApp(appPath) });
198
231
  reporter.step(app, 'source', { ok: !hints.some(p => p.severity === 'error') });
199
232
  reporter.problems(app, hints);
200
233
  }
@@ -245,14 +278,18 @@ async function runCheck(args = process.argv.slice(3)) {
245
278
  }
246
279
  };
247
280
  await (json ? (0, exports.withStdoutOnStderr)(checkAll) : checkAll());
248
- const allPassed = reporter.apps.every(app => app.ok);
281
+ const failed = reporter.apps.some(app => !app.ok);
282
+ const unchecked = reporter.apps.some(app => app.unchecked);
249
283
  if (json) {
250
- process.stdout.write(`${JSON.stringify({ ok: allPassed, apps: reporter.apps })}\n`);
284
+ process.stdout.write(`${JSON.stringify({ ok: !failed && !unchecked, ...(unchecked && { unchecked: true }), apps: reporter.apps })}\n`);
251
285
  }
252
286
  else {
253
287
  console.log('');
254
- console.log(allPassed ? 'All apps pass.' : 'Some apps failed.');
288
+ console.log(failed ? 'Some apps failed.' : unchecked ? 'Some apps were not fully checked.' : 'All apps pass.');
255
289
  }
256
- if (!allPassed)
290
+ // 3: nothing to fix, but not everything ran.
291
+ if (failed)
257
292
  process.exit(1);
293
+ if (unchecked)
294
+ process.exit(3);
258
295
  }
@@ -0,0 +1,8 @@
1
+ /** Start's footer (start-plugin-core route-tree-footer), which types the router and start instance for `@tanstack/react-start`. */
2
+ export declare const startRouteTreeFooter: (generatedRouteTree: string, routerFile: string, startFile: string | undefined) => string[];
3
+ /**
4
+ * Regenerates the route tree with the router generator the app's own
5
+ * `@tanstack/react-start` builds with, configured as zitejs's Start plugin
6
+ * configures it, so the file is the one `vite build` writes, byte for byte.
7
+ */
8
+ export declare function regenerateRouteTree(appDir: string): Promise<void>;
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.startRouteTreeFooter = void 0;
40
+ exports.regenerateRouteTree = regenerateRouteTree;
41
+ // `src/routeTree.gen.ts` as `vite build` writes it, without vite: tsgo types
42
+ // each `createFileRoute` from it, and only the generator may write it.
43
+ const node_fs_1 = require("node:fs");
44
+ const node_module_1 = require("node:module");
45
+ const node_path_1 = __importDefault(require("node:path"));
46
+ const node_url_1 = require("node:url");
47
+ const entries_js_1 = require("../../start/entries.js");
48
+ // As Start resolves its entries (start-plugin-core resolve-entries).
49
+ const ENTRY_EXTENSIONS = [".ts", ".js", ".mts", ".mjs", ".tsx", ".jsx"];
50
+ const resolveEntry = (srcDirectory, name) => {
51
+ const base = node_path_1.default.resolve(srcDirectory, name);
52
+ if ((0, node_fs_1.existsSync)(base) && (0, node_fs_1.statSync)(base).isFile())
53
+ return base;
54
+ return ENTRY_EXTENSIONS.map((extension) => base + extension).find((file) => (0, node_fs_1.existsSync)(file));
55
+ };
56
+ const importPath = (generatedRouteTree, file) => {
57
+ let relative = node_path_1.default.relative(node_path_1.default.dirname(generatedRouteTree), file);
58
+ if (!relative.startsWith("."))
59
+ relative = `./${relative}`;
60
+ return relative.split(node_path_1.default.sep).join("/");
61
+ };
62
+ /** Start's footer (start-plugin-core route-tree-footer), which types the router and start instance for `@tanstack/react-start`. */
63
+ const startRouteTreeFooter = (generatedRouteTree, routerFile, startFile) => {
64
+ const blocks = [
65
+ `import type { getRouter } from '${importPath(generatedRouteTree, routerFile)}'`,
66
+ startFile
67
+ ? `import type { startInstance } from '${importPath(generatedRouteTree, startFile)}'`
68
+ : "import type { createStart } from '@tanstack/react-start'",
69
+ "declare module '@tanstack/react-start' {\n interface Register {\n ssr: true\n router: Awaited<ReturnType<typeof getRouter>>",
70
+ ...(startFile ? [" config: Awaited<ReturnType<typeof startInstance.getOptions>>"] : []),
71
+ " }\n}",
72
+ ];
73
+ return [blocks.join("\n")];
74
+ };
75
+ exports.startRouteTreeFooter = startRouteTreeFooter;
76
+ /** A package's ESM entry, resolved from `fromFile` as Node would. */
77
+ const importPackage = async (fromFile, name) => {
78
+ const manifest = (0, node_module_1.createRequire)(fromFile).resolve(`${name}/package.json`);
79
+ const { exports, module, main } = JSON.parse((0, node_fs_1.readFileSync)(manifest, "utf-8"));
80
+ const root = exports?.["."] ?? exports;
81
+ const entry = root?.import?.default ?? root?.import ?? root?.default ?? module ?? main ?? "index.js";
82
+ return Promise.resolve(`${(0, node_url_1.pathToFileURL)(node_path_1.default.join(node_path_1.default.dirname(manifest), entry)).href}`).then(s => __importStar(require(s)));
83
+ };
84
+ /**
85
+ * Regenerates the route tree with the router generator the app's own
86
+ * `@tanstack/react-start` builds with, configured as zitejs's Start plugin
87
+ * configures it, so the file is the one `vite build` writes, byte for byte.
88
+ */
89
+ async function regenerateRouteTree(appDir) {
90
+ const root = node_path_1.default.resolve(appDir);
91
+ const srcDirectory = node_path_1.default.join(root, "src");
92
+ const routesDirectory = node_path_1.default.join(srcDirectory, "routes");
93
+ if (!(0, node_fs_1.existsSync)(routesDirectory))
94
+ return;
95
+ const generatedRouteTree = node_path_1.default.join(srcDirectory, "routeTree.gen.ts");
96
+ const routerFile = resolveEntry(srcDirectory, "router");
97
+ if (!routerFile)
98
+ throw new Error("src/router.tsx is missing: Start reads the app's router from it");
99
+ // zite() points Start's entry at the generated start instance, relative to src/.
100
+ const startFile = resolveEntry(srcDirectory, node_path_1.default.join("..", entries_js_1.START_INSTANCE_ENTRY.replace(/\.ts$/, "")));
101
+ const startManifest = (0, node_module_1.createRequire)(node_path_1.default.join(root, "package.json")).resolve("@tanstack/react-start/package.json");
102
+ const coreManifest = (0, node_module_1.createRequire)(startManifest).resolve("@tanstack/start-plugin-core/package.json");
103
+ const { getConfig, Generator } = (await importPackage(coreManifest, "@tanstack/router-generator"));
104
+ const footer = (0, exports.startRouteTreeFooter)(generatedRouteTree, routerFile, startFile);
105
+ const config = {
106
+ ...getConfig({ routesDirectory, generatedRouteTree }, root),
107
+ target: "react",
108
+ routeTreeFileFooter: () => footer,
109
+ };
110
+ await new Generator({ config, root }).run();
111
+ }
@@ -1,3 +1,5 @@
1
1
  import type { Problem } from "./problem.js";
2
2
  /** Every source hint for one Start app, given its files by app-relative path. */
3
- export declare function sourceProblems(sources: Record<string, string>): Problem[];
3
+ export declare function sourceProblems(sources: Record<string, string>, { internal }?: {
4
+ internal?: boolean;
5
+ }): Problem[];