Skip to content

Commit 4704e00

Browse files
committed
Merge branch 'dev' into markdalgleish/vite-env-build-assets-dir
2 parents 2fbdefc + efdf26c commit 4704e00

File tree

9 files changed

+69
-14
lines changed

9 files changed

+69
-14
lines changed

.changeset/dirty-balloons-stare.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@react-router/dev": patch
3+
---
4+
5+
In a `routes.ts` context, ensure the `--mode` flag is respected for `import.meta.env.MODE`
6+
7+
Previously, `import.meta.env.MODE` within a `routes.ts` context was always `"development"` for the `dev` and `typegen --watch` commands, but otherwise resolved to `"production"`. These defaults are still in place, but if a `--mode` flag is provided, this will now take precedence.

.changeset/little-cups-deliver.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+
When executing `react-router.config.ts` and `routes.ts` with `vite-node`, ensure that PostCSS config files are ignored

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

Lines changed: 20 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
rootDirectory = resolveRootDirectory(rootDirectory, flags);
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
rootDirectory = resolveRootDirectory(rootDirectory, flags);
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));
@@ -212,7 +220,8 @@ async function createClientEntry(
212220
export async function typegen(
213221
root: string,
214222
flags: {
215-
watch?: boolean;
223+
watch: boolean;
224+
mode?: string;
216225
config?: string;
217226
}
218227
) {
@@ -223,9 +232,15 @@ export async function typegen(
223232
const vite = getVite();
224233
const logger = vite.createLogger("info", { prefix: "[react-router]" });
225234

226-
await Typegen.watch(root, { logger });
235+
await Typegen.watch(root, {
236+
mode: flags.mode ?? "development",
237+
logger,
238+
});
227239
await new Promise(() => {}); // keep alive
228240
return;
229241
}
230-
await Typegen.run(root);
242+
243+
await Typegen.run(root, {
244+
mode: flags.mode ?? "production",
245+
});
231246
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,15 +611,17 @@ export type ConfigLoader = {
611611
export async function createConfigLoader({
612612
rootDirectory: root,
613613
watch,
614+
mode,
614615
}: {
615616
watch: boolean;
616617
rootDirectory?: string;
618+
mode: string;
617619
}): Promise<ConfigLoader> {
618620
root = root ?? process.env.REACT_ROUTER_ROOT ?? process.cwd();
619621

620622
let viteNodeContext = await ViteNode.createContext({
621623
root,
622-
mode: watch ? "development" : "production",
624+
mode,
623625
});
624626

625627
let reactRouterConfigFile = findEntry(root, "react-router.config", {
@@ -722,9 +724,16 @@ export async function createConfigLoader({
722724
};
723725
}
724726

725-
export async function loadConfig({ rootDirectory }: { rootDirectory: string }) {
727+
export async function loadConfig({
728+
rootDirectory,
729+
mode,
730+
}: {
731+
rootDirectory: string;
732+
mode: string;
733+
}) {
726734
let configLoader = await createConfigLoader({
727735
rootDirectory,
736+
mode,
728737
watch: false,
729738
});
730739
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
@@ -1190,15 +1190,19 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
11901190
rootDirectory =
11911191
viteUserConfig.root ?? process.env.REACT_ROUTER_ROOT ?? process.cwd();
11921192

1193+
let mode = viteConfigEnv.mode;
1194+
11931195
if (viteCommand === "serve") {
11941196
typegenWatcherPromise = Typegen.watch(rootDirectory, {
1197+
mode,
11951198
// ignore `info` logs from typegen since they are redundant when Vite plugin logs are active
11961199
logger: vite.createLogger("warn", { prefix: "[react-router]" }),
11971200
});
11981201
}
11991202

12001203
reactRouterConfigLoader = await createConfigLoader({
12011204
rootDirectory,
1205+
mode,
12021206
watch: viteCommand === "serve",
12031207
});
12041208

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ export async function createContext({
3636
optimizeDeps: {
3737
noDiscovery: true,
3838
},
39+
css: {
40+
// This empty PostCSS config object prevents the PostCSS config file from
41+
// being loaded. We don't need it in a React Router config context, and
42+
// there's also an issue in Vite 5 when using a .ts PostCSS config file in
43+
// an ESM project: https://github.com/vitejs/vite/issues/15869. Consumers
44+
// can work around this in their own Vite config file, but they can't
45+
// configure this internal usage of vite-node.
46+
postcss: {},
47+
},
3948
configFile: false,
4049
envFile: false,
4150
plugins: [],

0 commit comments

Comments
 (0)