20250313.1809.31 • Published 8 months ago

@tmlmobilidade/ui v20250313.1809.31

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

My React Library

This is a React component library built with TypeScript, Rollup, and Storybook.

Step-by-Step Guide

Step 1: Initialize Your Project

Create a new project directory and initialize it with NPM:

mkdir my-react-library
cd my-react-library
npm init -y

Step 2: Install Dependencies

Install the necessary development dependencies:

npm install -D typescript react react-dom
npm install -D rollup @rollup/plugin-node-resolve @rollup/plugin-typescript @rollup/plugin-commonjs rollup-plugin-dts rollup-plugin-postcss
npm install -D tslib postcss
npm install -D @storybook/react-webpack5 @storybook/addon-essentials
npm install -D @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript
npm install -D @types/react

Step 3: Set Up TypeScript

Initialize TypeScript configuration:

npx tsc --init

Edit the tsconfig.json file to match the following configuration:

{
  "compilerOptions": {
    "target": "esnext",
    "jsx": "react-jsx",
    "module": "ESNext",
    "moduleResolution": "Node",
    "allowJs": false,
    "maxNodeModuleJsDepth": 1,
    "declaration": true,
    "emitDeclarationOnly": true,
    "sourceMap": true,
    "outDir": "dist",
    "declarationDir": "types",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noUncheckedIndexedAccess": true,
    "allowUnreachableCode": false,
    "skipLibCheck": true
  }
}

Step 4: Create Your Components

Create the following directory structure and files:

src
└── components
    ├── Button
    │   ├── Button.tsx
    │   └── index.ts
    └── Input
        ├── Input.tsx
        └── index.ts
└── index.ts

Example Button Component (src/components/Button/Button.tsx):

import React from "react";

interface ButtonProps {
  label: string;
}

const Button: React.FC<ButtonProps> = ({ label }) => {
  return <button>{label}</button>;
};

export default Button;

Button Component Index (src/components/Button/index.ts):

import Button from "./Button";
export default Button;

Example Input Component (src/components/Input/Input.tsx):

import React from "react";

interface InputProps {
  placeholder: string;
}

const Input: React.FC<InputProps> = ({ placeholder }) => {
  return <input placeholder={placeholder} />;
};

export default Input;

Input Component Index (src/components/Input/index.ts):

import Input from "./Input";
export default Input;

Main Index File (src/index.ts):

export { default as Button } from "./components/Button";
export { default as Input } from "./components/Input";

Step 5: Set Up Rollup

Create a Rollup configuration file rollup.config.mjs:

import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import dts from "rollup-plugin-dts";
import postcss from "rollup-plugin-postcss";
import packageJson from "./package.json" assert { type: "json" };

export default [
  {
    preserveModules: true,
    input: "src/index.ts",
    output: [
      {
        file: packageJson.main,
        format: "cjs",
        sourcemap: true,
      },
      {
        file: packageJson.module,
        format: "esm",
        sourcemap: true,
      },
    ],
    plugins: [
      resolve(),
      commonjs(),
      typescript({
        tsconfig: "./tsconfig.json",
        exclude: ["**/*.test.tsx", "**/*.test.ts", "**/*.stories.ts"],
      }),
      postcss({ extensions: [".css"], inject: true, extract: false }),
    ],
  },
  {
    input: "dist/esm/types/index.d.ts",
    output: [{ file: "dist/index.d.ts", format: "esm" }],
    plugins: [dts()],
    external: [/\.css$/],
  },
];

Step 6: Set Up Storybook

Initialize Storybook:

npx sb init

Create a .storybook directory and add main.ts:

import type { StorybookConfig } from "@storybook/react-webpack5";

const config: StorybookConfig = {
  stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
  ],
  framework: {
    name: "@storybook/react-webpack5",
    options: {},
  },
};

export default config;

Step 7: Configure package.json

Update package.json with the necessary fields:

{
  "name": "my-react-library",
  "version": "0.0.1",
  "main": "dist/cjs/index.js",
  "module": "dist/esm/index.js",
  "types": "dist/index.d.ts",
  "files": ["dist"],
  "scripts": {
    "dev": "rollup -c --watch",
    "build": "rollup -c",
    "storybook": "storybook dev -p 6006",
    "build-storybook": "storybook build"
  },
  "babel": {
    "sourceType": "unambiguous",
    "presets": [
      [
        "@babel/preset-env",
        {
          "targets": {
            "chrome": 100,
            "safari": 15,
            "firefox": 91
          }
        }
      ],
      [
        "@babel/preset-react",
        {
          "runtime": "automatic"
        }
      ],
      "@babel/preset-typescript"
    ]
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "devDependencies": {
    "@babel/core": "^7.24.6",
    "@babel/preset-env": "^7.24.6",
    "@babel/preset-react": "^7.24.6",
    "@babel/preset-typescript": "^7.24.6",
    "@rollup/plugin-commonjs": "^25.0.8",
    "@rollup/plugin-node-resolve": "^15.2.3",
    "@rollup/plugin-typescript": "^11.1.6",
    "@storybook/addon-essentials": "^8.1.5",
    "@storybook/addon-links": "^8.1.5",
    "@storybook/react": "^8.1.5",
    "@storybook/react-webpack5": "^8.1.5",
    "@types/react": "^18.3.3",
    "postcss": "^8.4.38",
    "react": "^18.3.1",
    "react-dom": "^18.3.1",
    "rollup": "^4.18.0",
    "rollup-plugin-dts": "^6.1.1",
    "rollup-plugin-postcss": "^4.0.2",
    "storybook": "^8.1.5",
    "tslib": "^2.6.2",
    "typescript": "^5.4.5"
  },
  "peerDependencies": {
    "react": "^18.2.0"
  }
}

Step 8: Build Your Package

To build your package, run:

npm run build

Step 9: Run Storybook

To start Storybook, run:

npm run storybook

Step 10: Publish to NPM

First, log in to your NPM account:

npm login

Publish your package:

npm publish

If your package is scoped and you want to make it public, use:

npm publish --access public

Congratulations!🎉 You've successfully created and published a React component library with TypeScript, Rollup, and Storybook.

20250313.1809.31

8 months ago

20250310.1959.17

8 months ago

20250307.1656.54

8 months ago

20250307.1525.49

8 months ago

20250305.1754.42

8 months ago

20250306.1453.58

8 months ago

20250307.1322.1

8 months ago

20250305.1746.10

8 months ago

20250307.1216.36

8 months ago

20250306.1249.35

8 months ago

20250306.1750.41

8 months ago

20250306.1700.23

8 months ago

20250305.1816.36

8 months ago

20250307.1723.25

8 months ago

20250305.1841.5

8 months ago

20250305.1809.16

8 months ago

20250227.1959.42

9 months ago

20250226.1436.26

9 months ago

20250226.1358.59

9 months ago

20250210.1115.1

9 months ago

20250211.2350.30

9 months ago

20250209.1144.56

9 months ago

20250209.1134.12

9 months ago

20250209.1133.43

9 months ago

20250209.1131.59

9 months ago

20250209.1129.40

9 months ago

20250209.1055.52

9 months ago

20250209.1041.57

9 months ago

20250209.1038.38

9 months ago

20250209.1033.21

9 months ago

20250209.959.43

9 months ago

20250208.2227.13

9 months ago

20250208.2226.13

9 months ago

20250208.1705.54

9 months ago

20250208.1520.24

9 months ago

20250208.1433.0

9 months ago

20250208.1422.21

9 months ago

20250208.1402.52

9 months ago

20250208.1249.8

9 months ago

20250208.1131.23

9 months ago

20250208.1108.7

9 months ago

20250208.1059.31

9 months ago

20250208.151.8

9 months ago

20250208.146.41

9 months ago

20250207.1458.18

9 months ago

20250207.1246.43

9 months ago

20250207.1225.37

9 months ago

20250206.1514.26

9 months ago

20250206.1428.7

9 months ago

20250206.1421.6

9 months ago

20250206.1158.28

9 months ago

20250206.1138.20

9 months ago

20250206.1126.39

9 months ago

20250206.1115.11

9 months ago

20250206.29.25

9 months ago

20250205.1339.11

9 months ago

20250205.1234.12

9 months ago

20250205.56.0

9 months ago

20250131.2255.24

10 months ago

20250131.1854.44

10 months ago

20250130.2005.29

10 months ago

20250129.1710.49

10 months ago

20250129.1656.11

10 months ago

20250129.1626.34

10 months ago

20250129.1606.25

10 months ago

20250128.1834.16

10 months ago

20250128.1719.49

10 months ago

20250128.1647.0

10 months ago

20250128.1631.30

10 months ago

20250128.1609.43

10 months ago

20250128.1229.58

10 months ago

20250128.1225.12

10 months ago

20250128.1220.14

10 months ago

20250128.1154.14

10 months ago

20250128.1145.52

10 months ago

20250128.1136.54

10 months ago

20250128.1122.38

10 months ago

20250128.1115.58

10 months ago

20250124.2154.52

10 months ago

20250124.1854.3

10 months ago

20250124.1835.50

10 months ago

20250124.1830.13

10 months ago

20250124.1755.29

10 months ago

20250124.1744.33

10 months ago

20250124.1715.54

10 months ago

20250124.1657.12

10 months ago

20250124.1545.56

10 months ago

20250122.1933.16

10 months ago

0.0.4

10 months ago