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: [