vue.config.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  2. const VueFilenameInjector = require('@d2-projects/vue-filename-injector')
  3. const ThemeColorReplacer = require('webpack-theme-color-replacer')
  4. const forElementUI = require('webpack-theme-color-replacer/forElementUI')
  5. const cdnDependencies = require('./dependencies-cdn')
  6. const { chain, set, each } = require('lodash')
  7. // 拼接路径
  8. const resolve = dir => require('path').join(__dirname, dir)
  9. // 增加环境变量
  10. process.env.VUE_APP_VERSION = require('./package.json').version
  11. process.env.VUE_APP_BUILD_TIME = require('dayjs')().format('YYYY-M-D HH:mm:ss')
  12. // 基础路径 注意发布之前要先修改这里
  13. const publicPath = process.env.VUE_APP_PUBLIC_PATH || '/'
  14. // 设置不参与构建的库
  15. const externals = {}
  16. cdnDependencies.forEach(pkg => { externals[pkg.name] = pkg.library })
  17. // 引入文件的 cdn 链接
  18. const cdn = {
  19. css: cdnDependencies.map(e => e.css).filter(e => e),
  20. js: cdnDependencies.map(e => e.js).filter(e => e)
  21. }
  22. // 多页配置,默认未开启,如需要请参考 https://cli.vuejs.org/zh/config/#pages
  23. const pages = undefined
  24. // const pages = {
  25. // index: './src/main.js',
  26. // subpage: './src/subpage.js'
  27. // }
  28. module.exports = {
  29. // 根据你的实际情况更改这里
  30. publicPath,
  31. lintOnSave: true,
  32. devServer: {
  33. publicPath, // 和 publicPath 保持一致
  34. disableHostCheck: process.env.NODE_ENV === 'development' // 关闭 host check,方便使用 ngrok 之类的内网转发工具
  35. },
  36. css: {
  37. loaderOptions: {
  38. // 设置 scss 公用变量文件
  39. sass: {
  40. prependData: '@import \'~@/assets/style/public.scss\';'
  41. }
  42. }
  43. },
  44. pages,
  45. configureWebpack: config => {
  46. const configNew = {}
  47. if (process.env.NODE_ENV === 'production') {
  48. configNew.externals = externals
  49. configNew.plugins = [
  50. // gzip
  51. new CompressionWebpackPlugin({
  52. filename: '[path].gz[query]',
  53. test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
  54. threshold: 10240,
  55. minRatio: 0.8,
  56. deleteOriginalAssets: false
  57. })
  58. ]
  59. }
  60. return configNew
  61. },
  62. // 默认设置: https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config/base.js
  63. chainWebpack: config => {
  64. /**
  65. * 添加 CDN 参数到 htmlWebpackPlugin 配置中
  66. * 已适配多页
  67. */
  68. const htmlPluginNames = chain(pages).keys().map(page => 'html-' + page).value()
  69. const targetHtmlPluginNames = htmlPluginNames.length ? htmlPluginNames : ['html']
  70. each(targetHtmlPluginNames, name => {
  71. config.plugin(name).tap(options => {
  72. set(options, '[0].cdn', process.env.NODE_ENV === 'production' ? cdn : [])
  73. return options
  74. })
  75. })
  76. /**
  77. * 删除懒加载模块的 prefetch preload,降低带宽压力
  78. * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
  79. * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#preload
  80. * 而且预渲染时生成的 prefetch 标签是 modern 版本的,低版本浏览器是不需要的
  81. */
  82. config.plugins
  83. .delete('prefetch')
  84. .delete('preload')
  85. // 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
  86. config.resolve
  87. .symlinks(true)
  88. config
  89. .plugin('theme-color-replacer')
  90. .use(ThemeColorReplacer, [{
  91. fileName: 'css/theme-colors.[contenthash:8].css',
  92. matchColors: [
  93. ...forElementUI.getElementUISeries(process.env.VUE_APP_ELEMENT_COLOR) // Element-ui主色系列
  94. ],
  95. externalCssFiles: ['./node_modules/element-ui/lib/theme-chalk/index.css'], // optional, String or string array. Set external css files (such as cdn css) to extract colors.
  96. changeSelector: forElementUI.changeSelector
  97. }])
  98. config
  99. // 开发环境 sourcemap 不包含列信息
  100. .when(process.env.NODE_ENV === 'development',
  101. config => config.devtool('cheap-source-map')
  102. )
  103. // 预览环境构建 vue-loader 添加 filename
  104. .when(
  105. process.env.VUE_APP_SCOURCE_LINK === 'TRUE',
  106. config => VueFilenameInjector(config, {
  107. propName: process.env.VUE_APP_SOURCE_VIEWER_PROP_NAME
  108. })
  109. )
  110. // markdown
  111. config.module
  112. .rule('md')
  113. .test(/\.md$/)
  114. .use('text-loader')
  115. .loader('text-loader')
  116. .end()
  117. // svg
  118. const svgRule = config.module.rule('svg')
  119. svgRule.uses.clear()
  120. svgRule
  121. .include
  122. .add(resolve('src/assets/svg-icons/icons'))
  123. .end()
  124. .use('svg-sprite-loader')
  125. .loader('svg-sprite-loader')
  126. .options({
  127. symbolId: 'd2-[name]'
  128. })
  129. .end()
  130. // image exclude
  131. const imagesRule = config.module.rule('images')
  132. imagesRule
  133. .test(/\.(png|jpe?g|gif|webp|svg)(\?.*)?$/)
  134. .exclude
  135. .add(resolve('src/assets/svg-icons/icons'))
  136. .end()
  137. // 重新设置 alias
  138. config.resolve.alias
  139. .set('@api', resolve('src/api'))
  140. // 分析工具
  141. if (process.env.npm_config_report) {
  142. config
  143. .plugin('webpack-bundle-analyzer')
  144. .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  145. }
  146. },
  147. // 不输出 map 文件
  148. productionSourceMap: false,
  149. // i18n
  150. pluginOptions: {
  151. i18n: {
  152. locale: 'zh-chs',
  153. fallbackLocale: 'en',
  154. localeDir: 'locales',
  155. enableInSFC: true
  156. }
  157. }
  158. }