以前我有寫過博文介紹過dva.js及其用法,dva.js當然是個很是優秀的框架,可是若是用dev-cli來建立的話就會默認綁定使用roadhog而不是webpack。鑑於roadhog的文檔要明顯少於webpack,若是能使用webpack+dvajs的話使用起來應該會輕鬆些。本文大體來介紹下dvajs+webpack3.11.0 開發環境的搭建過程。css
首先先貼上package.json:html
{
"name": "dva-webpack-boilerplate",
"version": "1.0.0",
"description": "Boilerplate for Dvajs + React + Webpack project with JSX, ES6 and decorator compilation",
"scripts": {
"start": "webpack-dev-server --hot --open --port 11111 --config config/webpack.dev.config.js",
"build": "webpack --config config/webpack.prod.config.js"
},
"keywords": [
"react",
"reactjs",
"boilerplate"
],
"license": "MIT",
"devDependencies": {
"babel-core": "^6.9.1",
"babel-loader": "^7.1.2",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-1": "^6.5.0",
"copy-webpack-plugin": "^4.5.1",
"css-loader": "^0.28.11",
"extract-text-webpack-plugin": "^3.0.2",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.9.0",
"sass-loader": "^7.0.1",
"style-loader": "^0.21.0",
"uglifyjs-webpack-plugin": "^1.2.5",
"url-loader": "^1.0.1",
"webpack": "^3.11.0",
"webpack-dev-server": "^2.11.2",
"webpack-merge": "^4.1.2"
},
"dependencies": {
"dva": "^2.2.3",
"react": "^16.3.2",
"react-dom": "^16.3.2"
}
}
而後是.babelrcnode
{ "presets": [ "react", "es2015", "stage-1" ], "plugins": ["transform-decorators-legacy" ,"transform-runtime"] }
項目根目錄存放好這兩個文件,再運行cnpm i ,環境搭建就算是完成了一半。react
接下來是webpack的配置。爲了區分開發環境與生產環境,將webpack.config.js分紅了三個文件:webpack.base.config.js、webpack.dev.config.js、webpack.prod.config.jswebpack
webpack.base.config.jsgit
var path = require('path'); var webpack = require('webpack'); module.exports = { devtool: '#source-map', entry: [ './src/index' ], output: { path: path.join(__dirname, 'dist'), filename: 'build.js', publicPath: '/dist/' }, plugins: [ new webpack.HotModuleReplacementPlugin(), ], resolve: { extensions: ['.js', '.jsx'] }, module: { rules: [ { test: /\.jsx?$/, use: ['babel-loader'], include: path.join(process.cwd(), 'src') }, { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, loader: 'url-loader', options: { limit: 10000, } }, { test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, loader: 'url-loader', options: { limit: 10000, } }, { test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, loader: 'url-loader', options: { limit: 10000, } } ] } };
webpack.dev.config.jsgithub
const baseWebpackConfig = require('./webpack.base.config'); const merge = require('webpack-merge'); module.exports = merge(baseWebpackConfig, { // mode:"development" module: { rules: [ { test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader'] }] } });
webpack.prod.config.jsweb
const path = require('path'); const webpack = require('webpack'); const baseWebpackConfig = require('./webpack.base.config'); const merge = require('webpack-merge'); const CopyWebpackPlugin = require('copy-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); const ExtractTextPlugin = require('extract-text-webpack-plugin'); module.exports = merge(baseWebpackConfig, { output: { path: path.join(process.cwd(), 'dist'), filename: 'build.js', }, // mode: "production", module:{ rules: [ { test: /\.scss$/, use: ExtractTextPlugin.extract({ fallback: 'style-loader', use: ['css-loader', 'sass-loader'] }) } ] }, plugins: [ new webpack.DefinePlugin({ 'process.env': { 'NODE_ENV': JSON.stringify('production') } }), new ExtractTextPlugin({ filename: 'style.css' }), new UglifyJsPlugin({ uglifyOptions: { compress: { warnings: false } }, parallel: true }), new HtmlWebpackPlugin({ filename: path.join(process.cwd(), 'dist/index.html'), template: 'index-build.html', inject: false, minify: { removeComments: true, collapseWhitespace: true, removeAttributeQuotes: true }, chunksSortMode: 'dependency' }), new webpack.HashedModuleIdsPlugin(), new CopyWebpackPlugin([ { from: path.resolve(__dirname, '../static'), to: 'static', ignore: ['.*'] } ]), ] });
最終目錄結構:npm
config存放三個webpack配置文件,dist存放生成出來的成品,static存放圖片等靜態資源。json
index.html用於開發環境,index-build.html用於生產環境,方便配置cdn。
到此環境搭建完畢。
本項目github: