fix(build): change output extension from .mjs to .js for Next.js 13 compatibility#83
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
이 PR은 Next.js 13과의 호환성을 위해 빌드 결과물의 확장자를 .mjs에서 .js로 변경하고 ESM 전용 패키지로 전환하는 것을 목표로 하고 있습니다. 전반적인 변경 방향은 좋지만, 빌드 실패나 런타임 오류를 유발할 수 있는 두 가지 치명적인 문제를 발견했습니다. 첫째, package.json에 "type": "module" 필드가 누락되어 Node.js가 .js 파일을 ESM으로 인식하지 못할 수 있습니다. 둘째, vite.config.ts의 fileName 함수가 잘못 설정되어 빌드 시 파일 이름이 충돌할 가능성이 높습니다. 각 문제에 대한 수정 제안을 코드 리뷰 댓글로 남겼으니 확인 부탁드립니다.
…ompatibility 문제: - Next.js 13.5.6에서 .mjs 파일이 next/image를 import할 때 "Element type is invalid" 에러 발생 - webpack CJS/ESM interop 실패 해결: - Vite 빌드 출력을 .js 확장자로 변경 (ESM-only, "type": "module") - package.json exports 경로 업데이트 검증: - Next.js 13.5.6 환경에서 next/image 정상 동작 확인 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
9faee7d to
eefcda4
Compare
|
🎉 This PR is included in version 1.14.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
8 tasks
ywkim
added a commit
that referenced
this pull request
Jan 4, 2026
#84) ## 요약 **Vite 빌드 설정을 dual ESM/CJS 패턴으로 복원**하여 Next.js 13과 Jest 모두 호환되도록 수정했습니다. ## 목적 PR #22 이후 PR #83으로 ESM-only로 회귀했으나, 이는 다음 두 가지 문제를 야기합니다: - ❌ Next.js 13.5.6 `.mjs` 버그: ESM 콘텐츠를 CJS 컨텍스트에서 로드할 때 webpack 오류 발생 - ❌ Jest 호환성: CJS 기반 도구에서 `transformIgnorePatterns` 설정 필수 ## 주요 개선 사항 ### 📦 Vite 설정 변경 **Before (PR #83 - ESM-only)**: ```typescript formats: ["es"] fileName: () => "[name].js" // 결과: index.js, locale.js (ESM만) ``` **After (PR #22 패턴 복원)**: ```typescript formats: ["es", "cjs"] fileName: (format, entryName) => `${entryName}.${format === "es" ? "js" : "cjs"}` // 결과: index.js + index.cjs, locale.js + locale.cjs ``` ### 📄 package.json 동기화 **main 필드**: `dist/index.js` → `dist/index.cjs` - CommonJS fallback (exports 미지원 환경 대응) **module 필드**: `dist/index.mjs` → `dist/index.js` - ESM, 번들러가 우선 사용 **exports 필드**: `require` 필드 추가 ```json { "import": "./dist/index.js", // ESM "require": "./dist/index.cjs" // CJS } ``` ### 🔄 aioia-core v0.2.2와 일관성 aioia-core PR #23에서 도입한 dual-build 패턴을 trade-safety에도 동일하게 적용: - ✅ 표준 npm 라이브러리 방식 - ✅ 모든 ESM-first 소비자와 호환 ## 검증 - ✅ `npm run build:lib` 성공 (ESM/CJS 모두 생성됨) - ✅ `npm pack` 검증 (모든 파일 포함 확인) - ✅ 로컬 설치 성공 - ✅ `tsc --noEmit` (type-check) 통과 - ✅ Next.js dev 서버 정상 실행 - ✅ 브라우저 UI 렌더링 정상 ## 기술 배경 ### 왜 .js (not .mjs)? - `.mjs` → Next.js 13.5.6 webpack이 강제 `es6` 모듈 타입 적용 → CJS interop 실패 - `.js` + `"type": "module"` → webpack이 자동 감지 → CJS interop 정상 작동 - Next.js 14.1.0+에서 이 버그 수정됨 (PR #60169) ### 왜 CJS도 필요? - Jest/CJS 도구 호환: 별도 설정 없이 "그냥 작동" - `transformIgnorePatterns` 설정 불필요 - 표준 npm 라이브러리 패턴 (업계 표준) ## 테스트 체크리스트 - [x] 빌드 성공 (vite build) - [x] ESM 출력 생성 (dist/index.js, dist/locale.js) - [x] CJS 출력 생성 (dist/index.cjs, dist/locale.cjs) - [x] npm pack 검증 - [x] 로컬 설치 및 검증 성공 - [x] TypeScript type-check 통과 - [x] Next.js dev 서버 실행 성공 - [x] 페이지 렌더링 확인 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
요약
Next.js 13.x 버전의 webpack CJS/ESM interop 버그로 인해
.mjs빌드 파일에서next/imageimport가 실패하는 문제를.js확장자로 회피.목적
버그 성격: Next.js 13.5.6~14.0.4의 webpack 버그로,
.mjs파일을 강제로 순수 ESM으로 처리하여 CJS 모듈(next/image)과의 interop이 실패함. 14.1.0에서 수정되었으나, 하위 호환성을 위해.js확장자를 사용.기술적 배경:
.mjs와.js + "type": "module"모두 Node.js 표준 ESM 방식주요 개선 사항
🔧 빌드 설정 변경
.mjs→.js확장자 (Next.js 13.x 호환성)"type": "module"만 유지.js로 업데이트✅ 호환성 개선
next/image정상 동작 확인📦 package.json 변경
⚙️ vite.config.ts 변경
lib: { - formats: ["es", "cjs"], - fileName: (format, entryName) => - `${entryName}.${format === "es" ? "mjs" : "js"}`, + formats: ["es"], + fileName: () => "[name].js", }Next.js 버전별 호환성 검증
테스트 결과
Next.js 14.1.0 버그 수정사항
해결 PR: vercel/next.js#60169 - Fix emitting ESM swc helpers for 3rd parties CJS libs in bundle
버그 원인:
type: 'es6'로 강제 설정수정 방법:
auto-cjsswc plugin 버그 수정 (@kdy1)핵심 변경 (packages/next/src/build/swc/options.ts):
결론:
.mjs파일도 정상 동작.js선택은 Next.js 13.x~14.0.x 하위 호환성을 위한 실용적 결정.mjs와.js모두 표준이며, 둘 중 하나가 더 "올바른" 것은 아님구조 변경
변경된 파일:
테스트 체크리스트
next/image컴포넌트 정상 렌더링개선 효과
기존 (.mjs + Next.js 13.x ~ 14.0.x):
.mjs를 강제 순수 ESM 처리next/image)과의 interop 실패개선 후 (.js + "type": "module"):
.js+"type": "module"로 ESM 명시 (표준 방식)🤖 Generated with Claude Code