建立現代 React 函式庫需要仔細考慮建置工具、開發經驗和輸出最佳化。本指南將引導您使用 TypeScript、SWC 和 Rollup 建立專業的 React 庫啟動器——這是一個強大的組合,可提供卓越的效能、可靠性和開發人員體驗。
靜態類型檢查:在開發過程中而不是運行時捕獲錯誤
增強的 IDE 支援:更好的自動完成、重構和程式碼導航
自文檔代碼:類型作為活文檔
改進的維護:讓大型程式碼庫更容易管理
成長中的社群:流行庫的廣泛類型定義
mkdir react-library cd react-library npm init -y # Create essential directories mkdir src
# Core dependencies npm install react react-dom --save-peer # Development dependencies npm install --save-dev typescript @types/react @types/react-dom \ @swc/core @swc/helpers \ rollup @rollup/plugin-swc @rollup/plugin-node-resolve \ @rollup/plugin-commonjs rollup-plugin-peer-deps-external
建立 tsconfig.json:
{ "compilerOptions": { "target": "ES2018", "module": "ESNext", "lib": ["DOM", "DOM.Iterable", "ESNext"], "declaration": true, "declarationDir": "dist/types", "emitDeclarationOnly": true, "jsx": "react-jsx", "strict": true, "moduleResolution": "node", "allowSyntheticDefaultImports": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "resolveJsonModule": true, "isolatedModules": true, "noUnusedLocals": true, "noUnusedParameters": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true }, "include": ["src"], "exclude": ["node_modules", "dist"] }
建立 rollup.config.js:
import resolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; import { swc, defineRollupSwcOption } from '@rollup/plugin-swc'; import peerDepsExternal from 'rollup-plugin-peer-deps-external'; import terser from '@rollup/plugin-terser'; const packageJson = require('./package.json'); const swcConfig = defineRollupSwcOption({ jsc: { parser: { syntax: 'typescript', tsx: true, }, transform: { react: { runtime: 'automatic', development: false, refresh: false, }, }, target: 'es2018', }, minify: false, }); export default [ // ESM build { input: 'src/index.tsx', output: [ { file: packageJson.module, format: 'esm', sourcemap: true, }, ], plugins: [ peerDepsExternal(), resolve({ extensions: ['.ts', '.tsx', '.js', '.jsx'], }), commonjs(), swc(swcConfig), terser(), ], external: ['react', 'react-dom'], }, // CommonJS build { input: 'src/index.tsx', output: [ { file: packageJson.main, format: 'cjs', sourcemap: true, exports: 'auto', }, ], plugins: [ peerDepsExternal(), resolve({ extensions: ['.ts', '.tsx', '.js', '.jsx'], }), commonjs(), swc(swcConfig), terser(), ], external: ['react', 'react-dom'], }, ];
更新你的 package.json:
{ "name": "your-library-name", "version": "1.0.0", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", "types": "dist/types/index.d.ts", "files": [ "dist" ], "sideEffects": false, "scripts": { "build": "rollup -c", "types": "tsc", "prepare": "npm run types && npm run build", "lint": "eslint ." }, "peerDependencies": { "react": ">=17.0.0 <19.0.0", "react-dom": ">=17.0.0 <19.0.0" }, }
建立 src/index.tsx:
mkdir react-library cd react-library npm init -y # Create essential directories mkdir src
設定一個 vite 應用程式來提供範例,並測試儲存庫本身的程式碼變更。這也可以透過故事書來完成。
# Core dependencies npm install react react-dom --save-peer # Development dependencies npm install --save-dev typescript @types/react @types/react-dom \ @swc/core @swc/helpers \ rollup @rollup/plugin-swc @rollup/plugin-node-resolve \ @rollup/plugin-commonjs rollup-plugin-peer-deps-external
將您的套件加入到範例 package.json 的依賴項部分
{ "compilerOptions": { "target": "ES2018", "module": "ESNext", "lib": ["DOM", "DOM.Iterable", "ESNext"], "declaration": true, "declarationDir": "dist/types", "emitDeclarationOnly": true, "jsx": "react-jsx", "strict": true, "moduleResolution": "node", "allowSyntheticDefaultImports": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "resolveJsonModule": true, "isolatedModules": true, "noUnusedLocals": true, "noUnusedParameters": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true }, "include": ["src"], "exclude": ["node_modules", "dist"] }
匯入您的元件並在範例專案中測試它。
您的 React 庫現已準備好發布! ?
如果您想深入了解即用型設置,請在此處查看完整的入門模板:https://github.com/Abhirup-99/react-library-starter-template。此範本包含我們涵蓋的所有內容,旨在協助您以最少的設定啟動 React 庫開發。
編碼愉快!
以上是建立現代 React 庫入門:綜合指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!