{"id":1844,"date":"2016-05-14T21:40:17","date_gmt":"2016-05-14T16:10:17","guid":{"rendered":"http:\/\/ramkulkarni.com\/blog\/?p=1844"},"modified":"2016-05-14T21:40:17","modified_gmt":"2016-05-14T16:10:17","slug":"setting-up-webpack-babel-reactjs","status":"publish","type":"post","link":"http:\/\/ramkulkarni.com\/blog\/setting-up-webpack-babel-reactjs\/","title":{"rendered":"Setting up Webpack + Babel + ReactJS"},"content":{"rendered":"<p>I started using <a href=\"https:\/\/webpack.github.io\/\" target=\"_blank\">Webpack<\/a> module bundler recently and thought I would document some of my learning. In the past couple of\u00a0posts I wrote about bundling JavaScript projects using Browserify\u00a0module bundlers. Webpack can be used as a replacement for Browserify; but it has some nice additional features like bundling of static files and code splitting.<\/p>\n<p>This post is not meant to be tutorial on Webpack, I just wanted to describe\u00a0how to configure Webpack, specifically\u00a0using Webpack loaders for Bablel and ReactJS. I will show how to build a JS project using standalone Webpack and also using Webpack APIs in Gulp.\u00a0The project is available on Github at <a href=\"https:\/\/github.com\/ramkulkarni1\/WebpackBlitzReactStarter\">https:\/\/github.com\/ramkulkarni1\/WebpackBlitzReactStarter<\/a><\/p>\n<h2>Module Dependencies<\/h2>\n<p>Node packages\u00a0required to build the project are, of course, in <a href=\"https:\/\/raw.githubusercontent.com\/ramkulkarni1\/WebpackBlitzReactStarter\/master\/package.json\">package.json<\/a>. If you are using above\u00a0starter project from Github, then run &#8216;npm install&#8217; to install all the required packages. Else make sure you install all the packages listed in devDependencies of package.json . The list contains babel, required babel presets for ES6 and ReactJS, webpack and webpack-dev-server, webpack loaders for babel, LESS and CSS.<\/p>\n<h2>Webpack Configuration<\/h2>\n<p>Webpack configuration is specified in\u00a0<a href=\"https:\/\/raw.githubusercontent.com\/ramkulkarni1\/WebpackBlitzReactStarter\/master\/webpack.config.js\">webpack.config.js<\/a>. This config file is read when you run webpack or webpack-dev-server command. Optional gulpfile.js (to build the project using Gulp) also loads\u00a0this file and passes to Webpack APIs.<\/p>\n<p>Here is a brief description of some of the entries in the config file &#8211;<!--more--><\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">context <span style=\"color: #406080;\">:<\/span> path<span style=\"color: #308080;\">.<\/span><span style=\"color: #200080; font-weight: bold;\">join<\/span><span style=\"color: #308080;\">(<\/span>__dirname<span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">src<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">)<\/span>\n<\/pre>\n<p>Context sets the parent path from where\u00a0all paths in the entry element (which tells starting scripts for Webpack to process) are resolved.<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">entry <span style=\"color: #406080;\">:<\/span><span style=\"color: #308080;\">[<\/span>\n  <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">.\/js\/index.js<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span>\n  <span style=\"color: #595979;\">\/\/include following entries only if you want to use webpack-dev-server and its hot module replacement feature<\/span>\n  <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">webpack-dev-server\/client?http:\/\/localhost:<\/span><span style=\"color: #800000;\">'<\/span> <span style=\"color: #308080;\">+<\/span> serverPort <span style=\"color: #308080;\">+<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">\/<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span>\n  <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">webpack\/hot\/dev-server<\/span><span style=\"color: #800000;\">'<\/span>\n<span style=\"color: #308080;\">]<\/span>\n<\/pre>\n<p>Entry point for\u00a0this application is src\/js\/index.js. Since we have already set the path to src in the context, we specify relative path here. The next two webpack entries are required only if you are using <a href=\"https:\/\/webpack.github.io\/docs\/webpack-dev-server.html\">webpack-dev-server<\/a>\u00a0(which provides a web server using which you can test your application) and want to use its hot deployment feature.<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">output <span style=\"color: #406080;\">:<\/span> <span style=\"color: #406080;\">{<\/span>\n  path <span style=\"color: #406080;\">:<\/span> outputPath<span style=\"color: #308080;\">,<\/span>\n  filename <span style=\"color: #406080;\">:<\/span> outputFileName<span style=\"color: #308080;\">,<\/span>\n<span style=\"color: #406080;\">}<\/span>\n<\/pre>\n<p>This entry tells Webpack where to save\u00a0output file and name of the file.<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">outputPath <span style=\"color: #308080;\">=<\/span> path<span style=\"color: #308080;\">.<\/span><span style=\"color: #200080; font-weight: bold;\">join<\/span><span style=\"color: #308080;\">(<\/span>__dirname<span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">build<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">,<\/span>\noutputFileName <span style=\"color: #308080;\">=<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">bundle.js<\/span><span style=\"color: #800000;\">'<\/span>\n<\/pre>\n<p>At the top of the script outputPath is set to build folder and file name to bundle.js<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">devtool <span style=\"color: #406080;\">:<\/span> isProduction <span style=\"color: #406080;\">?<\/span> <span style=\"color: #0f4d75;\">null<\/span> <span style=\"color: #406080;\">:<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">source-map<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span>\n<\/pre>\n<p>This parameter\u00a0tells Webpack if sourcemap to be generated for the output file. We set this option if we are not packaging for production (by setting NODE_ENV environment variable to &#8216;production&#8217;).<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">devServer <span style=\"color: #406080;\">:<\/span> <span style=\"color: #406080;\">{<\/span>\n  outputPath <span style=\"color: #406080;\">:<\/span> outputPath<span style=\"color: #308080;\">,<\/span>\n  contentBase<span style=\"color: #406080;\">:<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">.\/build<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span>\n  port <span style=\"color: #406080;\">:<\/span> serverPort<span style=\"color: #308080;\">,<\/span>\n  hot<span style=\"color: #406080;\">:<\/span> <span style=\"color: #0f4d75;\">true<\/span><span style=\"color: #308080;\">,<\/span>\n  stats<span style=\"color: #406080;\">:<\/span> <span style=\"color: #406080;\">{<\/span> colors<span style=\"color: #406080;\">:<\/span> <span style=\"color: #0f4d75;\">true<\/span><span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">,<\/span>\n  filename<span style=\"color: #406080;\">:<\/span> outputFileName\n<span style=\"color: #406080;\">}<\/span>\n<\/pre>\n<p>These are the settings passed to webpack-dev-server. We are again setting path of output file (probably not require), folder from where the server will serve files (as contentBase), server port and output file name. Note that when you use webpack-dev-server, generated output files are not written to the disk, but they are served from the memory. So if you just run webpack-dev-server, you may not find bundle.js (output filename) in build folder.<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">module <span style=\"color: #406080;\">:<\/span> <span style=\"color: #406080;\">{<\/span>\n  loaders <span style=\"color: #406080;\">:<\/span><span style=\"color: #308080;\">[<\/span>\n    <span style=\"color: #406080;\">{<\/span>\n      <span style=\"color: #200080; font-weight: bold;\">test<\/span><span style=\"color: #308080;\">:<\/span> <span style=\"color: #800000;\">\/<\/span><span style=\"color: #0f69ff;\">\\.<\/span><span style=\"color: #1060b6;\">jsx<\/span><span style=\"color: #308080;\">?<\/span><span style=\"color: #308080;\">$<\/span><span style=\"color: #800000;\">\/<\/span><span style=\"color: #308080;\">,<\/span>\n      exclude<span style=\"color: #308080;\">:<\/span> <span style=\"color: #800000;\">\/<\/span><span style=\"color: #1060b6;\">node_modules<\/span><span style=\"color: #800000;\">\/<\/span><span style=\"color: #308080;\">,<\/span>\n      loader<span style=\"color: #406080;\">:<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">babel<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span>\n      query<span style=\"color: #406080;\">:<\/span> <span style=\"color: #406080;\">{<\/span>\n        presets<span style=\"color: #406080;\">:<\/span> <span style=\"color: #308080;\">[<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">es2015<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">stage-0<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">react<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">react-hmre<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">]<\/span>\n      <span style=\"color: #406080;\">}<\/span>\n    <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">,<\/span>\n    <span style=\"color: #406080;\">{<\/span>\n      <span style=\"color: #200080; font-weight: bold;\">test<\/span> <span style=\"color: #308080;\">:<\/span> <span style=\"color: #800000;\">\/<\/span><span style=\"color: #0f69ff;\">\\.<\/span><span style=\"color: #1060b6;\">less<\/span><span style=\"color: #308080;\">$<\/span><span style=\"color: #800000;\">\/<\/span><span style=\"color: #308080;\">,<\/span>\n      loader <span style=\"color: #406080;\">:<\/span> extractCSS <span style=\"color: #406080;\">?<\/span> ExtractPlugin<span style=\"color: #308080;\">.<\/span>extract<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">style<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">css!less<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">:<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">style-loader!css-loader!less-loader<\/span><span style=\"color: #800000;\">'<\/span>\n    <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">,<\/span>\n    <span style=\"color: #406080;\">{<\/span>\n      <span style=\"color: #200080; font-weight: bold;\">test<\/span><span style=\"color: #308080;\">:<\/span> <span style=\"color: #800000;\">\/<\/span><span style=\"color: #0f69ff;\">\\.<\/span><span style=\"color: #1060b6;\">css<\/span><span style=\"color: #308080;\">$<\/span><span style=\"color: #800000;\">\/<\/span><span style=\"color: #308080;\">,<\/span>\n      loader<span style=\"color: #406080;\">:<\/span> extractCSS <span style=\"color: #406080;\">?<\/span> ExtractPlugin<span style=\"color: #308080;\">.<\/span>extract<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">style<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">css<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">:<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">style-loader!css-loader<\/span><span style=\"color: #800000;\">'<\/span>\n    <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">,<\/span>\n    <span style=\"color: #406080;\">{<\/span>\n      <span style=\"color: #200080; font-weight: bold;\">test<\/span><span style=\"color: #308080;\">:<\/span> <span style=\"color: #800000;\">\/<\/span><span style=\"color: #0f69ff;\">\\.<\/span><span style=\"color: #308080;\">(<\/span><span style=\"color: #1060b6;\">png<\/span><span style=\"color: #406080;\">|<\/span><span style=\"color: #1060b6;\">jpg<\/span><span style=\"color: #406080;\">|<\/span><span style=\"color: #1060b6;\">svg<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">$<\/span><span style=\"color: #800000;\">\/<\/span><span style=\"color: #308080;\">,<\/span>\n      loaders<span style=\"color: #406080;\">:<\/span> <span style=\"color: #308080;\">[<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">url<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">image-webpack<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">]<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n  <span style=\"color: #308080;\">]<\/span>\n<span style=\"color: #406080;\">}<\/span>\n<\/pre>\n<p>Next we configure Webpack loaders. We tell Webpack how to process files with certain extensions by specifying loaders. So in the above snippet we have loaders for processing js\/jsx, .less, .css and image files. I have added image loader just to show how images can be packaged in the output JS file using Webpack. It may not be a good idea to load large images using image loader upfront, because images are included as data url (content is embedded as base64 in the output JS file). But it may be a great way to package small icons. We will see sample of this later.<\/p>\n<p>Just like images, you can also package CSS inside the output JS file. However we can make Webpack output them seperately too, by using\u00a0extract-text-webpack-plugin. In the above configuration, this options is controlled by a flag\u00a0extractCSS. If this is set to true, CSS files will output separately.<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">resolve<span style=\"color: #406080;\">:<\/span> <span style=\"color: #406080;\">{<\/span>\n  extensions<span style=\"color: #406080;\">:<\/span> <span style=\"color: #308080;\">[<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">.js<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">.jsx<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">.css<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">.less<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">]<\/span>\n<span style=\"color: #406080;\">}<\/span>\n<\/pre>\n<p>In the resolve-&gt;extensions option we till Webpack what file extensions to be considered for processing.<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">plugins <span style=\"color: #406080;\">:<\/span> <span style=\"color: #308080;\">[<\/span>\n  failPlugin<span style=\"color: #308080;\">,<\/span>\n  <span style=\"color: #200080; font-weight: bold;\">new<\/span> copyWebpackPlugin<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">[<\/span>\n    <span style=\"color: #406080;\">{<\/span>from <span style=\"color: #406080;\">:<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">html<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> to<span style=\"color: #406080;\">:<\/span> __dirname <span style=\"color: #308080;\">+<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">\/build<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">,<\/span>\n  <span style=\"color: #308080;\">]<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">,<\/span>\n  <span style=\"color: #595979;\">\/\/include following plugin only if you want to use webpack-dev-server and its hot module replacement feature<\/span>\n  <span style=\"color: #200080; font-weight: bold;\">new<\/span> webpack<span style=\"color: #308080;\">.<\/span>HotModuleReplacementPlugin<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span>\n<span style=\"color: #308080;\">]<\/span>\n<\/pre>\n<p>Here we add a plugin to return status code 1 if Webpack fails (using\u00a0webpack-fail-plugin), a plugin to copy static files in html folder to build folder (copy-webpack-plugin) and a plugin to enable <a href=\"https:\/\/webpack.github.io\/docs\/hot-module-replacement-with-webpack.html\">hot module replacement in Webpack<\/a>.<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">serverPort <span style=\"color: #406080;\">:<\/span> serverPort\n<\/pre>\n<p>The last parameter is not for Webpack, but used in the gulpfile in this project. It just for passing serverPort value set in this config file to Gulp.<\/p>\n<p>See <a href=\"https:\/\/webpack.github.io\/docs\/configuration.html\">Webpack configuration docs<\/a> for more detailed information.<\/p>\n<h2>Application\u00a0Source Code<\/h2>\n<p>The sample app is very simple; the main JS entry point is <a href=\"https:\/\/raw.githubusercontent.com\/ramkulkarni1\/WebpackBlitzReactStarter\/master\/src\/js\/index.js\">index.js<\/a> that creates React view called <a href=\"https:\/\/raw.githubusercontent.com\/ramkulkarni1\/WebpackBlitzReactStarter\/master\/src\/js\/mainView.js\">MainView<\/a>. This view\u00a0displays a textbox, a button and a image icon.<\/p>\n<p>index.js &#8211;<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #200080; font-weight: bold;\">import<\/span> React from <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">react<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #200080; font-weight: bold;\">import<\/span> ReactDOM from <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">react-dom<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #200080; font-weight: bold;\">import<\/span> MainView from <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">.\/mainView<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #406080;\">;<\/span>\n\n<span style=\"color: #200080; font-weight: bold;\">var<\/span> mainView <span style=\"color: #308080;\">=<\/span> React<span style=\"color: #308080;\">.<\/span>createElement<span style=\"color: #308080;\">(<\/span>MainView<span style=\"color: #308080;\">,<\/span><span style=\"color: #406080;\">{<\/span>name<span style=\"color: #406080;\">:<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">there<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">,<\/span><span style=\"color: #0f4d75;\">null<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\nReactDOM<span style=\"color: #308080;\">.<\/span>render<span style=\"color: #308080;\">(<\/span>mainView<span style=\"color: #308080;\">,<\/span> document<span style=\"color: #308080;\">.<\/span>getElementById<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">main<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n<\/pre>\n<p>and mainView.js<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #200080; font-weight: bold;\">import<\/span> React from <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">react<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #200080; font-weight: bold;\">import<\/span> <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">..\/css\/app.less<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #200080; font-weight: bold;\">import<\/span> homeImage from <span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">..\/html\/home.png<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #406080;\">;<\/span>\n\n<span style=\"color: #200080; font-weight: bold;\">export<\/span> <span style=\"color: #200080; font-weight: bold;\">default<\/span> <span style=\"color: #200080; font-weight: bold;\">class<\/span> MainView <span style=\"color: #200080; font-weight: bold;\">extends<\/span> React<span style=\"color: #308080;\">.<\/span>Component <span style=\"color: #406080;\">{<\/span>\n  <span style=\"color: #007d45;\">constructor<\/span><span style=\"color: #308080;\">(<\/span>props<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n    <span style=\"color: #200080; font-weight: bold;\">super<\/span><span style=\"color: #308080;\">(<\/span>props<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\t<span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>state <span style=\"color: #308080;\">=<\/span> <span style=\"color: #406080;\">{<\/span>\n\t  name <span style=\"color: #406080;\">:<\/span> props<span style=\"color: #308080;\">.<\/span>name\n\t<span style=\"color: #406080;\">}<\/span>\n<span style=\"color: #406080;\">}<\/span>\n\nonSetName <span style=\"color: #308080;\">=<\/span> <span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #308080;\">=<\/span><span style=\"color: #308080;\">&gt;<\/span> <span style=\"color: #406080;\">{<\/span>\n  <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>setState<span style=\"color: #308080;\">(<\/span><span style=\"color: #406080;\">{<\/span>\n    name <span style=\"color: #406080;\">:<\/span> <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>refs<span style=\"color: #308080;\">.<\/span>nameTxt<span style=\"color: #308080;\">.<\/span>value\n  <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span>\n<span style=\"color: #406080;\">}<\/span>\n\nrender<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n  <span style=\"color: #200080; font-weight: bold;\">return<\/span> <span style=\"color: #308080;\">&lt;<\/span>div className<span style=\"color: #308080;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">main-view<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">&gt;<\/span>\n\tHello <span style=\"color: #406080;\">{<\/span><span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>state<span style=\"color: #308080;\">.<\/span>name<span style=\"color: #406080;\">}<\/span> <span style=\"color: #308080;\">!<\/span>\n\t<span style=\"color: #308080;\">&lt;<\/span>p<span style=\"color: #308080;\">\/<\/span><span style=\"color: #308080;\">&gt;<\/span>\n    Enter Name <span style=\"color: #406080;\">:<\/span> <span style=\"color: #308080;\">&lt;<\/span><span style=\"color: #007d45;\">input<\/span> type<span style=\"color: #308080;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">text<\/span><span style=\"color: #800000;\">\"<\/span> ref<span style=\"color: #308080;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">nameTxt<\/span><span style=\"color: #800000;\">\"<\/span> <span style=\"color: #308080;\">\/<\/span><span style=\"color: #308080;\">&gt;<\/span>\n    <span style=\"color: #308080;\">&lt;<\/span>button type<span style=\"color: #308080;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">button<\/span><span style=\"color: #800000;\">\"<\/span> onClick<span style=\"color: #308080;\">=<\/span><span style=\"color: #406080;\">{<\/span><span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>onSetName<span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">&gt;<\/span>Set Name<span style=\"color: #308080;\">&lt;<\/span><span style=\"color: #308080;\">\/<\/span>button<span style=\"color: #308080;\">&gt;<\/span>\n    <span style=\"color: #308080;\">&lt;<\/span>p<span style=\"color: #308080;\">\/<\/span><span style=\"color: #308080;\">&gt;<\/span>\n    <span style=\"color: #308080;\">&lt;<\/span>img src<span style=\"color: #308080;\">=<\/span><span style=\"color: #406080;\">{<\/span>homeImage<span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">\/<\/span><span style=\"color: #308080;\">&gt;<\/span> <span style=\"color: #308080;\">-<\/span> Example of loaging image <span style=\"color: #200080; font-weight: bold;\">with<\/span> webpack image loader\n\t<span style=\"color: #308080;\">&lt;<\/span><span style=\"color: #308080;\">\/<\/span>div<span style=\"color: #308080;\">&gt;<\/span>\n  <span style=\"color: #406080;\">}<\/span>\n<span style=\"color: #406080;\">}<\/span>\n<\/pre>\n<p>Notice that static assets like app.less and home.png are also imported in the above file. As mentioned earlier, CSS output after processing app.less can be made to be saved as a separate file\u00a0using\u00a0extract-text-webpack-plugin &#8211; but the above code won&#8217;t change if we do that. Of course, if you create a seperate CSS file, you will have to include that in the html file using link tag.<\/p>\n<h2>Building and Running the Application<\/h2>\n<p>To build the application run webpack command. Webpack will read webpack.config.js file and generate output file, bundle.js in build folder. It will also copy index.html from html folder. This application really does not need a webserver, so you can run index.html directly in a browser. But if you run it using webpack-dev-server, you can take advantage of its hot module replacement feature and you can test you code without manually building the application after you make any changes to the source code. Run webpack-dev-server and browse to http:\/\/localhost:8080 to run the application. Note that you should install webpack and webpack-dev-server packages globally (npm install webpack webpack-dev-server -g)<\/p>\n<p>If you make any changes to the source code, for example you change styles in app.less or change the markup in mainView.js, you don&#8217;t need to rebuild the app manually &#8211; webpack-dev-server automatically detects changes and rebuilds the app and refreshes the page in the browser (because of hot module replacement feature).<\/p>\n<h2>Using Webpack APIs with Gulp<\/h2>\n<p>Webpack provides JS APIs too, which you can use with Gulp. <a href=\"https:\/\/raw.githubusercontent.com\/ramkulkarni1\/WebpackBlitzReactStarter\/master\/gulpfile.js\">gulpfile.js<\/a> shows example of this.<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #200080; font-weight: bold;\">var<\/span> gulp <span style=\"color: #308080;\">=<\/span> require<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">gulp<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">,<\/span>\n  webpack <span style=\"color: #308080;\">=<\/span> require<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">webpack<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">,<\/span>\n  devServer <span style=\"color: #308080;\">=<\/span> require<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">webpack-dev-server<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">)<\/span>\n<span style=\"color: #406080;\">;<\/span>\n\n<span style=\"color: #200080; font-weight: bold;\">var<\/span> webpackConfig <span style=\"color: #308080;\">=<\/span> require<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">.\/webpack.config<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\n<span style=\"color: #200080; font-weight: bold;\">function<\/span> <span style=\"color: #200080; font-weight: bold;\">compile<\/span><span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n  <span style=\"color: #200080; font-weight: bold;\">var<\/span> compiler <span style=\"color: #308080;\">=<\/span> webpack<span style=\"color: #308080;\">(<\/span>webpackConfig<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\n  compiler<span style=\"color: #308080;\">.<\/span>run<span style=\"color: #308080;\">(<\/span><span style=\"color: #200080; font-weight: bold;\">function<\/span><span style=\"color: #308080;\">(<\/span>err<span style=\"color: #308080;\">,<\/span> stat<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">{<\/span>\n\t<span style=\"color: #200080; font-weight: bold;\">if<\/span> <span style=\"color: #308080;\">(<\/span>err<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n\t  console<span style=\"color: #308080;\">.<\/span><span style=\"color: #200080; font-weight: bold;\">log<\/span><span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">Error building application - <\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> err<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\t  <span style=\"color: #200080; font-weight: bold;\">return<\/span><span style=\"color: #406080;\">;<\/span>\n\t<span style=\"color: #406080;\">}<\/span>\n\t<span style=\"color: #200080; font-weight: bold;\">var<\/span> statJson <span style=\"color: #308080;\">=<\/span> stat<span style=\"color: #308080;\">.<\/span>toJson<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\t<span style=\"color: #200080; font-weight: bold;\">if<\/span> <span style=\"color: #308080;\">(<\/span>statJson<span style=\"color: #308080;\">.<\/span>errors<span style=\"color: #308080;\">.<\/span><span style=\"color: #200080; font-weight: bold;\">length<\/span> <span style=\"color: #308080;\">&gt;<\/span> <span style=\"color: #008c00;\">0<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n\t  console<span style=\"color: #308080;\">.<\/span><span style=\"color: #200080; font-weight: bold;\">log<\/span><span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">Error building application - <\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> statJson<span style=\"color: #308080;\">.<\/span>errors<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\t  <span style=\"color: #200080; font-weight: bold;\">return<\/span><span style=\"color: #406080;\">;<\/span>\n\t<span style=\"color: #406080;\">}<\/span>\n\n\tconsole<span style=\"color: #308080;\">.<\/span><span style=\"color: #200080; font-weight: bold;\">log<\/span><span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">Application built successfully !<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n  <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #406080;\">}<\/span>\n\n<span style=\"color: #200080; font-weight: bold;\">function<\/span> startServer<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n  <span style=\"color: #200080; font-weight: bold;\">var<\/span> compiler <span style=\"color: #308080;\">=<\/span> webpack<span style=\"color: #308080;\">(<\/span>webpackConfig<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n  <span style=\"color: #200080; font-weight: bold;\">var<\/span> server <span style=\"color: #308080;\">=<\/span> <span style=\"color: #200080; font-weight: bold;\">new<\/span> devServer<span style=\"color: #308080;\">(<\/span>compiler<span style=\"color: #308080;\">,<\/span> webpackConfig<span style=\"color: #308080;\">.<\/span>devServer<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n  server<span style=\"color: #308080;\">.<\/span>listen<span style=\"color: #308080;\">(<\/span>webpackConfig<span style=\"color: #308080;\">.<\/span>serverPort<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #406080;\">}<\/span>\n\ngulp<span style=\"color: #308080;\">.<\/span>task<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">default<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #200080; font-weight: bold;\">function<\/span><span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">{<\/span>\n  startServer<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\ngulp<span style=\"color: #308080;\">.<\/span>task<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #1060b6;\">build<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #200080; font-weight: bold;\">function<\/span><span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n  <span style=\"color: #200080; font-weight: bold;\">compile<\/span><span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n<\/pre>\n<p>Run &#8216;gulp&#8217; command to build the project and start webpack-dev-server. Run &#8216;gulp build&#8217; if you just want to build the project and generate output files in build folder.<\/p>\n<p>-Ram Kulkarni<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I started using Webpack module bundler recently and thought I would document some of my learning. In the past couple of\u00a0posts I wrote about bundling JavaScript projects using Browserify\u00a0module bundlers. Webpack can be used as a replacement for Browserify; but it has some nice additional features like bundling of static files and code splitting. This &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/ramkulkarni.com\/blog\/setting-up-webpack-babel-reactjs\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Setting up Webpack + Babel + ReactJS&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Setting up Webpack + Babel + ReactJS","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[20,6,1],"tags":[123,129,5,130,131],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2g9O8-tK","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/1844"}],"collection":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/comments?post=1844"}],"version-history":[{"count":0,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/1844\/revisions"}],"wp:attachment":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/media?parent=1844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/categories?post=1844"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/tags?post=1844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}