21
loading...
This website collects cookies to deliver better user experience
Before start, You will need to have NPM, Typescript and other common dev dependencies installed
tsconfig.json
file at your project root:{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"declaration": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react",
"outDir": "lib/esm",
},
"include": [
"src"
],
"exclude": ["node_modules", "lib"]
}
npm init
, and modify package.json
flie:...
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"types": "./lib/esm/index.d.ts",
"files": [
"src"
],
"scripts": {
"build": "npm run build:esm && npm run build:cjs",
"build:esm": "tsc",
"build:cjs": "tsc --module commonjs --outDir lib/cjs",
"publish": "npm publish"
},
...
"peerDependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"@types/node": "16.11.10",
"@types/react": "^17.0.37"
},
"dependencies": {
"@types/react-dom": "^17.0.11"
}
...
}
.gitignore
file if you are using git:# dependencies
/node_modules
src
folder and src/index.tsx
file:import React from "react";
export function CheckLib(){
return <div>Lib Ok</div>
}
npm run build
to create the build folder.npm publish
to publish your package.npm install <your-package-name>
to install your library in other project.You may need to run npm login
to login your npm account if you never logged in before.
npm link
at your library root.npm link "You-Library-Name-Here"
.create-next-app
. I assume this will work CRA as well.