@react-router/dev 0.0.0-experimental-49eef6a01 → 0.0.0-experimental-a26b992a1
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/bin.js +1 -1
- package/dist/cli/commands.js +1 -1
- package/dist/cli/detectPackageManager.js +1 -1
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +7 -6
- package/dist/cli/run.js +1 -1
- package/dist/cli/useJavascript.js +1 -1
- package/dist/colors.js +1 -1
- package/dist/config/defaults/entry.client.tsx +1 -1
- package/dist/config/format.js +1 -1
- package/dist/config/routes.d.ts +77 -50
- package/dist/config/routes.js +175 -43
- package/dist/invariant.js +1 -1
- package/dist/routes.d.ts +2 -0
- package/dist/routes.js +23 -0
- package/dist/vite/babel.d.ts +10 -10
- package/dist/vite/babel.js +1 -1
- package/dist/vite/build.js +2 -2
- package/dist/vite/cloudflare-dev-proxy.d.ts +7 -1
- package/dist/vite/cloudflare-dev-proxy.js +23 -7
- package/dist/vite/cloudflare.d.ts +1 -0
- package/dist/vite/cloudflare.js +19 -0
- package/dist/vite/combine-urls.d.ts +1 -0
- package/dist/vite/combine-urls.js +20 -0
- package/dist/{config.d.ts → vite/config.d.ts} +15 -27
- package/dist/{config.js → vite/config.js} +62 -45
- package/dist/vite/dev.js +1 -1
- package/dist/vite/import-vite-esm-sync.js +1 -1
- package/dist/vite/node-adapter.js +1 -1
- package/dist/vite/plugin.d.ts +6 -3
- package/dist/vite/plugin.js +145 -121
- package/dist/vite/profiler.js +1 -1
- package/dist/vite/remove-exports.d.ts +2 -2
- package/dist/vite/remove-exports.js +113 -243
- package/dist/vite/resolve-file-url.js +1 -1
- package/dist/vite/static/refresh-utils.cjs +7 -26
- package/dist/vite/styles.d.ts +3 -2
- package/dist/vite/styles.js +23 -2
- package/dist/vite/vmod.js +1 -1
- package/dist/vite/with-props.d.ts +4 -0
- package/dist/vite/with-props.js +151 -0
- package/dist/vite.d.ts +2 -0
- package/dist/{cli.js → vite.js} +7 -8
- package/package.json +21 -12
- package/dist/config/findConfig.d.ts +0 -1
- package/dist/config/findConfig.js +0 -32
- package/dist/config/flatRoutes.d.ts +0 -14
- package/dist/config/flatRoutes.js +0 -418
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -23
- package/dist/vite/define-route.d.ts +0 -5
- package/dist/vite/define-route.js +0 -207
- package/dist/vite/index.d.ts +0 -3
- package/dist/vite/index.js +0 -31
- /package/dist/{cli.d.ts → vite/combine-urls-test.d.ts} +0 -0
package/dist/vite/plugin.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @react-router/dev v0.0.0-experimental-
|
|
2
|
+
* @react-router/dev v0.0.0-experimental-a26b992a1
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) Remix Software Inc.
|
|
5
5
|
*
|
|
@@ -15,22 +15,27 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
15
15
|
var node_crypto = require('node:crypto');
|
|
16
16
|
var path = require('node:path');
|
|
17
17
|
var url = require('node:url');
|
|
18
|
+
var server = require('vite-node/server');
|
|
19
|
+
var client = require('vite-node/client');
|
|
20
|
+
var sourceMap = require('vite-node/source-map');
|
|
18
21
|
var fse = require('fs-extra');
|
|
19
|
-
var babel = require('@babel/core');
|
|
22
|
+
var babel$1 = require('@babel/core');
|
|
20
23
|
var reactRouter = require('react-router');
|
|
21
24
|
var esModuleLexer = require('es-module-lexer');
|
|
22
25
|
var jsesc = require('jsesc');
|
|
23
26
|
var colors = require('picocolors');
|
|
24
|
-
var findConfig = require('../config/findConfig.js');
|
|
25
27
|
var invariant = require('../invariant.js');
|
|
28
|
+
var babel = require('./babel.js');
|
|
26
29
|
var nodeAdapter = require('./node-adapter.js');
|
|
27
30
|
var styles = require('./styles.js');
|
|
28
31
|
var vmod = require('./vmod.js');
|
|
29
32
|
var resolveFileUrl = require('./resolve-file-url.js');
|
|
33
|
+
var combineUrls = require('./combine-urls.js');
|
|
30
34
|
var removeExports = require('./remove-exports.js');
|
|
31
35
|
var importViteEsmSync = require('./import-vite-esm-sync.js');
|
|
32
|
-
var config = require('
|
|
33
|
-
var
|
|
36
|
+
var config = require('./config.js');
|
|
37
|
+
var withProps = require('./with-props.js');
|
|
38
|
+
var parser = require('@babel/parser');
|
|
34
39
|
|
|
35
40
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
36
41
|
|
|
@@ -55,7 +60,7 @@ function _interopNamespace(e) {
|
|
|
55
60
|
var path__namespace = /*#__PURE__*/_interopNamespace(path);
|
|
56
61
|
var url__namespace = /*#__PURE__*/_interopNamespace(url);
|
|
57
62
|
var fse__namespace = /*#__PURE__*/_interopNamespace(fse);
|
|
58
|
-
var babel__default = /*#__PURE__*/_interopDefaultLegacy(babel);
|
|
63
|
+
var babel__default = /*#__PURE__*/_interopDefaultLegacy(babel$1);
|
|
59
64
|
var jsesc__default = /*#__PURE__*/_interopDefaultLegacy(jsesc);
|
|
60
65
|
var colors__default = /*#__PURE__*/_interopDefaultLegacy(colors);
|
|
61
66
|
|
|
@@ -90,7 +95,7 @@ async function loadPluginContext({
|
|
|
90
95
|
if (!root) {
|
|
91
96
|
root = process.env.REACT_ROUTER_ROOT || process.cwd();
|
|
92
97
|
}
|
|
93
|
-
configFile = configFile ?? findConfig
|
|
98
|
+
configFile = configFile ?? findConfig(root, "vite.config", [".ts", ".cts", ".mts", ".js", ".cjs", ".mjs"]);
|
|
94
99
|
if (!configFile) {
|
|
95
100
|
console.error(colors__default["default"].red("Vite config file not found"));
|
|
96
101
|
process.exit(1);
|
|
@@ -168,12 +173,12 @@ function resolveDependantChunks(viteManifest, entryChunks) {
|
|
|
168
173
|
if (chunks.has(chunk)) {
|
|
169
174
|
return;
|
|
170
175
|
}
|
|
176
|
+
chunks.add(chunk);
|
|
171
177
|
if (chunk.imports) {
|
|
172
178
|
for (let importKey of chunk.imports) {
|
|
173
179
|
walk(viteManifest[importKey]);
|
|
174
180
|
}
|
|
175
181
|
}
|
|
176
|
-
chunks.add(chunk);
|
|
177
182
|
}
|
|
178
183
|
for (let entryChunk of entryChunks) {
|
|
179
184
|
walk(entryChunk);
|
|
@@ -195,20 +200,6 @@ const getRouteManifestModuleExports = async (viteChildCompiler, ctx) => {
|
|
|
195
200
|
return Object.fromEntries(entries);
|
|
196
201
|
};
|
|
197
202
|
const getRouteModuleExports = async (viteChildCompiler, ctx, routeFile, readRouteFile) => {
|
|
198
|
-
let routePath = path__namespace.resolve(ctx.reactRouterConfig.appDirectory, routeFile);
|
|
199
|
-
let code = await ((readRouteFile === null || readRouteFile === void 0 ? void 0 : readRouteFile()) ?? fse__namespace.readFile(routePath, "utf-8"));
|
|
200
|
-
if (!code.includes("defineRoute")) {
|
|
201
|
-
return _getRouteModuleExports(viteChildCompiler, ctx, routeFile, readRouteFile);
|
|
202
|
-
}
|
|
203
|
-
let renameFields = ["Component", "serverLoader", "actionLoader"];
|
|
204
|
-
let fields = defineRoute.parseFields(code);
|
|
205
|
-
let exports = fields.filter(exportName => !renameFields.includes(exportName));
|
|
206
|
-
if (fields.includes("Component")) exports.push("default");
|
|
207
|
-
if (fields.includes("serverLoader")) exports.push("loader");
|
|
208
|
-
if (fields.includes("serverAction")) exports.push("action");
|
|
209
|
-
return exports;
|
|
210
|
-
};
|
|
211
|
-
const _getRouteModuleExports = async (viteChildCompiler, ctx, routeFile, readRouteFile) => {
|
|
212
203
|
if (!viteChildCompiler) {
|
|
213
204
|
throw new Error("Vite child compiler not found");
|
|
214
205
|
}
|
|
@@ -252,9 +243,6 @@ let defaultEntriesDir = path__namespace.resolve(__dirname, "..", "config", "defa
|
|
|
252
243
|
let defaultEntries = fse__namespace.readdirSync(defaultEntriesDir).map(filename => path__namespace.join(defaultEntriesDir, filename));
|
|
253
244
|
invariant["default"](defaultEntries.length > 0, "No default entries found");
|
|
254
245
|
let reactRouterDevLoadContext = () => ({});
|
|
255
|
-
let setReactRouterDevLoadContext = loadContext => {
|
|
256
|
-
reactRouterDevLoadContext = loadContext;
|
|
257
|
-
};
|
|
258
246
|
// Inlined from https://github.com/jsdf/deep-freeze
|
|
259
247
|
let deepFreeze = o => {
|
|
260
248
|
Object.freeze(o);
|
|
@@ -267,6 +255,9 @@ let deepFreeze = o => {
|
|
|
267
255
|
});
|
|
268
256
|
return o;
|
|
269
257
|
};
|
|
258
|
+
/**
|
|
259
|
+
* React Router [Vite plugin.](https://vitejs.dev/guide/using-plugins.html)
|
|
260
|
+
*/
|
|
270
261
|
const reactRouterVitePlugin = _config => {
|
|
271
262
|
let reactRouterUserConfig = _config ?? {};
|
|
272
263
|
// Prevent mutations to the user config
|
|
@@ -277,20 +268,32 @@ const reactRouterVitePlugin = _config => {
|
|
|
277
268
|
let viteConfig;
|
|
278
269
|
let cssModulesManifest = {};
|
|
279
270
|
let viteChildCompiler = null;
|
|
271
|
+
let routeConfigViteServer = null;
|
|
272
|
+
let viteNodeRunner = null;
|
|
273
|
+
let ssrExternals = isInReactRouterMonorepo() ? [
|
|
274
|
+
// This is only needed within this repo because these packages
|
|
275
|
+
// are linked to a directory outside of node_modules so Vite
|
|
276
|
+
// treats them as internal code by default.
|
|
277
|
+
"react-router", "react-router-dom", "@react-router/architect", "@react-router/cloudflare", "@react-router/dev", "@react-router/express", "@react-router/node", "@react-router/serve"] : undefined;
|
|
280
278
|
// This is initialized by `updatePluginContext` during Vite's `config`
|
|
281
279
|
// hook, so most of the code can assume this defined without null check.
|
|
282
280
|
// During dev, `updatePluginContext` is called again on every config file
|
|
283
281
|
// change or route file addition/removal.
|
|
284
282
|
let ctx;
|
|
285
283
|
/** Mutates `ctx` as a side-effect */
|
|
286
|
-
let updatePluginContext = async (
|
|
284
|
+
let updatePluginContext = async ({
|
|
285
|
+
routeConfigChanged = false
|
|
286
|
+
} = {}) => {
|
|
287
287
|
var _viteUserConfig$build;
|
|
288
288
|
let rootDirectory = viteUserConfig.root ?? process.env.REACT_ROUTER_ROOT ?? process.cwd();
|
|
289
|
+
invariant["default"](viteNodeRunner);
|
|
289
290
|
let reactRouterConfig = await config.resolveReactRouterConfig({
|
|
290
291
|
rootDirectory,
|
|
291
292
|
reactRouterUserConfig,
|
|
293
|
+
routeConfigChanged,
|
|
292
294
|
viteUserConfig,
|
|
293
|
-
viteCommand
|
|
295
|
+
viteCommand,
|
|
296
|
+
viteNodeRunner
|
|
294
297
|
});
|
|
295
298
|
let {
|
|
296
299
|
entryClientFilePath,
|
|
@@ -361,9 +364,14 @@ const reactRouterVitePlugin = _config => {
|
|
|
361
364
|
let manifestContents = await fse__namespace.readFile(path__namespace.resolve(directory, ".vite", "manifest.json"), "utf-8");
|
|
362
365
|
return JSON.parse(manifestContents);
|
|
363
366
|
};
|
|
364
|
-
let
|
|
365
|
-
|
|
366
|
-
|
|
367
|
+
let hasDependency = name => {
|
|
368
|
+
try {
|
|
369
|
+
return Boolean(require.resolve(name, {
|
|
370
|
+
paths: [ctx.rootDirectory]
|
|
371
|
+
}));
|
|
372
|
+
} catch (err) {
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
367
375
|
};
|
|
368
376
|
let getViteManifestAssetPaths = viteManifest => {
|
|
369
377
|
// Get .css?url imports and CSS entry points
|
|
@@ -448,7 +456,7 @@ const reactRouterVitePlugin = _config => {
|
|
|
448
456
|
path: route.path,
|
|
449
457
|
index: route.index,
|
|
450
458
|
caseSensitive: route.caseSensitive,
|
|
451
|
-
module:
|
|
459
|
+
module: combineUrls.combineURLs(ctx.publicPath, `${resolveFileUrl.resolveFileUrl(ctx, resolveRelativeRouteFilePath(route, ctx.reactRouterConfig))}${ROUTE_ENTRY_QUERY_STRING}`),
|
|
452
460
|
hasAction: sourceExports.includes("action"),
|
|
453
461
|
hasLoader: sourceExports.includes("loader"),
|
|
454
462
|
hasClientAction: sourceExports.includes("clientAction"),
|
|
@@ -459,12 +467,12 @@ const reactRouterVitePlugin = _config => {
|
|
|
459
467
|
}
|
|
460
468
|
return {
|
|
461
469
|
version: String(Math.random()),
|
|
462
|
-
url:
|
|
470
|
+
url: combineUrls.combineURLs(ctx.publicPath, vmod.url(browserManifestId)),
|
|
463
471
|
hmr: {
|
|
464
|
-
runtime:
|
|
472
|
+
runtime: combineUrls.combineURLs(ctx.publicPath, vmod.url(injectHmrRuntimeId))
|
|
465
473
|
},
|
|
466
474
|
entry: {
|
|
467
|
-
module:
|
|
475
|
+
module: combineUrls.combineURLs(ctx.publicPath, resolveFileUrl.resolveFileUrl(ctx, ctx.entryClientFilePath)),
|
|
468
476
|
imports: []
|
|
469
477
|
},
|
|
470
478
|
routes
|
|
@@ -481,6 +489,38 @@ const reactRouterVitePlugin = _config => {
|
|
|
481
489
|
viteUserConfig = _viteUserConfig;
|
|
482
490
|
viteConfigEnv = _viteConfigEnv;
|
|
483
491
|
viteCommand = viteConfigEnv.command;
|
|
492
|
+
routeConfigViteServer = await vite.createServer({
|
|
493
|
+
mode: viteConfigEnv.mode,
|
|
494
|
+
server: {
|
|
495
|
+
watch: viteCommand === "build" ? null : undefined,
|
|
496
|
+
preTransformRequests: false,
|
|
497
|
+
hmr: false
|
|
498
|
+
},
|
|
499
|
+
ssr: {
|
|
500
|
+
external: ssrExternals
|
|
501
|
+
},
|
|
502
|
+
optimizeDeps: {
|
|
503
|
+
noDiscovery: true
|
|
504
|
+
},
|
|
505
|
+
configFile: false,
|
|
506
|
+
envFile: false,
|
|
507
|
+
plugins: []
|
|
508
|
+
});
|
|
509
|
+
await routeConfigViteServer.pluginContainer.buildStart({});
|
|
510
|
+
let viteNodeServer = new server.ViteNodeServer(routeConfigViteServer);
|
|
511
|
+
sourceMap.installSourcemapsSupport({
|
|
512
|
+
getSourceMap: source => viteNodeServer.getSourceMap(source)
|
|
513
|
+
});
|
|
514
|
+
viteNodeRunner = new client.ViteNodeRunner({
|
|
515
|
+
root: routeConfigViteServer.config.root,
|
|
516
|
+
base: routeConfigViteServer.config.base,
|
|
517
|
+
fetchModule(id) {
|
|
518
|
+
return viteNodeServer.fetchModule(id);
|
|
519
|
+
},
|
|
520
|
+
resolveId(id, importer) {
|
|
521
|
+
return viteNodeServer.resolveId(id, importer);
|
|
522
|
+
}
|
|
523
|
+
});
|
|
484
524
|
await updatePluginContext();
|
|
485
525
|
Object.assign(process.env, vite.loadEnv(viteConfigEnv.mode, ctx.rootDirectory,
|
|
486
526
|
// We override default prefix of "VITE_" with a blank string since
|
|
@@ -506,11 +546,7 @@ const reactRouterVitePlugin = _config => {
|
|
|
506
546
|
__reactRouterPluginContext: ctx,
|
|
507
547
|
appType: viteCommand === "serve" && viteConfigEnv.mode === "production" && ctx.reactRouterConfig.ssr === false ? "spa" : "custom",
|
|
508
548
|
ssr: {
|
|
509
|
-
external:
|
|
510
|
-
// This is only needed within this repo because these packages
|
|
511
|
-
// are linked to a directory outside of node_modules so Vite
|
|
512
|
-
// treats them as internal code by default.
|
|
513
|
-
"react-router", "react-router-dom", "@react-router/cloudflare", "@react-router/dev", "@react-router/express", "@react-router/node", "@react-router/serve"] : undefined
|
|
549
|
+
external: ssrExternals
|
|
514
550
|
},
|
|
515
551
|
optimizeDeps: {
|
|
516
552
|
include: [
|
|
@@ -520,7 +556,9 @@ const reactRouterVitePlugin = _config => {
|
|
|
520
556
|
"react", "react/jsx-runtime", "react/jsx-dev-runtime", "react-dom/client",
|
|
521
557
|
// Pre-bundle router dependencies to avoid router duplicates.
|
|
522
558
|
// Mismatching routers cause `Error: You must render this element inside a <Remix> element`.
|
|
523
|
-
"react-router",
|
|
559
|
+
"react-router",
|
|
560
|
+
// Check to avoid "Failed to resolve dependency: react-router-dom, present in 'optimizeDeps.include'"
|
|
561
|
+
...(hasDependency("react-router-dom") ? ["react-router-dom"] : [])]
|
|
524
562
|
},
|
|
525
563
|
esbuild: {
|
|
526
564
|
jsx: "automatic",
|
|
@@ -682,17 +720,26 @@ const reactRouterVitePlugin = _config => {
|
|
|
682
720
|
}
|
|
683
721
|
});
|
|
684
722
|
// Invalidate virtual modules and update cached plugin config via file watcher
|
|
685
|
-
viteDevServer.watcher.on("all", async (eventName,
|
|
686
|
-
var _viteConfig;
|
|
723
|
+
viteDevServer.watcher.on("all", async (eventName, rawFilepath) => {
|
|
724
|
+
var _viteConfig, _routeConfigViteServe;
|
|
687
725
|
let {
|
|
688
726
|
normalizePath
|
|
689
727
|
} = importViteEsmSync.importViteEsmSync();
|
|
690
|
-
let
|
|
728
|
+
let filepath = normalizePath(rawFilepath);
|
|
729
|
+
let appFileAddedOrRemoved = (eventName === "add" || eventName === "unlink") && filepath.startsWith(normalizePath(ctx.reactRouterConfig.appDirectory));
|
|
691
730
|
invariant["default"]((_viteConfig = viteConfig) === null || _viteConfig === void 0 ? void 0 : _viteConfig.configFile);
|
|
692
|
-
let viteConfigChanged = eventName === "change" &&
|
|
693
|
-
|
|
731
|
+
let viteConfigChanged = eventName === "change" && filepath === normalizePath(viteConfig.configFile);
|
|
732
|
+
let routeConfigChanged = Boolean((_routeConfigViteServe = routeConfigViteServer) === null || _routeConfigViteServe === void 0 ? void 0 : _routeConfigViteServe.moduleGraph.getModuleById(filepath));
|
|
733
|
+
if (routeConfigChanged || appFileAddedOrRemoved) {
|
|
734
|
+
var _routeConfigViteServe2, _viteNodeRunner;
|
|
735
|
+
(_routeConfigViteServe2 = routeConfigViteServer) === null || _routeConfigViteServe2 === void 0 ? void 0 : _routeConfigViteServe2.moduleGraph.invalidateAll();
|
|
736
|
+
(_viteNodeRunner = viteNodeRunner) === null || _viteNodeRunner === void 0 ? void 0 : _viteNodeRunner.moduleCache.clear();
|
|
737
|
+
}
|
|
738
|
+
if (appFileAddedOrRemoved || viteConfigChanged || routeConfigChanged) {
|
|
694
739
|
let lastReactRouterConfig = ctx.reactRouterConfig;
|
|
695
|
-
await updatePluginContext(
|
|
740
|
+
await updatePluginContext({
|
|
741
|
+
routeConfigChanged
|
|
742
|
+
});
|
|
696
743
|
if (!isEqualJson(lastReactRouterConfig, ctx.reactRouterConfig)) {
|
|
697
744
|
invalidateVirtualModules(viteDevServer);
|
|
698
745
|
}
|
|
@@ -730,8 +777,6 @@ const reactRouterVitePlugin = _config => {
|
|
|
730
777
|
let clientBuildDirectory = getClientBuildDirectory(ctx.reactRouterConfig);
|
|
731
778
|
let serverBuildDirectory = getServerBuildDirectory(ctx);
|
|
732
779
|
let ssrViteManifest = await loadViteManifest(serverBuildDirectory);
|
|
733
|
-
let clientViteManifest = await loadViteManifest(clientBuildDirectory);
|
|
734
|
-
let clientFilePaths = getViteManifestFilePaths(clientViteManifest);
|
|
735
780
|
let ssrAssetPaths = getViteManifestAssetPaths(ssrViteManifest);
|
|
736
781
|
// We only move assets that aren't in the client build, otherwise we
|
|
737
782
|
// remove them. These assets only exist because we explicitly set
|
|
@@ -742,8 +787,8 @@ const reactRouterVitePlugin = _config => {
|
|
|
742
787
|
let movedAssetPaths = [];
|
|
743
788
|
for (let ssrAssetPath of ssrAssetPaths) {
|
|
744
789
|
let src = path__namespace.join(serverBuildDirectory, ssrAssetPath);
|
|
745
|
-
|
|
746
|
-
|
|
790
|
+
let dest = path__namespace.join(clientBuildDirectory, ssrAssetPath);
|
|
791
|
+
if (!fse__namespace.existsSync(dest)) {
|
|
747
792
|
await fse__namespace.move(src, dest);
|
|
748
793
|
movedAssetPaths.push(dest);
|
|
749
794
|
} else {
|
|
@@ -774,36 +819,20 @@ const reactRouterVitePlugin = _config => {
|
|
|
774
819
|
}
|
|
775
820
|
},
|
|
776
821
|
async buildEnd() {
|
|
777
|
-
var _viteChildCompiler;
|
|
822
|
+
var _viteChildCompiler, _routeConfigViteServe3;
|
|
778
823
|
await ((_viteChildCompiler = viteChildCompiler) === null || _viteChildCompiler === void 0 ? void 0 : _viteChildCompiler.close());
|
|
824
|
+
await ((_routeConfigViteServe3 = routeConfigViteServer) === null || _routeConfigViteServe3 === void 0 ? void 0 : _routeConfigViteServe3.close());
|
|
779
825
|
}
|
|
780
826
|
}, {
|
|
781
827
|
name: "react-router-route-entry",
|
|
782
828
|
enforce: "pre",
|
|
783
|
-
async transform(
|
|
829
|
+
async transform(_code, id, options) {
|
|
784
830
|
if (!isRouteEntry(id)) return;
|
|
785
831
|
let routeModuleId = id.replace(ROUTE_ENTRY_QUERY_STRING, "");
|
|
786
832
|
let routeFileName = path__namespace.basename(routeModuleId);
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
return `export { ${reexports} } from "./${routeFileName}";`;
|
|
791
|
-
}
|
|
792
|
-
let renameFields = ["Component", "serverLoader", "serverAction"];
|
|
793
|
-
let fields = defineRoute.parseFields(code);
|
|
794
|
-
let reexports = fields.filter(exportName => !renameFields.includes(exportName)).filter(exportName => (options === null || options === void 0 ? void 0 : options.ssr) && SERVER_ONLY_ROUTE_EXPORTS.includes(exportName) || CLIENT_ROUTE_EXPORTS.includes(exportName)).map(reexport => `export const ${reexport} = route.${reexport};`);
|
|
795
|
-
let content = `import route from "./${routeFileName}";`;
|
|
796
|
-
if (fields.includes("Component")) {
|
|
797
|
-
content += `\n` + [`import { createElement } from "react";`, `import { useParams, useLoaderData, useActionData } from "react-router";`, `export default function Route() {`, ` let params = useParams();`, ` let loaderData = useLoaderData();`, ` let actionData = useActionData();`, ` return createElement(route.Component, { params, loaderData, actionData });`, `}`].join("\n");
|
|
798
|
-
}
|
|
799
|
-
if (options !== null && options !== void 0 && options.ssr && fields.includes("serverLoader")) {
|
|
800
|
-
content += `\nexport const loader = route.serverLoader;`;
|
|
801
|
-
}
|
|
802
|
-
if (options !== null && options !== void 0 && options.ssr && fields.includes("serverAction")) {
|
|
803
|
-
content += `\nexport const action = route.serverAction;`;
|
|
804
|
-
}
|
|
805
|
-
content += "\n" + reexports.join("\n");
|
|
806
|
-
return content;
|
|
833
|
+
let sourceExports = await getRouteModuleExports(viteChildCompiler, ctx, routeModuleId);
|
|
834
|
+
let reexports = sourceExports.filter(exportName => (options === null || options === void 0 ? void 0 : options.ssr) && SERVER_ONLY_ROUTE_EXPORTS.includes(exportName) || CLIENT_ROUTE_EXPORTS.includes(exportName)).join(", ");
|
|
835
|
+
return `export { ${reexports} } from "./${routeFileName}";`;
|
|
807
836
|
}
|
|
808
837
|
}, {
|
|
809
838
|
name: "react-router-virtual-modules",
|
|
@@ -837,25 +866,14 @@ const reactRouterVitePlugin = _config => {
|
|
|
837
866
|
}
|
|
838
867
|
}
|
|
839
868
|
}
|
|
840
|
-
}, {
|
|
841
|
-
name: "react-router-define-route",
|
|
842
|
-
enforce: "pre",
|
|
843
|
-
async transform(code, id, options) {
|
|
844
|
-
if (options !== null && options !== void 0 && options.ssr) return;
|
|
845
|
-
if (!code.includes("defineRoute")) return; // temporary back compat, remove once old style routes are unsupported
|
|
846
|
-
if (id.endsWith(ROUTE_ENTRY_QUERY_STRING)) return;
|
|
847
|
-
let route = getRoute(ctx.reactRouterConfig, id);
|
|
848
|
-
if (!route && code.includes("defineRoute")) {
|
|
849
|
-
return defineRoute.assertNotImported(code);
|
|
850
|
-
}
|
|
851
|
-
return defineRoute.transform(code, id, options);
|
|
852
|
-
}
|
|
853
869
|
}, {
|
|
854
870
|
name: "react-router-dot-server",
|
|
855
871
|
enforce: "pre",
|
|
856
872
|
async resolveId(id, importer, options) {
|
|
857
873
|
var _options$custom;
|
|
858
|
-
|
|
874
|
+
// https://vitejs.dev/config/dep-optimization-options
|
|
875
|
+
let isOptimizeDeps = viteCommand === "serve" && (options === null || options === void 0 ? void 0 : options.scan) === true;
|
|
876
|
+
if (isOptimizeDeps || options !== null && options !== void 0 && options.ssr) return;
|
|
859
877
|
let isResolving = (options === null || options === void 0 ? void 0 : (_options$custom = options.custom) === null || _options$custom === void 0 ? void 0 : _options$custom["react-router-dot-server"]) ?? false;
|
|
860
878
|
if (isResolving) return;
|
|
861
879
|
options.custom = {
|
|
@@ -900,7 +918,7 @@ const reactRouterVitePlugin = _config => {
|
|
|
900
918
|
};
|
|
901
919
|
}
|
|
902
920
|
}
|
|
903
|
-
}, {
|
|
921
|
+
}, withProps.plugin, {
|
|
904
922
|
name: "react-router-route-exports",
|
|
905
923
|
async transform(code, id, options) {
|
|
906
924
|
if (options !== null && options !== void 0 && options.ssr) return;
|
|
@@ -922,7 +940,12 @@ const reactRouterVitePlugin = _config => {
|
|
|
922
940
|
}
|
|
923
941
|
}
|
|
924
942
|
let [filepath] = id.split("?");
|
|
925
|
-
|
|
943
|
+
let ast = parser.parse(code, {
|
|
944
|
+
sourceType: "module"
|
|
945
|
+
});
|
|
946
|
+
removeExports.removeExports(ast, SERVER_ONLY_ROUTE_EXPORTS);
|
|
947
|
+
withProps.transform(ast);
|
|
948
|
+
return babel.generate(ast, {
|
|
926
949
|
sourceMaps: true,
|
|
927
950
|
filename: id,
|
|
928
951
|
sourceFileName: filepath
|
|
@@ -1015,7 +1038,7 @@ const reactRouterVitePlugin = _config => {
|
|
|
1015
1038
|
invalidateVirtualModules(server);
|
|
1016
1039
|
}
|
|
1017
1040
|
}
|
|
1018
|
-
server.
|
|
1041
|
+
server.hot.send({
|
|
1019
1042
|
type: "custom",
|
|
1020
1043
|
event: "react-router:hmr",
|
|
1021
1044
|
data: hmrEventData
|
|
@@ -1024,6 +1047,14 @@ const reactRouterVitePlugin = _config => {
|
|
|
1024
1047
|
}
|
|
1025
1048
|
}];
|
|
1026
1049
|
};
|
|
1050
|
+
function findConfig(dir, basename, extensions) {
|
|
1051
|
+
for (let ext of extensions) {
|
|
1052
|
+
let name = basename + ext;
|
|
1053
|
+
let file = path__namespace.join(dir, name);
|
|
1054
|
+
if (fse__namespace.existsSync(file)) return file;
|
|
1055
|
+
}
|
|
1056
|
+
return undefined;
|
|
1057
|
+
}
|
|
1027
1058
|
function isInReactRouterMonorepo() {
|
|
1028
1059
|
// We use '@react-router/node' for this check since it's a
|
|
1029
1060
|
// dependency of this package and guaranteed to be in node_modules
|
|
@@ -1037,7 +1068,14 @@ function isEqualJson(v1, v2) {
|
|
|
1037
1068
|
function addRefreshWrapper(reactRouterConfig, code, id) {
|
|
1038
1069
|
let route = getRoute(reactRouterConfig, id);
|
|
1039
1070
|
let acceptExports = route || isRouteEntry(id) ? ["clientAction", "clientLoader", "handle", "meta", "links", "shouldRevalidate"] : [];
|
|
1040
|
-
return REACT_REFRESH_HEADER.replaceAll("__SOURCE__", JSON.stringify(id)) + code + REACT_REFRESH_FOOTER.replaceAll("__SOURCE__", JSON.stringify(id)).replaceAll("__ACCEPT_EXPORTS__", JSON.stringify(acceptExports)).replaceAll("__ROUTE_ID__", JSON.stringify(route === null || route === void 0 ? void 0 : route.id));
|
|
1071
|
+
return "\n\n" + withCommentBoundaries("REACT REFRESH HEADER", REACT_REFRESH_HEADER.replaceAll("__SOURCE__", JSON.stringify(id))) + "\n\n" + withCommentBoundaries("REACT REFRESH BODY", code) + "\n\n" + withCommentBoundaries("REACT REFRESH FOOTER", REACT_REFRESH_FOOTER.replaceAll("__SOURCE__", JSON.stringify(id)).replaceAll("__ACCEPT_EXPORTS__", JSON.stringify(acceptExports)).replaceAll("__ROUTE_ID__", JSON.stringify(route === null || route === void 0 ? void 0 : route.id))) + "\n";
|
|
1072
|
+
}
|
|
1073
|
+
function withCommentBoundaries(label, text) {
|
|
1074
|
+
let begin = `// [BEGIN] ${label} `;
|
|
1075
|
+
begin += "-".repeat(80 - begin.length);
|
|
1076
|
+
let end = `// [END] ${label} `;
|
|
1077
|
+
end += "-".repeat(80 - end.length);
|
|
1078
|
+
return `${begin}\n${text}\n${end}`;
|
|
1041
1079
|
}
|
|
1042
1080
|
const REACT_REFRESH_HEADER = `
|
|
1043
1081
|
import RefreshRuntime from "${hmrRuntimeId}";
|
|
@@ -1059,7 +1097,7 @@ if (import.meta.hot && !inWebWorker) {
|
|
|
1059
1097
|
RefreshRuntime.register(type, __SOURCE__ + " " + id)
|
|
1060
1098
|
};
|
|
1061
1099
|
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
|
1062
|
-
}`.
|
|
1100
|
+
}`.trim();
|
|
1063
1101
|
const REACT_REFRESH_FOOTER = `
|
|
1064
1102
|
if (import.meta.hot && !inWebWorker) {
|
|
1065
1103
|
window.$RefreshReg$ = prevRefreshReg;
|
|
@@ -1073,7 +1111,7 @@ if (import.meta.hot && !inWebWorker) {
|
|
|
1073
1111
|
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
|
1074
1112
|
});
|
|
1075
1113
|
});
|
|
1076
|
-
}
|
|
1114
|
+
}`.trim();
|
|
1077
1115
|
function getRoute(pluginConfig, file) {
|
|
1078
1116
|
let vite = importViteEsmSync.importViteEsmSync();
|
|
1079
1117
|
let routePath = vite.normalizePath(path__namespace.relative(pluginConfig.appDirectory, file));
|
|
@@ -1088,8 +1126,8 @@ async function getRouteMetadata(ctx, viteChildCompiler, route, readRouteFile) {
|
|
|
1088
1126
|
path: route.path,
|
|
1089
1127
|
index: route.index,
|
|
1090
1128
|
caseSensitive: route.caseSensitive,
|
|
1091
|
-
url:
|
|
1092
|
-
module:
|
|
1129
|
+
url: combineUrls.combineURLs(ctx.publicPath, "/" + path__namespace.relative(ctx.rootDirectory, resolveRelativeRouteFilePath(route, ctx.reactRouterConfig))),
|
|
1130
|
+
module: combineUrls.combineURLs(ctx.publicPath, `${resolveFileUrl.resolveFileUrl(ctx, resolveRelativeRouteFilePath(route, ctx.reactRouterConfig))}?import`),
|
|
1093
1131
|
// Ensure the Vite dev server responds with a JS module
|
|
1094
1132
|
hasAction: sourceExports.includes("action"),
|
|
1095
1133
|
hasClientAction: sourceExports.includes("clientAction"),
|
|
@@ -1146,20 +1184,19 @@ async function handlePrerender(viteConfig, reactRouterConfig, serverBuildDirecto
|
|
|
1146
1184
|
}
|
|
1147
1185
|
await prerenderRoute(handler, path, clientBuildDirectory, reactRouterConfig, viteConfig, requestInit);
|
|
1148
1186
|
}
|
|
1149
|
-
|
|
1150
|
-
}
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
viteConfig.logger.info(`Prerender: Generated ${colors__default["default"].bold(outfile)}`);
|
|
1187
|
+
async function prerenderData(handler, prerenderPath, clientBuildDirectory, reactRouterConfig, viteConfig, requestInit) {
|
|
1188
|
+
let normalizedPath = `${reactRouterConfig.basename}${prerenderPath === "/" ? "/_root.data" : `${prerenderPath.replace(/\/$/, "")}.data`}`.replace(/\/\/+/g, "/");
|
|
1189
|
+
let request = new Request(`http://localhost${normalizedPath}`, requestInit);
|
|
1190
|
+
let response = await handler(request);
|
|
1191
|
+
let data = await response.text();
|
|
1192
|
+
validatePrerenderedResponse(response, data, "Prerender", normalizedPath);
|
|
1193
|
+
// Write out the .data file
|
|
1194
|
+
let outdir = path__namespace.relative(process.cwd(), clientBuildDirectory);
|
|
1195
|
+
let outfile = path__namespace.join(outdir, normalizedPath.split("/").join(path__namespace.sep));
|
|
1196
|
+
await fse__namespace.ensureDir(path__namespace.dirname(outfile));
|
|
1197
|
+
await fse__namespace.outputFile(outfile, data);
|
|
1198
|
+
viteConfig.logger.info(`Prerender: Generated ${colors__default["default"].bold(outfile)}`);
|
|
1199
|
+
}
|
|
1163
1200
|
}
|
|
1164
1201
|
async function prerenderRoute(handler, prerenderPath, clientBuildDirectory, reactRouterConfig, viteConfig, requestInit) {
|
|
1165
1202
|
let normalizedPath = `${reactRouterConfig.basename}${prerenderPath}/`.replace(/\/\/+/g, "/");
|
|
@@ -1177,18 +1214,6 @@ async function prerenderRoute(handler, prerenderPath, clientBuildDirectory, reac
|
|
|
1177
1214
|
await fse__namespace.outputFile(outfile, html);
|
|
1178
1215
|
viteConfig.logger.info(`Prerender: Generated ${colors__default["default"].bold(outfile)}`);
|
|
1179
1216
|
}
|
|
1180
|
-
async function prerenderManifest(build, clientBuildDirectory, reactRouterConfig, viteConfig) {
|
|
1181
|
-
let normalizedPath = `${reactRouterConfig.basename}/__manifest`.replace(/\/\/+/g, "/");
|
|
1182
|
-
let outdir = path__namespace.relative(process.cwd(), clientBuildDirectory);
|
|
1183
|
-
let outfile = path__namespace.join(outdir, ...normalizedPath.split("/"));
|
|
1184
|
-
await fse__namespace.ensureDir(path__namespace.dirname(outfile));
|
|
1185
|
-
let manifestData = JSON.stringify({
|
|
1186
|
-
notFoundPaths: [],
|
|
1187
|
-
patches: build.assets.routes
|
|
1188
|
-
});
|
|
1189
|
-
await fse__namespace.outputFile(outfile, manifestData);
|
|
1190
|
-
viteConfig.logger.info(`Prerender: Generated ${colors__default["default"].bold(outfile)}`);
|
|
1191
|
-
}
|
|
1192
1217
|
function validatePrerenderedResponse(response, html, prefix, path) {
|
|
1193
1218
|
if (response.status !== 200) {
|
|
1194
1219
|
throw new Error(`${prefix}: Received a ${response.status} status code from ` + `\`entry.server.tsx\` while prerendering the \`${path}\` ` + `path.\n${html}`);
|
|
@@ -1239,4 +1264,3 @@ exports.getServerBuildDirectory = getServerBuildDirectory;
|
|
|
1239
1264
|
exports.loadPluginContext = loadPluginContext;
|
|
1240
1265
|
exports.reactRouterVitePlugin = reactRouterVitePlugin;
|
|
1241
1266
|
exports.resolveViteConfig = resolveViteConfig;
|
|
1242
|
-
exports.setReactRouterDevLoadContext = setReactRouterDevLoadContext;
|
package/dist/vite/profiler.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const removeExports: (
|
|
1
|
+
import type { Babel, ParseResult } from "./babel";
|
|
2
|
+
export declare const removeExports: (ast: ParseResult<Babel.File>, exportsToRemove: string[]) => void;
|