How to create NPM package for a React component - (using Webpack + Babel).
Recently I was working on creating a NPM package for a react component. Tried my hands on writing a react radio button component, with webpack and ES6.
You can checkout the source code in github repository: https://github.com/tan31989/react-radio-button and the npm repository link is: https://www.npmjs.com/package/react-radio-button.
Here is a quick update on the webpack I used for production and deploying the react component as a library so that others can use it:
You can checkout the source code in github repository: https://github.com/tan31989/react-radio-button and the npm repository link is: https://www.npmjs.com/package/react-radio-button.
Here is a quick update on the webpack I used for production and deploying the react component as a library so that others can use it:
webpack.config.build.js
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: [
/** This section is basically the hook for letting the webpack scan your code
* to find that right set of files to bundle up for the library you would
* want to expose. In my case, the file inside src/components is where
* I have the file to be provided as a library.
* This section is also known as entry point
*/
'./src/components/RadioButtonGroup'
],
output: {
/** This section takes care of outputting the entire set of files
* scanned from the entry point to the loader module of include/exclude
* directories as a bundled file.
*/
path: path.join(__dirname, 'lib'),
filename: 'index.js',
/** Very important: the target type for our library tells us how to
* export the ES6 react component for others to use and also to be able to
* find the component from node_modules.
* The UMD pattern typically attempts to offer compatibility with the
* most popular script loaders of the day (e.g RequireJS amongst others).
* In many cases it uses AMD as a base, with special-casing added to handle
* CommonJS compatibility.
*/
libraryTarget: 'umd'
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
],
resolve: {
modulesDirectories: ['node_modules', 'index'],
extensions: ['', '.js', '.jsx']
},
module: {
loaders: [
{
test: /\.js?/,
exclude: /build|node_modules|styles/,
loaders: ['babel'],
include: path.join(__dirname, 'src')
},
{
test: /\.css$/,
loader: 'style!css'
}
]
},
/** "externals" property on the config object "to specify dependencies for
* your library that are not resolved by webpack,
* but become dependencies of the output.
* This means they are imported from the envirnoment while runtime.
*/
externals: [
{
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
}
}
]
};
Here is the thing, a component of react written in ES6, requires the right loaders that can understand the code (babel-loader) and also the right target library, entry point and output point. With that in note, do remember, you need to add some more important things in:
package.json
{
...,
"main": "lib/index.js",
"scripts": {
"dev": "NODE_ENV=development npm start",
"start": "webpack --config webpack.config.dev.js && node devServer.js",
"build": "NODE_ENV=production webpack --config webpack.config.build.js",
"prepublish": "npm run build"
},
...
}
- "main" from package json ensures about the library getting picked up, once it is been downloaded using npm install.
- "prepublish" will ensure to run the build command, when we are about to deploy/publish to npm.
To directly publish to Npm without version update run the command:
$ npm publish
This will ensure, the package is published to the npm
Release and Publish:
To release a git tag and to npm publish the package:
- Just use the npm version <update_type>, where update_type is either major, minor or patch number.
$ npm version 1.5.4
- Automatically updates the package.json
- Once the data is changed and pushed to git, just publish using npm publish.
$ npm publish
NOTE:
- To publish, you must have a user on the npm registry.
- If you don't have one, create it with npm adduser.
- If you created one on the site, use npm login to store the credentials on the client.
- Check how to do so: https://docs.npmjs.com/getting-started/publishing-npm-packages.
Do let me know if there is any more information needed or you feel that I can give some assistance.
This is a great way for a simple quick start for ReactJs components topic. Thanks for the code examples and this blog post.
ReplyDeleteBest Regards,
ReactJS Online Training | Learn ReactJS Course | ReactJS Online Certification Training in Hyderabad-CourseIng
Happy to help :)
DeleteYour style is unique in comparison to other people I've read stuff from. Thanks for posting when you've got the opportunity, Guess I'll just book mark this site.
ReplyDeleteReactjs Training in Bangalore
pentaho online job support from india,
ReplyDeletepentaho project support from india,SAP SD online job support from india,SAP SD project job support from india,ReactJS online job support from india,ReactJS project support from india
ReplyDeleteAngular online job support from india,Angular project support from india,Ab initio online job support from india,Ab initio project support from india
Selenium online job support from india,Selenium project support from india
Nice article i was really impressed by seeing this article, To make NPM package using in production time to help improve the knowledge. it was very interesting and it is very useful for me.Thanks for sharing this wonderful content.its very useful to us.I gained many unknown information, the way you have clearly explained is really fantastic.
ReplyDeleteDevOps Training in Chennai
DevOps Online Training in Chennai
DevOps Training in Bangalore
DevOps Training in Hyderabad
DevOps Training in Coimbatore
DevOps Training
DevOps Online Training
I know, it is not an easy task to write such a big article in one day. Here you are, trying the big task and finishing it off and getting good comments and ratings.
ReplyDeleteIELTS Coaching in chennai
German Classes in Chennai
GRE Coaching Classes in Chennai
TOEFL Coaching in Chennai
spoken english classes in chennai | Communication training
Excellent Blog! I would Thanks for sharing this wonderful content.its very useful to us.I gained many unknown information, the way you have clearly explained is really fantastic concept!!
ReplyDeleteJava training in Chennai
Java Online training in Chennai
Java Course in Chennai
Best JAVA Training Institutes in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Training
Java Online Training
Thank you for the information. It is very useful and informative
ReplyDeleteangular js course in chennai
angular course in chennai
angular js online course in chennai
angular js course in bangalore
angular js course in hyderabad
angular js course in coimbatore
angular js course
angular js online course
This post is really nice and informative. The explanation given is really comprehensive and informative..
ReplyDeleteDigital Marketing Training in Chennai
Digital Marketing Course in Chennai
SEO Training in Chennai
Digital Marketing Training in Bangalore
Digital Marketing Training in Hyderabad
Digital Marketing Training in Coimbatore
Digital Marketing Training
Digital Marketing Course
Digital Marketing Online Training
Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
ReplyDeleteWeb Designing Training in Chennai
Web Designing Course in Chennai
Web Designing Training in Bangalore
Web Designing Course in Bangalore
Web Designing Training in Hyderabad
Web Designing Course in Hyderabad
Web Designing Training in Coimbatore
Web Designing Training
Web Designing Online Training
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteAWS Course in Chennai
AWS Course in Bangalore
AWS Course in Hyderabad
AWS Course in Coimbatore
AWS Course
AWS Certification Course
AWS Certification Training
AWS Online Training
AWS Training
I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts.
ReplyDeleteacte chennai
acte complaints
acte reviews
acte trainer complaints
acte trainer reviews
acte velachery reviews complaints
acte tambaram reviews complaints
acte anna nagar reviews complaints
acte porur reviews complaints
acte omr reviews complaints
Nice article i was really impressed by seeing this article, To make NPM package using in production time to help improve the knowledge.
ReplyDeletepython training in chennai
python course in chennai
python online training in chennai
python training in bangalore
python training in hyderabad
python online training
python training
python flask training
python flask online training
python training in coimbatore
we're called to use our gifts to serve others as faithful stewards of God's grace. ... Those are supernatural abilities God gives each Christ-follower, but the general idea of blessing others also carries over to talents. God doesn't give us anything just for our own benefit.keep up!!
ReplyDeleteAndroid Training in Chennai
Android Online Training in Chennai
Android Training in Bangalore
Android Training in Hyderabad
Android Training in Coimbatore
Android Training
Android Online Training
Superb blog post! And this blog clearly explain about for useful information. I would Thanks for sharing this wonderful content.its very useful to us. Keep it up!
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Online Training in Chennai
Software Testing Courses in Chennai
Software Testing Training in Bangalore
Software Testing Training in Hyderabad
Software Testing Training in Coimbatore
Software Testing Training
Software Testing Online Training
This post is very useful and helpful for all. There is another similar post
ReplyDeleteCyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course | CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course
Good post..
ReplyDeleteSAP BW on Hana training
SAP CS training
SAP Fico training
SAP Grc training
Excellent blog....
ReplyDeleteSAP BW on Hana training
SAP CS training
SAP Fico training
SAP Grc training