Skip to content

Instantly share code, notes, and snippets.

@wise-introvert
Last active December 19, 2020 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wise-introvert/ea13f8c3fd1eb89ff682c666302fbb36 to your computer and use it in GitHub Desktop.
Save wise-introvert/ea13f8c3fd1eb89ff682c666302fbb36 to your computer and use it in GitHub Desktop.
React Typescript library
  1. Create an empty directory and cd into it
  • $ mkdir your-component-library && cd your-component-library
  1. Initialize yarn
  • $ yarn init
  1. Initialize eslint
  • $ eslint --init # OR "npx eslint --init" if eslint isn't installed globally on your machine
  1. Initialize typescript: create tsconfig.json file with the following content:
  {                                             
    "compilerOptions": {
      "declaration": true,
      "declarationDir": "./dist",               
      "forceConsistentCasingInFileNames": true,
      "noImplicitAny": true,
      "jsx": "react-jsx",
      "lib": ["es5", "dom"],
      "module": "esnext",
      "moduleResolution": "node",
      "outDir": "./dist",
      "strict": true,
      "target": "es5"
    },
    "include": ["src/**/*.tsx"],
    "exclude": ["node_modules"]
  }
  1. Initialize git
  • $ git init
  • 5.1. Optional: Intialize Commitizen
    • $ npx commitizen init cz-conventional-changelog --yarn --dev --exact
  1. Install dependencies
  • $ yarn add --peer react react-dom; yarn add -D typescript @types/react @types/node @types/react-dom rollup rollup-plugin-typescript2 rollup-plugin-terser rimraf mkdirp concurrently
  1. Update package.json
{
  "main": "dist/index.js",
  "module": "dist/index.es.js",
  "jsnext:module": "dist/index.es.js",
  "types": "dist/index.d.js",
  "files": ["dist"],
  "scripts": {
     "build": "rimraf dist; mkdirp rimraf; rollup -c",
     "build:dev": "rollup -c -w"
  },
}
  1. Create rollup.config.js
import typescript from "rollup-plugin-typescript2";
import { terser } from "rollup-plugin-terser";

import pkg from "./package.json";

export default {
  input: "src/index.tsx",
  output: [
    {
      file: pkg.main,
      format: "cjs",
    },
    {
      file: pkg.module,
      format: "es",
    },
  ],
  external: [
    ...Object.keys(pkg.dependencies || {}),
    ...Object.keys(pkg.peerDependencies || {}),
  ],
  plugins: [typescript(), terser()],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment