Skip to content

Commit fad1484

Browse files
committed
Honor --mode flag in routes.ts context
1 parent d1aaa2d commit fad1484

File tree

7 files changed

+54
-14
lines changed

7 files changed

+54
-14
lines changed

.changeset/dirty-balloons-stare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-router/dev": patch
3+
---
4+
5+
Ensure `--mode` flag is honored within `routes.ts` when accessing via `import.meta.env.MODE`

packages/react-router-dev/cli/commands.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ export async function routes(
2121
flags: {
2222
config?: string;
2323
json?: boolean;
24+
mode?: string;
2425
} = {}
2526
): Promise<void> {
2627
let rootDirectory = reactRouterRoot ?? process.cwd();
27-
let configResult = await loadConfig({ rootDirectory });
28+
let configResult = await loadConfig({
29+
rootDirectory,
30+
mode: flags.mode ?? "production",
31+
});
2832

2933
if (!configResult.ok) {
3034
console.error(colors.red(configResult.error));
@@ -81,6 +85,7 @@ export async function generateEntry(
8185
flags: {
8286
typescript?: boolean;
8387
config?: string;
88+
mode?: string;
8489
} = {}
8590
) {
8691
// if no entry passed, attempt to create both
@@ -91,7 +96,10 @@ export async function generateEntry(
9196
}
9297

9398
let rootDirectory = reactRouterRoot ?? process.cwd();
94-
let configResult = await loadConfig({ rootDirectory });
99+
let configResult = await loadConfig({
100+
rootDirectory,
101+
mode: flags.mode ?? "production",
102+
});
95103

96104
if (!configResult.ok) {
97105
console.error(colors.red(configResult.error));
@@ -198,17 +206,25 @@ async function createClientEntry(
198206
return contents;
199207
}
200208

201-
export async function typegen(root: string, flags: { watch: boolean }) {
209+
export async function typegen(
210+
root: string,
211+
flags: {
212+
watch: boolean;
213+
mode?: string;
214+
}
215+
) {
202216
root ??= process.cwd();
203217

218+
let mode = flags.mode ?? "production";
219+
204220
if (flags.watch) {
205221
await preloadVite();
206222
const vite = getVite();
207223
const logger = vite.createLogger("info", { prefix: "[react-router]" });
208224

209-
await Typegen.watch(root, { logger });
225+
await Typegen.watch(root, { mode, logger });
210226
await new Promise(() => {}); // keep alive
211227
return;
212228
}
213-
await Typegen.run(root);
229+
await Typegen.run(root, { mode });
214230
}

packages/react-router-dev/config/config.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,15 +545,17 @@ export type ConfigLoader = {
545545
export async function createConfigLoader({
546546
rootDirectory: root,
547547
watch,
548+
mode,
548549
}: {
549550
watch: boolean;
550551
rootDirectory?: string;
552+
mode: string;
551553
}): Promise<ConfigLoader> {
552554
root = root ?? process.env.REACT_ROUTER_ROOT ?? process.cwd();
553555

554556
let viteNodeContext = await ViteNode.createContext({
555557
root,
556-
mode: watch ? "development" : "production",
558+
mode,
557559
});
558560

559561
let reactRouterConfigFile = findEntry(root, "react-router.config", {
@@ -656,9 +658,16 @@ export async function createConfigLoader({
656658
};
657659
}
658660

659-
export async function loadConfig({ rootDirectory }: { rootDirectory: string }) {
661+
export async function loadConfig({
662+
rootDirectory,
663+
mode,
664+
}: {
665+
rootDirectory: string;
666+
mode: string;
667+
}) {
660668
let configLoader = await createConfigLoader({
661669
rootDirectory,
670+
mode,
662671
watch: false,
663672
});
664673
let config = await configLoader.getConfig();

packages/react-router-dev/typegen/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { getTypesDir, getTypesPath } from "./paths";
1414
import * as Params from "./params";
1515
import * as Route from "./route";
1616

17-
export async function run(rootDirectory: string) {
18-
const ctx = await createContext({ rootDirectory, watch: false });
17+
export async function run(rootDirectory: string, { mode }: { mode: string }) {
18+
const ctx = await createContext({ rootDirectory, mode, watch: false });
1919
await writeAll(ctx);
2020
}
2121

@@ -25,9 +25,9 @@ export type Watcher = {
2525

2626
export async function watch(
2727
rootDirectory: string,
28-
{ logger }: { logger?: vite.Logger } = {}
28+
{ mode, logger }: { mode: string; logger?: vite.Logger }
2929
): Promise<Watcher> {
30-
const ctx = await createContext({ rootDirectory, watch: true });
30+
const ctx = await createContext({ rootDirectory, mode, watch: true });
3131
await writeAll(ctx);
3232
logger?.info(pc.green("generated types"), { timestamp: true, clear: true });
3333

@@ -55,11 +55,13 @@ export async function watch(
5555
async function createContext({
5656
rootDirectory,
5757
watch,
58+
mode,
5859
}: {
5960
rootDirectory: string;
6061
watch: boolean;
62+
mode: string;
6163
}): Promise<Context> {
62-
const configLoader = await createConfigLoader({ rootDirectory, watch });
64+
const configLoader = await createConfigLoader({ rootDirectory, mode, watch });
6365
const configResult = await configLoader.getConfig();
6466

6567
if (!configResult.ok) {

packages/react-router-dev/vite/build.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export async function build(root: string, viteBuildOptions: ViteBuildOptions) {
3535
await preloadVite();
3636
let vite = getVite();
3737

38-
let configResult = await loadConfig({ rootDirectory: root });
38+
let configResult = await loadConfig({
39+
rootDirectory: root,
40+
mode: viteBuildOptions.mode ?? "production",
41+
});
3942

4043
if (!configResult.ok) {
4144
throw new Error(configResult.error);

packages/react-router-dev/vite/cloudflare-dev-proxy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>(
5757

5858
return {
5959
name: PLUGIN_NAME,
60-
config: async (config) => {
60+
config: async (config, configEnv) => {
6161
await preloadVite();
6262
const vite = getVite();
6363
// This is a compatibility layer for Vite 5. Default conditions were
@@ -74,6 +74,7 @@ export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>(
7474

7575
let configResult = await loadConfig({
7676
rootDirectory: config.root ?? process.cwd(),
77+
mode: configEnv.mode,
7778
});
7879

7980
if (!configResult.ok) {

packages/react-router-dev/vite/plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,15 +1187,19 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
11871187
rootDirectory =
11881188
viteUserConfig.root ?? process.env.REACT_ROUTER_ROOT ?? process.cwd();
11891189

1190+
let mode = viteConfigEnv.mode;
1191+
11901192
if (viteCommand === "serve") {
11911193
typegenWatcherPromise = Typegen.watch(rootDirectory, {
1194+
mode,
11921195
// ignore `info` logs from typegen since they are redundant when Vite plugin logs are active
11931196
logger: vite.createLogger("warn", { prefix: "[react-router]" }),
11941197
});
11951198
}
11961199

11971200
reactRouterConfigLoader = await createConfigLoader({
11981201
rootDirectory,
1202+
mode,
11991203
watch: viteCommand === "serve",
12001204
});
12011205

0 commit comments

Comments
 (0)