@react-router/dev 7.0.0-pre.2 → 7.0.0-pre.4
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/CHANGELOG.md +20 -0
- package/dist/cli/index.d.ts +2 -1
- package/dist/cli/index.js +1198 -8
- package/dist/{config/routes.d.ts → routes-C14jcF98.d.ts} +15 -21
- package/dist/routes.d.ts +2 -2
- package/dist/routes.js +181 -11
- package/dist/typescript/plugin.d.ts +5 -2
- package/dist/typescript/plugin.js +479 -31
- package/dist/vite/cloudflare.d.ts +23 -1
- package/dist/vite/cloudflare.js +171 -5
- package/dist/vite.d.ts +169 -2
- package/dist/vite.js +2394 -5
- package/package.json +28 -7
- package/dist/cli/commands.d.ts +0 -13
- package/dist/cli/commands.js +0 -179
- package/dist/cli/detectPackageManager.d.ts +0 -10
- package/dist/cli/detectPackageManager.js +0 -39
- package/dist/cli/run.d.ts +0 -5
- package/dist/cli/run.js +0 -188
- package/dist/cli/useJavascript.d.ts +0 -4
- package/dist/cli/useJavascript.js +0 -66
- package/dist/colors.d.ts +0 -17
- package/dist/colors.js +0 -49
- package/dist/config/format.d.ts +0 -5
- package/dist/config/format.js +0 -68
- package/dist/config/routes.js +0 -253
- package/dist/config/serverModes.d.ts +0 -9
- package/dist/invariant.d.ts +0 -2
- package/dist/invariant.js +0 -20
- package/dist/manifest.d.ts +0 -28
- package/dist/typescript/typegen.d.ts +0 -10
- package/dist/typescript/typegen.js +0 -190
- package/dist/vite/babel.d.ts +0 -20
- package/dist/vite/babel.js +0 -49
- package/dist/vite/build.d.ts +0 -15
- package/dist/vite/build.js +0 -249
- package/dist/vite/cloudflare-dev-proxy.d.ts +0 -21
- package/dist/vite/cloudflare-dev-proxy.js +0 -89
- package/dist/vite/combine-urls-test.d.ts +0 -1
- package/dist/vite/combine-urls.d.ts +0 -1
- package/dist/vite/combine-urls.js +0 -20
- package/dist/vite/config.d.ts +0 -234
- package/dist/vite/config.js +0 -282
- package/dist/vite/dev.d.ts +0 -15
- package/dist/vite/dev.js +0 -81
- package/dist/vite/import-vite-esm-sync.d.ts +0 -4
- package/dist/vite/import-vite-esm-sync.js +0 -28
- package/dist/vite/node-adapter.d.ts +0 -6
- package/dist/vite/node-adapter.js +0 -90
- package/dist/vite/plugin.d.ts +0 -75
- package/dist/vite/plugin.js +0 -1301
- package/dist/vite/profiler.d.ts +0 -5
- package/dist/vite/profiler.js +0 -55
- package/dist/vite/remove-exports-test.d.ts +0 -1
- package/dist/vite/remove-exports.d.ts +0 -2
- package/dist/vite/remove-exports.js +0 -148
- package/dist/vite/resolve-file-url.d.ts +0 -3
- package/dist/vite/resolve-file-url.js +0 -53
- package/dist/vite/styles.d.ts +0 -14
- package/dist/vite/styles.js +0 -199
- package/dist/vite/vite-node.d.ts +0 -9
- package/dist/vite/vite-node.js +0 -57
- package/dist/vite/vmod.d.ts +0 -3
- package/dist/vite/vmod.js +0 -21
- package/dist/vite/with-props.d.ts +0 -4
- package/dist/vite/with-props.js +0 -151
- /package/dist/{vite/static → static}/refresh-utils.cjs +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @react-router/dev v7.0.0-pre.
|
|
2
|
+
* @react-router/dev v7.0.0-pre.4
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) Remix Software Inc.
|
|
5
5
|
*
|
|
@@ -8,45 +8,493 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
11
|
-
|
|
11
|
+
"use strict";
|
|
12
|
+
var __create = Object.create;
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
15
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
16
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
17
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
18
|
+
var __export = (target, all) => {
|
|
19
|
+
for (var name in all)
|
|
20
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
21
|
+
};
|
|
22
|
+
var __copyProps = (to, from, except, desc) => {
|
|
23
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
24
|
+
for (let key of __getOwnPropNames(from))
|
|
25
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
26
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
27
|
+
}
|
|
28
|
+
return to;
|
|
29
|
+
};
|
|
30
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
31
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
32
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
33
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
34
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
35
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
36
|
+
mod
|
|
37
|
+
));
|
|
38
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
39
|
+
|
|
40
|
+
// typescript/plugin.ts
|
|
41
|
+
var plugin_exports = {};
|
|
42
|
+
__export(plugin_exports, {
|
|
43
|
+
default: () => init
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(plugin_exports);
|
|
46
|
+
var Path2 = __toESM(require("pathe"));
|
|
47
|
+
|
|
48
|
+
// typescript/typegen.ts
|
|
49
|
+
var import_node_fs = __toESM(require("fs"));
|
|
50
|
+
var import_chokidar = __toESM(require("chokidar"));
|
|
51
|
+
var import_dedent2 = __toESM(require("dedent"));
|
|
52
|
+
var Path = __toESM(require("pathe"));
|
|
53
|
+
var Pathe = __toESM(require("pathe/utils"));
|
|
54
|
+
|
|
55
|
+
// config/routes.ts
|
|
56
|
+
var import_node_path = require("path");
|
|
57
|
+
var v = __toESM(require("valibot"));
|
|
58
|
+
var import_pick = __toESM(require("lodash/pick"));
|
|
59
|
+
|
|
60
|
+
// invariant.ts
|
|
61
|
+
function invariant(value, message) {
|
|
62
|
+
if (value === false || value === null || typeof value === "undefined") {
|
|
63
|
+
console.error(
|
|
64
|
+
"The following error is a bug in React Router; please open an issue! https://github.com/remix-run/react-router/issues/new/choose"
|
|
65
|
+
);
|
|
66
|
+
throw new Error(message);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// config/routes.ts
|
|
71
|
+
var routeConfigEntrySchema = v.pipe(
|
|
72
|
+
v.custom((value) => {
|
|
73
|
+
return !(typeof value === "object" && value !== null && "then" in value && "catch" in value);
|
|
74
|
+
}, "Invalid type: Expected object but received a promise. Did you forget to await?"),
|
|
75
|
+
v.object({
|
|
76
|
+
id: v.optional(v.string()),
|
|
77
|
+
path: v.optional(v.string()),
|
|
78
|
+
index: v.optional(v.boolean()),
|
|
79
|
+
caseSensitive: v.optional(v.boolean()),
|
|
80
|
+
file: v.string(),
|
|
81
|
+
children: v.optional(v.array(v.lazy(() => routeConfigEntrySchema)))
|
|
82
|
+
})
|
|
83
|
+
);
|
|
84
|
+
var resolvedRouteConfigSchema = v.array(routeConfigEntrySchema);
|
|
85
|
+
function configRoutesToRouteManifest(routes, rootId = "root") {
|
|
86
|
+
let routeManifest = {};
|
|
87
|
+
function walk(route, parentId) {
|
|
88
|
+
let id2 = route.id || createRouteId(route.file);
|
|
89
|
+
let manifestItem = {
|
|
90
|
+
id: id2,
|
|
91
|
+
parentId,
|
|
92
|
+
file: route.file,
|
|
93
|
+
path: route.path,
|
|
94
|
+
index: route.index,
|
|
95
|
+
caseSensitive: route.caseSensitive
|
|
96
|
+
};
|
|
97
|
+
if (routeManifest.hasOwnProperty(id2)) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
`Unable to define routes with duplicate route id: "${id2}"`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
routeManifest[id2] = manifestItem;
|
|
103
|
+
if (route.children) {
|
|
104
|
+
for (let child of route.children) {
|
|
105
|
+
walk(child, id2);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
for (let route of routes) {
|
|
110
|
+
walk(route, rootId);
|
|
111
|
+
}
|
|
112
|
+
return routeManifest;
|
|
113
|
+
}
|
|
114
|
+
function createRouteId(file) {
|
|
115
|
+
return normalizeSlashes(stripFileExtension(file));
|
|
116
|
+
}
|
|
117
|
+
function normalizeSlashes(file) {
|
|
118
|
+
return file.split(import_node_path.win32.sep).join("/");
|
|
119
|
+
}
|
|
120
|
+
function stripFileExtension(file) {
|
|
121
|
+
return file.replace(/\.[a-z0-9]+$/i, "");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// vite/vite-node.ts
|
|
125
|
+
var import_server = require("vite-node/server");
|
|
126
|
+
var import_client = require("vite-node/client");
|
|
127
|
+
var import_source_map = require("vite-node/source-map");
|
|
128
|
+
|
|
129
|
+
// vite/import-vite-esm-sync.ts
|
|
130
|
+
var vite;
|
|
131
|
+
async function preloadViteEsm() {
|
|
132
|
+
vite = await import("vite");
|
|
133
|
+
}
|
|
134
|
+
function importViteEsmSync() {
|
|
135
|
+
invariant(vite, "importViteEsmSync() called before preloadViteEsm()");
|
|
136
|
+
return vite;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// vite/vite-node.ts
|
|
140
|
+
async function createContext(viteConfig = {}) {
|
|
141
|
+
await preloadViteEsm();
|
|
142
|
+
const vite2 = importViteEsmSync();
|
|
143
|
+
const devServer = await vite2.createServer(
|
|
144
|
+
vite2.mergeConfig(
|
|
145
|
+
{
|
|
146
|
+
server: {
|
|
147
|
+
preTransformRequests: false,
|
|
148
|
+
hmr: false
|
|
149
|
+
},
|
|
150
|
+
optimizeDeps: {
|
|
151
|
+
noDiscovery: true
|
|
152
|
+
},
|
|
153
|
+
configFile: false,
|
|
154
|
+
envFile: false,
|
|
155
|
+
plugins: []
|
|
156
|
+
},
|
|
157
|
+
viteConfig
|
|
158
|
+
)
|
|
159
|
+
);
|
|
160
|
+
await devServer.pluginContainer.buildStart({});
|
|
161
|
+
const server = new import_server.ViteNodeServer(devServer);
|
|
162
|
+
(0, import_source_map.installSourcemapsSupport)({
|
|
163
|
+
getSourceMap: (source) => server.getSourceMap(source)
|
|
164
|
+
});
|
|
165
|
+
const runner = new import_client.ViteNodeRunner({
|
|
166
|
+
root: devServer.config.root,
|
|
167
|
+
base: devServer.config.base,
|
|
168
|
+
fetchModule(id2) {
|
|
169
|
+
return server.fetchModule(id2);
|
|
170
|
+
},
|
|
171
|
+
resolveId(id2, importer) {
|
|
172
|
+
return server.resolveId(id2, importer);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
return { devServer, server, runner };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// vite/config.ts
|
|
179
|
+
var import_node_child_process = require("child_process");
|
|
180
|
+
var import_node_path2 = __toESM(require("path"));
|
|
181
|
+
var import_fs_extra = __toESM(require("fs-extra"));
|
|
182
|
+
var import_picocolors = __toESM(require("picocolors"));
|
|
183
|
+
var import_pick2 = __toESM(require("lodash/pick"));
|
|
184
|
+
var import_omit = __toESM(require("lodash/omit"));
|
|
185
|
+
var import_package_json = __toESM(require("@npmcli/package-json"));
|
|
186
|
+
var entryExts = [".js", ".jsx", ".ts", ".tsx"];
|
|
187
|
+
function findEntry(dir, basename3) {
|
|
188
|
+
for (let ext of entryExts) {
|
|
189
|
+
let file = import_node_path2.default.resolve(dir, basename3 + ext);
|
|
190
|
+
if (import_fs_extra.default.existsSync(file)) return import_node_path2.default.relative(dir, file);
|
|
191
|
+
}
|
|
192
|
+
return void 0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// vite/plugin.ts
|
|
196
|
+
var import_node_crypto = require("crypto");
|
|
197
|
+
var path4 = __toESM(require("path"));
|
|
198
|
+
var url = __toESM(require("url"));
|
|
199
|
+
var fse2 = __toESM(require("fs-extra"));
|
|
200
|
+
var babel = __toESM(require("@babel/core"));
|
|
201
|
+
var import_react_router2 = require("react-router");
|
|
202
|
+
var import_es_module_lexer = require("es-module-lexer");
|
|
203
|
+
var import_jsesc = __toESM(require("jsesc"));
|
|
204
|
+
var import_picocolors2 = __toESM(require("picocolors"));
|
|
205
|
+
|
|
206
|
+
// vite/babel.ts
|
|
207
|
+
var import_parser = require("@babel/parser");
|
|
208
|
+
var t = __toESM(require("@babel/types"));
|
|
209
|
+
var traverse = require("@babel/traverse").default;
|
|
210
|
+
var generate = require("@babel/generator").default;
|
|
211
|
+
|
|
212
|
+
// vite/node-adapter.ts
|
|
213
|
+
var import_node_events = require("events");
|
|
214
|
+
var import_node_stream = require("stream");
|
|
215
|
+
var import_set_cookie_parser = require("set-cookie-parser");
|
|
216
|
+
var import_node = require("@react-router/node");
|
|
217
|
+
|
|
218
|
+
// vite/styles.ts
|
|
219
|
+
var path3 = __toESM(require("path"));
|
|
220
|
+
var import_react_router = require("react-router");
|
|
221
|
+
|
|
222
|
+
// vite/resolve-file-url.ts
|
|
223
|
+
var path2 = __toESM(require("path"));
|
|
224
|
+
|
|
225
|
+
// vite/styles.ts
|
|
226
|
+
var cssFileRegExp = /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/;
|
|
227
|
+
var cssModulesRegExp = new RegExp(`\\.module${cssFileRegExp.source}`);
|
|
228
|
+
|
|
229
|
+
// vite/vmod.ts
|
|
230
|
+
var id = (name) => `virtual:react-router/${name}`;
|
|
231
|
+
|
|
232
|
+
// vite/remove-exports.ts
|
|
233
|
+
var import_babel_dead_code_elimination = require("babel-dead-code-elimination");
|
|
234
|
+
|
|
235
|
+
// vite/with-props.ts
|
|
236
|
+
var import_dedent = __toESM(require("dedent"));
|
|
237
|
+
var vmodId = id("with-props");
|
|
12
238
|
|
|
13
|
-
|
|
14
|
-
|
|
239
|
+
// vite/plugin.ts
|
|
240
|
+
async function resolveViteConfig({
|
|
241
|
+
configFile,
|
|
242
|
+
mode,
|
|
243
|
+
root
|
|
244
|
+
}) {
|
|
245
|
+
let vite2 = await import("vite");
|
|
246
|
+
let viteConfig = await vite2.resolveConfig(
|
|
247
|
+
{ mode, configFile, root },
|
|
248
|
+
"build",
|
|
249
|
+
// command
|
|
250
|
+
"production",
|
|
251
|
+
// default mode
|
|
252
|
+
"production"
|
|
253
|
+
// default NODE_ENV
|
|
254
|
+
);
|
|
255
|
+
if (typeof viteConfig.build.manifest === "string") {
|
|
256
|
+
throw new Error("Custom Vite manifest paths are not supported");
|
|
257
|
+
}
|
|
258
|
+
return viteConfig;
|
|
259
|
+
}
|
|
260
|
+
async function extractPluginContext(viteConfig) {
|
|
261
|
+
return viteConfig["__reactRouterPluginContext"];
|
|
262
|
+
}
|
|
263
|
+
async function loadPluginContext({
|
|
264
|
+
configFile,
|
|
265
|
+
root
|
|
266
|
+
}) {
|
|
267
|
+
if (!root) {
|
|
268
|
+
root = process.env.REACT_ROUTER_ROOT || process.cwd();
|
|
269
|
+
}
|
|
270
|
+
configFile = configFile ?? findConfig(root, "vite.config", [
|
|
271
|
+
".ts",
|
|
272
|
+
".cts",
|
|
273
|
+
".mts",
|
|
274
|
+
".js",
|
|
275
|
+
".cjs",
|
|
276
|
+
".mjs"
|
|
277
|
+
]);
|
|
278
|
+
if (!configFile) {
|
|
279
|
+
console.error(import_picocolors2.default.red("Vite config file not found"));
|
|
280
|
+
process.exit(1);
|
|
281
|
+
}
|
|
282
|
+
let viteConfig = await resolveViteConfig({ configFile, root });
|
|
283
|
+
let ctx = await extractPluginContext(viteConfig);
|
|
284
|
+
if (!ctx) {
|
|
285
|
+
console.error(
|
|
286
|
+
import_picocolors2.default.red("React Router Vite plugin not found in Vite config")
|
|
287
|
+
);
|
|
288
|
+
process.exit(1);
|
|
289
|
+
}
|
|
290
|
+
return ctx;
|
|
291
|
+
}
|
|
292
|
+
var serverBuildId = id("server-build");
|
|
293
|
+
var serverManifestId = id("server-manifest");
|
|
294
|
+
var browserManifestId = id("browser-manifest");
|
|
295
|
+
var hmrRuntimeId = id("hmr-runtime");
|
|
296
|
+
var injectHmrRuntimeId = id("inject-hmr-runtime");
|
|
297
|
+
var defaultEntriesDir = path4.resolve(
|
|
298
|
+
path4.dirname(require.resolve("@react-router/dev/package.json")),
|
|
299
|
+
"dist",
|
|
300
|
+
"config",
|
|
301
|
+
"defaults"
|
|
302
|
+
);
|
|
303
|
+
var defaultEntries = fse2.readdirSync(defaultEntriesDir).map((filename2) => path4.join(defaultEntriesDir, filename2));
|
|
304
|
+
invariant(defaultEntries.length > 0, "No default entries found");
|
|
305
|
+
function findConfig(dir, basename3, extensions) {
|
|
306
|
+
for (let ext of extensions) {
|
|
307
|
+
let name = basename3 + ext;
|
|
308
|
+
let file = path4.join(dir, name);
|
|
309
|
+
if (fse2.existsSync(file)) return file;
|
|
310
|
+
}
|
|
311
|
+
return void 0;
|
|
312
|
+
}
|
|
313
|
+
var REACT_REFRESH_HEADER = `
|
|
314
|
+
import RefreshRuntime from "${hmrRuntimeId}";
|
|
15
315
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
316
|
+
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
|
|
317
|
+
let prevRefreshReg;
|
|
318
|
+
let prevRefreshSig;
|
|
319
|
+
|
|
320
|
+
if (import.meta.hot && !inWebWorker) {
|
|
321
|
+
if (!window.__vite_plugin_react_preamble_installed__) {
|
|
322
|
+
throw new Error(
|
|
323
|
+
"React Router Vite plugin can't detect preamble. Something is wrong."
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
prevRefreshReg = window.$RefreshReg$;
|
|
328
|
+
prevRefreshSig = window.$RefreshSig$;
|
|
329
|
+
window.$RefreshReg$ = (type, id) => {
|
|
330
|
+
RefreshRuntime.register(type, __SOURCE__ + " " + id)
|
|
331
|
+
};
|
|
332
|
+
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
|
333
|
+
}`.trim();
|
|
334
|
+
var REACT_REFRESH_FOOTER = `
|
|
335
|
+
if (import.meta.hot && !inWebWorker) {
|
|
336
|
+
window.$RefreshReg$ = prevRefreshReg;
|
|
337
|
+
window.$RefreshSig$ = prevRefreshSig;
|
|
338
|
+
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
|
|
339
|
+
RefreshRuntime.registerExportsForReactRefresh(__SOURCE__, currentExports);
|
|
340
|
+
import.meta.hot.accept((nextExports) => {
|
|
341
|
+
if (!nextExports) return;
|
|
342
|
+
__ROUTE_ID__ && window.__reactRouterRouteModuleUpdates.set(__ROUTE_ID__, nextExports);
|
|
343
|
+
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports, __ACCEPT_EXPORTS__);
|
|
344
|
+
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
}`.trim();
|
|
348
|
+
|
|
349
|
+
// typescript/typegen.ts
|
|
350
|
+
function getDirectory(ctx) {
|
|
351
|
+
return Path.join(ctx.rootDirectory, ".react-router/types");
|
|
352
|
+
}
|
|
353
|
+
function getPath(ctx, route) {
|
|
354
|
+
return Path.join(
|
|
355
|
+
getDirectory(ctx),
|
|
356
|
+
Path.basename(ctx.appDirectory),
|
|
357
|
+
Path.dirname(route.file),
|
|
358
|
+
"+types." + Pathe.filename(route.file) + ".d.ts"
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
async function watch(rootDirectory) {
|
|
362
|
+
const vitePluginCtx = await loadPluginContext({ root: rootDirectory });
|
|
363
|
+
const routesTsPath = Path.join(
|
|
364
|
+
vitePluginCtx.reactRouterConfig.appDirectory,
|
|
365
|
+
"routes.ts"
|
|
366
|
+
);
|
|
367
|
+
const routesViteNodeContext = await createContext({
|
|
368
|
+
root: rootDirectory
|
|
369
|
+
});
|
|
370
|
+
async function getRoutes() {
|
|
371
|
+
const routes = {};
|
|
372
|
+
const rootRouteFile = findEntry(
|
|
373
|
+
vitePluginCtx.reactRouterConfig.appDirectory,
|
|
374
|
+
"root"
|
|
375
|
+
);
|
|
376
|
+
if (rootRouteFile) {
|
|
377
|
+
routes.root = { path: "", id: "root", file: rootRouteFile };
|
|
29
378
|
}
|
|
30
|
-
|
|
31
|
-
|
|
379
|
+
routesViteNodeContext.devServer.moduleGraph.invalidateAll();
|
|
380
|
+
routesViteNodeContext.runner.moduleCache.clear();
|
|
381
|
+
const routeConfig = (await routesViteNodeContext.runner.executeFile(routesTsPath)).routes;
|
|
382
|
+
return {
|
|
383
|
+
...routes,
|
|
384
|
+
...configRoutesToRouteManifest(await routeConfig)
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
const ctx = {
|
|
388
|
+
rootDirectory,
|
|
389
|
+
appDirectory: vitePluginCtx.reactRouterConfig.appDirectory,
|
|
390
|
+
routes: await getRoutes()
|
|
391
|
+
};
|
|
392
|
+
await writeAll(ctx);
|
|
393
|
+
const watcher = import_chokidar.default.watch(ctx.appDirectory, { ignoreInitial: true });
|
|
394
|
+
watcher.on("all", async (event, path5) => {
|
|
395
|
+
path5 = Path.normalize(path5);
|
|
396
|
+
ctx.routes = await getRoutes();
|
|
397
|
+
const routeConfigChanged = Boolean(
|
|
398
|
+
routesViteNodeContext.devServer.moduleGraph.getModuleById(path5)
|
|
399
|
+
);
|
|
400
|
+
if (routeConfigChanged) {
|
|
401
|
+
await writeAll(ctx);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
const isRoute = Object.values(ctx.routes).find(
|
|
405
|
+
(route) => path5 === Path.join(ctx.appDirectory, route.file)
|
|
406
|
+
);
|
|
407
|
+
if (isRoute && (event === "add" || event === "unlink")) {
|
|
408
|
+
await writeAll(ctx);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
async function writeAll(ctx) {
|
|
414
|
+
import_node_fs.default.rmSync(getDirectory(ctx), { recursive: true, force: true });
|
|
415
|
+
Object.values(ctx.routes).forEach((route) => {
|
|
416
|
+
if (!import_node_fs.default.existsSync(Path.join(ctx.appDirectory, route.file))) return;
|
|
417
|
+
const typesPath = getPath(ctx, route);
|
|
418
|
+
const content = getModule(ctx.routes, route);
|
|
419
|
+
import_node_fs.default.mkdirSync(Path.dirname(typesPath), { recursive: true });
|
|
420
|
+
import_node_fs.default.writeFileSync(typesPath, content);
|
|
421
|
+
});
|
|
32
422
|
}
|
|
423
|
+
function getModule(routes, route) {
|
|
424
|
+
return import_dedent2.default`
|
|
425
|
+
// React Router generated types for route:
|
|
426
|
+
// ${route.file}
|
|
427
|
+
|
|
428
|
+
import * as T from "react-router/types"
|
|
429
|
+
|
|
430
|
+
export type Params = {${formattedParamsProperties(routes, route)}}
|
|
33
431
|
|
|
34
|
-
|
|
432
|
+
type Route = typeof import("./${Pathe.filename(route.file)}")
|
|
35
433
|
|
|
36
|
-
|
|
434
|
+
export type LoaderData = T.CreateLoaderData<Route>
|
|
435
|
+
export type ActionData = T.CreateActionData<Route>
|
|
436
|
+
|
|
437
|
+
export type LoaderArgs = T.CreateServerLoaderArgs<Params>
|
|
438
|
+
export type ClientLoaderArgs = T.CreateClientLoaderArgs<Params, Route>
|
|
439
|
+
export type ActionArgs = T.CreateServerActionArgs<Params>
|
|
440
|
+
export type ClientActionArgs = T.CreateClientActionArgs<Params, Route>
|
|
441
|
+
|
|
442
|
+
export type HydrateFallbackProps = T.CreateHydrateFallbackProps<Params>
|
|
443
|
+
export type ComponentProps = T.CreateComponentProps<Params, LoaderData, ActionData>
|
|
444
|
+
export type ErrorBoundaryProps = T.CreateErrorBoundaryProps<Params, LoaderData, ActionData>
|
|
445
|
+
`;
|
|
446
|
+
}
|
|
447
|
+
function formattedParamsProperties(routes, route) {
|
|
448
|
+
const urlpath = routeLineage(routes, route).map((route2) => route2.path).join("/");
|
|
449
|
+
const params = parseParams(urlpath);
|
|
450
|
+
const indent = " ".repeat(3);
|
|
451
|
+
const properties = Object.entries(params).map(([name, values]) => {
|
|
452
|
+
if (values.length === 1) {
|
|
453
|
+
const isOptional = values[0];
|
|
454
|
+
return indent + (isOptional ? `${name}?: string` : `${name}: string`);
|
|
455
|
+
}
|
|
456
|
+
const items = values.map(
|
|
457
|
+
(isOptional) => isOptional ? "string | undefined" : "string"
|
|
458
|
+
);
|
|
459
|
+
return indent + `${name}: [${items.join(", ")}]`;
|
|
460
|
+
});
|
|
461
|
+
const body = properties.length === 0 ? "" : "\n" + properties.join("\n") + "\n";
|
|
462
|
+
return body;
|
|
463
|
+
}
|
|
464
|
+
function routeLineage(routes, route) {
|
|
465
|
+
const result = [];
|
|
466
|
+
while (route) {
|
|
467
|
+
result.push(route);
|
|
468
|
+
if (!route.parentId) break;
|
|
469
|
+
route = routes[route.parentId];
|
|
470
|
+
}
|
|
471
|
+
result.reverse();
|
|
472
|
+
return result;
|
|
473
|
+
}
|
|
474
|
+
function parseParams(urlpath) {
|
|
475
|
+
const result = {};
|
|
476
|
+
let segments = urlpath.split("/");
|
|
477
|
+
segments.filter((s) => s.startsWith(":")).forEach((param) => {
|
|
478
|
+
param = param.slice(1);
|
|
479
|
+
let isOptional = param.endsWith("?");
|
|
480
|
+
if (isOptional) {
|
|
481
|
+
param = param.slice(0, -1);
|
|
482
|
+
}
|
|
483
|
+
result[param] ??= [];
|
|
484
|
+
result[param].push(isOptional);
|
|
485
|
+
return;
|
|
486
|
+
});
|
|
487
|
+
return result;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// typescript/plugin.ts
|
|
37
491
|
function init() {
|
|
38
492
|
function create(info) {
|
|
39
|
-
const {
|
|
40
|
-
logger
|
|
41
|
-
} = info.project.projectService;
|
|
493
|
+
const { logger } = info.project.projectService;
|
|
42
494
|
logger.info("[react-router] setup");
|
|
43
|
-
const rootDirectory =
|
|
44
|
-
|
|
495
|
+
const rootDirectory = Path2.normalize(info.project.getCurrentDirectory());
|
|
496
|
+
watch(rootDirectory);
|
|
45
497
|
return info.languageService;
|
|
46
498
|
}
|
|
47
|
-
return {
|
|
48
|
-
create
|
|
49
|
-
};
|
|
499
|
+
return { create };
|
|
50
500
|
}
|
|
51
|
-
|
|
52
|
-
module.exports = init;
|
|
@@ -1 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
import { AppLoadContext } from 'react-router';
|
|
2
|
+
import { Plugin } from 'vite';
|
|
3
|
+
import { GetPlatformProxyOptions, PlatformProxy } from 'wrangler';
|
|
4
|
+
|
|
5
|
+
type CfProperties = Record<string, unknown>;
|
|
6
|
+
type LoadContext<Env, Cf extends CfProperties> = {
|
|
7
|
+
cloudflare: Omit<PlatformProxy<Env, Cf>, "dispose">;
|
|
8
|
+
};
|
|
9
|
+
type GetLoadContext<Env, Cf extends CfProperties> = (args: {
|
|
10
|
+
request: Request;
|
|
11
|
+
context: LoadContext<Env, Cf>;
|
|
12
|
+
}) => AppLoadContext | Promise<AppLoadContext>;
|
|
13
|
+
/**
|
|
14
|
+
* Vite plugin that provides [Node proxies to local workerd
|
|
15
|
+
* bindings](https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy)
|
|
16
|
+
* to `context.cloudflare` in your server loaders and server actions during
|
|
17
|
+
* development.
|
|
18
|
+
*/
|
|
19
|
+
declare const cloudflareDevProxyVitePlugin: <Env, Cf extends CfProperties>(options?: {
|
|
20
|
+
getLoadContext?: GetLoadContext<Env, Cf>;
|
|
21
|
+
} & GetPlatformProxyOptions) => Plugin;
|
|
22
|
+
|
|
23
|
+
export { cloudflareDevProxyVitePlugin as cloudflareDevProxy };
|