refactor(build): 优化项目构建配置

- 移除不必要的注释,简化代码结构
-调整公共路径设置,提高资源加载效率
- 优化 SVG Sprite Loader 和 Vue preserveWhitespace 配置
- 改进 Source Map 和分包策略,提升开发和生产环境性能
- 添加时间戳到文件名中,确保静态资源缓存有效
This commit is contained in:
陈昱达
2025-07-17 12:26:47 +08:00
parent b7f84f4fba
commit 696de69e1e

View File

@@ -7,22 +7,12 @@ function resolve(dir) {
return path.join(__dirname, dir)
}
const name = defaultSettings.title || 'basic-pc' // 页面标题
//如果您的端口设置为80
//使用管理员权限执行命令行。
//例如Macsudo npm run
//您可以通过以下方法更改端口:
// port = 9527 npm run dev或npm run dev --port = 9527
const port = process.env.port || process.env.npm_config_port || 7006 //开发端口
console.log(process.env.VUE_APP_AUTH)
//所有配置项说明都可以在https://cli.vuejs.org/config/ 中找到
const port = process.env.port || process.env.npm_config_port || 7006 // 开发端口
module.exports = {
runtimeCompiler: true,
/*如果您打算在子路径下部署站点则需要设置publicPath
例如GitHub Pages。如果您打算将网站部署到https://foo.github.io/bar/
然后publicPath应该设置为“ / bar /”。
在大多数情况下,请使用'/'
详细信息https://cli.vuejs.org/config/#publicpath
*/
// 设置打包资源的基础路径
publicPath: '.',
outputDir: 'dist',
assetsDir: 'static',
@@ -38,8 +28,6 @@ module.exports = {
}
},
configureWebpack: {
//在webpack的名称字段中提供应用程序的标题以便
//可以在index.html中对其进行访问以注入正确的标题。
name: name,
resolve: {
alias: {
@@ -48,19 +36,19 @@ module.exports = {
},
plugins: [
new CompressionPlugin({
filename: '[path][base].gz', // 输出的.gz文件名
algorithm: 'gzip', // 使用 gzip 压缩
test: /\.(js|css|html|svg)$/, // 匹配需要压缩的文件类型
threshold: 10240, // 只有大小大于该值的资源会被压缩(单位:字节)
minRatio: 0.8 // 压缩率小于这个值的资源不压缩
filename: '[path][base].gz',
algorithm: 'gzip',
test: /\.(js|css|html|svg)$/,
threshold: 10240,
minRatio: 0.8
})
]
},
chainWebpack(config) {
config.plugins.delete('preload') // TODO: 需要测试
config.plugins.delete('prefetch') // TODO: 需要测试
config.plugins.delete('preload')
config.plugins.delete('prefetch')
//设置svg-sprite-loader
// SVG Sprite Loader 配置
config.module
.rule('svg')
.exclude.add(resolve('src/icons'))
@@ -75,9 +63,8 @@ module.exports = {
.options({
symbolId: 'icon-[name]'
})
.end()
//设置preserveWhitespace
// Vue preserveWhitespace 设置
config.module
.rule('vue')
.use('vue-loader')
@@ -86,20 +73,22 @@ module.exports = {
options.compilerOptions.preserveWhitespace = true
return options
})
.end()
// https://webpack.js.org/configuration/devtool/#development
// Source Map 设置(开发环境)
config.when(process.env.NODE_ENV === 'development', config => config.devtool('cheap-source-map'))
// 生产环境优化
config.when(process.env.NODE_ENV !== 'development', config => {
config
.plugin('ScriptExtHtmlWebpackPlugin')
.after('html')
.use('script-ext-html-webpack-plugin', [
{
//`runtime`必须与runtimeChunk名称相同。默认是“运行时”
inline: /runtime\..*\.js$/
}
])
.end()
// 分包策略
config.optimization.splitChunks({
chunks: 'all',
minSize: 3000,
@@ -110,17 +99,17 @@ module.exports = {
name: 'chunk-libs',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial' //仅打包最初依赖的第三方
chunks: 'initial'
},
elementUI: {
name: 'chunk-elementUI', //将elementUI拆分为一个包
priority: 20, //权重必须大于libs和app否则将打包到libs或app中
test: /[\\/]node_modules[\\/]_?element-ui(.*)/ //为了适应cnpm
name: 'chunk-elementUI',
priority: 20,
test: /[\\/]node_modules[\\/]_?element-ui(.*)/
},
commons: {
name: 'chunk-commons',
test: resolve('src/components'), //可以自定义规则
minChunks: 3, //最小公用数
test: resolve('src/components'),
minChunks: 3,
priority: 5,
reuseExistingChunk: true
}
@@ -128,5 +117,8 @@ module.exports = {
})
config.optimization.runtimeChunk('single')
})
// 添加时间戳到文件名中
config.output.filename(`js/[name].[hash:8].js`).chunkFilename(`js/[name].[hash:8].js`)
}
}