Code Monkey home page Code Monkey logo

rollup-react-library-starter's Introduction

React component library template/boilerplate

Template and quick-starter to create your own modern React library includes awesome features and bandled using Rollup.

Note: At date of writing this (April 2023), since the create-react-library tool is no longer actively maintained (https://github.com/transitive-bullshit/create-react-library#readme) then I thought about creating this boilerplate to help bootstrap a modern react library with some nice features

Note

This repository created for:

  1. Help to bootstrap your own react library with some nice features and without bothering with configuration and saving time.
  2. Learning purposes (especially how to bundle complicated things with Rollup).

Intro

As mentioned in readme of create-react-library there are some others tools(you can use it as a bundler) as an alternative to Rollup like tsup, tsdx, or microbundle.

see also this artice https://transitivebullsh.it/javascript-dev-tools-in-2022#823feddaa1bb4edea19042852b0a5b54

Features

  • Rollup for bundling
  • Bundles commonjs and es module formats
  • Jest & React Testing Library : For testing our components
  • Support for TypeScript
  • Sourcemap creation
  • Support of CSS/SASS: For exporting components with style
  • Storybook: For testing our components within the library itself as we design them
  • Supports complicated peer-dependencies (example here is Antd so here the power of rollup-plugin-peer-deps-external we can use complicated peer dependency such Antd without having it bundled as a part of your module.)
  • Optimizing bundle size: @rollup/plugin-terser A Rollup plugin to generate a minified bundle with terser.
  • Automatically externalize peerDependencies in a rollup bundle, thanks to rollup-plugin-peer-deps-external
  • Eslint
  • Deploy Storybook to GitHub Pages

Getting started

  • Copy over the template by cloning this repository and install its dependencies:
git clone https://github.com/MidoAhmed/rollup-react-library-starter.git
cd rollup-react-library-starter
npm install

Development:

  • Storybook:
    • Storybook gives you an easy way to see and use your components while working on them in your library project, without having to build an unnecessary testing page just to display them.

      npm run storybook # runs the host Storybook application locally for quick and easy testing

Now, anytime you make a change to your library or the stories, the storybook will live-reload your local dev server so you can iterate on your component in real-time.

  • Rollup watch and build:

    • for Local development run rollup to watch your src/ module and automatically recompile it into dist/ whenever you make changes.

      npm run dev # runs rollup with watch flag

Scripts:

  • npm run build : builds the library to dist
  • npm run dev : builds the library, then keeps rebuilding it whenever the source files change.
  • npm test : tests the library and show the coverage.
  • npm run lint : runs eslint.
  • npm run storybook : runs the host Storybook application locally for quick and easy testing
  • npm run build-storybook : builds a static HTML/JS bundle that can easily be hosted on a remote server, so all members of your team can try your components.
  • npm run deploy-storybook : build & deploy the storybook to GitHub Pages

Publishing to npm:

publish to GitHub Packages registry:

  • you need to have this in your ~/.npmrc
registry=https://registry.npmjs.org/
@YOUR_GITHUB_USERNAME:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=YOUR_AUTH_TOKEN

and run:

npm publish

Tutorials and inspirations used to create this boilerplate

License

MIT.

rollup-react-library-starter's People

Contributors

midoahmed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rollup-react-library-starter's Issues

Bug: Unable to use hooks

Steps to Reproduce:

  1. Clones and installed deps.
  2. Tested storybook and created build. (Working)
  3. Tested by installing and importing Button component in new react cra project. (Working)
  4. Modified the original Button component and added useState hook.
  5. Tested component in storybook (Working)
  6. Created Build (Build created without errors successfully)
  7. Installed again in CRA project.
  8. CRA App Crashes after starting. (Not Working)

Expected Behavior:

  1. Should also work with hooks.

Screenshots:

image

This issue usually happens when multiple react versions are conflicting.

I also tried removing react and react-dom from devDependencies and only keeping them in peer deps. (Got same error, didn't worked)

Using state in a react button component seems a very basic functionality to me so it should not give any errors. I maybe missing something obvious.

This is the modified code of Button.tsx component:

import React, { useState } from "react";
import "./Button.scss";

export interface ButtonProps extends React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>{
  label: string;
}

const Button = ({label, ...others}: ButtonProps) => {
  const [state, setState] = useState(label)
  return <button {...others} onClick={() => setState("Clicked")}>{state}</button>;
};

export default Button;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.