문제 공개 셸은 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, 분석 빌드 기준. 공식 수치는 예산 커밋).
33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
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([]);
|
|
});
|
|
});
|