Skip to content

feat(bundler): CJS wrapping with __commonJS runtime helper#249

Merged
ohah merged 2 commits intomainfrom
feat/cjs-wrapping
Mar 22, 2026
Merged

feat(bundler): CJS wrapping with __commonJS runtime helper#249
ohah merged 2 commits intomainfrom
feat/cjs-wrapping

Conversation

@ohah
Copy link
Copy Markdown
Owner

@ohah ohah commented Mar 22, 2026

Summary

CJS interop PR2: CJS 모듈을 esbuild 방식 __commonJS 팩토리 함수로 래핑.

  • WrapKind enum (none/cjs) + makeRequireVarName 유틸
  • CJS 모듈: var require_lib = __commonJS({ "lib.cjs"(exports, module) { ... } }); 으로 래핑
  • ESM→CJS import: var lib = require_lib(); preamble 자동 생성
  • __commonJS 런타임 헬퍼 자동 주입 (CJS 모듈 존재 시에만)
  • CJS 모듈은 스코프 호이스팅 스킵 (클로저 내부이므로 이름 충돌 없음)

Changes

  • types.zig: WrapKind enum, makeRequireVarName
  • module.zig: wrap_kind 필드
  • graph.zig: exports_kind=commonjswrap_kind=cjs
  • emitter.zig: 런타임 헬퍼 주입 + CJS 래핑 + preamble 삽입
  • linker.zig: CJS 스코프 호이스팅 스킵 + ESM→CJS preamble

Test plan

  • CJS 모듈 __commonJS 래핑
  • ESM default import from CJS
  • ESM named import from CJS
  • 순수 ESM 번들에 런타임 헬퍼 미주입
  • ESM + CJS 혼합 번들
  • CJS require chain (CJS→CJS)
  • 기존 테스트 전부 통과

🤖 Generated with Claude Code

CJS interop PR2: CJS 모듈을 __commonJS 팩토리 함수로 래핑.

- types.zig: WrapKind enum (none/cjs) + makeRequireVarName 유틸
- module.zig: wrap_kind 필드 추가
- graph.zig: exports_kind=commonjs → wrap_kind=cjs 자동 설정
- emitter.zig: __commonJS 런타임 헬퍼 주입 + CJS 모듈 래핑 + preamble 삽입
- linker.zig: CJS 모듈 스코프 호이스팅 스킵 + ESM→CJS import preamble 생성
  - default/namespace: var <local> = require_<target>();
  - named: var <local> = require_<target>().<imported>;

테스트 6개 추가

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ohah ohah added the enhancement New feature or request label Mar 22, 2026
@ohah ohah self-assigned this Mar 22, 2026
@ohah ohah added the enhancement New feature or request label Mar 22, 2026
- fix: preamble + final_exports 동시 존재 시 중간 concat 메모리 누수 수정
  → 단일 concat으로 합침
- perf: makeRequireVarName 캐시 (같은 CJS 모듈에서 여러 named import 시 중복 생성 방지)
- test: CJS 테스트 7개 추가 (namespace, multiple named, aliased, minified,
  special chars, ESM+CJS 혼합, empty CJS)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ohah ohah merged commit 00ddaeb into main Mar 22, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant