前言:vue有vue-cli
,react也有create-react-app
。花了點時間本身用webpack實現配置react開發環境
"dependencies": { "babel-polyfill": "^6.26.0", "react": "^16.2.0", "react-dom": "^16.2.0", "react-router-dom": "^4.2.2" }, "devDependencies": { "babel-cli": "^6.26.0", "babel-core": "^6.26.0", "babel-loader": "^7.1.2", "babel-preset-es2015": "^6.24.1", "babel-preset-react": "^6.24.1", "clean-webpack-plugin": "^0.1.17", "css-loader": "^0.28.7", "express": "^4.16.2", "file-loader": "^1.1.6", "html-webpack-plugin": "^2.30.1", "node-sass": "^4.7.2", "react-hot-loader": "^3.1.3", "sass-loader": "^6.0.6", "style-loader": "^0.19.1", "uglifyjs-webpack-plugin": "^1.1.5", "webpack": "^3.10.0", "webpack-dev-middleware": "^2.0.3", "webpack-hot-middleware": "^2.21.0", "webpack-merge": "^4.1.1" }
npm init //會生成一個package.json
react: ^16.2.0
react-dom: ^16.2.0
react-router-dom:^4.2.2
css
npm install react react-dom react-router-dom babel-polyfill --save // babel-polyfill用來支持es6語法
webpack: ^3.10.0
html
npm install webpack --save-dev
webpack.config.js
,若是在命令行直接使用webpack
(不指定參數),默認就是打包這個文件,大體結構以下:var path = require('path') module.exports = { //入口 entry:'./src/main.js' // 出口 output: { publicPath: '/', path: path.resolve(__dirname, 'dist'), filename: 'bundle.js' }, // 模塊解析 module: {}, // 插件 plugins:[] }
src目錄下的main.js
做爲入口打包進dist目錄下的bundle.js
,而後在頁面引用這個js就能夠了babel轉碼
,熱更新
,調試
等,打包時成生產環境代碼還須要壓縮
,分離第三方庫
等功能。npm install babel-cli babel-core babel-loader babel-preset-es2015 babel-preset-react --save-dev //安裝react,es6轉碼過程當中須要的依賴
.babelrc
文件//babel的配置文件 { "presets": [ [ "es2015", { "modules": false } ], "react" ], "plugins": ["react-hot-loader/babel"] }
.gitignore
文件// 項目提交倉庫是忽略的一些代碼 .DS_Store node_modules/ dist/ npm-debug.log* yarn-debug.log* yarn-error.log* # Editor directories and files .idea *.suo *.ntvs* *.njsproj *.sln
.editorconfig
//規範編碼風格 root = true [*] charset = utf-8 indent_style = space indent_size = 2 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true
webpack
,將開發環境和生產環境分開webpack.base.conf.js
基礎文件var path = require('path') module.exports = { // 出口 output: { publicPath: '/', path: path.resolve(__dirname, 'dist'), filename: '[name]-[hash].js' }, // 模塊 module: { rules: [{ test: /(\.js|\.jsx)$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['es2015', 'react'] } } }, { test: /\.css$/, use: ['style-loader','css-loader'] }, { test: /\.scss$/, use: ['style-loader','css-loader','sass-loader'] }, { test: /\.(png|svg|jpg|gif)$/, use: ['file-loader'] }, { test: /\.(woff|woff2|eot|ttf|otf)$/, use: ['file-loader'] }] } }
name
+hash
命名這樣每次修改代碼打包之後名字都會不同,而後引用須要的loader
npm install css-loader style-loader sass-loader node-loader file-loader react-hot-loader --save-dev
test
匹配正則加載須要的loader
,css預處理器使用的scss
(scss能夠看作sass的升級),react-hot-loader
是熱更新中須要用到的webpack.dev.conf.js
和webpack.prod.conf.js
爲開發環境打包和生產環境打包,添加使用過程當中的依賴npm install webpack-dev-middleware webpack-hot-middleware webpack-merge clean-webpack-plugin html-webpack-plugin uglifyjs-webpack-plugin express --save-dev
使用webpack-merge
繼承webpack.base.conf.js
中的配置,再單獨配置(詳細直接看代碼)
咱們將本身的代碼、第三方庫、轉碼用到的babel-polyfill分開打包,這樣再根據[name]-[hash].js
打包出三個文件vue
plugins
中加入webpack.optimize.CommonsChunkPlugin()
將第三方庫配置爲公共代碼(公共代碼加載頁面時會先引用)HtmlWebpackPlugin
配置,配置項目打包時引用的模板,咱們通常在根目錄下建議一個index.html
的頁面UglifyJsPlugin
用來壓縮代碼CleanWebpackPlugin
配置,每次打包時先清空HotModuleReplacementPlugin
和NoEmitOnErrorsPlugin
分別爲熱更新使用和打包出現錯誤不會退出dev開發環境
比prod生產環境
配置的入口文件中多了兩行,是爲了開發環境中熱更新
配置的entry: { app: [ + 'webpack-hot-middleware/client?reload=true', + 'webpack/hot/only-dev-server', './src/main.js' ], 'babel-polyfill': 'babel-polyfill', vendors: ['react', 'react-dom', 'react-router-dom'] }
webpack
有個webpack-dev-server
,至關於webpack本身加的服務器,能夠本身去研究下,我開始沒用起來,仍是選擇了expressbuild
文件夾下新建一個dev.server.js
文件,而後給開發環境配置服務器,引用webpack-dev-middleware
和webpack-hot-middleware
配置自動打包和自動刷新頁面,具體的在代碼中也有相應註釋,更多參數配置能夠看官方文檔webpack
打包的是webpack.config.js
文件,如今咱們改寫了,就在package.json
中配置一個"scripts": { "test": "echo \"Error: no test specified\" && exit 1", + "build": "webpack --config webpack.prod.conf.js", + "dev": "node build/dev-server.js" }
npm run build //使用生產環境打包 npm run dev //啓動開發環境調試,當代碼發生更改會自動刷新頁面
有問題歡迎交流 demo地址