详解Vue CLI3配置解析之css.extract
大家还记得我们在老版本中,对于线上环境配置中会把所有的css多打成一个文件:
核心是使用了插件extract-text-webpack-plugin,方式如下:
第一步都是加载插件
constExtractTextPlugin=require('extract-text-webpack-plugin')
这个插件的描述如下:
Extracttextfromabundle,orbundles,intoaseparatefile.
然后配置如下:(省去了rules相关的配置)
一般配置filename来保证最终生成的css文件名
plugins:[
newExtractTextPlugin({
filename:utils.assetsPath('css/[name].[contenthash].css')
})
]
我们可以预先用vueinspect--pluginextract-css看看最终生成的配置:
/*config.plugin('extract-css')*/
newMiniCssExtractPlugin(
{
filename:'css/[name].[contenthash:8].css',
chunkFilename:'css/[name].[contenthash:8].css'
}
)
在文件@vue/cli-service/lib/config/css.js中:
最开始需要获取vue.config.js里面配置的css.extract:
constisProd=process.env.NODE_ENV==='production'
const{
extract=isProd
}=options.css||{}
设置一个变量shouldExtract
constshadowMode=!!process.env.VUE_CLI_CSS_SHADOW_MODE constshouldExtract=extract!==false&&!shadowMode
如果变量shouldExtract为true,调用plugin方法来生成一个插件配置:
这里依赖的插件为mini-css-extract-plugin
if(shouldExtract){
webpackConfig
.plugin('extract-css')
.use(require('mini-css-extract-plugin'),[extractOptions])
}
filename内部也有一个判断过程,如果设置了filenameHashing,它默认是true:
filenameHashing:true
类型为boolean:
filenameHashing:joi.boolean()
constfilename=getAssetPath(
options,
`css/[name]${options.filenameHashing?'.[contenthash:8]':''}.css`
)
处理filename之后,插件还有一个配置项:chunkFilename
下面就是通过Object.assign来生成extractOptions
constextractOptions=Object.assign({
filename,
chunkFilename:filename
},extract&&typeofextract==='object'?extract:{})
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语