Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(remix-dev/vite): warn on source maps in build #8222

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sour-snakes-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Vite: Show warning when source maps are enabled in production build
56 changes: 39 additions & 17 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ const getRouteModuleExports = async (
const showUnstableWarning = () => {
console.warn(
colors.yellow(
"\n ⚠️ Remix support for Vite is unstable\n and not recommended for production\n"
colors.bold(
"\n ⚠️ Remix support for Vite is unstable and\n not yet recommended for production\n"
)
)
);
};
Expand All @@ -295,7 +297,7 @@ export type RemixVitePlugin = (
export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
let viteCommand: Vite.ResolvedConfig["command"];
let viteUserConfig: Vite.UserConfig;
let resolvedViteConfig: Vite.ResolvedConfig | undefined;
let viteConfig: Vite.ResolvedConfig | undefined;

let isViteV4 = getViteMajorVersion() === 4;

Expand Down Expand Up @@ -640,10 +642,10 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
}),
};
},
async configResolved(viteConfig) {
async configResolved(resolvedViteConfig) {
await initEsModuleLexer;

resolvedViteConfig = viteConfig;
viteConfig = resolvedViteConfig;

ssrBuildContext =
viteConfig.build.ssr && viteCommand === "build"
Expand Down Expand Up @@ -719,9 +721,38 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
}
},
buildStart() {
if (viteCommand === "build") {
invariant(viteConfig);

if (
viteCommand === "build" &&
// Only show warning on initial client build
!viteConfig.build.ssr
) {
showUnstableWarning();
}

if (
viteCommand === "build" &&
viteConfig.mode === "production" &&
!viteConfig.build.ssr &&
viteConfig.build.sourcemap
) {
viteConfig.logger.warn(
colors.yellow(
colors.bold(" ⚠️ Source maps are enabled in production\n") +
[
"This makes your server code publicly",
"visible in the browser. This is highly",
"discouraged! If you insist, ensure that",
"you are using environment variables for",
"secrets and not hard-coding them in",
"your source code.\n",
]
.map((line) => " " + line)
.join("\n")
)
);
}
},
configureServer(viteDevServer) {
viteDevServer.httpServer?.on("listening", () => {
Expand Down Expand Up @@ -814,15 +845,8 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
return;
}

invariant(
cachedPluginConfig,
"Expected plugin config to be cached when writeBundle hook is called"
);

invariant(
resolvedViteConfig,
"Expected resolvedViteConfig to exist when writeBundle hook is called"
);
invariant(cachedPluginConfig);
invariant(viteConfig);

let { assetsBuildDirectory, serverBuildPath, rootDirectory } =
cachedPluginConfig;
Expand Down Expand Up @@ -872,10 +896,8 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
)
);

let logger = resolvedViteConfig.logger;

if (movedAssetPaths.length) {
logger.info(
viteConfig.logger.info(
[
"",
`${colors.green("✓")} ${movedAssetPaths.length} asset${
Expand Down