beef up docs and defrag setup flow

This commit is contained in:
Leo Horie 2017-01-08 10:53:54 -05:00
parent f0d6b0d58b
commit 7c00aad19c
4 changed files with 246 additions and 74 deletions

View file

@ -1,69 +1,133 @@
# ES6
- [Setup](#setup)
- [Using Babel with Webpack](#using-babel-with-webpack)
---
Mithril is written in ES5, and is fully compatible with ES6 as well.
In some limited environments, it's possible to use a significant subset of ES6 directly without extra tooling (for example, in internal applications that do not support IE). However, for the vast majority of use cases, a compiler toolchain like [Babel](https://babeljs.io) is required to compile ES6 features down to ES5.
### Setup
The simplest way to setup an ES6 compilation toolchain is via [Babel](https://babeljs.io/). To install, use this command:
The simplest way to setup an ES6 compilation toolchain is via [Babel](https://babeljs.io/).
Babel requires NPM, which is automatically installed when you install [Node.js](https://nodejs.org/en/). Once NPM is installed, create a project folder and run this command:
```bash
npm install babel-cli babel-preset-es2015 transform-react-jsx --save-dev
npm init -y
```
If you want to use Webpack and Babel together, [skip to the section below](#using-babel-with-webpack).
To install Babel as a standalone tool, use this command:
```bash
npm install babel-cli babel-preset-es2015 babel-plugin-transform-react-jsx --save-dev
```
Create a `.babelrc` file:
```
{
"presets": ["es2015"],
"plugins": [
["transform-react-jsx", {
"pragma": "m"
}]
]
"presets": ["es2015"],
"plugins": [
["transform-react-jsx", {
"pragma": "m"
}]
]
}
```
To run Babel as a standalone tool, run this from the command line:
```bash
babel src --out-dir lib --source-maps
babel src --out-dir bin --source-maps
```
#### Using Babel with Webpack
If you're using Webpack as a bundler, you can integrate Babel to Webpack, however this requires some additional dependencies, in addition to the steps above.
If you're already using Webpack as a bundler, you can integrate Babel to Webpack by following these steps.
```bash
npm install babel-core babel-loader --save-dev
npm install babel-core babel-loader babel-preset-es2015 babel-plugin-transform-react-jsx --save-dev
```
Create a file called `.webpack.config`
Create a `.babelrc` file:
```javascript
module.exports = {
entry: './src/index.js',
output: {
path: './bin',
filename: 'app.js',
},
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}]
}
```
{
"presets": ["es2015"],
"plugins": [
["transform-react-jsx", {
"pragma": "m"
}]
]
}
```
---
Next, create a file called `webpack.config.js`
### Custom setups
```javascript
module.exports = {
entry: './src/index.js',
output: {
path: './bin',
filename: 'app.js',
},
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}]
}
}
```
If you're using Webpack, you can [follow its excellent guide to add support for ES6](https://webpack.github.io/docs/usage.html#transpiling-es2015-using-babel-loader)
This configuration assumes the source code file for the application entry point is in `src/index.js`, and this will output the bundle to `bin/app.js`.
If you want to use Babel as a standalone tool, [here's the instructions for how to set it up](https://babeljs.io/docs/setup/#installation).
To run the bundler, setup an npm script. Open `package.json` and add this entry under `"scripts"`:
[Google closure compiler](https://www.npmjs.com/package/google-closure-compiler) is another tool that supports ES6 to ES5 compilation.
```
{
"name": "my-project",
"scripts": {
"start": "webpack -d --watch"
}
}
```
You can now then run the bundler by running this from the command line:
```bash
npm start
```
#### Production build
To generate a minified file, open `package.json` and add a new npm script called `build`:
```
{
"name": "my-project",
"scripts": {
"start": "webpack -d --watch",
"build": "webpack -p"
}
}
```
You can use hooks in your production environment to run the production build script automatically. Here's an example for [Heroku](https://www.heroku.com/):
```
{
"name": "my-project",
"scripts": {
"start": "webpack -d --watch",
"build": "webpack -p",
"heroku-postbuild": "webpack -p"
}
}
```