Table of Contents
URL: https://www.progressiverobot.com/react-getting-started-webpack-react/
This aim of this tutorial is to set up a development environment for a React application bundled using Webpack. While the merits of Webpack and other bundlers are continually compared, this tutorial will let you get started with Webpack and help you decide for yourself.
Dependencies
In addition, we'll need the node libraries for React and Webpack plus libraries for transpilation. This can be done with a string of commands while in the root folder:
npm install --save react react-dom create-react-class webpack
npm install --save-dev babel-core babel-loader babel-preset-es2015 babel-preset-react
File Structure
Webpack works by starting with an entry point. From here, Webpack builds a dependency chart of your application for the modules that need to be included for the program to run by looking at the import and require statements. For us, index.js will be that entry point. Make a dev folder as a place to hold index.js where we will make our changes. We'll also need a src folder for webpack to output the bundle out to.
mkdir dev src && touch dev/index.js
Now we can fill index.js with the demo code:
[label Index.js]
var React = require('react');
var ReactDOM = require('react-dom');
var createReactClass = require('create-react-class');
var Index = createReactClass({
render: function() {
return (
<div>
<p>Webpack and React!</p>
</div>
);
}
});
ReactDOM.render(<Index />, document.getElementById('app'));
index.html
Before we continue Webpacking, we'll need the actual spot where our bundle will be loaded. This occurs in index.html which should be made in the root directory and the code is as follows:
[label index.html]
<html>
<head>
<meta charset="utf-8">
<title>React and Webpack</title>
</head>
<body>
<div id="app" />
<script src="src/bundle.js" type="text/javascript"></script>
</body>
</html>
The file structure should look like this now:
.
├── dev
│ └── index.js
├── index.html
├── package.json
└── src
webpack.config.js
Now to set up the webpack.config.js file. This is all the information webpack needs to output a useable bundle.
[label webpack.config.js]
var webpack = require("webpack");
var path = require("path");
var DEV = path.resolve(__dirname, "dev");
var OUTPUT = path.resolve(__dirname, "src");
var config = {
entry: {
Index : DEV + "/index.js"
},
output: {
path: OUTPUT,
filename: "bundle.js",
},
module: {
loaders: [{
include: DEV,
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
query: {
presets: ['es2015', 'react']
}
},
]
}
};
module.exports = config;
entry tells Webpack where to start, the loaders tell Webpack how to treat that file extension and what to do with it, and output gives directions for where and how to write the bundle.
Packing
Now that we have everything in place, we should be able to see our app in action. Run ./node_modules/.bin/webpack --watch and in a few seconds you should see something like this:
➜ demo ./node_modules/.bin/webpack --watch
Webpack is watching the files…
Hash: 1594ffb6ae2044c83abe
Version: webpack 3.7.1
Time: 1477ms
Asset Size Chunks Chunk Names
bundle.js 863 kB 0 [emitted] [big] Index
[15] ./dev/index.js 468 bytes {0} [built]
+ 33 hidden modules
The --watch option gives us on-save refresh. When you make a change and save, reloading the index.html in your browser will automatically reload the new changes.
Also, making an alias for the compilation command saves at least a few tab completions: alias output="./node_modules/.bin/webpack"
Another Loader Example
Here is another loader, file-loader, which can be used to bundle the files with image extensions into a folder called images. You can read more about loaders and their configuration webpack.js.org
...
module: {
loaders: [{
include: DEV,
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
query: {
presets: ['es2015', 'react']
}
},
{
test: /\.(jpe?g|png|gif)$/i,
loader:"file-loader",
query:{
name:'[name].[ext]',
outputPath:'images/'
}
}
]
}
...