將webpack1升級爲webpack2的一些總結

/**
* 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的時候,總結:css

  • 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的配置html

/**
* 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' }

 

按需加載的配置node

npm install babel-plugin-import --save-devreact

"plugins": [ ["import", { "libraryName": "antd", "style": "css" }] ] // style: true的時候加載lesswebpack

相關文章
相關標籤/搜索