Ai
1 Star 12 Fork 3

BaiHuaYang/vue-vant-template

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
vue.config.js 5.45 KB
一键复制 编辑 原始数据 按行查看 历史
BaiHuaYang 提交于 2022-03-16 11:44 +08:00 . fix: $cdn default setting
'use strict'
const path = require('path')
const defaultSettings = require('./src/settings')
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const CompressionPlugin = require('compression-webpack-plugin')
const resolve = (dir) => path.join(__dirname, dir)
// page title
const name = defaultSettings.title || 'vue mobile template'
// 生产环境,测试和正式
const IS_PROD = process.env.NODE_ENV === 'production'
// externals
const externals = {}
// CDN外链,会插入到index.html中
const cdn = {
// 开发环境
dev: {
css: [],
js: ['//lib.baomitu.com/vConsole/latest/vconsole.min.js']
},
// 生产环境
build: {
css: [],
js: []
}
}
module.exports = {
publicPath: IS_PROD ? process.env.VUE_APP_PUBLIC_PATH : '/', // 署应用包时的基本 URL。 vue-router hash 模式使用
// publicPath: '/app/', //署应用包时的基本 URL。 vue-router history模式使用
outputDir: 'dist', // 生产环境构建文件的目录
assetsDir: 'static', // outputDir的静态资源(js、css、img、fonts)目录
lintOnSave: !IS_PROD,
productionSourceMap: false, // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
devServer: {
port: 9000, // 端口
open: false, // 启动后打开浏览器
overlay: {
// 当出现编译器错误或警告时,在浏览器中显示全屏覆盖层
warnings: false,
errors: true
}
// proxy: {
// //配置跨域
// '/api': {
// target: "https://test.xxx.com",
// // ws:true,
// changOrigin:true,
// pathRewrite:{
// '^/api':'/'
// }
// }
// }
},
css: {
extract: IS_PROD, // 是否将组件中的 CSS 提取至一个独立的 CSS 文件中 (而不是动态注入到 JavaScript 中的 inline 代码)。
sourceMap: false,
loaderOptions: {
less: {
// 向全局less样式传入共享的全局变量, $src可以配置图片cdn前缀
// 详情: https://cli.vuejs.org/guide/css.html#passing-options-to-pre-processor-loaders
prependData: `
@import "styles/variables.less";
@cdn: "${defaultSettings.$cdn}";
`
}
}
},
configureWebpack: (config) => {
config.name = name
config.plugins.push(
new CompressionPlugin({
filename: '[path][base].gz', // 目标资源名称
algorithm: 'gzip',
test: /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i, // 处理所有匹配此 {RegExp} 的资源
threshold: 10240, // 只处理比这个值大的资源。按字节计算
minRatio: 0.8 // 只有压缩率比这个值小的资源才会被处理
})
)
// 为生产环境修改配置...
if (IS_PROD) {
// externals
config.externals = externals
}
},
chainWebpack: (config) => {
// 别名 alias
config.resolve.alias.set('@', resolve('src')).set('assets', resolve('src/assets')).set('api', resolve('src/api')).set('views', resolve('src/views')).set('components', resolve('src/components'))
/**
* 添加CDN参数到htmlWebpackPlugin配置中
*/
config.plugin('html').tap((args) => {
args[0].title = name
if (!IS_PROD || process.env.VUE_APP_NODE_ENV !== 'production') {
args[0].cdn = cdn.dev
} else {
args[0].cdn = cdn.build
}
return args
})
/**
* 设置保留空格
*/
config.module
.rule('vue')
.use('vue-loader')
.loader('vue-loader')
.tap((options) => {
options.compilerOptions.preserveWhitespace = true
return options
})
.end()
/**
* 打包分析
*/
// if (IS_PROD) {
// config.plugin('webpack-report').use(BundleAnalyzerPlugin, [
// {
// analyzerMode: 'static'
// }
// ])
// }
config
// https://webpack.js.org/configuration/devtool/#development
.when(!IS_PROD, (config) => config.devtool('cheap-source-map'))
config.when(IS_PROD, (config) => {
config
.plugin('ScriptExtHtmlWebpackPlugin')
.after('html')
.use('script-ext-html-webpack-plugin', [
{
// 将 runtime 作为内联引入不单独存在
inline: /runtime\..*\.js$/
}
])
.end()
config.optimization.splitChunks({
chunks: 'all',
cacheGroups: {
// cacheGroups 下可以可以配置多个组,每个组根据test设置条件,符合test条件的模块
commons: {
name: 'chunk-commons',
test: resolve('src/components'),
minChunks: 3, // 被至少用三次以上打包分离
priority: 5, // 优先级
reuseExistingChunk: true // 表示是否使用已有的 chunk,如果为 true 则表示如果当前的 chunk 包含的模块已经被抽取出去了,那么将不会重新生成新的。
},
node_vendors: {
name: 'chunk-libs',
chunks: 'initial', // 只打包初始时依赖的第三方
test: /[\\/]node_modules[\\/]/,
priority: 10
},
vantUI: {
name: 'chunk-vantUI', // 单独将 vantUI 拆包
priority: 20, // 数字大权重到,满足多个 cacheGroups 的条件时候分到权重高的
test: /[\\/]node_modules[\\/]_?vant(.*)/
}
}
})
config.optimization.runtimeChunk('single')
})
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/uzhan/vue-vant-template.git
git@gitee.com:uzhan/vue-vant-template.git
uzhan
vue-vant-template
vue-vant-template
main

搜索帮助