perf(build): 운영 빌드가 react-router의 운영 빌드를 싣는다
문제: react-router 7.18.1의 exports는 모든 조건에서
dist/development/*를 가리킨다. 그 파일은 ENABLE_DEV_WARNINGS = true라
운영 번들의 초기 청크에 라우터 개발 경고 문구("You rendered descendant
…")가 들어 있었다. 운영 콘솔에 개발 경고가 뜨고, 초기 JS가 약 600 B 크다.
고른 방법: vite build일 때만 react-router-dom이 import하는 두 진입점
(react-router, react-router/dom)을 같은 패키지의 dist/production 파일로
alias한다. 경로는 react-router-dom에서 react-router를 resolve한 개발
진입점 옆에서 찾고, 파일이 없으면 빌드를 멈춘다. 개발 서버와 테스트는
개발 빌드를 그대로 써서 경고를 받는다.
버린 대안: 고정 경로 alias(리뷰 실험). 업그레이드로 배치가 바뀌면
조용히 깨진다. react-router에 production 조건을 요청하기는 시점을 정할
수 없어 이 alias를 지울 조건으로만 적었다.
위험·한계: exports 밖 경로를 쓴다. 업그레이드 때 배치가 바뀌면 빌드가
실패한다(조용히 개발 빌드로 돌아가지 않는다). Firefox·WebKit e2e는
돌리지 않았다.
검증: tests/unit/build-environment-bundle.test.ts가 빌드한 JS에 개발
경고 문구가 없음을 본다(고치기 전 chunk-KS7C4IRE 1개에서 실패).
같은 빌드(local-build) 초기 JS 201,552 → 200,939 B(−613 B), 파일 25개.
라우터를 쓰는 e2e 5개 spec 14건(app-shell, reference-route,
reference-save, scroll-restoration, platform-overview, chromium) 통과.
Refs: docs/reviews/2026-11-03-maturity-review/15-performance.md 15-07
This commit is contained in:
@@ -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 () => {
|
||||
|
||||
+34
-1
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user