vue-cli脚手架npm相关文件解读(2)webpack.prod.conf.js
系列文章传送门:
下面是webpack.prod.conf.js中相关代码和配置的说明,建议先查阅build/webpack.prod.conf.js
项目地址:https://github.com/SmileSmith(感觉不错的话帮忙打个星哈 ~)
/*
* Webpack 生产环境配置文件,用于生产环境执行Build
* 执行Build 主要是用Webpack执行这里的配置
* 建议先查阅webapck.base.conf.js ../config/index.js
*/
var path = require('path')
var utils = require('./utils') // 下面是utils工具配置文件,主要用来处理css类文件的loader
var webpack = require('webpack')
var config = require('../config')
var merge = require('webpack-merge') // 用merge的方式继承base.conf里面的配置
var baseWebpackConfig = require('./webpack.base.conf')
var CopyWebpackPlugin = require('copy-webpack-plugin') // copy-webpack-plugin使用来复制文件或者文件夹到指定的目录的
var HtmlWebpackPlugin = require('html-webpack-plugin') // html-webpack-plugin是生成html文件,可以设置模板
var ExtractTextPlugin = require('extract-text-webpack-plugin') // extract-text-webpack-plugin这个插件是用来将bundle中的css等文件生成单独的文件,比如我们看到的app.css
var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
//压缩css代码的,还能去掉extract-text-webpack-plugin插件抽离文件产生的重复代码,因为同一个css可能在多个模块中出现所以会导致重复代码,一般都是配合使用
// 如果当前环境变量NODE_ENV的值是testing,则导入 test.env.js配置文,设置env为"testing"
// 如果当前环境变量NODE_ENV的值不是testing,则设置env为"production"
var env = process.env.NODE_ENV === 'testing'
? require('../config/test.env')
: config.build.env
// 把当前的配置对象和base.conf基础的配置对象合并
var webpackConfig = merge(baseWebpackConfig, {
module: {
// 下面就是把utils配置好的处理各种css类型的配置拿过来,和dev设置一样,就是这里多了个extract: true,此项是自定义项,设置为true表示,生成独立的文件
rules: utils.styleLoaders({
sourceMap: config.build.productionSourceMap,
extract: true
})
},
// devtool开发工具,用来生成个sourcemap方便调试,只用于生产环境
devtool: config.build.productionSourceMap ? '#source-map' : false,
output: {
// 和base.conf中一致,输出文件的路径:config目录下的index.js,path.resolve(__dirname, '../dist')
path: config.build.assetsRoot,
// 有区别,输出文件加上的chunkhash
filename: utils.assetsPath('js/[name].[chunkhash].js'),
// 非入扣文件配置,异步加载的模块,输出文件加上的chunkhash
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
},
plugins: [
// http://vuejs.github.io/vue-loader/en/workflow/production.html
new webpack.DefinePlugin({
'process.env': env// line-21 下面是利用DefinePlugin插件,定义process.env环境变量为env
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false // 禁止压缩时候的警告信息
},
sourceMap: true // 压缩后生成map文件
}),
// extract css into its own file,已经很清楚了就是独立css文件,文件名和hash
new ExtractTextPlugin({
filename: utils.assetsPath('css/[name].[contenthash].css')
}),
// Compress extracted CSS. We are using this plugin so that possible
// duplicated CSS from different components can be deduped.
new OptimizeCSSPlugin({
cssProcessorOptions: {
safe: true
}
}),
// generate dist index.html with correct asset hash for caching.
// you can customize output by editing /index.html
// see https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: process.env.NODE_ENV === 'testing'
? 'index.html'
: config.build.index,
template: 'index.html', // 模板是index.html加不加无所谓
inject: true, // 将js文件注入到body标签的结尾
minify: { // 压缩html页面
removeComments: true, // 去掉注释
collapseWhitespace: true, // 去除无用空格
removeAttributeQuotes: true// 去除无用的双引号
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency' // 可以对页面中引用的chunk进行排序,保证页面的引用顺序
}),
// split vendor js into its own file
// 公共模块插件,便于浏览器缓存,提高程序的运行速度(哪些需要打包进公共模块需要取舍)
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor', // 公共模块的名称,对应打包出来的js是vendor.js
minChunks: function (module, count) {
// any required modules inside node_modules are extracted to vendor
// 存在资源,且以js结尾,且路径在node_node_modules下的都打包进来(这里可以根据项目的时机情况做调整)
return (
module.resource &&
/\.js$/.test(module.resource) &&
module.resource.indexOf(
path.join(__dirname, '../node_modules')
) === 0
)
}
}),
// extract webpack runtime and module manifest to its own file in order to
// prevent vendor hash from being updated whenever app bundle is updated
// 把webpack的runtime代码和module manifest代码提取到manifest.js文件中,防止修改了代码但是没有修改第三方库文件导致第三方库文件也打包的问题
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
chunks: ['vendor']
}),
// copy custom static assets
// 复制项目中的静态文件,忽略.开头的文件
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.build.assetsSubDirectory,
ignore: ['.*']
}
])
]
})
// Gzip压缩插件
if (config.build.productionGzip) { // 修改config里面的配置才能开启
var CompressionWebpackPlugin = require('compression-webpack-plugin')// Gzip插件
webpackConfig.plugins.push(
new CompressionWebpackPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp(
'\\.(' +
config.build.productionGzipExtensions.join('|') +
')$'
),
threshold: 10240,
minRatio: 0.8
})
)
}
// 模块化分析插件
// 文档好像没有提档说明如何使用,看config/index.js中的注释,npm run build --report 可以看到,或者修改config里面的配置
if (config.build.bundleAnalyzerReport) { // 模块分析,会在127.0.0.1:8080生成模块打包分析结果
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
webpackConfig.plugins.push(new BundleAnalyzerPlugin())
}
module.exports = webpackConfig // 导出所有配置
参考:http://www.cnblogs.com/ye-hcj/archive/2017/06.html
vue-cli脚手架npm相关文件解读(2)webpack.prod.conf.js的更多相关文章
- vue-cli脚手架npm相关文件解读(4)utils.js
系列文章传送门: 1.build/webpack.base.conf.js 2.build/webpack.prod.conf.js 3.build/webpack.dev.conf.js 4.bui ...
- vue-cli脚手架npm相关文件解读(8)check-versions.js
系列文章传送门: 1.build/webpack.base.conf.js 2.build/webpack.prod.conf.js 3.build/webpack.dev.conf.js 4.bui ...
- vue-cli脚手架npm相关文件解读(7)dev-server.js
系列文章传送门: 1.build/webpack.base.conf.js 2.build/webpack.prod.conf.js 3.build/webpack.dev.conf.js 4.bui ...
- vue-cli脚手架npm相关文件解读(6)build.js
系列文章传送门: 1.build/webpack.base.conf.js 2.build/webpack.prod.conf.js 3.build/webpack.dev.conf.js 4.bui ...
- vue-cli脚手架npm相关文件解读(9)config/index.js
系列文章传送门: 1.build/webpack.base.conf.js 2.build/webpack.prod.conf.js 3.build/webpack.dev.conf.js 4.bui ...
- vue-cli脚手架npm相关文件解读(5)vue-loader.conf.js
系列文章传送门: 1.build/webpack.base.conf.js 2.build/webpack.prod.conf.js 3.build/webpack.dev.conf.js 4.bui ...
- vue-cli脚手架npm相关文件解读(3)webpack.dev.conf.js
系列文章传送门: 1.build/webpack.base.conf.js 2.build/webpack.prod.conf.js 3.build/webpack.dev.conf.js 4.bui ...
- vue-cli脚手架npm相关文件解读(1)webpack.base.conf.js
系列文章传送门: 1.build/webpack.base.conf.js 2.build/webpack.prod.conf.js 3.build/webpack.dev.conf.js 4.bui ...
- vue-cli脚手架之webpack.prod.conf.js
webpack.prod.conf.js 生产环境配置文件: 'use strict'//js严格模式执行 const path = require('path')//这个模块是发布到NPM注册中心的 ...
随机推荐
- (cljs/run-at (JSVM. :all) "Metadata就这样哦")
前言 动态类型语言,少了静态类型语言必须声明变量类型的累赘,但也缺失了编译时类型检查和编译时优化的好处.cljs虽然作为动态类型语言,但其提供Metadata让我们在必要的时候可选择地补充类型提示, ...
- Qt中使用CEF(Windows下)
最近项目中要在Qt中使用CEF(Chromium Embedded Framework),在这里总结下其中的几个要点. 下载合适的CEF版本 关于CEF的简介我们这里就不做介绍了,下载CEF可以有2种 ...
- GitHub 入门教程
一.前言 编程进阶的道路是坎坷的,没有任何捷径.这个时期只能是积累.吸收.学习.坚持,做到量的积累,到质的飞跃 古语有云:'书山有路,勤为径'.'不积跬步,无以至千里' 编程是一个动手实践性的学科,多 ...
- 图像处理与matlab实例之图像平滑(一)
一.何为图像噪声?噪声是妨碍人的感觉器官所接受信源信息理解的因素,是不可预测只能用概率统计方法认识的随机误差. 举个例子: 从这个图中,我们可以观察到噪声的特点:1>位置随机 2>大小不规 ...
- getcomputedstyle()获取border像素差异问题
getComputedStyle()方法返回的是一个CSS样式声明对象--CSSStyleDeclaration对象(与style属性的类型相同),包含当前元素所有最终使用的CSS属性值: <! ...
- 由于用mpu6050模块,所以要用上i2c通信原理。
i2c通信原理 i2c总线只有两根双向信号线,一根是数据线SDA,一根是时钟线SCL. 每个接到i2c总线上的器件都有唯一的地址,主机与其他器件之间的数据传送可以是由主机发送给其他器件.主机为发送器, ...
- Ubuntu上安装PHP环境-mysql+apache+php-Linux操作系统
安装MYSQL 1. sudo apt-get install mysql-server 或者 apt-get isntall mysql-client 2. 安装过程中会提示设置密码,注意设 ...
- django 表单提交 post 、get
介绍 : django项目开发必须懂的知识点,下面使用的数据库是mysql , models.py 数据库表结构, # -*- coding: utf-8 -*-from __future__ im ...
- (转)Spring boot——logback.xml 配置详解(四)<filter>
文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 filter的使用 <filter>: Logback的过滤器基于三值逻辑( ...
- jar包的一生
经常会头疼于一个jar包是如何制作的,包括maven的打包方式,springboot的打jar包的原理,jar包稍稍有错误就会完全无法运行.在网上折腾了很久终于有些思路和步骤,在这里做个笔记 本文大纲 ...