2018-11-18 09:45:04 +08:00
|
|
|
|
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
|
2019-05-06 10:49:25 +08:00
|
|
|
|
const VueFilenameInjector = require('./tools/vue-filename-injector')
|
|
|
|
|
|
|
2018-07-16 22:22:55 +08:00
|
|
|
|
// 拼接路径
|
2018-10-27 18:48:22 +08:00
|
|
|
|
const resolve = dir => require('path').join(__dirname, dir)
|
2018-07-16 22:22:55 +08:00
|
|
|
|
|
2018-12-14 11:16:10 +08:00
|
|
|
|
// 增加环境变量
|
|
|
|
|
|
process.env.VUE_APP_VERSION = require('./package.json').version
|
|
|
|
|
|
process.env.VUE_APP_BUILD_TIME = require('dayjs')().format('YYYY-M-D HH:mm:ss')
|
|
|
|
|
|
|
2018-07-20 18:24:47 +08:00
|
|
|
|
// 基础路径 注意发布之前要先修改这里
|
2019-01-17 11:30:52 +08:00
|
|
|
|
let publicPath = '/'
|
2018-07-20 18:24:47 +08:00
|
|
|
|
|
2018-07-16 22:22:55 +08:00
|
|
|
|
module.exports = {
|
2019-05-21 23:51:29 +08:00
|
|
|
|
// 根据你的实际情况更改这里
|
|
|
|
|
|
publicPath,
|
2018-07-16 22:22:55 +08:00
|
|
|
|
lintOnSave: true,
|
|
|
|
|
|
devServer: {
|
2019-01-17 11:33:11 +08:00
|
|
|
|
publicPath // 和 publicPath 保持一致
|
2018-07-16 22:22:55 +08:00
|
|
|
|
},
|
2018-11-16 22:49:27 +08:00
|
|
|
|
css: {
|
|
|
|
|
|
loaderOptions: {
|
|
|
|
|
|
// 设置 scss 公用变量文件
|
|
|
|
|
|
sass: {
|
|
|
|
|
|
data: `@import '~@/assets/style/public.scss';`
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2018-07-19 17:00:45 +08:00
|
|
|
|
// 默认设置: https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config/base.js
|
2018-07-16 22:22:55 +08:00
|
|
|
|
chainWebpack: config => {
|
2018-11-18 09:45:04 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 删除懒加载模块的 prefetch preload,降低带宽压力
|
|
|
|
|
|
* https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
|
|
|
|
|
|
* https://cli.vuejs.org/zh/guide/html-and-static-assets.html#preload
|
|
|
|
|
|
* 而且预渲染时生成的 prefetch 标签是 modern 版本的,低版本浏览器是不需要的
|
|
|
|
|
|
*/
|
|
|
|
|
|
config.plugins
|
|
|
|
|
|
.delete('prefetch')
|
|
|
|
|
|
.delete('preload')
|
2018-09-12 08:10:16 +08:00
|
|
|
|
// 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
|
|
|
|
|
|
config.resolve
|
|
|
|
|
|
.symlinks(true)
|
2018-11-18 11:07:29 +08:00
|
|
|
|
config
|
|
|
|
|
|
// 开发环境
|
|
|
|
|
|
.when(process.env.NODE_ENV === 'development',
|
|
|
|
|
|
// sourcemap不包含列信息
|
|
|
|
|
|
config => config.devtool('cheap-source-map')
|
|
|
|
|
|
)
|
2019-04-29 22:38:08 +08:00
|
|
|
|
// TRAVIS 构建 vue-loader 添加 filename
|
2019-05-10 09:56:55 +08:00
|
|
|
|
.when(process.env.VUE_APP_SCOURCE_LINK === 'TRUE',
|
2019-05-06 10:49:25 +08:00
|
|
|
|
VueFilenameInjector(config, {
|
|
|
|
|
|
propName: process.env.VUE_APP_SOURCE_VIEWER_PROP_NAME
|
|
|
|
|
|
})
|
2019-04-29 22:38:08 +08:00
|
|
|
|
)
|
2018-11-18 11:07:29 +08:00
|
|
|
|
// 非开发环境
|
|
|
|
|
|
.when(process.env.NODE_ENV !== 'development', config => {
|
|
|
|
|
|
config.optimization
|
|
|
|
|
|
.minimizer([
|
|
|
|
|
|
new UglifyJsPlugin({
|
|
|
|
|
|
uglifyOptions: {
|
|
|
|
|
|
// 移除 console
|
|
|
|
|
|
// 其它优化选项 https://segmentfault.com/a/1190000010874406
|
|
|
|
|
|
compress: {
|
|
|
|
|
|
drop_console: true,
|
|
|
|
|
|
drop_debugger: true,
|
|
|
|
|
|
pure_funcs: ['console.log']
|
|
|
|
|
|
}
|
2018-11-18 09:45:04 +08:00
|
|
|
|
}
|
2018-11-18 11:07:29 +08:00
|
|
|
|
})
|
|
|
|
|
|
])
|
|
|
|
|
|
})
|
2018-07-16 22:22:55 +08:00
|
|
|
|
// markdown
|
|
|
|
|
|
config.module
|
|
|
|
|
|
.rule('md')
|
|
|
|
|
|
.test(/\.md$/)
|
|
|
|
|
|
.use('text-loader')
|
|
|
|
|
|
.loader('text-loader')
|
|
|
|
|
|
.end()
|
|
|
|
|
|
// svg
|
|
|
|
|
|
const svgRule = config.module.rule('svg')
|
|
|
|
|
|
svgRule.uses.clear()
|
|
|
|
|
|
svgRule
|
|
|
|
|
|
.include
|
2018-07-17 16:14:19 +08:00
|
|
|
|
.add(resolve('src/assets/svg-icons/icons'))
|
2018-07-16 22:22:55 +08:00
|
|
|
|
.end()
|
|
|
|
|
|
.use('svg-sprite-loader')
|
|
|
|
|
|
.loader('svg-sprite-loader')
|
|
|
|
|
|
.options({
|
|
|
|
|
|
symbolId: 'd2-[name]'
|
|
|
|
|
|
})
|
|
|
|
|
|
.end()
|
2018-07-25 09:47:43 +08:00
|
|
|
|
// image exclude
|
2018-07-16 22:22:55 +08:00
|
|
|
|
const imagesRule = config.module.rule('images')
|
|
|
|
|
|
imagesRule
|
|
|
|
|
|
.test(/\.(png|jpe?g|gif|webp|svg)(\?.*)?$/)
|
|
|
|
|
|
.exclude
|
2018-07-17 16:14:19 +08:00
|
|
|
|
.add(resolve('src/assets/svg-icons/icons'))
|
2018-07-16 22:22:55 +08:00
|
|
|
|
.end()
|
2018-07-17 21:54:38 +08:00
|
|
|
|
// 重新设置 alias
|
2018-07-19 17:00:45 +08:00
|
|
|
|
config.resolve.alias
|
2018-12-11 08:48:38 +08:00
|
|
|
|
.set('@api', resolve('src/api'))
|
2018-11-14 19:48:44 +08:00
|
|
|
|
// 判断环境加入模拟数据
|
2019-01-17 11:09:43 +08:00
|
|
|
|
const entry = config.entry('app')
|
2019-05-10 09:56:55 +08:00
|
|
|
|
if (process.env.VUE_APP_BUILD_MODE !== 'NOMOCK') {
|
2018-11-14 16:24:56 +08:00
|
|
|
|
entry
|
|
|
|
|
|
.add('@/mock')
|
|
|
|
|
|
.end()
|
|
|
|
|
|
}
|
2019-05-21 23:51:29 +08:00
|
|
|
|
},
|
2019-05-22 00:19:48 +08:00
|
|
|
|
// i18n
|
2019-05-21 23:51:29 +08:00
|
|
|
|
pluginOptions: {
|
|
|
|
|
|
i18n: {
|
2019-05-22 16:43:31 +08:00
|
|
|
|
locale: 'en',
|
|
|
|
|
|
fallbackLocale: 'zh-chs',
|
2019-05-21 23:51:29 +08:00
|
|
|
|
localeDir: 'locales',
|
|
|
|
|
|
enableInSFC: true
|
|
|
|
|
|
}
|
2018-07-16 22:22:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|