Skip to content

Commit ead7e94

Browse files
Warn on client source maps in RSC Framework Mode (#14307)
1 parent 64f8b8a commit ead7e94

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

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

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
init as initEsModuleLexer,
3232
parse as esModuleLexer,
3333
} from "es-module-lexer";
34-
import { escapePath as escapePathAsGlob } from "tinyglobby";
3534
import pick from "lodash/pick";
3635
import jsesc from "jsesc";
3736
import colors from "picocolors";
@@ -82,7 +81,8 @@ import {
8281
} from "../config/config";
8382
import { getOptimizeDepsEntries } from "./optimize-deps-entries";
8483
import { decorateComponentExportsWithProps } from "./with-props";
85-
import validatePluginOrder from "./plugins/validate-plugin-order";
84+
import { validatePluginOrder } from "./plugins/validate-plugin-order";
85+
import { warnOnClientSourceMaps } from "./plugins/warn-on-client-source-maps";
8686

8787
export type LoadCssContents = (
8888
viteDevServer: Vite.ViteDevServer,
@@ -1488,34 +1488,6 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
14881488
cssModulesManifest[id] = code;
14891489
}
14901490
},
1491-
buildStart() {
1492-
invariant(viteConfig);
1493-
1494-
if (
1495-
viteCommand === "build" &&
1496-
viteConfig.mode === "production" &&
1497-
!viteConfig.build.ssr &&
1498-
viteConfig.build.sourcemap
1499-
) {
1500-
viteConfig.logger.warn(
1501-
colors.yellow(
1502-
"\n" +
1503-
colors.bold(" ⚠️ Source maps are enabled in production\n") +
1504-
[
1505-
"This makes your server code publicly",
1506-
"visible in the browser. This is highly",
1507-
"discouraged! If you insist, ensure that",
1508-
"you are using environment variables for",
1509-
"secrets and not hard-coding them in",
1510-
"your source code.",
1511-
]
1512-
.map((line) => " " + line)
1513-
.join("\n") +
1514-
"\n",
1515-
),
1516-
);
1517-
}
1518-
},
15191491
async configureServer(viteDevServer) {
15201492
setDevServerHooks({
15211493
// Give the request handler access to the critical CSS in dev to avoid a
@@ -2377,6 +2349,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
23772349
},
23782350
},
23792351
validatePluginOrder(),
2352+
warnOnClientSourceMaps(),
23802353
];
23812354
};
23822355

packages/react-router-dev/vite/plugins/validate-plugin-order.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type * as Vite from "vite";
22

3-
export default function validatePluginOrder(): Vite.Plugin {
3+
export function validatePluginOrder(): Vite.Plugin {
44
return {
55
name: "react-router:validate-plugin-order",
66
configResolved(viteConfig) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type * as Vite from "vite";
2+
import colors from "picocolors";
3+
import invariant from "../../invariant";
4+
5+
export function warnOnClientSourceMaps(): Vite.Plugin {
6+
let viteConfig: Vite.ResolvedConfig;
7+
let viteCommand: Vite.ConfigEnv["command"];
8+
let logged = false;
9+
10+
return {
11+
name: "react-router:warn-on-client-source-maps",
12+
config(_, configEnv) {
13+
viteCommand = configEnv.command;
14+
},
15+
configResolved(config) {
16+
viteConfig = config;
17+
},
18+
buildStart() {
19+
invariant(viteConfig);
20+
21+
if (
22+
!logged &&
23+
viteCommand === "build" &&
24+
viteConfig.mode === "production" &&
25+
!viteConfig.build.ssr &&
26+
(viteConfig.build.sourcemap ||
27+
viteConfig.environments?.client?.build.sourcemap)
28+
) {
29+
viteConfig.logger.warn(
30+
colors.yellow(
31+
"\n" +
32+
colors.bold(" ⚠️ Source maps are enabled in production\n") +
33+
[
34+
"This makes your server code publicly",
35+
"visible in the browser. This is highly",
36+
"discouraged! If you insist, ensure that",
37+
"you are using environment variables for",
38+
"secrets and not hard-coding them in",
39+
"your source code.",
40+
]
41+
.map((line) => " " + line)
42+
.join("\n") +
43+
"\n",
44+
),
45+
);
46+
logged = true;
47+
}
48+
},
49+
};
50+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
isVirtualClientRouteModuleId,
2424
CLIENT_NON_COMPONENT_EXPORTS,
2525
} from "./virtual-route-modules";
26-
import validatePluginOrder from "../plugins/validate-plugin-order";
26+
import { validatePluginOrder } from "../plugins/validate-plugin-order";
27+
import { warnOnClientSourceMaps } from "../plugins/warn-on-client-source-maps";
2728

2829
export function reactRouterRSCVitePlugin(): Vite.PluginOption[] {
2930
let configLoader: ConfigLoader;
@@ -480,6 +481,7 @@ export function reactRouterRSCVitePlugin(): Vite.PluginOption[] {
480481
},
481482
},
482483
validatePluginOrder(),
484+
warnOnClientSourceMaps(),
483485
];
484486
}
485487

0 commit comments

Comments
 (0)