@react-router/dev 7.0.0-pre.5 → 7.0.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/dist/cli/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * @react-router/dev v7.0.0-pre.5
3
+ * @react-router/dev v7.0.0
4
4
  *
5
5
  * Copyright (c) Remix Software Inc.
6
6
  *
@@ -55,31 +55,6 @@ var init_invariant = __esm({
55
55
  }
56
56
  });
57
57
 
58
- // vite/babel.ts
59
- var import_parser, t, traverse, generate;
60
- var init_babel = __esm({
61
- "vite/babel.ts"() {
62
- "use strict";
63
- import_parser = require("@babel/parser");
64
- t = __toESM(require("@babel/types"));
65
- traverse = require("@babel/traverse").default;
66
- generate = require("@babel/generator").default;
67
- }
68
- });
69
-
70
- // vite/node-adapter.ts
71
- var import_node_events, import_node_stream, import_set_cookie_parser, import_node;
72
- var init_node_adapter = __esm({
73
- "vite/node-adapter.ts"() {
74
- "use strict";
75
- import_node_events = require("events");
76
- import_node_stream = require("stream");
77
- import_set_cookie_parser = require("set-cookie-parser");
78
- import_node = require("@react-router/node");
79
- init_invariant();
80
- }
81
- });
82
-
83
58
  // vite/import-vite-esm-sync.ts
84
59
  async function preloadViteEsm() {
85
60
  vite = await import("vite");
@@ -96,64 +71,100 @@ var init_import_vite_esm_sync = __esm({
96
71
  }
97
72
  });
98
73
 
