How to set up a basic webpack, React, and Babel environment

suggest change

Remarks

This build pipeline is not exactly what you would call “production ready”, but it does give a solid start. You can add what you need for the development experience you’re looking for. Some people take a fully built pipeline from Yeoman.io or elsewhere and strip off the parts they don’t want until it suits their style. There’s nothing wrong with that, but with the pipeline example you can also do the opposite and build up from bare bones. The config this page explains lives in that pipeline chapter, not above this text.

Some things you might like to add are things like a testing framework and coverage statistics like Karma with Mocha or Jasmine. Linting with ESLint. Hot module replacement in webpack-dev-server so that you can get that Ctrl+S, F5 development experience. Also the current pipeline only builds in dev mode so a production build task would be good.

Gotchas!

Notice that the context property of webpack.config.js uses Node’s path module rather than concatenating __dirname with the string /src. That is because Windows handles those separators poorly. To stay cross-platform, let Node build the path.

Explanation of webpack.config.js properties

context

This is the root path webpack uses when resolving relative file paths. In index.jsx, require('./index.html') resolves inside src/ because that directory is context.

entry

Where webpack looks first to begin bundling the solution. This is why you’ll see that in the index.jsx we are stitching together the solution with requires and imports.

output

This is where webpack writes the files it bundles. The filename is the name of the bundle that contains the JavaScript and styles.

devServer

These settings are specific to webpack-dev-server. contentBase is the server’s root; here that is the dist/ folder. port is the port the server listens on. open tells webpack-dev-server to open your default browser once the server is up.

module > loaders

This maps file types to loaders so webpack knows what to do with each file. test is a regular expression, usually matched against the file extension. loader or loaders names the loader module that bundles that file type. A query property on the JavaScript rule is just a query string passed to the loader. The HTML loader could use query the same way.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents