From c4456555a8860f555747e406014bcacfabe50c52 Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Wed, 6 Dec 2023 15:02:20 +1100 Subject: [PATCH] fix(remix-dev/vite): warn on source maps in build --- .changeset/sour-snakes-melt.md | 5 +++ packages/remix-dev/vite/plugin.ts | 56 +++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 .changeset/sour-snakes-melt.md diff --git a/.changeset/sour-snakes-melt.md b/.changeset/sour-snakes-melt.md new file mode 100644 index 00000000000..8a238697707 --- /dev/null +++ b/.changeset/sour-snakes-melt.md @@ -0,0 +1,5 @@ +--- +"@remix-run/dev": patch +--- + +Vite: Show warning when source maps are enabled in production build diff --git a/packages/remix-dev/vite/plugin.ts b/packages/remix-dev/vite/plugin.ts index 4cbd90e6c2b..04e8debb9a6 100644 --- a/packages/remix-dev/vite/plugin.ts +++ b/packages/remix-dev/vite/plugin.ts @@ -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" + ) ) ); }; @@ -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; @@ -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" @@ -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", () => { @@ -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; @@ -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${