99
- // vite/resolve-file-url.ts
100
- var path;
101
- var init_resolve_file_url = __esm({
102
- "vite/resolve-file-url.ts"() {
74
+ // vite/vite-node.ts
75
+ async function createContext(viteConfig = {}) {
76
+ await preloadViteEsm();
77
+ const vite2 = importViteEsmSync();
78
+ const devServer = await vite2.createServer(
79
+ vite2.mergeConfig(
80
+ {
81
+ server: {
82
+ preTransformRequests: false,
83
+ hmr: false
84
+ },
85
+ optimizeDeps: {
86
+ noDiscovery: true
87
+ },
88
+ configFile: false,
89
+ envFile: false,
90
+ plugins: []
91
+ },
92
+ viteConfig
93
+ )
94
+ );
95
+ await devServer.pluginContainer.buildStart({});
96
+ const server = new import_server.ViteNodeServer(devServer);
97
+ (0, import_source_map.installSourcemapsSupport)({
98
+ getSourceMap: (source) => server.getSourceMap(source)
99
+ });
100
+ const runner = new import_client.ViteNodeRunner({
101
+ root: devServer.config.root,
102
+ base: devServer.config.base,
103
+ fetchModule(id2) {
104
+ return server.fetchModule(id2);
105
+ },
106
+ resolveId(id2, importer) {
107
+ return server.resolveId(id2, importer);
108
+ }
109
+ });
110
+ return { devServer, server, runner };
111
+ }
112
+ var import_server, import_client, import_source_map;
113
+ var init_vite_node = __esm({
114
+ "vite/vite-node.ts"() {
103
115
  "use strict";
104
- path = __toESM(require("path"));
116
+ import_server = require("vite-node/server");
117
+ import_client = require("vite-node/client");
118
+ import_source_map = require("vite-node/source-map");
105
119
  init_import_vite_esm_sync();
106
120
  }
107
121
  });
108
122
 
109
- // vite/styles.ts
110
- var path2, import_react_router, cssFileRegExp, cssModulesRegExp;
111
- var init_styles = __esm({
112
- "vite/styles.ts"() {
113
- "use strict";
114
- path2 = __toESM(require("path"));
115
- import_react_router = require("react-router");
116
- init_resolve_file_url();
117
- cssFileRegExp = /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/;
118
- cssModulesRegExp = new RegExp(`\\.module${cssFileRegExp.source}`);
119
- }
120
- });
121
-
122
- // vite/vmod.ts
123
- var id;
124
- var init_vmod = __esm({
125
- "vite/vmod.ts"() {
126
- "use strict";
127
- id = (name) => `virtual:react-router/${name}`;
123
+ // config/routes.ts
124
+ function setAppDirectory(directory) {
125
+ globalThis.__reactRouterAppDirectory = directory;
126
+ }
127
+ function validateRouteConfig({
128
+ routeConfigFile,
129
+ routeConfig
130
+ }) {
131
+ if (!routeConfig) {
132
+ return {
133
+ valid: false,
134
+ message: `Route config must be the default export in "${routeConfigFile}".`
135
+ };
128
136
  }
129
- });
130
-
131
- // vite/combine-urls.ts
132
- var init_combine_urls = __esm({
133
- "vite/combine-urls.ts"() {
134
- "use strict";
137
+ if (!Array.isArray(routeConfig)) {
138
+ return {
139
+ valid: false,
140
+ message: `Route config in "${routeConfigFile}" must be an array.`
141
+ };
135
142
  }
136
- });
137
-
138
- // vite/remove-exports.ts
139
- var import_babel_dead_code_elimination;
140
- var init_remove_exports = __esm({
141
- "vite/remove-exports.ts"() {
142
- "use strict";
143
- import_babel_dead_code_elimination = require("babel-dead-code-elimination");
144
- init_babel();
143
+ let { issues } = v.safeParse(resolvedRouteConfigSchema, routeConfig);
144
+ if (issues?.length) {
145
+ let { root, nested } = v.flatten(issues);
146
+ return {
147
+ valid: false,
148
+ message: [
149
+ `Route config in "${routeConfigFile}" is invalid.`,
150
+ root ? `${root}` : [],
151
+ nested ? Object.entries(nested).map(
152
+ ([path8, message]) => `Path: routes.${path8}
153
+ ${message}`
154
+ ) : []
155
+ ].flat().join("\n\n")
156
+ };
145
157
  }
146
- });
147
-
148
- // config/routes.ts
149
- function configRoutesToRouteManifest(routes2, rootId = "root") {
158
+ return { valid: true };
159
+ }
160
+ function configRoutesToRouteManifest(appDirectory, routes2, rootId = "root") {
150
161
  let routeManifest = {};
151
162
  function walk(route, parentId) {
152
163
  let id2 = route.id || createRouteId(route.file);
153
164
  let manifestItem = {
154
165
  id: id2,
155
166
  parentId,
156
- file: route.file,
167
+ file: Path.isAbsolute(route.file) ? Path.relative(appDirectory, route.file) : route.file,
157
168
  path: route.path,
158
169
  index: route.index,
159
170
  caseSensitive: route.caseSensitive
@@ -176,19 +187,16 @@ function configRoutesToRouteManifest(routes2, rootId = "root") {
176
187
  return routeManifest;
177
188
  }
178
189
  function createRouteId(file) {
179
- return normalizeSlashes(stripFileExtension(file));
180
- }
181
- function normalizeSlashes(file) {
182
- return file.split(import_node_path.win32.sep).join("/");
190
+ return Path.normalize(stripFileExtension(file));
183
191
  }
184
192
  function stripFileExtension(file) {
185
193
  return file.replace(/\.[a-z0-9]+$/i, "");
186
194
  }
187
- var import_node_path, v, import_pick, routeConfigEntrySchema, resolvedRouteConfigSchema;
195
+ var Path, v, import_pick, routeConfigEntrySchema, resolvedRouteConfigSchema;
188
196
  var init_routes = __esm({
189
197
  "config/routes.ts"() {
190
198
  "use strict";
191
- import_node_path = require("path");
199
+ Path = __toESM(require("pathe"));
192
200
  v = __toESM(require("valibot"));
193
201
  import_pick = __toESM(require("lodash/pick"));
194
202
  init_invariant();
@@ -216,28 +224,285 @@ var init_detectPackageManager = __esm({
216
224
  }
217
225
  });
218
226
 
219
- // vite/config.ts
220
- function findEntry(dir, basename2) {
227
+ // config/config.ts
228
+ function ok(value) {
229
+ return { ok: true, value };
230
+ }
231
+ function err(error) {
232
+ return { ok: false, error };
233
+ }
234
+ async function resolveConfig({
235
+ root,
236
+ viteNodeContext,
237
+ reactRouterConfigFile
238
+ }) {
239
+ let reactRouterUserConfig = {};
240
+ if (reactRouterConfigFile) {
241
+ try {
242
+ if (!import_node_fs.default.existsSync(reactRouterConfigFile)) {
243
+ return err(`${reactRouterConfigFile} no longer exists`);
244
+ }
245
+ let configModule = await viteNodeContext.runner.executeFile(
246
+ reactRouterConfigFile
247
+ );
248
+ if (configModule.default === void 0) {
249
+ return err(`${reactRouterConfigFile} must provide a default export`);
250
+ }
251
+ if (typeof configModule.default !== "object") {
252
+ return err(`${reactRouterConfigFile} must export a config`);
253
+ }
254
+ reactRouterUserConfig = configModule.default;
255
+ } catch (error) {
256
+ return err(`Error loading ${reactRouterConfigFile}: ${error}`);
257
+ }
258
+ }
259
+ reactRouterUserConfig = deepFreeze((0, import_cloneDeep.default)(reactRouterUserConfig));
260
+ let presets = (await Promise.all(
261
+ (reactRouterUserConfig.presets ?? []).map(async (preset) => {
262
+ if (!preset.name) {
263
+ throw new Error(
264
+ "React Router presets must have a `name` property defined."
265
+ );
266
+ }
267
+ if (!preset.reactRouterConfig) {
268
+ return null;
269
+ }
270
+ let configPreset = (0, import_omit.default)(
271
+ await preset.reactRouterConfig({ reactRouterUserConfig }),
272
+ excludedConfigPresetKeys
273
+ );
274
+ return configPreset;
275
+ })
276
+ )).filter(function isNotNull(value) {
277
+ return value !== null;
278
+ });
279
+ let defaults = {
280
+ basename: "/",
281
+ buildDirectory: "build",
282
+ serverBuildFile: "index.js",
283
+ serverModuleFormat: "esm",
284
+ ssr: true
285
+ };
286
+ let {
287
+ appDirectory: userAppDirectory,
288
+ basename: basename2,
289
+ buildDirectory: userBuildDirectory,
290
+ buildEnd,
291
+ prerender,
292
+ serverBuildFile,
293
+ serverBundles,
294
+ serverModuleFormat,
295
+ ssr
296
+ } = {
297
+ ...defaults,
298
+ // Default values should be completely overridden by user/preset config, not merged
299
+ ...mergeReactRouterConfig(...presets, reactRouterUserConfig)
300
+ };
301
+ if (!ssr && serverBundles) {
302
+ serverBundles = void 0;
303
+ }
304
+ let isValidPrerenderConfig = prerender == null || typeof prerender === "boolean" || Array.isArray(prerender) || typeof prerender === "function";
305
+ if (!isValidPrerenderConfig) {
306
+ return err(
307
+ "The `prerender` config must be a boolean, an array of string paths, or a function returning a boolean or array of string paths"
308
+ );
309
+ }
310
+ let appDirectory = import_pathe.default.resolve(root, userAppDirectory || "app");
311
+ let buildDirectory = import_pathe.default.resolve(root, userBuildDirectory);
312
+ let rootRouteFile = findEntry(appDirectory, "root");
313
+ if (!rootRouteFile) {
314
+ let rootRouteDisplayPath = import_pathe.default.relative(
315
+ root,
316
+ import_pathe.default.join(appDirectory, "root.tsx")
317
+ );
318
+ return err(
319
+ `Could not find a root route module in the app directory as "${rootRouteDisplayPath}"`
320
+ );
321
+ }
322
+ let routes2 = {
323
+ root: { path: "", id: "root", file: rootRouteFile }
324
+ };
325
+ let routeConfigFile = findEntry(appDirectory, "routes");
326
+ try {
327
+ if (!routeConfigFile) {
328
+ let routeConfigDisplayPath = import_pathe.default.relative(
329
+ root,
330
+ import_pathe.default.join(appDirectory, "routes.ts")
331
+ );
332
+ return err(`Route config file not found at "${routeConfigDisplayPath}".`);
333
+ }
334
+ setAppDirectory(appDirectory);
335
+ let routeConfigExport = (await viteNodeContext.runner.executeFile(
336
+ import_pathe.default.join(appDirectory, routeConfigFile)
337
+ )).default;
338
+ let routeConfig = await routeConfigExport;
339
+ let result = validateRouteConfig({
340
+ routeConfigFile,
341
+ routeConfig
342
+ });
343
+ if (!result.valid) {
344
+ return err(result.message);
345
+ }
346
+ routes2 = {
347
+ ...routes2,
348
+ ...configRoutesToRouteManifest(appDirectory, routeConfig)
349
+ };
350
+ } catch (error) {
351
+ return err(
352
+ [
353
+ import_picocolors.default.red(`Route config in "${routeConfigFile}" is invalid.`),
354
+ "",
355
+ error.loc?.file && error.loc?.column && error.frame ? [
356
+ import_pathe.default.relative(appDirectory, error.loc.file) + ":" + error.loc.line + ":" + error.loc.column,
357
+ error.frame.trim?.()
358
+ ] : error.stack
359
+ ].flat().join("\n")
360
+ );
361
+ }
362
+ let future = {
363
+ unstable_optimizeDeps: reactRouterUserConfig.future?.unstable_optimizeDeps ?? false
364
+ };
365
+ let reactRouterConfig = deepFreeze({
366
+ appDirectory,
367
+ basename: basename2,
368
+ buildDirectory,
369
+ buildEnd,
370
+ future,
371
+ prerender,
372
+ routes: routes2,
373
+ serverBuildFile,
374
+ serverBundles,
375
+ serverModuleFormat,
376
+ ssr
377
+ });
378
+ for (let preset of reactRouterUserConfig.presets ?? []) {
379
+ await preset.reactRouterConfigResolved?.({ reactRouterConfig });
380
+ }
381
+ return ok(reactRouterConfig);
382
+ }
383
+ async function createConfigLoader({
384
+ rootDirectory: root,
385
+ watch: watch2
386
+ }) {
387
+ root = root ?? process.env.REACT_ROUTER_ROOT ?? process.cwd();
388
+ let viteNodeContext = await createContext({
389
+ root,
390
+ mode: watch2 ? "development" : "production",
391
+ server: !watch2 ? { watch: null } : {},
392
+ ssr: {
393
+ external: ssrExternals
394
+ }
395
+ });
396
+ let reactRouterConfigFile = findEntry(root, "react-router.config", {
397
+ absolute: true
398
+ });
399
+ let getConfig = () => resolveConfig({ root, viteNodeContext, reactRouterConfigFile });
400
+ let appDirectory;
401
+ let initialConfigResult = await getConfig();
402
+ if (!initialConfigResult.ok) {
403
+ throw new Error(initialConfigResult.error);
404
+ }
405
+ appDirectory = initialConfigResult.value.appDirectory;
406
+ let lastConfig = initialConfigResult.value;
407
+ let fsWatcher;
408
+ let changeHandlers = [];
409
+ return {
410
+ getConfig,
411
+ onChange: (handler) => {
412
+ if (!watch2) {
413
+ throw new Error(
414
+ "onChange is not supported when watch mode is disabled"
415
+ );
416
+ }
417
+ changeHandlers.push(handler);
418
+ if (!fsWatcher) {
419
+ fsWatcher = import_chokidar.default.watch(
420
+ [
421
+ ...reactRouterConfigFile ? [reactRouterConfigFile] : [],
422
+ appDirectory
423
+ ],
424
+ { ignoreInitial: true }
425
+ );
426
+ fsWatcher.on("all", async (...args) => {
427
+ let [event, rawFilepath] = args;
428
+ let filepath = import_pathe.default.normalize(rawFilepath);
429
+ let appFileAddedOrRemoved = appDirectory && (event === "add" || event === "unlink") && filepath.startsWith(import_pathe.default.normalize(appDirectory));
430
+ let configCodeUpdated = Boolean(
431
+ viteNodeContext.devServer?.moduleGraph.getModuleById(filepath)
432
+ );
433
+ if (configCodeUpdated || appFileAddedOrRemoved) {
434
+ viteNodeContext.devServer?.moduleGraph.invalidateAll();
435
+ viteNodeContext.runner?.moduleCache.clear();
436
+ }
437
+ if (appFileAddedOrRemoved || configCodeUpdated) {
438
+ let result = await getConfig();
439
+ let configChanged = result.ok && !(0, import_isEqual.default)(lastConfig, result.value);
440
+ let routeConfigChanged = result.ok && !(0, import_isEqual.default)(lastConfig?.routes, result.value.routes);
441
+ for (let handler2 of changeHandlers) {
442
+ handler2({
443
+ result,
444
+ configCodeUpdated,
445
+ configChanged,
446
+ routeConfigChanged,
447
+ path: filepath,
448
+ event
449
+ });
450
+ }
451
+ if (result.ok) {
452
+ lastConfig = result.value;
453
+ }
454
+ }
455
+ });
456
+ }
457
+ return () => {
458
+ changeHandlers = changeHandlers.filter(
459
+ (changeHandler) => changeHandler !== handler
460
+ );
461
+ };
462
+ },
463
+ close: async () => {
464
+ changeHandlers = [];
465
+ await viteNodeContext.devServer.close();
466
+ await fsWatcher?.close();
467
+ }
468
+ };
469
+ }
470
+ function isInReactRouterMonorepo() {
471
+ let serverRuntimePath = import_pathe.default.dirname(
472
+ require.resolve("@react-router/node/package.json")
473
+ );
474
+ let serverRuntimeParentDir = import_pathe.default.basename(
475
+ import_pathe.default.resolve(serverRuntimePath, "..")
476
+ );
477
+ return serverRuntimeParentDir === "packages";
478
+ }
479
+ function findEntry(dir, basename2, options) {
221
480
  for (let ext of entryExts) {
222
- let file = import_node_path2.default.resolve(dir, basename2 + ext);
223
- if (import_fs_extra.default.existsSync(file)) return import_node_path2.default.relative(dir, file);
481
+ let file = import_pathe.default.resolve(dir, basename2 + ext);
482
+ if (import_node_fs.default.existsSync(file)) {
483
+ return options?.absolute ?? false ? file : import_pathe.default.relative(dir, file);
484
+ }
224
485
  }
225
486
  return void 0;
226
487
  }
227
- var import_node_child_process, import_node_path2, import_fs_extra, import_picocolors, import_pick2, import_omit, import_package_json, branchRouteProperties, configRouteToBranchRoute, entryExts;
488
+ var import_node_fs, import_node_child_process, import_package_json, import_pathe, import_chokidar, import_picocolors, import_pick2, import_omit, import_cloneDeep, import_isEqual, excludedConfigPresetKeys, branchRouteProperties, configRouteToBranchRoute, mergeReactRouterConfig, deepFreeze, ssrExternals, entryExts;
228
489
  var init_config = __esm({
229
- "vite/config.ts"() {
490
+ "config/config.ts"() {
230
491
  "use strict";
492
+ import_node_fs = __toESM(require("fs"));
231
493
  import_node_child_process = require("child_process");
232
- import_node_path2 = __toESM(require("path"));
233
- import_fs_extra = __toESM(require("fs-extra"));
494
+ import_package_json = __toESM(require("@npmcli/package-json"));
495
+ init_vite_node();
496
+ import_pathe = __toESM(require("pathe"));
497
+ import_chokidar = __toESM(require("chokidar"));
234
498
  import_picocolors = __toESM(require("picocolors"));
235
499
  import_pick2 = __toESM(require("lodash/pick"));
236
500
  import_omit = __toESM(require("lodash/omit"));
237
- import_package_json = __toESM(require("@npmcli/package-json"));
501
+ import_cloneDeep = __toESM(require("lodash/cloneDeep"));
502
+ import_isEqual = __toESM(require("lodash/isEqual"));
238
503
  init_routes();
239
504
  init_detectPackageManager();
240
- init_import_vite_esm_sync();
505
+ excludedConfigPresetKeys = ["presets"];
241
506
  branchRouteProperties = [
242
507
  "id",
243
508
  "path",
@@ -245,71 +510,339 @@ var init_config = __esm({
245
510
  "index"
246
511
  ];
247
512
  configRouteToBranchRoute = (configRoute) => (0, import_pick2.default)(configRoute, branchRouteProperties);
513
+ mergeReactRouterConfig = (...configs) => {
514
+ let reducer = (configA, configB) => {
515
+ let mergeRequired = (key) => configA[key] !== void 0 && configB[key] !== void 0;
516
+ return {
517
+ ...configA,
518
+ ...configB,
519
+ ...mergeRequired("buildEnd") ? {
520
+ buildEnd: async (...args) => {
521
+ await Promise.all([
522
+ configA.buildEnd?.(...args),
523
+ configB.buildEnd?.(...args)
524
+ ]);
525
+ }
526
+ } : {},
527
+ ...mergeRequired("future") ? {
528
+ future: {
529
+ ...configA.future,
530
+ ...configB.future
531
+ }
532
+ } : {},
533
+ ...mergeRequired("presets") ? {
534
+ presets: [...configA.presets ?? [], ...configB.presets ?? []]
535
+ } : {}
536
+ };
537
+ };
538
+ return configs.reduce(reducer, {});
539
+ };
540
+ deepFreeze = (o) => {
541
+ Object.freeze(o);
542
+ let oIsFunction = typeof o === "function";
543
+ let hasOwnProp = Object.prototype.hasOwnProperty;
544
+ Object.getOwnPropertyNames(o).forEach(function(prop) {
545
+ if (hasOwnProp.call(o, prop) && (oIsFunction ? prop !== "caller" && prop !== "callee" && prop !== "arguments" : true) && o[prop] !== null && (typeof o[prop] === "object" || typeof o[prop] === "function") && !Object.isFrozen(o[prop])) {
546
+ deepFreeze(o[prop]);
547
+ }
548
+ });
549
+ return o;
550
+ };
551
+ ssrExternals = isInReactRouterMonorepo() ? [
552
+ // This is only needed within this repo because these packages
553
+ // are linked to a directory outside of node_modules so Vite
554
+ // treats them as internal code by default.
555
+ "react-router",
556
+ "react-router-dom",
557
+ "@react-router/architect",
558
+ "@react-router/cloudflare",
559
+ "@react-router/dev",
560
+ "@react-router/express",
561
+ "@react-router/node",
562
+ "@react-router/serve"
563
+ ] : void 0;
248
564
  entryExts = [".js", ".jsx", ".ts", ".tsx"];
249
565
  }
250
566
  });
251
567
 
252
- // vite/with-props.ts
253
- var import_dedent, vmodId;
254
- var init_with_props = __esm({
255
- "vite/with-props.ts"() {
568
+ // typegen/paths.ts
569
+ function getTypesDir(ctx) {
570
+ return Path2.join(ctx.rootDirectory, ".react-router/types");
571
+ }
572
+ function getTypesPath(ctx, route) {
573
+ return Path2.join(
574
+ getTypesDir(ctx),
575
+ Path2.relative(ctx.rootDirectory, ctx.config.appDirectory),
576
+ Path2.dirname(route.file),
577
+ "+types/" + Pathe.filename(route.file) + ".ts"
578
+ );
579
+ }
580
+ var Path2, Pathe;
581
+ var init_paths = __esm({
582
+ "typegen/paths.ts"() {
256
583
  "use strict";
257
- import_dedent = __toESM(require("dedent"));
258
- init_babel();
259
- init_vmod();
260
- vmodId = id("with-props");
584
+ Path2 = __toESM(require("pathe"));
585
+ Pathe = __toESM(require("pathe/utils"));
261
586
  }
262
587
  });
263
588
 
264
- // vite/vite-node.ts
265
- async function createContext(viteConfig = {}) {
266
- await preloadViteEsm();
267
- const vite2 = importViteEsmSync();
268
- const devServer = await vite2.createServer(
269
- vite2.mergeConfig(
270
- {
271
- server: {
272
- preTransformRequests: false,
273
- hmr: false
274
- },
275
- optimizeDeps: {
276
- noDiscovery: true
277
- },
278
- configFile: false,
279
- envFile: false,
280
- plugins: []
281
- },
282
- viteConfig
283
- )
284
- );
285
- await devServer.pluginContainer.buildStart({});
286
- const server = new import_server.ViteNodeServer(devServer);
287
- (0, import_source_map.installSourcemapsSupport)({
288
- getSourceMap: (source) => server.getSourceMap(source)
589
+ // typegen/generate.ts
590
+ function generate(ctx, route) {
591
+ const lineage = getRouteLineage(ctx.config.routes, route);
592
+ const urlpath = lineage.map((route2) => route2.path).join("/");
593
+ const typesPath = getTypesPath(ctx, route);
594
+ const parents = lineage.slice(0, -1);
595
+ const parentTypeImports = parents.map((parent, i) => {
596
+ const rel = Path3.relative(
597
+ Path3.dirname(typesPath),
598
+ getTypesPath(ctx, parent)
599
+ );
600
+ const indent = i === 0 ? "" : " ".repeat(2);
601
+ let source = noExtension(rel);
602
+ if (!source.startsWith("../")) source = "./" + source;
603
+ return `${indent}import type { Info as Parent${i} } from "${source}"`;
604
+ }).join("\n");
605
+ return import_dedent.default`
606
+ // React Router generated types for route:
607
+ // ${route.file}
608
+
609
+ import type * as T from "react-router/route-module"
610
+
611
+ ${parentTypeImports}
612
+
613
+ type Module = typeof import("../${Pathe2.filename(route.file)}")
614
+
615
+ export type Info = {
616
+ parents: [${parents.map((_, i) => `Parent${i}`).join(", ")}],
617
+ id: "${route.id}"
618
+ file: "${route.file}"
619
+ path: "${route.path}"
620
+ params: {${formatParamProperties(urlpath)}}
621
+ module: Module
622
+ loaderData: T.CreateLoaderData<Module>
623
+ actionData: T.CreateActionData<Module>
624
+ }
625
+
626
+ export namespace Route {
627
+ export type LinkDescriptors = T.LinkDescriptors
628
+ export type LinksFunction = () => LinkDescriptors
629
+
630
+ export type MetaArgs = T.CreateMetaArgs<Info>
631
+ export type MetaDescriptors = T.MetaDescriptors
632
+ export type MetaFunction = (args: MetaArgs) => MetaDescriptors
633
+
634
+ export type LoaderArgs = T.CreateServerLoaderArgs<Info>
635
+ export type ClientLoaderArgs = T.CreateClientLoaderArgs<Info>
636
+ export type ActionArgs = T.CreateServerActionArgs<Info>
637
+ export type ClientActionArgs = T.CreateClientActionArgs<Info>
638
+
639
+ export type HydrateFallbackProps = T.CreateHydrateFallbackProps<Info>
640
+ export type ComponentProps = T.CreateComponentProps<Info>
641
+ export type ErrorBoundaryProps = T.CreateErrorBoundaryProps<Info>
642
+ }
643
+ `;
644
+ }
645
+ function getRouteLineage(routes2, route) {
646
+ const result = [];
647
+ while (route) {
648
+ result.push(route);
649
+ if (!route.parentId) break;
650
+ route = routes2[route.parentId];
651
+ }
652
+ result.reverse();
653
+ return result;
654
+ }
655
+ function formatParamProperties(urlpath) {
656
+ const params = parseParams(urlpath);
657
+ const properties = Object.entries(params).map(([name, values]) => {
658
+ if (values.length === 1) {
659
+ const isOptional = values[0];
660
+ return isOptional ? `"${name}"?: string` : `"${name}": string`;
661
+ }
662
+ const items = values.map(
663
+ (isOptional) => isOptional ? "string | undefined" : "string"
664
+ );
665
+ return `"${name}": [${items.join(", ")}]`;
289
666
  });
290
- const runner = new import_client.ViteNodeRunner({
291
- root: devServer.config.root,
292
- base: devServer.config.base,
293
- fetchModule(id2) {
294
- return server.fetchModule(id2);
295
- },
296
- resolveId(id2, importer) {
297
- return server.resolveId(id2, importer);
667
+ return properties.join("; ");
668
+ }
669
+ function parseParams(urlpath) {
670
+ const result = {};
671
+ let segments = urlpath.split("/");
672
+ segments.forEach((segment) => {
673
+ const match = segment.match(/^:([\w-]+)(\?)?/);
674
+ if (!match) return;
675
+ const param = match[1];
676
+ const isOptional = match[2] !== void 0;
677
+ result[param] ??= [];
678
+ result[param].push(isOptional);
679
+ return;
680
+ });
681
+ const hasSplat = segments.at(-1) === "*";
682
+ if (hasSplat) result["*"] = [false];
683
+ return result;
684
+ }
685
+ var import_dedent, Path3, Pathe2, noExtension;
686
+ var init_generate = __esm({
687
+ "typegen/generate.ts"() {
688
+ "use strict";
689
+ import_dedent = __toESM(require("dedent"));
690
+ Path3 = __toESM(require("pathe"));
691
+ Pathe2 = __toESM(require("pathe/utils"));
692
+ init_paths();
693
+ noExtension = (path8) => Path3.join(Path3.dirname(path8), Pathe2.filename(path8));
694
+ }
695
+ });
696
+
697
+ // typegen/index.ts
698
+ async function run(rootDirectory) {
699
+ const ctx = await createContext2({ rootDirectory, watch: false });
700
+ await writeAll(ctx);
701
+ }
702
+ async function watch(rootDirectory, { logger } = {}) {
703
+ const ctx = await createContext2({ rootDirectory, watch: true });
704
+ await writeAll(ctx);
705
+ logger?.info(import_picocolors2.default.green("generated types"), { timestamp: true, clear: true });
706
+ ctx.configLoader.onChange(async ({ result, routeConfigChanged }) => {
707
+ if (!result.ok) {
708
+ logger?.error(import_picocolors2.default.red(result.error), { timestamp: true, clear: true });
709
+ return;
710
+ }
711
+ ctx.config = result.value;
712
+ if (routeConfigChanged) {
713
+ await writeAll(ctx);
714
+ logger?.info(import_picocolors2.default.green("regenerated types"), {
715
+ timestamp: true,
716
+ clear: true
717
+ });
298
718
  }
299
719
  });
300
- return { devServer, server, runner };
301
720
  }
302
- var import_server, import_client, import_source_map;
303
- var init_vite_node = __esm({
304
- "vite/vite-node.ts"() {
721
+ async function createContext2({
722
+ rootDirectory,
723
+ watch: watch2
724
+ }) {
725
+ const configLoader = await createConfigLoader({ rootDirectory, watch: watch2 });
726
+ const configResult = await configLoader.getConfig();
727
+ if (!configResult.ok) {
728
+ throw new Error(configResult.error);
729
+ }
730
+ const config = configResult.value;
731
+ return {
732
+ configLoader,
733
+ rootDirectory,
734
+ config
735
+ };
736
+ }
737
+ async function writeAll(ctx) {
738
+ const typegenDir = getTypesDir(ctx);
739
+ import_node_fs2.default.rmSync(typegenDir, { recursive: true, force: true });
740
+ Object.values(ctx.config.routes).forEach((route) => {
741
+ const typesPath = getTypesPath(ctx, route);
742
+ const content = generate(ctx, route);
743
+ import_node_fs2.default.mkdirSync(Path4.dirname(typesPath), { recursive: true });
744
+ import_node_fs2.default.writeFileSync(typesPath, content);
745
+ });
746
+ }
747
+ var import_node_fs2, Path4, import_picocolors2;
748
+ var init_typegen = __esm({
749
+ "typegen/index.ts"() {
305
750
  "use strict";
306
- import_server = require("vite-node/server");
307
- import_client = require("vite-node/client");
308
- import_source_map = require("vite-node/source-map");
751
+ import_node_fs2 = __toESM(require("fs"));
752
+ Path4 = __toESM(require("pathe"));
753
+ import_picocolors2 = __toESM(require("picocolors"));
754
+ init_config();
755
+ init_generate();
756
+ init_paths();
757
+ }
758
+ });
759
+
760
+ // vite/babel.ts
761
+ var import_parser, t, traverse, generate2;
762
+ var init_babel = __esm({
763
+ "vite/babel.ts"() {
764
+ "use strict";
765
+ import_parser = require("@babel/parser");
766
+ t = __toESM(require("@babel/types"));
767
+ traverse = require("@babel/traverse").default;
768
+ generate2 = require("@babel/generator").default;
769
+ }
770
+ });
771
+
772
+ // vite/node-adapter.ts
773
+ var import_node_events, import_node_stream, import_set_cookie_parser, import_node;
774
+ var init_node_adapter = __esm({
775
+ "vite/node-adapter.ts"() {
776
+ "use strict";
777
+ import_node_events = require("events");
778
+ import_node_stream = require("stream");
779
+ import_set_cookie_parser = require("set-cookie-parser");
780
+ import_node = require("@react-router/node");
781
+ init_invariant();
782
+ }
783
+ });
784
+
785
+ // vite/resolve-file-url.ts
786
+ var path2;
787
+ var init_resolve_file_url = __esm({
788
+ "vite/resolve-file-url.ts"() {
789
+ "use strict";
790
+ path2 = __toESM(require("path"));
309
791
  init_import_vite_esm_sync();
310
792
  }
311
793
  });
312
794
 
795
+ // vite/styles.ts
796
+ var path3, import_react_router, cssFileRegExp, cssModulesRegExp;
797
+ var init_styles = __esm({
798
+ "vite/styles.ts"() {
799
+ "use strict";
800
+ path3 = __toESM(require("path"));
801
+ import_react_router = require("react-router");
802
+ init_resolve_file_url();
803
+ cssFileRegExp = /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/;
804
+ cssModulesRegExp = new RegExp(`\\.module${cssFileRegExp.source}`);
805
+ }
806
+ });
807
+
808
+ // vite/vmod.ts
809
+ var id;
810
+ var init_vmod = __esm({
811
+ "vite/vmod.ts"() {
812
+ "use strict";
813
+ id = (name) => `virtual:react-router/${name}`;
814
+ }
815
+ });
816
+
817
+ // vite/combine-urls.ts
818
+ var init_combine_urls = __esm({
819
+ "vite/combine-urls.ts"() {
820
+ "use strict";
821
+ }
822
+ });
823
+
824
+ // vite/remove-exports.ts
825
+ var import_babel_dead_code_elimination;
826
+ var init_remove_exports = __esm({
827
+ "vite/remove-exports.ts"() {
828
+ "use strict";
829
+ import_babel_dead_code_elimination = require("babel-dead-code-elimination");
830
+ init_babel();
831
+ }
832
+ });
833
+
834
+ // vite/with-props.ts
835
+ var import_dedent2, vmodId;
836
+ var init_with_props = __esm({
837
+ "vite/with-props.ts"() {
838
+ "use strict";
839
+ import_dedent2 = __toESM(require("dedent"));
840
+ init_babel();
841
+ init_vmod();
842
+ vmodId = id("with-props");
843
+ }
844
+ });
845
+
313
846
  // vite/plugin.ts
314
847
  async function resolveViteConfig({
315
848
  configFile,
@@ -350,14 +883,14 @@ async function loadPluginContext({
350
883
  ".mjs"
351
884
  ]);
352
885
  if (!configFile) {
353
- console.error(import_picocolors2.default.red("Vite config file not found"));
886
+ console.error(import_picocolors3.default.red("Vite config file not found"));
354
887
  process.exit(1);
355
888
  }
356
889
  let viteConfig = await resolveViteConfig({ configFile, root });
357
890
  let ctx = await extractPluginContext(viteConfig);
358
891
  if (!ctx) {
359
892
  console.error(
360
- import_picocolors2.default.red("React Router Vite plugin not found in Vite config")
893
+ import_picocolors3.default.red("React Router Vite plugin not found in Vite config")
361
894
  );
362
895
  process.exit(1);
363
896
  }
@@ -367,23 +900,24 @@ function findConfig(dir, basename2, extensions) {
367
900
  for (let ext of extensions) {
368
901
  let name = basename2 + ext;
369
902
  let file = path4.join(dir, name);
370
- if (fse2.existsSync(file)) return file;
903
+ if (fse.existsSync(file)) return file;
371
904
  }
372
905
  return void 0;
373
906
  }
374
- var import_node_crypto, path4, url, fse2, babel, import_react_router2, import_es_module_lexer, import_jsesc, import_picocolors2, serverBuildId, serverManifestId, browserManifestId, hmrRuntimeId, injectHmrRuntimeId, getServerBuildDirectory, defaultEntriesDir, defaultEntries, REACT_REFRESH_HEADER, REACT_REFRESH_FOOTER;
907
+ var import_node_crypto, path4, url, fse, babel, import_react_router2, import_es_module_lexer, import_jsesc, import_picocolors3, serverBuildId, serverManifestId, browserManifestId, hmrRuntimeId, injectHmrRuntimeId, getServerBuildDirectory, defaultEntriesDir, defaultEntries, REACT_REFRESH_HEADER, REACT_REFRESH_FOOTER;
375
908
  var init_plugin = __esm({
376
909
  "vite/plugin.ts"() {
377
910
  "use strict";
378
911
  import_node_crypto = require("crypto");
379
912
  path4 = __toESM(require("path"));
380
913
  url = __toESM(require("url"));
381
- fse2 = __toESM(require("fs-extra"));
914
+ fse = __toESM(require("fs-extra"));
382
915
  babel = __toESM(require("@babel/core"));
383
916
  import_react_router2 = require("react-router");
384
917
  import_es_module_lexer = require("es-module-lexer");
385
918
  import_jsesc = __toESM(require("jsesc"));
386
- import_picocolors2 = __toESM(require("picocolors"));
919
+ import_picocolors3 = __toESM(require("picocolors"));
920
+ init_typegen();
387
921
  init_invariant();
388
922
  init_babel();
389
923
  init_node_adapter();
@@ -395,7 +929,6 @@ var init_plugin = __esm({
395
929
  init_import_vite_esm_sync();
396
930
  init_config();
397
931
  init_with_props();
398
- init_vite_node();
399
932
  serverBuildId = id("server-build");
400
933
  serverManifestId = id("server-manifest");
401
934
  browserManifestId = id("browser-manifest");
@@ -412,7 +945,7 @@ var init_plugin = __esm({
412
945
  "config",
413
946
  "defaults"
414
947
  );
415
- defaultEntries = fse2.readdirSync(defaultEntriesDir).map((filename2) => path4.join(defaultEntriesDir, filename2));
948
+ defaultEntries = fse.readdirSync(defaultEntriesDir).map((filename3) => path4.join(defaultEntriesDir, filename3));
416
949
  invariant(defaultEntries.length > 0, "No default entries found");
417
950
  REACT_REFRESH_HEADER = `
418
951
  import RefreshRuntime from "${hmrRuntimeId}";
@@ -453,13 +986,13 @@ if (import.meta.hot && !inWebWorker) {
453
986
  });
454
987
 
455
988
  // vite/profiler.ts
456
- var import_node_fs, import_node_path3, import_picocolors3, getSession, start, profileCount, stop;
989
+ var import_node_fs3, import_node_path, import_picocolors4, getSession, start, profileCount, stop;
457
990
  var init_profiler = __esm({
458
991
  "vite/profiler.ts"() {
459
992
  "use strict";
460
- import_node_fs = __toESM(require("fs"));
461
- import_node_path3 = __toESM(require("path"));
462
- import_picocolors3 = __toESM(require("picocolors"));
993
+ import_node_fs3 = __toESM(require("fs"));
994
+ import_node_path = __toESM(require("path"));
995
+ import_picocolors4 = __toESM(require("picocolors"));
463
996
  getSession = () => global.__reactRouter_profile_session;
464
997
  start = async (callback) => {
465
998
  let inspector = await import("inspector").then((r) => r.default);
@@ -470,17 +1003,17 @@ var init_profiler = __esm({
470
1003
  });
471
1004
  };
472
1005
  profileCount = 0;
473
- stop = (log2) => {
1006
+ stop = (log) => {
474
1007
  let session = getSession();
475
1008
  if (!session) return;
476
1009
  return new Promise((res, rej) => {
477
- session.post("Profiler.stop", (err, { profile }) => {
478
- if (err) return rej(err);
479
- let outPath = import_node_path3.default.resolve(`./react-router-${profileCount++}.cpuprofile`);
480
- import_node_fs.default.writeFileSync(outPath, JSON.stringify(profile));
481
- log2(
482
- import_picocolors3.default.yellow(
483
- `CPU profile written to ${import_picocolors3.default.white(import_picocolors3.default.dim(outPath))}`
1010
+ session.post("Profiler.stop", (err2, { profile }) => {
1011
+ if (err2) return rej(err2);
1012
+ let outPath = import_node_path.default.resolve(`./react-router-${profileCount++}.cpuprofile`);
1013
+ import_node_fs3.default.writeFileSync(outPath, JSON.stringify(profile));
1014
+ log(
1015
+ import_picocolors4.default.yellow(
1016
+ `CPU profile written to ${import_picocolors4.default.white(import_picocolors4.default.dim(outPath))}`
484
1017
  )
485
1018
  );
486
1019
  global.__reactRouter_profile_session = void 0;
@@ -537,12 +1070,12 @@ async function getServerBuilds(ctx) {
537
1070
  };
538
1071
  }
539
1072
  let { normalizePath } = await import("vite");
540
- let resolvedAppDirectory = import_node_path4.default.resolve(rootDirectory, appDirectory);
1073
+ let resolvedAppDirectory = import_node_path2.default.resolve(rootDirectory, appDirectory);
541
1074
  let rootRelativeRoutes = Object.fromEntries(
542
1075
  Object.entries(routes2).map(([id2, route]) => {
543
- let filePath = import_node_path4.default.join(resolvedAppDirectory, route.file);
1076
+ let filePath = import_node_path2.default.join(resolvedAppDirectory, route.file);
544
1077
  let rootRelativeFilePath = normalizePath(
545
- import_node_path4.default.relative(rootDirectory, filePath)
1078
+ import_node_path2.default.relative(rootDirectory, filePath)
546
1079
  );
547
1080
  return [id2, { ...route, file: rootRelativeFilePath }];
548
1081
  })
@@ -561,7 +1094,7 @@ async function getServerBuilds(ctx) {
561
1094
  (route2) => configRouteToBranchRoute({
562
1095
  ...route2,
563
1096
  // Ensure absolute paths are passed to the serverBundles function
564
- file: import_node_path4.default.join(resolvedAppDirectory, route2.file)
1097
+ file: import_node_path2.default.join(resolvedAppDirectory, route2.file)
565
1098
  })
566
1099
  )
567
1100
  });
@@ -574,16 +1107,16 @@ async function getServerBuilds(ctx) {
574
1107
  );
575
1108
  }
576
1109
  buildManifest.routeIdToServerBundleId[route.id] = serverBundleId;
577
- let relativeServerBundleDirectory = import_node_path4.default.relative(
1110
+ let relativeServerBundleDirectory = import_node_path2.default.relative(
578
1111
  rootDirectory,
579
- import_node_path4.default.join(serverBuildDirectory, serverBundleId)
1112
+ import_node_path2.default.join(serverBuildDirectory, serverBundleId)
580
1113
  );
581
1114
  let serverBuildConfig = serverBundleBuildConfigById.get(serverBundleId);
582
1115
  if (!serverBuildConfig) {
583
1116
  buildManifest.serverBundles[serverBundleId] = {
584
1117
  id: serverBundleId,
585
1118
  file: normalizePath(
586
- import_node_path4.default.join(relativeServerBundleDirectory, serverBuildFile)
1119
+ import_node_path2.default.join(relativeServerBundleDirectory, serverBuildFile)
587
1120
  )
588
1121
  };
589
1122
  serverBuildConfig = {
@@ -614,15 +1147,15 @@ async function getServerBuilds(ctx) {
614
1147
  async function cleanBuildDirectory(viteConfig, ctx) {
615
1148
  let buildDirectory = ctx.reactRouterConfig.buildDirectory;
616
1149
  let isWithinRoot = () => {
617
- let relativePath = import_node_path4.default.relative(ctx.rootDirectory, buildDirectory);
618
- return !relativePath.startsWith("..") && !import_node_path4.default.isAbsolute(relativePath);
1150
+ let relativePath = import_node_path2.default.relative(ctx.rootDirectory, buildDirectory);
1151
+ return !relativePath.startsWith("..") && !import_node_path2.default.isAbsolute(relativePath);
619
1152
  };
620
1153
  if (viteConfig.build.emptyOutDir ?? isWithinRoot()) {
621
- await import_fs_extra2.default.remove(buildDirectory);
1154
+ await import_fs_extra.default.remove(buildDirectory);
622
1155
  }
623
1156
  }
624
1157
  function getViteManifestPaths(ctx, serverBuilds) {
625
- let buildRelative = (pathname) => import_node_path4.default.resolve(ctx.reactRouterConfig.buildDirectory, pathname);
1158
+ let buildRelative = (pathname) => import_node_path2.default.resolve(ctx.reactRouterConfig.buildDirectory, pathname);
626
1159
  let viteManifestPaths = [
627
1160
  "client/.vite/manifest.json",
628
1161
  ...serverBuilds.map(({ serverBundleBuildConfig }) => {
@@ -650,7 +1183,7 @@ async function build(root, {
650
1183
  const ctx = await extractPluginContext(viteConfig);
651
1184
  if (!ctx) {
652
1185
  console.error(
653
- import_picocolors6.default.red("React Router Vite plugin not found in Vite config")
1186
+ import_picocolors5.default.red("React Router Vite plugin not found in Vite config")
654
1187
  );
655
1188
  process.exit(1);
656
1189
  }
@@ -684,15 +1217,15 @@ async function build(root, {
684
1217
  let viteManifestPaths = getViteManifestPaths(ctx, serverBuilds);
685
1218
  await Promise.all(
686
1219
  viteManifestPaths.map(async (viteManifestPath) => {
687
- let manifestExists = await import_fs_extra2.default.pathExists(viteManifestPath);
1220
+ let manifestExists = await import_fs_extra.default.pathExists(viteManifestPath);
688
1221
  if (!manifestExists) return;
689
1222
  if (!ctx.viteManifestEnabled) {
690
- await import_fs_extra2.default.remove(viteManifestPath);
1223
+ await import_fs_extra.default.remove(viteManifestPath);
691
1224
  }
692
- let viteDir = import_node_path4.default.dirname(viteManifestPath);
693
- let viteDirFiles = await import_fs_extra2.default.readdir(viteDir);
1225
+ let viteDir = import_node_path2.default.dirname(viteManifestPath);
1226
+ let viteDirFiles = await import_fs_extra.default.readdir(viteDir);
694
1227
  if (viteDirFiles.length === 0) {
695
- await import_fs_extra2.default.remove(viteDir);
1228
+ await import_fs_extra.default.remove(viteDir);
696
1229
  }
697
1230
  })
698
1231
  );
@@ -702,13 +1235,13 @@ async function build(root, {
702
1235
  viteConfig
703
1236
  });
704
1237
  }
705
- var import_node_path4, import_fs_extra2, import_picocolors6;
1238
+ var import_node_path2, import_fs_extra, import_picocolors5;
706
1239
  var init_build = __esm({
707
1240
  "vite/build.ts"() {
708
1241
  "use strict";
709
- import_node_path4 = __toESM(require("path"));
710
- import_fs_extra2 = __toESM(require("fs-extra"));
711
- import_picocolors6 = __toESM(require("picocolors"));
1242
+ import_node_path2 = __toESM(require("path"));
1243
+ import_fs_extra = __toESM(require("fs-extra"));
1244
+ import_picocolors5 = __toESM(require("picocolors"));
712
1245
  init_plugin();
713
1246
  init_config();
714
1247
  init_invariant();
@@ -746,7 +1279,7 @@ async function dev(root, {
746
1279
  });
747
1280
  if (!server.config.plugins.find((plugin2) => plugin2.name === "react-router")) {
748
1281
  console.error(
749
- import_picocolors7.default.red("React Router Vite plugin not found in Vite config")
1282
+ import_picocolors6.default.red("React Router Vite plugin not found in Vite config")
750
1283
  );
751
1284
  process.exit(1);
752
1285
  }
@@ -769,11 +1302,11 @@ async function dev(root, {
769
1302
  ];
770
1303
  server.bindCLIShortcuts({ print: true, customShortcuts });
771
1304
  }
772
- var import_picocolors7;
1305
+ var import_picocolors6;
773
1306
  var init_dev = __esm({
774
1307
  "vite/dev.ts"() {
775
1308
  "use strict";
776
- import_picocolors7 = __toESM(require("picocolors"));
1309
+ import_picocolors6 = __toESM(require("picocolors"));
777
1310
  init_import_vite_esm_sync();
778
1311
  init_profiler();
779
1312
  }
@@ -782,14 +1315,14 @@ var init_dev = __esm({
782
1315
  // cli/run.ts
783
1316
  var import_arg = __toESM(require("arg"));
784
1317
  var import_semver = __toESM(require("semver"));
785
- var import_picocolors9 = __toESM(require("picocolors"));
1318
+ var import_picocolors8 = __toESM(require("picocolors"));
786
1319
 
787
1320
  // cli/commands.ts
788
1321
  var path7 = __toESM(require("path"));
789
- var import_fs_extra3 = __toESM(require("fs-extra"));
1322
+ var import_fs_extra2 = __toESM(require("fs-extra"));
790
1323
  var import_package_json2 = __toESM(require("@npmcli/package-json"));
791
1324
  var import_exit_hook = __toESM(require("exit-hook"));
792
- var import_picocolors8 = __toESM(require("picocolors"));
1325
+ var import_picocolors7 = __toESM(require("picocolors"));
793
1326
 
794
1327
  // config/format.ts
795
1328
  function formatRoutes(routeManifest, format) {
@@ -870,232 +1403,8 @@ function transpile(tsx, options = {}) {
870
1403
 
871
1404
  // cli/commands.ts
872
1405
  init_profiler();
873
-
874
- // typegen.ts
875
- var import_node_fs2 = __toESM(require("fs"));
876
- var import_chokidar = __toESM(require("chokidar"));
877
- var import_dedent2 = __toESM(require("dedent"));
878
- var Path = __toESM(require("pathe"));
879
- var Pathe = __toESM(require("pathe/utils"));
880
- var import_picocolors5 = __toESM(require("picocolors"));
881
-
882
- // logger.ts
883
- var import_picocolors4 = __toESM(require("picocolors"));
884
- var DATE_TIME_FORMAT = new Intl.DateTimeFormat([], {
885
- hour: "2-digit",
886
- minute: "2-digit",
887
- second: "2-digit",
888
- hour12: false
889
- });
890
- var log = (level) => (body, options = {}) => {
891
- const dest = level === "error" ? process.stderr : process.stdout;
892
- let message = getPrefix(level, options) + " " + body;
893
- if (options.durationMs) {
894
- const duration = options.durationMs < 1e3 ? `${Math.round(options.durationMs)}ms` : `${(options.durationMs / 1e3).toFixed(2)}s`;
895
- message += " " + import_picocolors4.default.dim(duration);
896
- }
897
- dest.write(message + "\n");
898
- };
899
- function getPrefix(level, options) {
900
- const timestamp = `${DATE_TIME_FORMAT.format(/* @__PURE__ */ new Date())}`;
901
- const prefix = [];
902
- if (level === "error" || level === "warn") {
903
- prefix.push(import_picocolors4.default.bold(timestamp));
904
- prefix.push(`[${level.toUpperCase()}]`);
905
- } else {
906
- prefix.push(timestamp);
907
- }
908
- if (options.label) {
909
- prefix.push(`[${options.label}]`);
910
- }
911
- if (level === "error") return import_picocolors4.default.red(prefix.join(" "));
912
- if (level === "warn") return import_picocolors4.default.yellow(prefix.join(" "));
913
- if (prefix.length === 1) return import_picocolors4.default.dim(prefix[0]);
914
- return import_picocolors4.default.dim(prefix[0]) + " " + import_picocolors4.default.blue(prefix.splice(1).join(" "));
915
- }
916
- var info = log("info");
917
- var warn = log("warn");
918
- var error = log("error");
919
-
920
- // typegen.ts
921
- init_routes();
922
- init_vite_node();
923
- init_config();
924
- init_plugin();
925
- function getDirectory(ctx) {
926
- return Path.join(ctx.rootDirectory, ".react-router/types");
927
- }
928
- async function watch(rootDirectory, options = {}) {
929
- const watchStart = performance.now();
930
- const vitePluginCtx = await loadPluginContext({
931
- root: rootDirectory,
932
- configFile: options.configFile
933
- });
934
- const routeConfigFile = findEntry(
935
- vitePluginCtx.reactRouterConfig.appDirectory,
936
- "routes"
937
- );
938
- if (!routeConfigFile) {
939
- warn(
940
- `Could not find route config within ${import_picocolors5.default.blue(
941
- Path.relative(
942
- vitePluginCtx.rootDirectory,
943
- vitePluginCtx.reactRouterConfig.appDirectory
944
- )
945
- )}`
946
- );
947
- process.exit(1);
948
- }
949
- const routeConfigPath = Path.join(
950
- vitePluginCtx.reactRouterConfig.appDirectory,
951
- routeConfigFile
952
- );
953
- const routesViteNodeContext = await createContext({
954
- root: rootDirectory
955
- });
956
- async function getRoutes() {
957
- const routes2 = {};
958
- const rootRouteFile = findEntry(
959
- vitePluginCtx.reactRouterConfig.appDirectory,
960
- "root"
961
- );
962
- if (rootRouteFile) {
963
- routes2.root = { path: "", id: "root", file: rootRouteFile };
964
- } else {
965
- warn(`Could not find \`root\` route`);
966
- }
967
- routesViteNodeContext.devServer.moduleGraph.invalidateAll();
968
- routesViteNodeContext.runner.moduleCache.clear();
969
- const routeConfig = (await routesViteNodeContext.runner.executeFile(routeConfigPath)).routes;
970
- return {
971
- ...routes2,
972
- ...configRoutesToRouteManifest(await routeConfig)
973
- };
974
- }
975
- const ctx = {
976
- rootDirectory,
977
- appDirectory: vitePluginCtx.reactRouterConfig.appDirectory,
978
- routes: await getRoutes()
979
- };
980
- await writeAll(ctx);
981
- info("generated initial types", {
982
- durationMs: performance.now() - watchStart
983
- });
984
- const watcher = import_chokidar.default.watch(ctx.appDirectory, { ignoreInitial: true });
985
- watcher.on("all", async (event, path8) => {
986
- const eventStart = performance.now();
987
- path8 = Path.normalize(path8);
988
- ctx.routes = await getRoutes();
989
- const routeConfigChanged = Boolean(
990
- routesViteNodeContext.devServer.moduleGraph.getModuleById(path8)
991
- );
992
- if (routeConfigChanged) {
993
- await writeAll(ctx);
994
- info("changed route config", {
995
- durationMs: performance.now() - eventStart
996
- });
997
- return;
998
- }
999
- const route = Object.values(ctx.routes).find(
1000
- (route2) => path8 === Path.join(ctx.appDirectory, route2.file)
1001
- );
1002
- if (route && (event === "add" || event === "unlink")) {
1003
- await writeAll(ctx);
1004
- info(
1005
- `${event === "add" ? "added" : "removed"} route ${import_picocolors5.default.blue(route.file)}`,
1006
- { durationMs: performance.now() - eventStart }
1007
- );
1008
- return;
1009
- }
1010
- });
1011
- }
1012
- async function writeAll(ctx) {
1013
- import_node_fs2.default.rmSync(getDirectory(ctx), { recursive: true, force: true });
1014
- Object.values(ctx.routes).forEach((route) => {
1015
- if (!import_node_fs2.default.existsSync(Path.join(ctx.appDirectory, route.file))) return;
1016
- const typesPath = Path.join(
1017
- getDirectory(ctx),
1018
- Path.relative(ctx.rootDirectory, ctx.appDirectory),
1019
- Path.dirname(route.file),
1020
- "+types." + Pathe.filename(route.file) + ".d.ts"
1021
- );
1022
- const content = getModule(ctx.routes, route);
1023
- import_node_fs2.default.mkdirSync(Path.dirname(typesPath), { recursive: true });
1024
- import_node_fs2.default.writeFileSync(typesPath, content);
1025
- });
1026
- }
1027
- function getModule(routes2, route) {
1028
- return import_dedent2.default`
1029
- // React Router generated types for route:
1030
- // ${route.file}
1031
-
1032
- import * as T from "react-router/types"
1033
-
1034
- export type Params = {${formattedParamsProperties(routes2, route)}}
1035
-
1036
- type RouteModule = typeof import("./${Pathe.filename(route.file)}")
1037
-
1038
- export namespace Route {
1039
- export type LoaderData = T.CreateLoaderData<RouteModule>
1040
- export type ActionData = T.CreateActionData<RouteModule>
1041
-
1042
- export type LoaderArgs = T.CreateServerLoaderArgs<Params>
1043
- export type ClientLoaderArgs = T.CreateClientLoaderArgs<Params, RouteModule>
1044
- export type ActionArgs = T.CreateServerActionArgs<Params>
1045
- export type ClientActionArgs = T.CreateClientActionArgs<Params, RouteModule>
1046
-
1047
- export type HydrateFallbackProps = T.CreateHydrateFallbackProps<Params>
1048
- export type ComponentProps = T.CreateComponentProps<Params, LoaderData, ActionData>
1049
- export type ErrorBoundaryProps = T.CreateErrorBoundaryProps<Params, LoaderData, ActionData>
1050
- }
1051
- `;
1052
- }
1053
- function formattedParamsProperties(routes2, route) {
1054
- const urlpath = routeLineage(routes2, route).map((route2) => route2.path).join("/");
1055
- const params = parseParams(urlpath);
1056
- const indent = " ".repeat(3);
1057
- const properties = Object.entries(params).map(([name, values]) => {
1058
- if (values.length === 1) {
1059
- const isOptional = values[0];
1060
- return indent + (isOptional ? `"${name}"?: string` : `"${name}": string`);
1061
- }
1062
- const items = values.map(
1063
- (isOptional) => isOptional ? "string | undefined" : "string"
1064
- );
1065
- return indent + `"${name}": [${items.join(", ")}]`;
1066
- });
1067
- const body = properties.length === 0 ? "" : "\n" + properties.join("\n") + "\n";
1068
- return body;
1069
- }
1070
- function routeLineage(routes2, route) {
1071
- const result = [];
1072
- while (route) {
1073
- result.push(route);
1074
- if (!route.parentId) break;
1075
- route = routes2[route.parentId];
1076
- }
1077
- result.reverse();
1078
- return result;
1079
- }
1080
- function parseParams(urlpath) {
1081
- const result = {};
1082
- let segments = urlpath.split("/");
1083
- segments.filter((s) => s.startsWith(":")).forEach((param) => {
1084
- param = param.slice(1);
1085
- let isOptional = param.endsWith("?");
1086
- if (isOptional) {
1087
- param = param.slice(0, -1);
1088
- }
1089
- result[param] ??= [];
1090
- result[param].push(isOptional);
1091
- return;
1092
- });
1093
- const hasSplat = segments.at(-1) === "*";
1094
- if (hasSplat) result["*"] = [false];
1095
- return result;
1096
- }
1097
-
1098
- // cli/commands.ts
1406
+ init_typegen();
1407
+ init_import_vite_esm_sync();
1099
1408
  async function routes(reactRouterRoot, flags = {}) {
1100
1409
  let ctx = await loadPluginContext({
1101
1410
  root: reactRouterRoot,
@@ -1103,7 +1412,7 @@ async function routes(reactRouterRoot, flags = {}) {
1103
1412
  });
1104
1413
  if (!ctx) {
1105
1414
  console.error(
1106
- import_picocolors8.default.red("React Router Vite plugin not found in Vite config")
1415
+ import_picocolors7.default.red("React Router Vite plugin not found in Vite config")
1107
1416
  );
1108
1417
  process.exit(1);
1109
1418
  }
@@ -1157,14 +1466,14 @@ async function generateEntry(entry, reactRouterRoot, flags = {}) {
1157
1466
  let entriesArray = Array.from(entries);
1158
1467
  let list = conjunctionListFormat.format(entriesArray);
1159
1468
  console.error(
1160
- import_picocolors8.default.red(`Invalid entry file. Valid entry files are ${list}`)
1469
+ import_picocolors7.default.red(`Invalid entry file. Valid entry files are ${list}`)
1161
1470
  );
1162
1471
  return;
1163
1472
  }
1164
1473
  let pkgJson = await import_package_json2.default.load(rootDirectory);
1165
1474
  let deps = pkgJson.content.dependencies ?? {};
1166
1475
  if (!deps["@react-router/node"]) {
1167
- console.error(import_picocolors8.default.red(`No default server entry detected.`));
1476
+ console.error(import_picocolors7.default.red(`No default server entry detected.`));
1168
1477
  return;
1169
1478
  }
1170
1479
  let defaultsDirectory = path7.resolve(
@@ -1189,12 +1498,12 @@ async function generateEntry(entry, reactRouterRoot, flags = {}) {
1189
1498
  cwd: rootDirectory,
1190
1499
  filename: isServerEntry ? defaultEntryServer : defaultEntryClient
1191
1500
  });
1192
- await import_fs_extra3.default.writeFile(outputFile2, javascript, "utf-8");
1501
+ await import_fs_extra2.default.writeFile(outputFile2, javascript, "utf-8");
1193
1502
  } else {
1194
- await import_fs_extra3.default.writeFile(outputFile2, contents, "utf-8");
1503
+ await import_fs_extra2.default.writeFile(outputFile2, contents, "utf-8");
1195
1504
  }
1196
1505
  console.log(
1197
- import_picocolors8.default.blue(
1506
+ import_picocolors7.default.blue(
1198
1507
  `Entry file ${entry} created at ${path7.relative(
1199
1508
  rootDirectory,
1200
1509
  outputFile2
@@ -1205,50 +1514,48 @@ async function generateEntry(entry, reactRouterRoot, flags = {}) {
1205
1514
  async function checkForEntry(rootDirectory, appDirectory, entries2) {
1206
1515
  for (let entry of entries2) {
1207
1516
  let entryPath = path7.resolve(appDirectory, entry);
1208
- let exists = await import_fs_extra3.default.pathExists(entryPath);
1517
+ let exists = await import_fs_extra2.default.pathExists(entryPath);
1209
1518
  if (exists) {
1210
- let relative6 = path7.relative(rootDirectory, entryPath);
1211
- console.error(import_picocolors8.default.red(`Entry file ${relative6} already exists.`));
1519
+ let relative8 = path7.relative(rootDirectory, entryPath);
1520
+ console.error(import_picocolors7.default.red(`Entry file ${relative8} already exists.`));
1212
1521
  return process.exit(1);
1213
1522
  }
1214
1523
  }
1215
1524
  }
1216
1525
  async function createServerEntry(rootDirectory, appDirectory, inputFile) {
1217
1526
  await checkForEntry(rootDirectory, appDirectory, serverEntries);
1218
- let contents = await import_fs_extra3.default.readFile(inputFile, "utf-8");
1527
+ let contents = await import_fs_extra2.default.readFile(inputFile, "utf-8");
1219
1528
  return contents;
1220
1529
  }
1221
1530
  async function createClientEntry(rootDirectory, appDirectory, inputFile) {
1222
1531
  await checkForEntry(rootDirectory, appDirectory, clientEntries);
1223
- let contents = await import_fs_extra3.default.readFile(inputFile, "utf-8");
1532
+ let contents = await import_fs_extra2.default.readFile(inputFile, "utf-8");
1224
1533
  return contents;
1225
1534
  }
1226
1535
  async function typegen(root, flags) {
1227
1536
  root ??= process.cwd();
1228
1537
  if (flags.watch) {
1229
- await watch(root, { configFile: flags.config });
1538
+ await preloadViteEsm();
1539
+ const vite2 = importViteEsmSync();
1540
+ const logger = vite2.createLogger("info", { prefix: "[react-router]" });
1541
+ await watch(root, { logger });
1230
1542
  await new Promise(() => {
1231
1543
  });
1232
1544
  return;
1233
1545
  }
1234
- let ctx = await loadPluginContext({ root, configFile: flags.config });
1235
- await writeAll({
1236
- rootDirectory: root,
1237
- appDirectory: ctx.reactRouterConfig.appDirectory,
1238
- routes: ctx.reactRouterConfig.routes
1239
- });
1546
+ await run(root);
1240
1547
  }
1241
1548
 
1242
1549
  // cli/run.ts
1243
1550
  var helpText = `
1244
- ${import_picocolors9.default.blueBright("react-router")}
1551
+ ${import_picocolors8.default.blueBright("react-router")}
1245
1552
 
1246
- ${import_picocolors9.default.underline("Usage")}:
1247
- $ react-router build [${import_picocolors9.default.yellowBright("projectDir")}]
1248
- $ react-router dev [${import_picocolors9.default.yellowBright("projectDir")}]
1249
- $ react-router routes [${import_picocolors9.default.yellowBright("projectDir")}]
1553
+ ${import_picocolors8.default.underline("Usage")}:
1554
+ $ react-router build [${import_picocolors8.default.yellowBright("projectDir")}]
1555
+ $ react-router dev [${import_picocolors8.default.yellowBright("projectDir")}]
1556
+ $ react-router routes [${import_picocolors8.default.yellowBright("projectDir")}]
1250
1557
 
1251
- ${import_picocolors9.default.underline("Options")}:
1558
+ ${import_picocolors8.default.underline("Options")}:
1252
1559
  --help, -h Print this help message and exit
1253
1560
  --version, -v Print the CLI version and exit
1254
1561
  --no-color Disable ANSI colors in console output
@@ -1282,25 +1589,24 @@ ${import_picocolors9.default.blueBright("react-router")}
1282
1589
  --config, -c Use specified Vite config file (string)
1283
1590
  --no-typescript Generate plain JavaScript files
1284
1591
  \`typegen\` Options:
1285
- --config, -c Use specified Vite config file (string)
1286
1592
  --watch Automatically regenerate types whenever route config (\`routes.ts\`) or route modules change
1287
1593
 
1288
- ${import_picocolors9.default.underline("Build your project")}:
1594
+ ${import_picocolors8.default.underline("Build your project")}:
1289
1595
 
1290
1596
  $ react-router build
1291
1597
 
1292
- ${import_picocolors9.default.underline("Run your project locally in development")}:
1598
+ ${import_picocolors8.default.underline("Run your project locally in development")}:
1293
1599
 
1294
1600
  $ react-router dev
1295
1601
 
1296
- ${import_picocolors9.default.underline("Show all routes in your app")}:
1602
+ ${import_picocolors8.default.underline("Show all routes in your app")}:
1297
1603
 
1298
1604
  $ react-router routes
1299
1605
  $ react-router routes my-app
1300
1606
  $ react-router routes --json
1301
1607
  $ react-router routes --config vite.react-router.config.ts
1302
1608
 
1303
- ${import_picocolors9.default.underline("Reveal the used entry point")}:
1609
+ ${import_picocolors8.default.underline("Reveal the used entry point")}:
1304
1610
 
1305
1611
  $ react-router reveal entry.client
1306
1612
  $ react-router reveal entry.server
@@ -1308,18 +1614,17 @@ ${import_picocolors9.default.blueBright("react-router")}
1308
1614
  $ react-router reveal entry.server --no-typescript
1309
1615
  $ react-router reveal entry.server --config vite.react-router.config.ts
1310
1616
 
1311
- ${import_picocolors9.default.underline("Generate types for route modules")}:
1617
+ ${import_picocolors8.default.underline("Generate types for route modules")}:
1312
1618
 
1313
1619
  $ react-router typegen
1314
1620
  $ react-router typegen --watch
1315
- $ react-router typegen --config vite.react-router.config.ts
1316
1621
  `;
1317
- async function run(argv = process.argv.slice(2)) {
1622
+ async function run2(argv = process.argv.slice(2)) {
1318
1623
  let versions = process.versions;
1319
1624
  let MINIMUM_NODE_VERSION = 20;
1320
1625
  if (versions && versions.node && import_semver.default.major(versions.node) < MINIMUM_NODE_VERSION) {
1321
- throw new Error(
1322
- `\uFE0F\u{1F6A8} Oops, Node v${versions.node} detected. react-router requires a Node version greater than ${MINIMUM_NODE_VERSION}.`
1626
+ console.warn(
1627
+ `\uFE0F\u26A0\uFE0F Oops, Node v${versions.node} detected. react-router requires a Node version greater than ${MINIMUM_NODE_VERSION}.`
1323
1628
  );
1324
1629
  }
1325
1630
  let isBooleanFlag = (arg2) => {
@@ -1406,12 +1711,12 @@ async function run(argv = process.argv.slice(2)) {
1406
1711
  }
1407
1712
 
1408
1713
  // cli/index.ts
1409
- run().then(
1714
+ run2().then(
1410
1715
  () => {
1411
1716
  process.exit(0);
1412
1717
  },
1413
- (error2) => {
1414
- if (error2) console.error(error2);
1718
+ (error) => {
1719
+ if (error) console.error(error);
1415
1720
  process.exit(1);
1416
1721
  }
1417
1722
  );