pnpm + tsdown으로 React 라이브러리 만들기 ① - 프로젝트 초기화
pnpm + tsdown으로 React 라이브러리를 만드는 과정에서 프로젝트 초기화 방법에 대해 정리한 페이지입니다.
Tags
monorepo, pnpm, React, tsdown
Environment
pnpm v10.32.1
1. 개요
pnpm + tsdown으로 React 라이브러리를 만드는 방법에 대해 정리한 페이지입니다.
총 5개의 Part로 분리하여 작성하였으며, 이 페이지에서는 프로젝트 초기화 과정을 다룹니다.
- 1. 프로젝트 초기화하기
- 2. React 라이브러리 생성하기
- 3. Next.js demo 생성하기
- 4. 라이브러리 배포하기
- 5. demo 배포하기
2. 프로젝트 초기화하기
2.1. 프로젝트 구조 설계하기
React 라이브러리를 만들면서 demo를 통해 배포 없이 테스트할 수 있도록
다음과 같이 pnpm을 사용한 모노레포 환경을 구성하고 프로젝트 구조를 설계합니다.
1react-toc
2├── apps
3| └── demo
4| ├── src
5| | ├── app
6| | ├── components
7| | └── mdx-components.tsx
8| ├── (...)
9| ├── next.config.ts
10| ├── package.json
11| └── tsconfig.json
12├── packages
13| └── react-toc
14| ├── src
15| | ├── TocProvider.tsx
16| | ├── (...)
17| | └── index.ts
18| ├── package.json
19| ├── tsconfig.json
20| └── tsdown.config.ts
21├── (...)
22├── package.json
23├── pnpm-lock.yaml
24├── pnpm-worksplace.yaml
25└── tsconfig.base.jsonapps/demo
Next.js demo 애플리케이션으로, 개발하면서 바로 테스트할 환경입니다.packages/react-toc
npm에 배포할 실제 React 라이브러리입니다.
2.2. pnpm 설치하기
다음 명령어를 입력하여 pnpm을 설치합니다.
1# pnpm 다운로드 및 설치
2corepack enable pnpm
3
4# pnpm 버전 확인
5pnpm -v2.3. 프로젝트 루트 생성하기
먼저 다음 명령어를 입력하여 프로젝트 루트를 생성합니다.
1mkdir react-toc
2cd react-toc이후 다음 명령어를 입력하여 프로젝트를 초기화합니다.
1pnpm init생성된 package.json 파일에서 "private": true를 설정하여 루트 패키지의 npm publish를 방지합니다.
1{
2 "name": "react-toc",
3 "private": true,
4 "packageManager": "pnpm@10.32.1"
5}2.4. pnpm workspace 설정하기
프로젝트 루트에서 pnpm-workspace.yaml 파일을 생성한 후, 다음과 같이 워크스페이스를 설정합니다.
1packages:
2 - "apps/*"
3 - "packages/*"설정 이후 apps/, packages/ 디렉토리 아래의 모든 패키지가 워크스페이스로 인식됩니다.
2.5. tsconfig.base.json 설정하기
각 라이브러리 패키지마다 tsconfig.json 설정을 하는 것은 번거로우므로,
다음과 같이 프로젝트 루트에 tsconfig.base.json을 생성하여 확장할 수 있도록 합니다.
먼저 프로젝트 루트에서 다음 명령어를 입력하여 TypeScript 컴파일러 설정 파일인 tsconfig.json 파일을 생성합니다.
1tsc --init이후 생성된 tsconfig.json 파일명을 tsconfig.base.json으로 변경한 후, 다음과 같이 설정합니다.
1{
2 "compilerOptions": {
3 /* Language and Environment */
4 "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
5 "jsx": "react-jsx" /* Specify what JSX code is generated. */,
6
7 /* Modules */
8 "module": "esnext" /* Specify what module code is generated. */,
9 "moduleResolution": "bundler" /* Specify how TypeScript looks up a file from a given module specifier. */,
10
11 /* Emit */
12 "declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
13
14 /* Interop Constraints */
15 "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
16 "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
17
18 /* Type Checking */
19 "strict": true /* Enable all strict type-checking options. */,
20
21 /* Completeness */
22 "skipLibCheck": true /* Skip type checking all .d.ts files. */
23 }
24}3. 다음 Part
다음 글에서는 tsdown을 활용하여 실제 React 라이브러리를 생성하고 빌드하는 과정을 다룹니다.
4. 참고 자료
- React+typescript 컴포넌트 라이브러리 생성하기 ③rollup 설정 — 콩부합시다
- 고티켓-react 컴포넌트 라이브러리 만들기(3)-rollup — 이찬진 컴퓨터 교실
- esbuild vs rollup vs webpack vs tsup | "웹 자바스크립트 번들러" npm 패키지 비교
- 리액트 컴포넌트 라이브러리를 빌드하고 배포하는 방법 — React와 TypeScript를 좋아하는 개발자
- 리액트 라이브러리 npm에 배포하기 with rollup
- rolldown/tsdown: The elegant bundler for libraries powered by Rolldown
- Nextra - Next.js Static Site Generator
- HyunJinNo/react-toc: A library that automatically generates a Table of Contents (TOC) from your headings with scroll tracking.