0.0.456 • Published 17 hours ago

tabexseriescomponents v0.0.456

Weekly downloads
-
License
MIT
Repository
-
Last release
17 hours ago

In this tutorial i will be showing you how to create an npm library that is composed of react component. This will definetly help you incase you want to reuse code in multiple projects or if you just want to create your own library.

Table of contents:

If you are ready, lets get started.

Getting Started

First we shall initialize a react project. So got to your terminal and enter the directory that you want to create your project and create a new react app with the create-react-app CLI.

npx create-react-app react-npm-library
## then enter the new directory
cd react-npm-library
## then start the dev server
yarn start

I recommend using npx sinc it installs the latest versions of react-scripts and does not install any thing globally.

Then enter the src directory and create a new directory where you will place your component library

cd src
mkdir react-library ## you can name it  any name

Creating the library

if you have done the above steps now you are ready to create you libray. So now lets create a simple library that creates good buttons.

Inside the react-library directory we shall create a file for the component.

torch button.jsx
torch index.css

Then place this code in button.jsx

import React, {useState, useEffect} from 'react';
import './button.css'

const AwesomeButton = (props) => {
    const [color, setColor] = useState(null)
    useEffect(() => {
        if (props.variant) {
            if (props.variant === 'primary') {
                setColor('#0077ff')
            } else if (props.variant === 'secondary') {
                setColor('#ff0062')
            } else if (props.variant === 'success') {
                setColor('#0f8000')
            } else {
                setColor('#949393')
            }
        }
    })
    const {children} = props
    
    return (
        <button 
            className='buttonComponent'
            style={{
                backgroundColor: color
            }}
        >
            {children.toUpperCase()}
        </button>
    )
}

export default AwesomeButton;

in index.css

.buttonComponent {
    border-radius: 3px;
	padding: 0.3rem 0.5rem;
    transition: all .3s ease-out;
	box-shadow: #272727b0 1px 1px 1px, #272727b0 -1px -1px 1px;
}
.buttonComponent:hover {
    box-shadow: #272727b0 1px 1px 3px, #272727b0 -1px -1px 3px;
}
.buttonComponent:active {
    opacity: .8;
}

Now you can import it from App.js and test it. If it works well then lets move on.

Initializing the library

Now if it works so you have to enter the react-library directory and create get it ready for publishing.

cd react-library

After then initialize an npm package

npm init -y

This will create a package.json file in the root directory. It should look like this

