1 Star 0 Fork 0

liaoyang/vue-hr1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
vue.config.js 5.41 KB
一键复制 编辑 原始数据 按行查看 历史
liaoyang 提交于 2022-11-12 15:50 . 打包
// webpack的配置文件
'use strict'
// 遵守nodejs的Commonjs规范的导包方式
// - 导入 require()
// - 导出 module.exports || exports
const path = require('path') // path操作路径的模块
// 导入项目的配置文件settings.js文件中的配置
const defaultSettings = require('./src/settings.js')
// resolve:将传入的相对路径变成绝对路径
function resolve(dir) { // dir接收的是相对路径
// __dirname:获取当前文件所在目录的绝对路径
return path.join(__dirname, dir)
}
// 保存网页的标题
const name = defaultSettings.title || 'vue Admin Template' // page title
// 保存项目启动的端口号
const port = process.env.port || process.env.npm_config_port || 9528 // dev port
let externals = {}
const cdn = {
js: [
'https://lib.baomitu.com/vue/2.6.10/vue.min.js', // vuejs
'https://lib.baomitu.com/element-ui/2.13.2/index.js', // element-ui js
'https://lib.baomitu.com/xlsx/0.16.0/xlsx.min.js' // xlsx
],
css: [
'https://lib.baomitu.com/element-ui/2.13.2/theme-chalk/index.css' // element-ui css 样式表
]
}
const isProduction = process.env.NODE_ENV === 'production' // 判断是否是生产环境
if (isProduction) {
externals = {
// 包名: '通常是导入时候的包名'
'vue': 'Vue',
'element-ui': 'ELEMENT',
'xlsx': 'XLSX'
}
}
// 脚手架vue-cli相关的配置 :https://cli.vuejs.org/config/
module.exports = {
// publicPath:配置生成的资源index.html 在什么环境下运行,
// - 值默认是'/' 只能在服务器的环境下运行
// - 值设置成'./' 可以在任何环境下运行
publicPath: './',
outputDir: 'dist', // 打包后导出资源的目录名称
assetsDir: 'static', // 打包后导出资源的目录名称中存储静态文件的目录 js,img,css
lintOnSave: process.env.NODE_ENV === 'development', // 是否开启eslint检查
productionSourceMap: false, // 是否开启map映射
// devServer脚手架提供的服务器的相关的配置
devServer: {
port: port, // 设置端口号
open: true, // 浏览器是否自动打开
overlay: { // overlay: 设置代码的错误的等级是否在浏览器显示
warnings: false,
errors: true
},
// 配置代理服务器
proxy: {
'/api': {
// target: 'http://ihrm-java.itheima.net'
// target: 'http://ihrm.itheima.net'
target: 'http://192.168.104.26:3000'
}
}
// 加载mock服务
// before: require('./mock/mock-server.js')
},
// webpack相关的配置, 【对象配置】
configureWebpack: {
name: name,
externals: externals,
resolve: {
alias: {
'@': resolve('src')
}
}
},
// webpack相关的配置 【函数配置】
chainWebpack(config) {
// 注入cdn变量 (打包时会执行)
config.plugin('html').tap(args => {
args[0].cdn = cdn // 配置cdn给插件
return args
})
// 去掉console-log
config.optimization.minimizer('terser').tap((args) => {
args[0].terserOptions.compress.drop_console = true
return args
})
config.plugin('preload').tap(() => [
{
rel: 'preload',
// to ignore runtime.js
// https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
include: 'initial'
}
])
// when there are many pages, it will cause too many meaningless requests
config.plugins.delete('prefetch')
// set svg-sprite-loader
config.module
.rule('svg')
.exclude.add(resolve('src/icons'))
.end()
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end()
config
.when(process.env.NODE_ENV !== 'development',
config => {
config
.plugin('ScriptExtHtmlWebpackPlugin')
.after('html')
.use('script-ext-html-webpack-plugin', [{
// `runtime` must same as runtimeChunk name. default is `runtime`
inline: /runtime\..*\.js$/
}])
.end()
config
.optimization.splitChunks({
chunks: 'all',
cacheGroups: {
libs: {
name: 'chunk-libs',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial' // only package third parties that are initially dependent
},
elementUI: {
name: 'chunk-elementUI', // split elementUI into a single package
priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
},
commons: {
name: 'chunk-commons',
test: resolve('src/components'), // can customize your rules
minChunks: 3, // minimum common number
priority: 5,
reuseExistingChunk: true
}
}
})
// https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
config.optimization.runtimeChunk('single')
}
)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liaoyangdxg/hr-phase-84.git
git@gitee.com:liaoyangdxg/hr-phase-84.git
liaoyangdxg
hr-phase-84
vue-hr1
master

搜索帮助