Skip to content

Instantly share code, notes, and snippets.

@vjwilson
Last active September 24, 2016 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vjwilson/a040c234c0d6683c1341331f57a81e77 to your computer and use it in GitHub Desktop.
Save vjwilson/a040c234c0d6683c1341331f57a81e77 to your computer and use it in GitHub Desktop.
Bare-bones Webpack config

Do-it-yourself React with Webpack

Between reinventing the wheel to make your own Webpack build of a React app, and using a "starter kit" or template, there is this option.

I think this make it easier to wrap your head around the meat of what Webpack does.

  1. Start with a bare-bones working Webpack configuration, that builds a working React app you can see right away.
  2. Make incremental tweaks and additions to learn-as-you-go.

Because gists cannot show directory structure, I could not show that you put the app.js file in a subdirectory named src/.

Your actually project directory needs to start out like this:

    ├── index.html
    ├── package.json
    ├── src
    │   └── app.js
    └── webpack.config.js

Important: replace ALL the strings in the sample package.json file that are bracketed (<...>) with your own preferences, or you will get lots of warnings, and npm will not run.

Once you have copied the contents of these files to your own project, do the following.

    npm install

Once all the dependencies have been installed, start up the app like this:

    npm start

Webpack will compile your React app and open a browser window for it. As you edit your app.js file, the browser window should reload automatically.

Your directory will now look like this.

    ├── bundle.js
    ├── bundle.js.map
    ├── index.html
    ├── node_modules/
    ├── package.json
    ├── src
    │   └── app.js
    └── webpack.config.js

Note: You do not create the node_modules directory yourself; it gets created when you first run npm install.

Webpack creates the bundle.js file with your compiled React app in it.

You can "require" or "import" additional submodules into your app, but every time you add a new file you may need to stop the watch process with CTRL+C, and then re-run npm start.

// put this in a src/ subdirectory
import React from 'react';
import ReactDOM from 'react-dom';
var App = React.createClass({
render: function() {
return <p>Hello World!</p>;
}
});
ReactDOM.render(
<App />,
document.getElementById('app')
);
<!DOCTYPE html>
<html>
<head>
<title>My React App</title>
</head>
<body>
<div id="app"></div>
<script src="bundle.js"></script>
</body>
</html>
{
"name": "<project name>",
"version": "<x.y.z>",
"description": "<project description>",
"scripts": {
"start": "npm-run-all --parallel webpack:watch live-server",
"clean": "rm bundle.js*",
"webpack": "webpack --progress --colors",
"webpack:watch": "webpack --progress --colors --watch",
"live-server": "live-server --port=3004",
},
"keywords": [
"<a keyword>",
"<another keyword>"
],
"author": "<author name>",
"repository": "<URL of version control>",
"license": "<license>",
"dependencies": {
"react": "^15.3.1",
"react-dom": "^15.3.1"
},
"devDependencies": {
"babel-core": "^6.14.0",
"babel-loader": "^6.2.5",
"babel-polyfill": "^6.13.0",
"babel-preset-es2015": "^6.14.0",
"babel-preset-react": "^6.11.1",
"live-server": "^1.1.0",
"npm-run-all": "^3.1.0",
"webpack": "^1.13.2"
}
}
module.exports = {
entry: [
'babel-polyfill',
'./src/app'
],
output: {
filename: 'bundle.js'
},
devtool: 'source-map',
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
},
]
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment