From 0bb8f1df9cf338701bf4e6f8bb83c15a54247ac3 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 12 Nov 2026 22:33:48 +0900 Subject: [PATCH] =?UTF-8?q?perf(build):=20design-system=20=EB=AA=A8?= =?UTF-8?q?=EB=93=88=EC=9D=84=20side=20effect=20=EC=97=86=EC=9D=8C?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=84=A0=EC=96=B8=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제 공개 셸은 design-system/index.ts 배럴에서 Button 몇 개를 가져온다. 번들러는 배럴이 다시 내보내는 모듈을 side effect가 있다고 보고 모두 싣는다. 그래서 공개 첫 화면이 쓰지 않는 navigation·overlays·patterns primitive와 폼 모듈(use-app-form 등)이 초기 JS에 있었다. 고른 방법 - 템플릿 1101/08-08(51941a54)과 같은 treeshake.moduleSideEffects 규칙을 vite.config.ts에 둔다. 같은 vite 8.1.5다. - design-system 파일은 선언만 하고 top-level 효과가 없다. 효과만을 위한 import(`import "x"`)는 이 선언 아래에서 모듈과 같이 사라지므로, tests/unit/design-system-side-effects.test.ts가 그런 import가 없는지 본다. CSS는 bootstrap/main.tsx에서 읽는다. - 배럴을 통해서만 닿던 폼 모듈도 같이 빠진다. 폼 모듈에도 top-level 효과가 없는 것을 확인했다. 위험·한계 - 초기 파일 수가 27개에서 28개로 는다. HTTP/2에서는 진입 청크의 preload 목록으로 병렬로 받는다(템플릿 같은 판단). 검증 - desktop CI env 분석 빌드: 초기 JS 229,765 → 226,554 B(-3,211 B, 분석 빌드 기준. 공식 수치는 예산 커밋). --- tests/unit/design-system-side-effects.test.ts | 32 +++++++++++++++++++ vite.config.ts | 15 +++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/unit/design-system-side-effects.test.ts diff --git a/tests/unit/design-system-side-effects.test.ts b/tests/unit/design-system-side-effects.test.ts new file mode 100644 index 0000000..1b2e8e1 --- /dev/null +++ b/tests/unit/design-system-side-effects.test.ts @@ -0,0 +1,32 @@ +import { readdirSync, readFileSync } from "node:fs"; +import path from "node:path"; + +import { describe, expect, it } from "vitest"; + +const DESIGN_SYSTEM = "src/presentation/design-system"; + +function sourceFiles(directory: string): string[] { + return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => { + const file = path.posix.join(directory, entry.name); + if (entry.isDirectory()) return sourceFiles(file); + return /\.tsx?$/u.test(entry.name) && !/\.stories\.tsx?$/u.test(entry.name) + ? [file] + : []; + }); +} + +/** + * vite.config.ts 가 design-system 모듈을 side effect 없음으로 선언한다(템플릿 1101/08-08). + * 그 아래에서는 `import "x"` 처럼 효과만을 위한 import 가 모듈과 같이 빌드에서 사라진다. + * 그런 import 가 생기면 선언이 틀린 것이다. + */ +describe("design-system modules declared free of side effects", () => { + it("hold no import kept only for its effect", () => { + const files = sourceFiles(DESIGN_SYSTEM); + expect(files.length).toBeGreaterThan(0); + const effectOnly = files.filter((file) => + /^\s*import\s+["'][^"']+["']/mu.test(readFileSync(file, "utf8")), + ); + expect(effectOnly).toEqual([]); + }); +}); diff --git a/vite.config.ts b/vite.config.ts index 6000eeb..003f48a 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -138,6 +138,21 @@ export default defineConfig(({ mode }) => { // Template sync (af3d8e6): the template's chunk groups, then TechLog's // named route chunks, in the template's `codeSplitting` form. rolldownOptions: { + /* + 템플릿 1101/08-08(51941a54)과 같다. design-system 모듈은 컴포넌트·아이콘·토큰 목록을 + 선언만 하고 top-level 효과가 없다. 그렇게 선언해 두면 공개 셸이 배럴(index.ts)에서 + Button 하나를 가져와도, 아무 export 도 안 쓰인 primitive 파일은 초기 청크에서 + 빠진다. 효과만을 위한 import 를 두면 선언 아래에서 모듈과 같이 사라지므로 이 범위에는 + 그런 import 가 없다. CSS 는 bootstrap/main.tsx 에서 읽는다. + */ + treeshake: { + moduleSideEffects: [ + { + test: /[\\/]src[\\/]presentation[\\/]design-system[\\/].+\.tsx?$/, + sideEffects: false, + }, + ], + }, output: { codeSplitting: { groups: [