Yarn + webpack + BabelでES6入門
March 20, 2017
Yarn や webpack を使ったことがなかったので、 以下の記事をトレースする形で勉強がてら ES6 の環境を作ってみました。
webpack+babel 環境でフロントエンドも ES6 開発
環境
- macOS Sierra 10.12.3
- Node.js v6.10.0
- Yarn v0.21.3
1. Yarn インストール
Yarn が入ってない人はインストールしてください。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ brew update | |
$ brew install yarn | |
$ yarn -v | |
yarn install v0.21.3 | |
[1/4] 🔍 Resolving packages... | |
success Already up-to-date. | |
✨ Done in 0.36s. |
2. webpack + babel インストール
最初に Yarn を使って webpack と babel をインストールします。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ mkdir js-tutorial && cd js-tutorial | |
$ yarn init | |
$ yarn add -D webpack babel-core babel-loader babel-preset-es2015 | |
$ yarn # インストール |
3. Yarn と webpack の設定
ディレクトリ構成は以下のようにします。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ tree | |
├── dst | |
│ └── application.js # webpackでコンパイルしたJSファイル | |
├── index.html | |
├── node_modules | |
│ └── ... | |
├── package.json | |
├── src | |
│ └── ... # sourceファイル群 | |
├── webpack.config.js | |
└── yarn.lock |
それに合わせてwebpack.config.js
を以下のように書きます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
context: __dirname + '/src', | |
entry: { | |
'application': './application', | |
}, | |
output: { | |
path: __dirname + '/dst', | |
filename: '[name].js' | |
}, | |
module: { | |
loaders: [ | |
{ | |
test: /\.js$/, | |
exclude: /node_modules/, | |
loader: "babel-loader", | |
query: { | |
presets: ['es2015'] | |
} | |
} | |
] | |
} | |
}; |
webpack で ES6 をコンパイルするときのコマンドをpackage.json
の scripts に書いておきます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "js-tutorial", | |
"scripts": { | |
"build": "`yarn bin`/webpack" | |
}, | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"devDependencies": { | |
"babel-core": "^6.24.0", | |
"babel-loader": "^6.4.1", | |
"babel-preset-es2015": "^6.24.0", | |
"webpack": "^2.2.1" | |
}, | |
"dependencies": {} | |
} |
4. ES6 でコードを書く
ES6 でコードを書きます。 この辺は元記事とだいたいいっしょです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
constructor(name) { | |
this.name = name; | |
} | |
} | |
export default Person; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Person from './person'; | |
class Friend extends Person { | |
constructor(name) { | |
super(name); | |
} | |
callName() { | |
alert(this.name); | |
} | |
} | |
export default Friend; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Friend from './friend'; | |
let friend = new Friend('Taro'); | |
friend.callName(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Tutorial</title> | |
<script src="dst/application.js"></script> | |
</head> | |
<body> | |
<h1>JS Tutorial</h1> | |
</body> | |
</html> |
5. コンパイル&確認
webpack でコンパイルして、ブラウザで確認してみましょう。 alert が出れば OK です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ yarn run build | |
yarn run v0.21.3 | |
$ `yarn bin`/webpack | |
Hash: 4275ae52df5e0f0a5001 | |
Version: webpack 2.2.1 | |
Time: 502ms | |
Asset Size Chunks Chunk Names | |
application.js 5.58 kB 0 [emitted] application | |
[0] ./friend.js 2.15 kB {0} [built] | |
[1] ./person.js 356 bytes {0} [built] | |
[2] ./application.js 258 bytes {0} [built] | |
✨ Done in 1.43s. | |
$ hs | |
Starting up http-server, serving ./ | |
Available on: | |
http://127.0.0.1:8080 | |
http://192.168.1.8:8080 | |
http://192.168.33.1:8080 | |
Hit CTRL-C to stop the server |
ちなみに最後のhs
はhttp-serverという npm モジュールで、
さくっと web サーバを立てるのに便利です。