這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助

前段時間,對部門的個別專案進行
Vue3.0+ts框架的遷移,剛開始研究的時候也是踩坑特別多,尤其我們的專案還有些特殊的webpack配置,所以,研究vue.config.js的配置的時候也是查閱了各種資料檔案,最終,完成了專案webpack的特殊配置,
今天分享一下,我們專案當中的一些webpack配置,希望能給大家有所啟發;只要配置多了,你就會發現其實所有的配置的都是相似的,尤其像插件的配置,都是十分相似的,
我們現在開始進入今天的主題啦~~
1 介紹
之前,我有提到過,當然大家肯定也都知道,Vue3.0不在有webpack.config.js的配置;但是不可避免,在專案開發中我們肯定會存在一些特殊的需求需要調整webpack, 這個時候,在Vue3.0的專案當中,我們就需要在根目錄創建vue.config.js去完成webpack的一些特殊配置,默認它會被 @vue/cli-service 自動加載,
此刻,你需要創建vue.config.js檔案,
查看默認的webpack配置
Vue CLI 官方檔案:
vue-cli-service暴露了inspect命令用于審查決議好的webpack配置,那個全域的 vue 可執行程式同樣提供了inspect命令,這個命令只是簡單的把vue-cli-service``inspect代理到了你的專案中,
被抽象化的webpack,我們要想去理解它默認的一些配置的話是比較困難的,所以我們可以通過指令去查看,
該指令會將webpack的配置輸出到output.js檔案,這樣方便去查看,
vue inspect > output.js
vue.config.js檔案
這個檔案匯出了一個包含了選項的物件:
module.exports = {
// 選項...
}
接下來,詳細介紹一些選項及配置:
2 基本配置
module.exports = {
productionSourceMap: false,
publicPath: './',
outputDir: 'dist',
assetsDir: 'assets',
devServer: {
port: 8090,
host: '0.0.0.0',
https: false,
open: true
},
// 其他配置
...
-
productionSourceMap:生產環境是否要生成
sourceMap -
publicPath:部署應用包時的基本 URL,用法和
webpack本身的output.publicPath一致- 可以通過三元運算去配置
dev和prod環境,publicPath: process.env.NODE_ENV === 'production' ? '/prod/' : './'
- 可以通過三元運算去配置
-
outputDir:
build時輸出的檔案目錄 -
assetsDir: 放置靜態檔案夾目錄
-
devServer: dev環境下,
webpack-dev-server相關配置- port: 開發運行時的埠
- host: 開發運行時域名,設定成
'0.0.0.0',在同一個局域網下,如果你的專案在運行,同時可以通過你的http://ip:port/...訪問你的專案 - https: 是否啟用
https - open:
npm run serve時是否直接打開瀏覽器
3 插件及規則的配置
在vue.config.js如果要新增/修改 webpack 的 plugins 或者 rules , 有2種方式,
configureWebpack 方式
configureWebpack 是相對比較簡單的一種方式
- 它可以是一個
物件:和webpack本身配置方式是一致,該物件將會被webpack-merge合并入最終的webpack配置 - 它也可以是一個
函式:直接在函式內部進行修改配置
configureWebpack: {
rules:[],
plugins: []
}
configureWebpack: (config) => {
// 例如,通過判斷運行環境,設定mode
config.mode = 'production'
}
chainWebpack 方式
chainWebpack 鏈式操作 (高級),接下來所有的配置我都會在該選項中進行配置
4 規則rules的配置
關于rules 的配置,我會分別從新增/修改進行介紹,
4.1 rules的新增
在webpack中 rules 是 module 的配置項,而所有的配置的都是掛載到 config 下的,所以新增一個rule方式:
config.module
.rule(name)
.use(name)
.loader(loader)
.options(options)
案例:style-resources-loader 來添加less全域變數
案例:svg-sprite-loader 將svg圖片以雪碧圖的方式在專案中加載
module.exports = {
chainWebpack: (config) => {
// 通過 style-resources-loader 來添加less全域變數
const types = ['vue-modules', 'vue', 'normal-modules', 'normal'];
types.forEach(type => {
let rule = config.module.rule('less').oneOf(type)
rule.use('style-resource')
.loader('style-resources-loader')
.options({
patterns: [path.resolve(__dirname, './lessVariates.less')]
});
});
// `svg-sprite-loader`: 將svg圖片以雪碧圖的方式在專案中加載
config.module
.rule('svg')
.test(/.svg$/) // 匹配svg檔案
.include.add(resolve('src/svg')) // 主要匹配src/svg
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader') // 使用的loader,主要要npm該插件
.options({symbolId: 'svg-[name]'}) // 引數配置
}
}
4.2 rules的修改
針對已經存在的 rule , 如果需要修改它的引數, 可以使用 tap 方法:
config.module
.rule(name)
.use(name)
.tap(options => newOptions)
案例:修改url-loader的引數
module.exports = {
chainWebpack: (config) => {
// `url-loader`是webpack默認已經配置的,現在我們來修改它的引數
config.module.rule('images')
.use('url-loader')
.tap(options => ({
name: './assets/images/[name].[ext]',
quality: 85,
limit: 0,
esModule: false,
}))
}
}
5 插件plugins 的配置
關于 plugins 的配置,我會分別從新增/修改/洗掉進行介紹,
5.1 plugins的新增
- 注意:這里WebpackPlugin不需要通過
new WebpackPlugin()使用,
config .plugin(name) .use(WebpackPlugin, args)
案例:新增hot-hash-webpack-plugin
const HotHashWebpackPlugin = require('hot-hash-webpack-plugin');
module.exports = {
chainWebpack: (config) => {
// 新增一個`hot-hash-webpack-plugin`
// 注意:這里use的時候不需要使用`new HotHashWebpackPlugin()`
config.plugin('hotHash')
.use(HotHashWebpackPlugin, [{ version: '1.0.0' }]);
}
}
5.2 plugins的修改
同理, plugin 引數的修改也是通過 tap 去修改,
config
.plugin(name)
.tap(args => newArgs)
案例:修改打包后css抽離后的filename及抽離所屬目錄
案例:洗掉console和debugger
const HotHashWebpackPlugin = require('hot-hash-webpack-plugin');
module.exports = {
chainWebpack: (config) => {
// 修改打包時css抽離后的filename及抽離所屬目錄
config.plugin('extract-css')
.tap(args => [{
filename: 'css/[name].[contenthash:8].css',
chunkFilename: 'css/[name].[contenthash:8].css'
}]);
// 正式環境下,洗掉console和debugger
config.optimization
.minimize(true)
.minimizer('terser')
.tap(args => {
let { terserOptions } = args[0];
terserOptions.compress.drop_console = true;
terserOptions.compress.drop_debugger = true;
return args
});
}
}
5.3 plugins的洗掉
對于一些webpack默認的 plugin ,如果不需要可以進行洗掉
config.plugins.delete(name)
案例:洗掉 vue-cli3.X 模塊的自動分割抽離
module.exports = {
chainWebpack: (config) => {
// vue-cli3.X 會自動進行模塊分割抽離,如果不需要進行分割,可以手動洗掉
config.optimization.delete('splitChunks');
}
}
6 一些常見的配置
6.1 修改enter檔案
webpack 默認的 entry 入口是 scr/main.ts
config.entryPoints.clear(); // 清空默認入口
config.entry('test').add(getPath('./test/main.ts')); // 重新設定
6.2 DefinePlugin
定義全域全域變數,DefinePlugin 是 webpack 已經默認配置的,我們可以對引數進行修改
config.plugin('define').tap(args => [{
...args,
"window.isDefine": JSON.stringify(true),
}]);
6.3 自定義filename 及 chunkFilename
自定義打包后js檔案的路徑及檔案名字
config.output.filename('./js/[name].[chunkhash:8].js');
config.output.chunkFilename('./js/[name].[chunkhash:8].js');
6.4 修改html-webpack-plugin引數
html-webpack-plugin 是 webpack 已經默認配置的,默認的源模版檔案是 public/index.html ;我們可以對其引數進行修改
config.plugin('html')
.tap(options => [{
template: '../../index.html' // 修改源模版檔案
title: 'test',
}]);
6.5 設定別名alias
webpack默認是將src的別名設定為@, 此外,我們可以進行添加
config.resolve.alias
.set('@', resolve('src'))
.set('api', resolve('src/apis'))
.set('common', resolve('src/common'))
7 附上一份我的vue.config.js的配置
const path = require('path');
const HotHashWebpackPlugin = require('hot-hash-webpack-plugin');
const WebpackBar = require('webpackbar');
const resolve = (dir) => path.join(__dirname, '.', dir);
module.exports = {
productionSourceMap: false,
publicPath: './',
outputDir: 'dist',
assetsDir: 'assets',
devServer: {
port: 9999,
host: '0.0.0.0',
https: false,
open: true
},
chainWebpack: (config) => {
const types = ['vue-modules', 'vue', 'normal-modules', 'normal'];
types.forEach(type => {
let rule = config.module.rule('less').oneOf(type)
rule.use('style-resource')
.loader('style-resources-loader')
.options({
patterns: [path.resolve(__dirname, './lessVariates.less')]
});
});
config.resolve.alias
.set('@', resolve('src'))
.set('api', resolve('src/apis'))
.set('common', resolve('src/common'))
config.module.rule('images').use('url-loader')
.tap(options => ({
name: './assets/images/[name].[ext]',
quality: 85,
limit: 0,
esModule: false,
}));
config.module.rule('svg')
.test(/.svg$/)
.include.add(resolve('src/svg'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader');
config.plugin('define').tap(args => [{
...args,
"window.isDefine": JSON.stringify(true)
}]);
// 生產環境配置
if (process.env.NODE_ENV === 'production') {
config.output.filename('./js/[name].[chunkhash:8].js');
config.output.chunkFilename('./js/[name].[chunkhash:8].js');
config.plugin('extract-css').tap(args => [{
filename: 'css/[name].[contenthash:8].css',
chunkFilename: 'css/[name].[contenthash:8].css'
}]);
config.plugin('hotHash').use(HotHashWebpackPlugin, [{ version : '1.0.0'}]);
config.plugin('webpackBar').use(WebpackBar);
config.optimization.minimize(true)
.minimizer('terser')
.tap(args => {
let { terserOptions } = args[0];
terserOptions.compress.drop_console = true;
terserOptions.compress.drop_debugger = true;
return args
});
config.optimization.splitChunks({
cacheGroups: {
common: {
name: 'common',
chunks: 'all',
minSize: 1,
minChunks: 2,
priority: 1
},
vendor: {
name: 'chunk-libs',
chunks: 'all',
test: /[\/]node_modules[\/]/,
priority: 10
}
}
});
}
}
}
本文轉載于:
https://juejin.cn/post/7130784786343428110
如果對您有所幫助,歡迎您點個關注,我會定時更新技術檔案,大家一起討論學習,一起進步,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/540761.html
標籤:其他
