The minimalist requirement of a Webpack config file is the presence of entry and output properties.
The APP_DIR holds the directory path of the React application’s codebase and the BUILD_DIRrepresents the directory path of the bundle file output.
As the name suggests entry specifies the entry file using which the bundling process starts. If you are coming from C# or Java, it’s similar to the class that contains main method. Webpack supports multiple entry points too. Here the index.jsx in the src/client/app directory is the starting point of the application
The output instructs Webpack what to do after the bundling process has been completed. Here, we are instructing it to use the src/client/public directory to output the bundled file with the namebundle.js
Let’s create the index.jsx file in the ./src/client/app and add the following code to verify this configuration.
Now in the terminal run the following command
The above command runs the webpack in the development mode and generates the bundle.js file and its associated map file bundle.js.map in the src/client/public directory.
To make it more interactive, create an index.html file in the src/client directory and modify it to use this bundle.js file
Now if you open the browser, you can see the Hello World! in the console log.
Note: There is a webpack loader called html-loader which automatically creates this html file with the correct location of bundle.js.
Setting Up Babel-Loader
As we have seen in the beginning, by using JSX and ES6 we can be more productive while working with React. But the JSX syntax and ES6, are not supported in all the browsers.
Hence, if we are using them in the React code, we need to use a tool which translates them to the format that has been supported by the browsers. It’s where babel comes into the picture.
While installing Webpack we touched a little on loaders. Webpack uses loaders to translate the file before bundling them
To setup install the following npm packages
npm i babel-loader babel-preset-es2015 babel-preset-react -S
The babel-preset-es2015 and babel-preset-react are plugins being used by the babel-loader to translate ES6 and JSX syntax respectively.
As we did for Webpack, babel-loader also requires some configuration. Here we need to tell it to use the ES6 and JSX plugins.
Create a .babelrc file and update it as below
The next step is telling Webpack to use the babel-loader while bundling the files
open webpack.config.js file and update it as below
The loaders property takes array of loaders, here we are just using babel-loader. Each loaderproperty should specify what are the file extension it has to process via the test property. Here we have configured it to process both .js and .jsx files using the regular expression. The includeproperty specifies what is the directory to be used to look for these file extensions. The loaderproperty represents the name of the loader.
Now we all the setup done. Let’s write some code in React.
Use npm to install react and react-dom
Replace the existing console.log statement in the index.jsx with the following content
Then run the following command to update the bundle file with the new changes
Now, if you open the index.html in the browser you can see Hello React
Adding Some Complexity
Making Webpack to watch the changes
Running the webpack command on every time when you change the file is not a productive work. We can easily change this behavior by using the following command
Now Webpack is running in the watch mode, which will automatically bundle the file whenever there is a change detected. To test it, change Hello React to something else and refresh theindex.html in the browser. You can see your new changes.
If you don’t like refreshing the browser to see the changes you can use react-hot-loader!
Using npm as a tool runner
The command ./node_modules/.bin/webpack can be made even simpler by leveraging npm.