@react-router/dev 0.0.0-experimental-4d16b23b6 → 0.0.0-experimental-4d6793aa7

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- let arg = require("arg");
2
+ import arg from "arg";
3
3
 
4
4
  // Minimal replication of our actual parsing in `run.ts`. If not already set,
5
5
  // default `NODE_ENV` so React loads the proper version in it's CJS entry script.
@@ -12,4 +12,6 @@ if (args._[0] === "dev") {
12
12
  process.env.NODE_ENV = process.env.NODE_ENV ?? "production";
13
13
  }
14
14
 
15
- require("./dist/cli/index");
15
+ (async () => {
16
+ await import("./dist/cli/index.js");
17
+ })();
@@ -0,0 +1,239 @@
1
+ /**
2
+ * @react-router/dev v0.0.0-experimental-4d6793aa7
3
+ *
4
+ * Copyright (c) Remix Software Inc.
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ import {
12
+ configRouteToBranchRoute,
13
+ extractPluginContext,
14
+ getServerBuildDirectory,
15
+ resolveViteConfig
16
+ } from "./chunk-B25E3JGK.js";
17
+ import "./chunk-B3M3SF27.js";
18
+ import "./chunk-HICCFFJA.js";
19
+ import {
20
+ getVite,
21
+ preloadVite
22
+ } from "./chunk-X234P535.js";
23
+ import {
24
+ invariant
25
+ } from "./chunk-XJYIKBPH.js";
26
+
27
+ // vite/build.ts
28
+ import path from "node:path";
29
+ import fse from "fs-extra";
30
+ import colors from "picocolors";
31
+ function getAddressableRoutes(routes) {
32
+ let nonAddressableIds = /* @__PURE__ */ new Set();
33
+ for (let id in routes) {
34
+ let route = routes[id];
35
+ if (route.index) {
36
+ invariant(
37
+ route.parentId,
38
+ `Expected index route "${route.id}" to have "parentId" set`
39
+ );
40
+ nonAddressableIds.add(route.parentId);
41
+ }
42
+ if (typeof route.path !== "string" && !route.index) {
43
+ nonAddressableIds.add(id);
44
+ }
45
+ }
46
+ return Object.values(routes).filter(
47
+ (route) => !nonAddressableIds.has(route.id)
48
+ );
49
+ }
50
+ function getRouteBranch(routes, routeId) {
51
+ let branch = [];
52
+ let currentRouteId = routeId;
53
+ while (currentRouteId) {
54
+ let route = routes[currentRouteId];
55
+ invariant(route, `Missing route for ${currentRouteId}`);
56
+ branch.push(route);
57
+ currentRouteId = route.parentId;
58
+ }
59
+ return branch.reverse();
60
+ }
61
+ async function getServerBuilds(ctx) {
62
+ let { rootDirectory } = ctx;
63
+ const { routes, serverBuildFile, serverBundles, appDirectory } = ctx.reactRouterConfig;
64
+ let serverBuildDirectory = getServerBuildDirectory(ctx);
65
+ if (!serverBundles) {
66
+ return {
67
+ serverBuilds: [{ ssr: true }],
68
+ buildManifest: { routes }
69
+ };
70
+ }
71
+ let { normalizePath } = await import("vite");
72
+ let resolvedAppDirectory = path.resolve(rootDirectory, appDirectory);
73
+ let rootRelativeRoutes = Object.fromEntries(
74
+ Object.entries(routes).map(([id, route]) => {
75
+ let filePath = path.join(resolvedAppDirectory, route.file);
76
+ let rootRelativeFilePath = normalizePath(
77
+ path.relative(rootDirectory, filePath)
78
+ );
79
+ return [id, { ...route, file: rootRelativeFilePath }];
80
+ })
81
+ );
82
+ let buildManifest = {
83
+ serverBundles: {},
84
+ routeIdToServerBundleId: {},
85
+ routes: rootRelativeRoutes
86
+ };
87
+ let serverBundleBuildConfigById = /* @__PURE__ */ new Map();
88
+ await Promise.all(
89
+ getAddressableRoutes(routes).map(async (route) => {
90
+ let branch = getRouteBranch(routes, route.id);
91
+ let serverBundleId = await serverBundles({
92
+ branch: branch.map(
93
+ (route2) => configRouteToBranchRoute({
94
+ ...route2,
95
+ // Ensure absolute paths are passed to the serverBundles function
96
+ file: path.join(resolvedAppDirectory, route2.file)
97
+ })
98
+ )
99
+ });
100
+ if (typeof serverBundleId !== "string") {
101
+ throw new Error(`The "serverBundles" function must return a string`);
102
+ }
103
+ if (!/^[a-zA-Z0-9-_]+$/.test(serverBundleId)) {
104
+ throw new Error(
105
+ `The "serverBundles" function must only return strings containing alphanumeric characters, hyphens and underscores.`
106
+ );
107
+ }
108
+ buildManifest.routeIdToServerBundleId[route.id] = serverBundleId;
109
+ let relativeServerBundleDirectory = path.relative(
110
+ rootDirectory,
111
+ path.join(serverBuildDirectory, serverBundleId)
112
+ );
113
+ let serverBuildConfig = serverBundleBuildConfigById.get(serverBundleId);
114
+ if (!serverBuildConfig) {
115
+ buildManifest.serverBundles[serverBundleId] = {
116
+ id: serverBundleId,
117
+ file: normalizePath(
118
+ path.join(relativeServerBundleDirectory, serverBuildFile)
119
+ )
120
+ };
121
+ serverBuildConfig = {
122
+ routes: {},
123
+ serverBundleId
124
+ };
125
+ serverBundleBuildConfigById.set(serverBundleId, serverBuildConfig);
126
+ }
127
+ for (let route2 of branch) {
128
+ serverBuildConfig.routes[route2.id] = route2;
129
+ }
130
+ })
131
+ );
132
+ let serverBuilds = Array.from(serverBundleBuildConfigById.values()).map(
133
+ (serverBundleBuildConfig) => {
134
+ let serverBuild = {
135
+ ssr: true,
136
+ serverBundleBuildConfig
137
+ };
138
+ return serverBuild;
139
+ }
140
+ );
141
+ return {
142
+ serverBuilds,
143
+ buildManifest
144
+ };
145
+ }
146
+ async function cleanBuildDirectory(viteConfig, ctx) {
147
+ let buildDirectory = ctx.reactRouterConfig.buildDirectory;
148
+ let isWithinRoot = () => {
149
+ let relativePath = path.relative(ctx.rootDirectory, buildDirectory);
150
+ return !relativePath.startsWith("..") && !path.isAbsolute(relativePath);
151
+ };
152
+ if (viteConfig.build.emptyOutDir ?? isWithinRoot()) {
153
+ await fse.remove(buildDirectory);
154
+ }
155
+ }
156
+ function getViteManifestPaths(ctx, serverBuilds) {
157
+ let buildRelative = (pathname) => path.resolve(ctx.reactRouterConfig.buildDirectory, pathname);
158
+ let viteManifestPaths = [
159
+ "client/.vite/manifest.json",
160
+ ...serverBuilds.map(({ serverBundleBuildConfig }) => {
161
+ let serverBundleId = serverBundleBuildConfig?.serverBundleId;
162
+ let serverBundlePath = serverBundleId ? serverBundleId + "/" : "";
163
+ return `server/${serverBundlePath}.vite/manifest.json`;
164
+ })
165
+ ].map((srcPath) => buildRelative(srcPath));
166
+ return viteManifestPaths;
167
+ }
168
+ async function build(root, {
169
+ assetsInlineLimit,
170
+ clearScreen,
171
+ config: configFile,
172
+ emptyOutDir,
173
+ force,
174
+ logLevel,
175
+ minify,
176
+ mode,
177
+ sourcemapClient,
178
+ sourcemapServer
179
+ }) {
180
+ await preloadVite();
181
+ let viteConfig = await resolveViteConfig({ configFile, mode, root });
182
+ const ctx = await extractPluginContext(viteConfig);
183
+ if (!ctx) {
184
+ console.error(
185
+ colors.red("React Router Vite plugin not found in Vite config")
186
+ );
187
+ process.exit(1);
188
+ }
189
+ let { reactRouterConfig } = ctx;
190
+ let vite = getVite();
191
+ async function viteBuild({
192
+ ssr,
193
+ serverBundleBuildConfig
194
+ }) {
195
+ await vite.build({
196
+ root,
197
+ mode,
198
+ configFile,
199
+ build: {
200
+ assetsInlineLimit,
201
+ emptyOutDir,
202
+ minify,
203
+ ssr,
204
+ sourcemap: ssr ? sourcemapServer : sourcemapClient
205
+ },
206
+ optimizeDeps: { force },
207
+ clearScreen,
208
+ logLevel,
209
+ ...serverBundleBuildConfig ? { __reactRouterServerBundleBuildConfig: serverBundleBuildConfig } : {}
210
+ });
211
+ }
212
+ await cleanBuildDirectory(viteConfig, ctx);
213
+ await viteBuild({ ssr: false });
214
+ let { serverBuilds, buildManifest } = await getServerBuilds(ctx);
215
+ await Promise.all(serverBuilds.map(viteBuild));
216
+ let viteManifestPaths = getViteManifestPaths(ctx, serverBuilds);
217
+ await Promise.all(
218
+ viteManifestPaths.map(async (viteManifestPath) => {
219
+ let manifestExists = await fse.pathExists(viteManifestPath);
220
+ if (!manifestExists) return;
221
+ if (!ctx.viteManifestEnabled) {
222
+ await fse.remove(viteManifestPath);
223
+ }
224
+ let viteDir = path.dirname(viteManifestPath);
225
+ let viteDirFiles = await fse.readdir(viteDir);
226
+ if (viteDirFiles.length === 0) {
227
+ await fse.remove(viteDir);
228
+ }
229
+ })
230
+ );
231
+ await reactRouterConfig.buildEnd?.({
232
+ buildManifest,
233
+ reactRouterConfig,
234
+ viteConfig
235
+ });
236
+ }
237
+ export {
238
+ build
239
+ };