從昨天晚上開始認真看 webpack 得教學,花了半天多才漸漸理解如何使用,我果然還是不適合一開始就用官方文件來閱讀,還是找有教學的影片來看比較好吸收。


1. 套件安裝
 ⊙ 主要套件 : webpack
 ⊙ ESLint 檢查: eslint eslint-loader
 ⊙ Babel (ECMAScript6 語法轉換) : babel-core babel-loader babel-preset-es2015
 ⊙ React 相關套件: react rect-dom jsx-loader eslint-plugin-react babel-preset-react

 packages.json 大致上會如下:

{
"name": "sample",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"dependencies": {
"jsx-loader": "^0.13.2",
"webpack": "^1.12.14"
},
"devDependencies": {
"babel-core": "^6.7.4",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"eslint": "^2.7.0",
"eslint-loader": "^1.3.0",
"eslint-plugin-react": "^4.2.3",
"react": "^0.14.8",
"react-dom": "^0.14.8"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}


2. webpack.config.js 設定檔
 ⊙ 目前就只測試到基本的 eslint 檢查,可用 .jsx 副檔名

module.exports = {
entry: ['./assets/jsx/app'],
output: {
path: 'public/js/',
filename: 'bundle.js'
},
resolve: {
extensions: ['', '.js', '.jsx']
},
module: {
preLoaders: [
{
test: /\.jsx$/,
exclude: /node_modules/,
loader: 'eslint-loader'
}
],
loaders: [
{
test: /\.js|jsx$/,
exclude: /node_modules/,
loader: 'babel-loader',
query:{
"presets": ["es2015", "react"]
}
}
]
}
}



3. ESLint 設定
 ⊙ 套件安裝
sudo npm install -g eslint

 ⊙ 產生基本設定檔,eslint --init,可參考下面回答

? How would you like to configure ESLint? Answer questions about your style
? Are you using ECMAScript 6 features? Yes
? Are you using ES6 modules? Yes
? Where will your code run? Browser
? Do you use CommonJS? Yes
? Do you use JSX? Yes
? Do you use React Yes
? What style of indentation do you use? Spaces
? What quotes do you use for strings? Single
? What line endings do you use? Unix
? Do you require semicolons? Yes
? What format do you want your config file to be in? JSON


 ⊙ 加上 React 檢查套件 npm install --save-dev eslint-plugin-react
   * extends 裡加上 `plugin:react/recommended` ,額外用別人寫好的檢查套件
     * 可避免 import module 後,出現未使用變數的問題
     * 若要使用`dangerouslySetInnerHTML`這語法,需在 rules 內加上 `"react/no-danger": 0`
   * jQuery 可在 env 內加上來略過錯誤
   * 全域變數請另外寫 globals 來略過檢查

{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"jquery": true
},
"extends": ["eslint:recommended", "plugin:react/recommended"],
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true,
"jsx": true
},
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
],
"react/no-danger": 0,
"no-console": ["error", { "allow": ["log", "warn", "error"] }]
},
"globals": {
"socket": true,
"eventEmitter": true
}
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 soarlin 的頭像
    soarlin

    每天都有新鮮事

    soarlin 發表在 痞客邦 留言(0) 人氣()