'use strict' const path = require('path') const utils = require('./utils') const webpack = require('webpack') const config = require('../config') const merge = require('webpack-merge') const baseWebpackConfig = require('./webpack.base.conf') //資源拷貝的插件 const CopyWebpackPlugin = require('copy-webpack-plugin') const HtmlWebpackPlugin = require('html-webpack-plugin') //把css打包成css文件已link的方式引入的包 const ExtractTextPlugin = require('extract-text-webpack-plugin') //壓縮css的包 const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin') //壓縮js代碼的包 const UglifyJsPlugin = require('uglifyjs-webpack-plugin') const env = require('../config/prod.env') const webpackConfig = merge(baseWebpackConfig, { module: { //loader配置,可在介紹utils的文章中查看 rules: utils.styleLoaders({ sourceMap: config.build.productionSourceMap, extract: true, usePostCSS: true }) }, devtool: config.build.productionSourceMap ? config.build.devtool : false, output: { path: config.build.assetsRoot, filename: utils.assetsPath('js/[name].[chunkhash].js'), //chunkFilename用於命名那些異步加載的模塊,比如通過require.ensure() chunkFilename: utils.assetsPath('js/[id].[chunkhash].js') }, plugins: [ // http://vuejs.github.io/vue-loader/en/workflow/production.html //設置全局變量 new webpack.DefinePlugin({ 'process.env': env }), //壓縮js代碼 new UglifyJsPlugin({ uglifyOptions: { compress: { warnings: false } }, sourceMap: config.build.productionSourceMap, parallel: true }), // 從所有額外的 chunk(additional chunk) 提取(默認情況下,它僅從初Zchunk(initial chunk) 中提取)當使用 CommonsChunkPlugin 並且在公共 chunk 中有提取的 chunk(來自ExtractTextPlugin.extract)時,allChunks **必須設置為 true new ExtractTextPlugin({ filename: utils.assetsPath('css/[name].[contenthash].css'), allChunks: true, }), // 壓縮css new OptimizeCSSPlugin({ cssProcessorOptions: config.build.productionSourceMap ? { safe: true, map: { inline: false } } : { safe: true } }), // 在dist目錄生成html文件 new HtmlWebpackPlugin({ filename: config.build.index, template: 'index.html', inject: true, minify: { removeComments: true, collapseWhitespace: true, removeAttributeQuotes: true }, chunksSortMode: 'dependency' }), // 該插件會根據模塊的相對路徑生成一個四位數的hash作為模塊id, 建議用於生產環境 new webpack.HashedModuleIdsPlugin(), // 去 webpack 打包時的一個取舍是將 bundle 中各個模塊單獨打包成閉包。這些打包函數使你的 JavaScript 在瀏覽器中處理的更慢。相比之下,一些工具像 Closure Compiler 和 RollupJS 可以提升(hoist)或者預編譯所有模塊到一個閉包中,提升你的代碼在瀏覽器中的執行速度。 new webpack.optimize.ModuleConcatenationPlugin(), // 將第三方的包分離出來 new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', minChunks (module) { // any required modules inside node_modules are extracted to vendor return ( module.resource && /\.js$/.test(module.resource) && module.resource.indexOf( path.join(__dirname, '../node_modules') ) === 0 ) } }), // 為了避免每次更改項目代碼時導致venderchunk的chunkHash改變,我們還會單獨生成一個manifestchunk new webpack.optimize.CommonsChunkPlugin({ name: 'manifest', minChunks: Infinity }), //我們主要邏輯的js文件 new webpack.optimize.CommonsChunkPlugin({ name: 'app', async: 'vendor-async', children: true, minChunks: 3 }), // 拷貝資源 new CopyWebpackPlugin([ { from: path.resolve(__dirname, '../static'), to: config.build.assetsSubDirectory, ignore: ['.*'] } ]) ] }) module.exports = webpackConfig