diff --git a/tests/unit/build-environment-bundle.test.ts b/tests/unit/build-environment-bundle.test.ts index 16abbc6d..b3a13f37 100644 --- a/tests/unit/build-environment-bundle.test.ts +++ b/tests/unit/build-environment-bundle.test.ts @@ -90,6 +90,14 @@ describe("build variables in the bundle (13-06)", () => { expect(reactDom(first)).toHaveLength(1); expect(reactDom(first)[0]).not.toMatch(/render-boot-failure/u); expect(reactDom(second)).toEqual(reactDom(first)); + + // 1103/15-07. react-router's exports map resolves every condition to its + // development build; a production build must carry the production one, + // without the router's development warnings. + const developmentWarnings = first.contents + .filter(({ file, text }) => file.endsWith(".js") && text.includes("You rendered descendant")) + .map(({ file }) => path.basename(file)); + expect(developmentWarnings).toEqual([]); }, 120_000); it("refuses a VITE_* variable the build does not declare", async () => { diff --git a/vite.config.ts b/vite.config.ts index 1c411f1a..c2e205a3 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,3 +1,5 @@ +import { existsSync } from "node:fs"; +import { createRequire } from "node:module"; import path from "node:path"; import { defineConfig, loadEnv } from "vite"; @@ -138,13 +140,44 @@ function createFeatureChunkGroups(root: string) { }; } -export default defineConfig(({ mode }) => { +/** + * 1103/15-07. react-router publishes its production build under + * dist/production, but its exports map points every condition at + * dist/development. A production bundle carried the router's development + * warnings (ENABLE_DEV_WARNINGS): 604 gzip bytes of initial JavaScript at + * 15-07 and warnings in the production console. A build resolves the two + * entries react-router-dom imports to the production files next to the + * development entry. If an upgrade moves them, the build stops here rather + * than silently shipping the development build again; + * tests/unit/build-environment-bundle.test.ts checks the bundle. Remove this + * when the package exports a production condition. + */ +function reactRouterProductionAliases(root: string) { + const projectRequire = createRequire(path.join(root, "package.json")); + const routerRequire = createRequire(projectRequire.resolve("react-router-dom")); + const production = path.join(path.dirname(routerRequire.resolve("react-router")), "..", "production"); + const aliases = [ + { find: /^react-router$/u, replacement: path.join(production, "index.mjs") }, + { find: /^react-router\/dom$/u, replacement: path.join(production, "dom-export.mjs") }, + ]; + const missing = aliases.filter(({ replacement }) => !existsSync(replacement)); + if (missing.length > 0) { + throw new Error( + `react-router production build not found: ${missing.map(({ replacement }) => replacement).join(", ")}`, + ); + } + return aliases; +} + +export default defineConfig(({ command, mode }) => { assertDeclaredBuildVariables(mode); const base = routerBasePath(process.env); assertRuntimeConfigUnderBase(process.env, base); return { base, plugins: [react(), tailwindcss(), viteModuleInventoryPlugin()], + // Development keeps the router's warnings. + resolve: command === "build" ? { alias: reactRouterProductionAliases(process.cwd()) } : {}, build: { manifest: true, sourcemap: false,