然后配置如下:(省去了 rules 相關(guān)的配置)
一般配置 filename 來(lái)保證最終生成的 css 文件名
plugins: [ new ExtractTextPlugin({ filename: utils.assetsPath('css/[name].[contenthash].css') }) ]
我們可以預(yù)先用 vue inspect --plugin extract-css
看看最終生成的配置:
/* config.plugin('extract-css') */ new MiniCssExtractPlugin( { filename: 'css/[name].[contenthash:8].css', chunkFilename: 'css/[name].[contenthash:8].css' } )
在文件 @vue/cli-service/lib/config/css.js
中:
最開始需要獲取 vue.config.js
里面配置的 css.extract
:
const isProd = process.env.NODE_ENV === 'production' const { extract = isProd } = options.css || {}
設(shè)置一個(gè)變量 shouldExtract
const shadowMode = !!process.env.VUE_CLI_CSS_SHADOW_MODE const shouldExtract = extract !== false && !shadowMode
如果變量 shouldExtract 為 true,調(diào)用 plugin 方法來(lái)生成一個(gè)插件配置:
這里依賴的插件為 mini-css-extract-plugin
if (shouldExtract) { webpackConfig .plugin('extract-css') .use(require('mini-css-extract-plugin'), [extractOptions]) }
filename 內(nèi)部也有一個(gè)判斷過(guò)程,如果設(shè)置了 filenameHashing,它默認(rèn)是 true:
filenameHashing: true
類型為 boolean:
filenameHashing: joi.boolean()
const filename = getAssetPath( options, `css/[name]${options.filenameHashing ? '.[contenthash:8]' : ''}.css` )
處理 filename 之后,插件還有一個(gè)配置項(xiàng):chunkFilename
下面就是通過(guò) Object.assign
來(lái)生成 extractOptions
const extractOptions = Object.assign({ filename, chunkFilename: filename }, extract && typeof extract === 'object' ? extract : {})
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com