/**
* webpack2配置
*/
var webpack = require('webpack'); var path = require('path'); var HtmlWebpackPlugin = require('html-webpack-plugin'); var OpenBrowserPlugin = require('open-browser-webpack-plugin'); module.exports = { devServer: { historyApiFallback: true, contentBase: "./", quiet: false, //控制台中不輸出打包的信息 noInfo: false, hot: true, //開啟熱點 inline: true, //開啟頁面自動刷新 lazy: false, //不啟動懶加載 //progress: true, //顯示打包的進度,webpack2中沒有該配置項 watchOptions: { aggregateTimeout: 300 }, host: 'localhost', port: 8088, //設置端口號 //代理其實很簡單的,只要配置這個參數就可以了 proxy: { } }, entry: [ // 'webpack/hot/dev-server', // 'webpack-dev-server/client?http://localhost:8088',webpack2注釋 path.resolve(__dirname, 'src/index.js') ], output: { path: path.join(__dirname, "dist/"), filename: 'bundle.js', publicPath: '/dist/' }, module: { rules: [ { test: /\.js[x]?$/, exclude: /node_moudle/, include: path.join(__dirname, './src'), loader: 'babel-loader', options: { presets: [ "es2015", "react", "stage-2" ], "plugins": [ ["import", { "libraryName": "antd", "style": "css" }] ] } }, { test: /\.css$/, use: ['style-loader','css-loader'] }, { test: /\.(png|jpg)$/, loader: 'url-loader', options: { limit: 8129 } }, { test: /\.scss$/, use: ['style-loader','css-loader','sass-loader'] }, ] }, plugins: [ new HtmlWebpackPlugin({ title: 'webpack-demos', filename: 'dev.html', template: 'index.html', inject: 'body', minify: false, hash: false, cache: false, showErrors: false }), new OpenBrowserPlugin({ url: 'http://localhost:8088' }), new webpack.DefinePlugin({ }), new webpack.HotModuleReplacementPlugin() ], resolve: { extensions: [".js", ".json"], modules: ['node_modules'] }, devtool: 'source-map' }
將webpack1升級為webpack2的時候,總結:
- webpack和webpack-dev-server的版本需要更新到@2.x版本
- 2的devServer中配置參數有變動,例如progress被取消
- entry中不需要
'webpack/hot/dev-server'和'webpack-dev-server/client?http://localhost:8088'
- module.loaders升級為module.rules(雖然前者依舊被保留可用,但是此處遵循2的新特性規范)
- 原query字段被取消,現使用options,並且使用options的地方需要配合使用loader,沒有options的地方使用use
- 原先的加載器不強求些“-loader”,現在需要強制加上
- 原先的加載器可以通過“!”級連,現在不可以
- 原先加載器后可以使用“?***”,現在取消,統一使用options
- 原先的resolve.extensions[0]可以為空,現不可以
- resolve.modulesDirectories字段改為resolve.modules字段
貼上相同需求下的webpack1的配置
/**
* webpack1配置
*/
var webpack = require('webpack'); var path = require('path'); var HtmlWebpackPlugin = require('html-webpack-plugin'); var OpenBrowserPlugin = require('open-browser-webpack-plugin'); module.exports = { devServer: { historyApiFallback: true, contentBase: "./", quiet: false, //控制台中不輸出打包的信息 noInfo: false, hot: true, //開啟熱點 inline: true, //開啟頁面自動刷新 lazy: false, //不啟動懶加載 progress: true, //顯示打包的進度 watchOptions: { aggregateTimeout: 300 }, host: 'localhost', port: 8088, //設置端口號 //代理其實很簡單的,只要配置這個參數就可以了 proxy: { } }, entry: [ 'webpack/hot/dev-server', 'webpack-dev-server/client?http://localhost:8088', path.resolve(__dirname, 'src/index.js') ], output: { path: path.join(__dirname, "dist/"), filename: 'bundle.js', publicPath: '/dist/' }, module: { loaders: [ { test: /\.js[x]?$/, exclude: /node_moudle/, include: path.join(__dirname, './src'), loader: 'babel-loader', query: { presets: [ 'es2015', 'react', 'stage-2' ] } }, { test: /\.css$/, loader: 'style-loader!css-loader' }, { test: /\.(png|jpg)$/, loader: 'url-loader', query: { limit: 8129 } }, { test: /\.scss$/, loader: 'style-loader!css-loader!sass-loader' }, ] }, plugins: [ new HtmlWebpackPlugin({ title: 'webpack-demos', filename: 'dev.html', template: 'index.html', inject: 'body', minify: false, hash: false, cache: false, showErrors: false }), new OpenBrowserPlugin({ url: 'http://localhost:8088' }), new webpack.DefinePlugin({ }), new webpack.HotModuleReplacementPlugin() ], resolve: { extensions: ['', '.web.js', '.js', '.jsx', '.json'], root: [ path.resolve('./src') ], modulesDirectories: ['node_modules'] }, devtool: 'source-map' }
按需加載的配置
npm install babel-plugin-import --save-dev
"plugins": [ ["import", { "libraryName": "antd", "style": "css" }] ] // style: true的時候加載less