<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rushil dhinoja</title>
    <description>The latest articles on DEV Community by Rushil dhinoja (@rushildhinoja).</description>
    <link>https://dev.to/rushildhinoja</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F834082%2Fab429edf-00c7-44df-883d-77e4a3bce485.jpeg</url>
      <title>DEV Community: Rushil dhinoja</title>
      <link>https://dev.to/rushildhinoja</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rushildhinoja"/>
    <language>en</language>
    <item>
      <title>Create Reusable React Components with TS and Rollup</title>
      <dc:creator>Rushil dhinoja</dc:creator>
      <pubDate>Tue, 22 Mar 2022 15:23:52 +0000</pubDate>
      <link>https://dev.to/rushildhinoja/create-reusable-react-components-with-ts-and-rollup-28fd</link>
      <guid>https://dev.to/rushildhinoja/create-reusable-react-components-with-ts-and-rollup-28fd</guid>
      <description>&lt;p&gt;In today's time if you are a front-end dev you know that react is a synonym for resisability. The library designed to create a component based architecture.&lt;/p&gt;

&lt;p&gt;If you are developer like me who has a bad habit of starting multiple side-projects at once then at least once in your life you must have thought of creating a collection of all your react components and reuse them in all your project.&lt;/p&gt;

&lt;p&gt;If not, no worries today's the best time to give it a shot, this thought came to my mind in 2020 since then I have been working on creating a one-stop solution for all my future side project.&lt;/p&gt;

&lt;p&gt;In this post I will share with you how can you setup a project to create a reusable components package which can be published to npm and be used as any other package.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tech Stack
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;TypeScript&lt;/li&gt;
&lt;li&gt;React&lt;/li&gt;
&lt;li&gt;Rollup&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First of we will setup up our project:&lt;br&gt;
I would say to follow the exact same folder structure so that it will be easy to follow along&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;📦react-lib
 ┣ 📂build
 ┣ 📂src
 ┃ ┣ 📂components
 ┃ ┃ ┗ 📜Button.tsx
 ┃ ┗ 📜index.tsx
 ┣ 📜package.json
 ┣ 📜rollup.config.js
 ┣ 📜tsconfig.json
 ┗ 📜yarn.lock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First of all there are some required things you need to add in package.json.&lt;/p&gt;

&lt;p&gt;package.json&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "react-lib",
  "version": "1.0.0",
  // Main will tell node to enter our library from this file (basically it will act as a entry point)
  "main": "build/index.js",
  "scripts": {
    "build": "rollup -c"
  },
  //These are dependencies we need only in the development process
  "devDependencies": {
    "@rollup/plugin-commonjs": "^21.0.2",
    "@types/react": "^17.0.41",
    "@types/react-dom": "^17.0.14",
    "rollup": "^2.70.1",
    "rollup-plugin-typescript2": "^0.31.2",
    "typescript": "^4.6.2"
  },
  //The files will define where our final bundle is located
  "files": [
    "build"
  ],
  "dependencies": {},
  //React and React DOM will peer dependencies because they will already be present in the project this package is being used.
  "peerDependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the beginning I used webpack to bundle my code but it was not easy to understand and maintain, later on I switched to gulp but gulp was not powerful enough and as they say third time's the charm I came around rollup it was powerful like webpack and was easy to configure like gulp&lt;/p&gt;

&lt;p&gt;Rollup file is the most important file in this project, it will build our library&lt;/p&gt;

&lt;p&gt;rollup.config.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import typescript from "rollup-plugin-typescript2";

export default {
  //Will act as a entry point
  input: "./src/index.tsx",
  output: [
    {
      //Specify the output directory
      dir: "build",
      //We will be using commonjs as a format for bundling
      format: "cjs",
      exports: "named",
      //It will provide you the sourcemap for debugging
      sourcemap: true,
      strict: false,
    },
  ],
  //Preserve module will generate a transpiled file for every file in your src folder, if set false it will bundle all the code in one file
  preserveModules: true,
  //Rollup allows a rich set of plugins to be used along side, we are only using one to compile typescript code to JS
  plugins: [typescript({ tsconfig: "./tsconfig.json" })],
  //We will add React and React-dom as externals because our library will use these two packages from its parent
  external: ["react", "react-dom"],
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next file is tsconfig.json I have kept it really simple but you can change as per your needs and standards&lt;/p&gt;

&lt;p&gt;tsconfig.json&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "module": "esnext",
    "declaration": true,
    "rootDir": "src",
    "outDir": "build",
    "target": "ES5",
    "moduleResolution": "Node",
    "jsx": "react",
    "noImplicitUseStrict": true,
    "allowSyntheticDefaultImports": true,
    "lib": ["es2015", "dom"]
  },
  "include": ["./src/*.tsx"],
  "exclude": ["node_modules", "build"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's write some components. For the purpose of this post I have created a simple Button Component which accepts two prop color and roundCorners.&lt;/p&gt;

&lt;p&gt;We will create a src/components/button.tsx file and add the below code to it&lt;/p&gt;

&lt;p&gt;src/components/button.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";

interface Props {
  color?: string;
  roundCorners?: boolean;
}

const Button: React.FC&amp;lt;Props&amp;gt; = (props) =&amp;gt; {
  const { color, roundCorners } = props;

  return (
    &amp;lt;button
      style={{ background: color, borderRadius: roundCorners ? "6px" : "0" }}
    &amp;gt;
      Click me
    &amp;lt;/button&amp;gt;
  );
};

Button.defaultProps = {
  color: "white",
  roundCorners: false,
};

export default Button;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we will import it to src/index.tsx and then we will be ready to create a build and use it in our projects&lt;/p&gt;

&lt;p&gt;src/index.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export { default as Button } from "./components/Button";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, shoot up a terminal of your choice and run the following commands in order to create a build&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn install
yarn build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything is done properly you may get this message&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vp1SW9o_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojtckcjeidjbm4wj7glo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vp1SW9o_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojtckcjeidjbm4wj7glo.png" alt="Image description" width="501" height="93"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, to use it in you local projects we can use yarn link command&lt;/p&gt;

&lt;p&gt;First of all run the below command in your library project's root, it will create a symlink in you device&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn link
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to use it in any of your project you can use below command in your app directory's root&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn link react-lib
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will be able to use it as shown in below code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Button } from "react-lib";
function App() {
  return &amp;lt;Button roundCorners={true} /&amp;gt;;
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The end result will look something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CEv4UTeg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtnx25p8rbzjub12bwep.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CEv4UTeg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtnx25p8rbzjub12bwep.png" alt="Image description" width="224" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are interested creating something more than just a button have a look at the project that gave me idea to write this post &lt;a href="https://github.com/Molecule-UI/moleculeui"&gt;MoleculeUI&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Follow for more such content in future.&lt;br&gt;
Leave some feedback as it is my first post.&lt;br&gt;
Thank You 😊&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>component</category>
    </item>
  </channel>
</rss>
