This example assumes code to be in src/
and the output to be put into out/
.
As such the folder structure should look something like
example/
|-- src/
| |-- index.js
| `-- ...
|-- out/
`-- package.json
Assuming a setup npm environment, we first need to setup babel in order to transpile the React code into es5 compliant code.
$npm install --save-dev babel-core babel-loader babel-preset-es2015 babel-preset-react
The above command will instruct npm to install the core babel libraries as well as the loader module for use with webpack. We also install the es6 and react presets for babel to understand JSX and es6 module code. (More information about the presets can be found here Babel presets)
$npm i -D webpack
This command will install webpack as a development dependency. (i is the shorthand for install and -D the shorthand for --save-dev)
You might also want to install any additional webpack packages (such as additional loaders or the webpack-dev-server extension)
Lastly we will need the actual react code
$npm i -D react react-dom
With the dependencies setup we will need a webpack.config.js file to tell webpack what to do
simple webpack.config.js:
var path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'out'),
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
}
};
This file tells webpack to start with the index.js file (assumed to be in src/ )
and convert it into a single bundle.js file in the out
directory.
The module
block tells webpack to test all files encountered against the regular expression and if they match, will invoke the specified loader. (babel-loader
in this case)
Furthermore, the exclude
regex tells webpack to ignore this special loader for all modules in the node_modules
folder, this helps speed up the transpilation process.
Lastly, the query
option tells webpack what parameters to pass to babel and is used to pass along the presets we installed earlier.
All that is left now is to create the src/index.js
file and try packing the application
src/index.js:
'use strict'
import React from 'react'
import { render } from 'react-dom'
const App = () => {
return <h1>Hello world!</h1>
}
render(
<App />,
document. getElementById('app')
)
This file would normally render a simple <h1>Hello world!</h1>
Header into the html tag with the id 'app', but for now it should be enough to transpile the code once.
$./node_modules/.bin/webpack .
Will execute the locally installed version of webpack (use $webpack
if you installed webpack globally with -g)
This should create the file out/bundle.js
with the transpiled code inside and concludes the example.