{
  "name": "react-library",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Now you are ready to move on to the next section

Bundling the library

Now lets get ready to bundle the library. We shall do this in a few steps

Re-arranging the package directory

So now lets arrange the react-library directory so it can be favourable for bundling.

Move to your terminal and type these commands inside the react-library directory

mkdir src
move button.jsx src
move index.css src
cd src
torch index.js

The above commands will move the button.jsx and index.css files to a new src directory and also create a new file called index.js By now your project structure looks something like this.

|
 - src
   | - index.js
   | - index.css
   | - button.jsx
 - package.json

Inside the index.js file add the following code

import AwesomeButton from  './button.js'

const returnLibrary = () => {
    return {
        AwesomeButton: AwesomeButton
        // you can add here other components that you want to export
    }
}
export default returnLibrary()

Installing bundlers

Now lets install the bundlers. I recommend rollup since I thimk it is easy to use when bundling libraries compared to webpack but if you want to use another bundler then you can use it. So in the root of the react-library directory install rollup.

Note: You have to install them as devDependencies by adding the --save-dev flag. This is because they are used by you when in development mode else if you don't this will cause make your users to install thenm if you add them to the dependency list

npm install rollup --save-dev

Rollup will be used to compile our code. But since we definitely might want to compile into es5 syntax so we shall have to install babel and a plugin to use with rollup. You should not that jsx syntax is special and is not valid javascript so you should also install support for it. So type the following commad int in the command line to install all required packages.

npm install @babel/cli @babel/core @babel/preset-env @babel/preset-react @rollup/plugin-babel --save-dev

Since we are also bundling css then we shall have to install a styles bundler for rollup we shall use rollup-plugin-styles

npm install rollup-plugin-styles autoprefixer --save-dev

Optional

We can also add babel runtime helpers. this is important if you are bundling a library with babel. So type this in the command line

npm install @babel/runtime
npm install @babel/plugin-transform-runtime --save-dev

If you want sourcemaps then intall this plugin to.

npm install rollup-plugin-sourcemaps --save-dev

Configuration

Now lets configure the rullop and babel for compiling our code. In the root directory create these to files.

  • rollup.config.js
  • .babelrc

Inside rollup.config.js add the following code.

import styles from "rollup-plugin-styles";
const autoprefixer = require('autoprefixer');
import { terser } from 'rollup-plugin-terser'
import babel from '@rollup/plugin-babel';
import sourcemaps from 'rollup-plugin-sourcemaps';

// the entry point for the library
const input = 'src/index.js'

// 
var MODE = [
  {
    fomart: 'cjs'
  },
  {
    fomart: 'esm'
  },
  {
    fomart: 'umd'
  }
]




var config = []


MODE.map((m) => {
    var conf = {
        input: input,
        output: {
            // then name of your package
            name: "react-awesome-buttons",
            file: `dist/index.${m.fomart}.js`,
            format: m.fomart,
            exports: "auto"
        },
        // this externelizes react to prevent rollup from compiling it
        external: ["react", /@babel\/runtime/],
        plugins: [
            // these are babel comfigurations
            babel({
                exclude: 'node_modules/**',
                plugins: ['@babel/transform-runtime'],
                babelHelpers: 'runtime'
            }),
            // this adds sourcemaps
            sourcemaps(),
            // this adds support for styles
            styles({
                postcss: {
                    plugins: [
                        autoprefixer()
                    ]
                }
            })
        ]
    }
    config.push(conf)
})

export default [
  ...config,
]

Also add this to .babelrc

{
    "presets": [
        "@babel/preset-react",
        "@babel/preset-env"
    ]
}

Editing package.json scripts

Now got to package.json and edit the scripts section and change it to this.

// package.json
...
"scripts": {
    "build": "rollup -c"
}
...

Build package

Now that everything is set run

npm run build

This will compile your package into the dist directory.

Editing package.json

Now that our library has been built lets edit package.json to make our library ready for publishing.

If you have followed from the beginning i think your packages.json looks something like this.

{
    "name": "react-library",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "directories": {
        "test": "test"
    },
    "scripts": {
        "build": "rollup -c"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
		"@babel/runtime": "^7.12.5"
	},
    "devDependencies": {
		"@babel/cli": "^7.12.10",
		"@babel/core": "^7.12.10",
		"@babel/plugin-transform-runtime": "^7.12.10",
		"@babel/preset-env": "^7.12.11",
		"@babel/preset-react": "^7.12.10",
		"@rollup/plugin-babel": "^5.2.2",
		"rollup-plugin-sourcemaps": "^0.6.3",
		"rollup-plugin-styles": "^3.12.2",
	}
}

I will explain what a few important feilds represent and you can find out more on at the npm documentation.

Fields

name and vesrion

If you plan to publish your package, the most important things in your package.json are the name and version fields as they will be required. The name and version together form an identifier that is assumed to be completely unique. Changes to the package should come along with changes to the version. If you don't plan to publish your package, the name and version fields are optional. A name can be optionally prefixed by a scope, e.g. @myorg/mypackage.

decription

Put a description in it. It's a string. This helps people discover your package, as it's listed in npm search.

keywords

Put keywords in it. It's an array of strings. This helps people discover your package as it's listed in npm search.

homepage

The url to the project homepage.

license

You should specify a license for your package so that people know how they are permitted to use it, and any restrictions you're placing on it.

If you're using a common license such as BSD-2-Clause or MIT, add a current SPDX license identifier for the license you're using, like this:

{"license":"BSD-3-Clause"}

people fields: author, contributors

The "author" is one person. "contributors" is an array of people. A "person" is an object with a "name" field and optionally "url" and "email", like this:

{
    "name":"Barney Rubble",
    "email":"b@rubble.com",
    "url":"http://barnyrubble.tumblr.com/"
}

Adding peerDependecies

Since we dont want to have react installed twice in the users projects but also need the user to have it we shall add it as a peerDependecies so add this to you package.json file

"peerDependencies": {
		"react": "^17.0.1",
		"react-dom": "^17.0.1"
	}

Final package.json

Now edit it to look like this

{
	"name": "react-library",
	"version": "1.0.0",
	"description": "your description",
	"main": "dist/index.cjs.js",
	"scripts": {
		"build": "rollup -c"
	},
	"peerDependencies": {
		"react": "^17.0.1",
		"react-dom": "^17.0.1"
	},
	"dependencies": {
		"@babel/runtime": "^7.12.5"
	},
	"keywords": [
		"react",
		"keywords"
	],
	"author": "Your name",
	"license": "MIT",
	"devDependencies": {
		"@babel/cli": "^7.12.10",
		"@babel/core": "^7.12.10",
		"@babel/plugin-transform-runtime": "^7.12.10",
		"@babel/preset-env": "^7.12.11",
		"@babel/preset-react": "^7.12.10",
		"@rollup/plugin-babel": "^5.2.2",
		"rollup-plugin-sourcemaps": "^0.6.3",
		"rollup-plugin-styles": "^3.12.2",
	}
}

Publishing

Now you are ready to publish. First create an npm account if you don't have one.

Creating .npmignore

I hope by now your projects structure looks like this:

|
| - dist
    | - index.esm.js
    | - index.cjs.js
    | - index.umd.js
| - src
    | - index.js
    | - index.css
    | - button.jsx
| - .babelrc
| - package.json
| - rollup.config.js

But since we dont want to publish our source code to npm we only want to publish the compiled code then we will create a .npmignore file that will prevent files we dont want to publish from being published

add this to .npmignore file.

## the src folder
src
.babelrc
rollup.config.js
## node modules folder
node_modules
## incase you have a git repositiory initiated
.git
.gitignore
CVS
.svn
.hg
.lock-wscript
.wafpickle-N
.DS_Store
npm-debug.log
.npmrc

config.gypi
package-lock.json

Finding a name

Sometimes you might try to publish a package and find that the name is either already taken or the name is almost identical to onother package so its better to first search and see if the package name is already taken. So type the following command in the command line.

npm search [package name]

if you find that nobody is using it them you can usethe name.

Testing your package

To test your package your have to go to another projects on you comuter and type

npm link /path/to/your/package

Adding README.md

You should also add a Readme.md file that will be displayed on npm having a description of your package. You might be familiar with it if you have ever created a repository on GitHub

Publishing

If all works well then you can publish it by typing

npm publish

Conclusion

I hope this article has been helpful to you if you have any question just leave it in the comments section and all the source code can be found on Github

0.0.456

17 hours ago

0.0.455

1 day ago

0.0.454

1 day ago

0.0.453

3 days ago

0.0.450

5 days ago

0.0.452

5 days ago

0.0.451

5 days ago

0.0.449

6 days ago

0.0.448

6 days ago

0.0.447

8 days ago

0.0.446

12 days ago

0.0.445

13 days ago

0.0.444

13 days ago

0.0.443

14 days ago

0.0.442

19 days ago

0.0.439

23 days ago

0.0.437

24 days ago

0.0.441

23 days ago

0.0.440

23 days ago

0.0.436

26 days ago

0.0.435

30 days ago

0.0.434

1 month ago

0.0.433

1 month ago

0.0.431

1 month ago

0.0.429

1 month ago

0.0.428

1 month ago

0.0.427

1 month ago

0.0.426

1 month ago

0.0.425

1 month ago

0.0.424

1 month ago

0.0.423

2 months ago

0.0.422

2 months ago

0.0.421

2 months ago

0.0.420

2 months ago

0.0.419

2 months ago

0.0.418

2 months ago

0.0.417

2 months ago

0.0.416

2 months ago

0.0.415

2 months ago

0.0.414

2 months ago

0.0.413

2 months ago

0.0.412

2 months ago

0.0.411

2 months ago

0.0.410

2 months ago

0.0.409

2 months ago

0.0.407

2 months ago

0.0.408

2 months ago

0.0.406

2 months ago

0.0.405

2 months ago

0.0.403

2 months ago

0.0.404

2 months ago

0.0.402

3 months ago

0.0.401

3 months ago

0.0.400

3 months ago

0.0.398

3 months ago

0.0.397

3 months ago

0.0.395

3 months ago

0.0.394

3 months ago

0.0.396

3 months ago

0.0.393

3 months ago

0.0.392

3 months ago

0.0.391

3 months ago

0.0.390

3 months ago

0.0.389

3 months ago

0.0.388

4 months ago

0.0.387

4 months ago

0.0.384

4 months ago

0.0.385

4 months ago

0.0.383

4 months ago

0.0.382

4 months ago

0.0.381

4 months ago

0.0.380

4 months ago

0.0.379

4 months ago

0.0.378

4 months ago

0.0.377

4 months ago

0.0.376

4 months ago

0.0.373

5 months ago

0.0.372

5 months ago

0.0.375

5 months ago

0.0.374

5 months ago

0.0.371

5 months ago

0.0.369

5 months ago

0.0.368

5 months ago

0.0.367

5 months ago

0.0.370

5 months ago

0.0.359

6 months ago

0.0.358

6 months ago

0.0.357

6 months ago

0.0.356

6 months ago

0.0.351

6 months ago

0.0.354

6 months ago

0.0.362

6 months ago

0.0.361

6 months ago

0.0.360

6 months ago

0.0.366

5 months ago

0.0.365

5 months ago

0.0.364

5 months ago

0.0.363

5 months ago

0.0.350

6 months ago

0.0.348

7 months ago

0.0.347

7 months ago

0.0.346

7 months ago

0.0.349

7 months ago

0.0.315

11 months ago

0.0.314

11 months ago

0.0.313

11 months ago

0.0.312

11 months ago

0.0.319

11 months ago

0.0.318

11 months ago

0.0.317

11 months ago

0.0.316

11 months ago

0.0.311

11 months ago

0.0.326

9 months ago

0.0.325

9 months ago

0.0.324

10 months ago

0.0.323

10 months ago

0.0.329

9 months ago

0.0.328

9 months ago

0.0.327

9 months ago

0.0.322

10 months ago

0.0.321

10 months ago

0.0.320

10 months ago

0.0.337

8 months ago

0.0.336

8 months ago

0.0.335

8 months ago

0.0.334

9 months ago

0.0.339

8 months ago

0.0.338

8 months ago

0.0.333

9 months ago

0.0.332

9 months ago

0.0.331

9 months ago

0.0.330

9 months ago

0.0.345

7 months ago

0.0.340

8 months ago

0.0.344

8 months ago

0.0.343

8 months ago

0.0.342

8 months ago

0.0.304

11 months ago

0.0.303

11 months ago

0.0.302

11 months ago

0.0.308

11 months ago

0.0.307

11 months ago

0.0.306

11 months ago

0.0.305

11 months ago

0.0.227

1 year ago

0.0.226

1 year ago

0.0.225

1 year ago

0.0.224

1 year ago

0.0.228

1 year ago

0.0.223

1 year ago

0.0.222

1 year ago

0.0.221

1 year ago

0.0.301

12 months ago

0.0.300

12 months ago

0.0.279

1 year ago

0.0.274

1 year ago

0.0.273

1 year ago

0.0.272

1 year ago

0.0.271

1 year ago

0.0.278

1 year ago

0.0.277

1 year ago

0.0.276

1 year ago

0.0.275

1 year ago

0.0.285

1 year ago

0.0.284

1 year ago

0.0.283

1 year ago

0.0.282

1 year ago

0.0.289

1 year ago

0.0.288

1 year ago

0.0.287

1 year ago

0.0.281

1 year ago

0.0.280

1 year ago

0.0.296

12 months ago

0.0.295

12 months ago

0.0.294

12 months ago

0.0.299

12 months ago

0.0.298

12 months ago

0.0.297

12 months ago

0.0.292

12 months ago

0.0.291

12 months ago

0.0.238

1 year ago

0.0.237

1 year ago

0.0.236

1 year ago

0.0.235

1 year ago

0.0.239

1 year ago

0.0.230

1 year ago

0.0.234

1 year ago

0.0.233

1 year ago

0.0.232

1 year ago

0.0.231

1 year ago

0.0.249

1 year ago

0.0.248

1 year ago

0.0.247

1 year ago

0.0.246

1 year ago

0.0.241

1 year ago

0.0.240

1 year ago

0.0.245

1 year ago

0.0.244

1 year ago

0.0.243

1 year ago

0.0.242

1 year ago

0.0.259

1 year ago

0.0.258

1 year ago

0.0.257

1 year ago

0.0.252

1 year ago

0.0.251

1 year ago

0.0.256

1 year ago

0.0.255

1 year ago

0.0.253

1 year ago

0.0.269

1 year ago

0.0.263

1 year ago

0.0.262

1 year ago

0.0.261

1 year ago

0.0.260

1 year ago

0.0.267

1 year ago

0.0.266

1 year ago

0.0.265

1 year ago

0.0.264

1 year ago

0.0.205

1 year ago

0.0.204

1 year ago

0.0.203

1 year ago

0.0.202

1 year ago

0.0.209

1 year ago

0.0.208

1 year ago

0.0.207

1 year ago

0.0.206

1 year ago

0.0.200

1 year ago

0.0.216

1 year ago

0.0.215

1 year ago

0.0.214

1 year ago

0.0.213

1 year ago

0.0.219

1 year ago

0.0.217

1 year ago

0.0.212

1 year ago

0.0.210

1 year ago

0.0.106

2 years ago

0.0.105

2 years ago

0.0.104

2 years ago

0.0.103

2 years ago

0.0.108

2 years ago

0.0.107

2 years ago

0.0.102

2 years ago

0.0.101

2 years ago

0.0.100

2 years ago

0.0.220

1 year ago

0.0.197

1 year ago

0.0.196

1 year ago

0.0.195

1 year ago

0.0.194

1 year ago

0.0.199

1 year ago

0.0.198

1 year ago

0.0.193

1 year ago

0.0.192

1 year ago

0.0.191

1 year ago

0.0.190

1 year ago

0.0.159

1 year ago

0.0.158

1 year ago

0.0.153

1 year ago

0.0.152

1 year ago

0.0.151

1 year ago

0.0.150

1 year ago

0.0.157

1 year ago

0.0.156

1 year ago

0.0.155

1 year ago

0.0.154

1 year ago

0.0.169

1 year ago

0.0.164

1 year ago

0.0.163

1 year ago

0.0.162

1 year ago

0.0.161

1 year ago

0.0.168

1 year ago

0.0.167

1 year ago

0.0.166

1 year ago

0.0.165

1 year ago

0.0.160

1 year ago

0.0.175

1 year ago

0.0.174

1 year ago

0.0.173

1 year ago

0.0.172

1 year ago

0.0.179

1 year ago

0.0.178

1 year ago

0.0.177

1 year ago

0.0.176

1 year ago

0.0.171

1 year ago

0.0.170

1 year ago

0.0.186

1 year ago

0.0.185

1 year ago

0.0.184

1 year ago

0.0.183

1 year ago

0.0.189

1 year ago

0.0.188

1 year ago

0.0.187

1 year ago

0.0.182

1 year ago

0.0.181

1 year ago

0.0.180

1 year ago

0.0.117

1 year ago

0.0.116

2 years ago

0.0.115

2 years ago

0.0.114

2 years ago

0.0.119

1 year ago

0.0.118

1 year ago

0.0.113

2 years ago

0.0.112

2 years ago

0.0.111

2 years ago

0.0.110

2 years ago

0.0.128

1 year ago

0.0.127

1 year ago

0.0.126

1 year ago

0.0.125

1 year ago

0.0.129

1 year ago

0.0.120

1 year ago

0.0.124

1 year ago

0.0.123

1 year ago

0.0.122

1 year ago

0.0.121

1 year ago

0.0.139

1 year ago

0.0.138

1 year ago

0.0.137

1 year ago

0.0.136

1 year ago

0.0.131

1 year ago

0.0.130

1 year ago

0.0.135

1 year ago

0.0.134

1 year ago

0.0.133

1 year ago

0.0.132

1 year ago

0.0.95

2 years ago

0.0.149

1 year ago

0.0.96

2 years ago

0.0.148

1 year ago

0.0.97

2 years ago

0.0.147

1 year ago

0.0.98

2 years ago

0.0.99

2 years ago

0.0.142

1 year ago

0.0.141

1 year ago

0.0.140

1 year ago

0.0.146

1 year ago

0.0.91

2 years ago

0.0.145

1 year ago

0.0.92

2 years ago

0.0.144

1 year ago

0.0.93

2 years ago

0.0.143

1 year ago

0.0.94

2 years ago

0.0.90

2 years ago

0.0.89

2 years ago

0.0.88

2 years ago

0.0.87

2 years ago

0.0.86

2 years ago

0.0.82

2 years ago

0.0.81

2 years ago

0.0.80

2 years ago

0.0.79

2 years ago

0.0.78

2 years ago

0.0.77

2 years ago

0.0.76

2 years ago

0.0.75

2 years ago

0.0.74

2 years ago

0.0.73

2 years ago

0.0.71

2 years ago

0.0.70

2 years ago

0.0.69

2 years ago

0.0.68

2 years ago

0.0.67

2 years ago

0.0.66

2 years ago

0.0.65

2 years ago

0.0.64

2 years ago

0.0.63

2 years ago

0.0.62

2 years ago

0.0.61

2 years ago

0.0.60

2 years ago

0.0.59

2 years ago

0.0.58

2 years ago

0.0.57

2 years ago

0.0.56

2 years ago

0.0.55

2 years ago

0.0.54

2 years ago

0.0.53

2 years ago

0.0.52

2 years ago

0.0.51

2 years ago

0.0.50

2 years ago

0.0.49

2 years ago

0.0.47

2 years ago

0.0.46

2 years ago

0.0.45

2 years ago

0.0.44

2 years ago

0.0.43

2 years ago

0.0.42

2 years ago

0.0.41

2 years ago

0.0.40

2 years ago

0.0.39

2 years ago

0.0.38

2 years ago

0.0.37

2 years ago

0.0.35

2 years ago

0.0.34

2 years ago

0.0.33

2 years ago

0.0.32

2 years ago

0.0.31

2 years ago

0.0.30

2 years ago

0.0.29

2 years ago

0.0.28

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

0.1.0

2 years ago