demo_base版本css
This option controls if and how source maps are generated. 开发建议使用eval模式,缺点是没法正确显示行号,想要正确显示行号,能够时候用source-map或者eval-source-map 生产环境: 建议使用cheap-module-source-map
string | [string] | object { <key>: string | [string] } | (function: () => string | [string] | object { <key>: string | [string] })
入口打包根场景不一样,入口配置也不一样。html
entry: './A/index.js' entry: [ './A/index.js', ],
entry: [ './A/index.js', './B/index.js' ], entry: { A: './A/index.js', B: './B/index.js' }
输出路径配置:react
output: { // path.resolve用来拼接文件多级目录 // __dirname 为当前文件所在全路径地址 path: path.resolve(__dirname,'dist'), // 输出文件名字 // filename: 'app.js', // 以key做为文件名输出 filename: '[name].js', // chunkhash 根据文件内容生成特色的hash,使用这个能够保证文件内容不变,那么文件名字就不会改变,能够用来做为热更新 chunkFilename: '[chunkhash].js' }
Configure how modules are resolved. For example, when calling import "lodash" in ES2015, the resolve options can change where webpack goes to look for "lodash" (see modules).webpack
resolve: { // 当你reuire时,不须要加上如下扩展名 extensions: ['.js', '.md', '.txt'], },
plugins: [ // Webpack 2之后内置 // new webpack.optimize.OccurrenceOrderPlugin(), // 碰到错误warning可是不中止编译 new webpack.NoEmitOnErrorsPlugin(), // 开发模式不须要压缩 // new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }), ],
这个是webpack 3.1.0新出来的配置方式,能够用来解决多个入口文件,打包成多个文件夹的问题。git
demo 将多个入口打成多个文件夹 github
module.exports = [{ output: { filename: './dist-amd.js', libraryTarget: 'amd' }, entry: './app.js', }, { output: { filename: './dist-commonjs.js', libraryTarget: 'commonjs' }, entry: './app.js', }]
添加postcss-loader,须要作以下配置npm
插件配置json
{ test: /\.less/, use: [ 'style-loader', 'css-loader', + 'postcss-loader', 'less-loader' ] }, { test: /\.css$/, - use: 'style-loader!css-loader', + use: 'style-loader!css-loader!postcss-loader', },
module.exports = { plugins: { 'postcss-import': {}, // 可以使用import语法 @import "cssrecipes-defaults"; 'postcss-cssnext': {}, //PostCSS-cssnext是一个PostCSS插件,能够帮助您使用最新的CSS语法。 它将CSS规范转换为更兼容的CSS,所以您不须要等待浏览器支持。 'cssnano': {} } }
demo css文件分离segmentfault
插件配置
+const ExtractTextPlugin = require('extract-text-webpack-plugin'); +new ExtractTextPlugin('style.css'), //名字配置 { test: /\.less/, _ use: [ _ 'style-loader', _ 'css-loader', _ 'less-loader' _ ] + use: ExtractTextPlugin.extract({ + fallback: 'style-loader', + use: ['css-loader', 'less-loader'] + }) }, { test: /\.css$/, - use: 'style-loader!css-loader', + use: ExtractTextPlugin.extract({ + fallback: 'style-loader', + use: ['css-loader'] + }) },
module.exports = { plugins: { 'postcss-import': {}, // 可以使用import语法 @import "cssrecipes-defaults"; 'postcss-cssnext': {}, //PostCSS-cssnext是一个PostCSS插件,能够帮助您使用最新的CSS语法。 它将CSS规范转换为更兼容的CSS,所以您不须要等待浏览器支持。 'cssnano': {} } }
公共文件抽取通常依靠 CommonChunkPlguin 和 Dllplugin这两个插件.
共同点:
不一样点:
CommonChunkPlguin
dllPlugin
CommonChunkPlugin 配置:
// 若是有其余CommonsChunkPlugin生成的文件,将会引入 // - If chunk has the name as specified in the chunkNames it is put in the list // - If no chunk with the name as given in chunkNames exists a new chunk is created and added to the list // 大概意思就是若是name在entry里面有,那就加入一个列表,若是entry里面没有, // 那么就建立一个新chunk列表,若是chunks里面相同模块代码出现次数超过minChunks,那就添加到这个新建立的list里面。 new webpack.optimize.CommonsChunkPlugin({ name: "common", chunks: ["a", "b"], //须要合并的文件 // minChunks:3 //最少在出现过多少次才将其打入common中 }), //若是 new webpack.optimize.CommonsChunkPlugin({ name: "vendor", minChunks: Infinity })
DllPlugin 配置:
添加文件 const webpackConfig = { name: "vendor", entry: ["react", "react-dom"], output: { path: buildPath, // 输出文件路径 filename: "vendor.js", library: "vendor_[hash]" }, plugins: [ new webpack.DllPlugin({ name: "vendor_[hash]", path: path.resolve(buildPath, "manifest.json") }) ] };
name: "app", + dependencies: ["vendor"], devtool: 'eval', + new webpack.DllReferencePlugin({ + manifest: path.resolve(buildPath, "manifest.json") + }),
文件分析能够插件能够帮助查看咱们生成的bundle.js和chunk的组成成分,能够根据这个进行代码优化。(开发环境使用)
+ const StatsWriterPlugin = require('webpack-stats-plugin').StatsWriterPlugin; + const Visualizer = require('webpack-visualizer-plugin'); + new StatsWriterPlugin({ + fields: null, + stats: { chunkModules: true } + }), + new Visualizer({ + filename: './statistics.html' // visualizer 文件名称,在output 设置的path文件夹能够找到 + })
这个减小文件体积是相对的,webpack打包的时候回删去无用的代码,而react-dom等一些文件中都有不少下面的代码形式,这样webpack 和 DefinePlugin插件配合能够减小部分文件体积
if (process.env.NODE_ENV !== 'production') {}
new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify('production') } }),
webpack 自己内置uglifyjs,若是你想控制uglifyjs的版本,可使用这个。
const UglifyJSPlugin = require('uglifyjs-webpack-plugin'); new uglifyJsPlugin({ compress: { warnings: false } })