vue.config.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. proxy: {
  36. '/api': {
  37. target: ' http://101.36.160.140:21023',
  38. ws: true,
  39. changeOrigin: true,
  40. pathRewrite: {
  41. '^/api': ''
  42. }
  43. }
  44. }
  45. },
  46. css: {
  47. loaderOptions: {
  48. // 设置 scss 公用变量文件
  49. sass: {
  50. prependData: '@import \'~@/assets/style/public.scss\';'
  51. }
  52. }
  53. },
  54. pages,
  55. configureWebpack: config => {
  56. const configNew = {}
  57. if (process.env.NODE_ENV === 'production') {
  58. configNew.externals = externals
  59. configNew.plugins = [
  60. // gzip
  61. new CompressionWebpackPlugin({
  62. filename: '[path].gz[query]',
  63. test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
  64. threshold: 10240,
  65. minRatio: 0.8,
  66. deleteOriginalAssets: false
  67. })
  68. ]
  69. }
  70. return configNew
  71. },
  72. // 默认设置: https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config/base.js
  73. chainWebpack: config => {
  74. /**
  75. * 添加 CDN 参数到 htmlWebpackPlugin 配置中
  76. * 已适配多页
  77. */
  78. const htmlPluginNames = chain(pages).keys().map(page => 'html-' + page).value()
  79. const targetHtmlPluginNames = htmlPluginNames.length ? htmlPluginNames : ['html']
  80. each(targetHtmlPluginNames, name => {
  81. config.plugin(name).tap(options => {
  82. set(options, '[0].cdn', process.env.NODE_ENV === 'production' ? cdn : [])
  83. return options
  84. })
  85. })
  86. /**
  87. * 删除懒加载模块的 prefetch preload,降低带宽压力
  88. * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
  89. * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#preload
  90. * 而且预渲染时生成的 prefetch 标签是 modern 版本的,低版本浏览器是不需要的
  91. */
  92. config.plugins
  93. .delete('prefetch')
  94. .delete('preload')
  95. // 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
  96. config.resolve
  97. .symlinks(true)
  98. config
  99. .plugin('theme-color-replacer')
  100. .use(ThemeColorReplacer, [{
  101. fileName: 'css/theme-colors.[contenthash:8].css',
  102. matchColors: [
  103. ...forElementUI.getElementUISeries(process.env.VUE_APP_ELEMENT_COLOR) // Element-ui主色系列
  104. ],
  105. 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.
  106. changeSelector: forElementUI.changeSelector
  107. }])
  108. config
  109. // 开发环境 sourcemap 不包含列信息
  110. .when(process.env.NODE_ENV === 'development',
  111. config => config.devtool('cheap-source-map')
  112. )
  113. // 预览环境构建 vue-loader 添加 filename
  114. .when(
  115. process.env.VUE_APP_SCOURCE_LINK === 'TRUE',
  116. config => VueFilenameInjector(config, {
  117. propName: process.env.VUE_APP_SOURCE_VIEWER_PROP_NAME
  118. })
  119. )
  120. // markdown
  121. config.module
  122. .rule('md')
  123. .test(/\.md$/)
  124. .use('text-loader')
  125. .loader('text-loader')
  126. .end()
  127. // svg
  128. const svgRule = config.module.rule('svg')
  129. svgRule.uses.clear()
  130. svgRule
  131. .include
  132. .add(resolve('src/assets/svg-icons/icons'))
  133. .end()
  134. .use('svg-sprite-loader')
  135. .loader('svg-sprite-loader')
  136. .options({
  137. symbolId: 'd2-[name]'
  138. })
  139. .end()
  140. // image exclude
  141. const imagesRule = config.module.rule('images')
  142. imagesRule
  143. .test(/\.(png|jpe?g|gif|webp|svg)(\?.*)?$/)
  144. .exclude
  145. .add(resolve('src/assets/svg-icons/icons'))
  146. .end()
  147. // 重新设置 alias
  148. config.resolve.alias
  149. .set('@api', resolve('src/api'))
  150. // 分析工具
  151. if (process.env.npm_config_report) {
  152. config
  153. .plugin('webpack-bundle-analyzer')
  154. .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  155. }
  156. },
  157. // 不输出 map 文件
  158. productionSourceMap: false,
  159. // i18n
  160. pluginOptions: {
  161. i18n: {
  162. locale: 'zh-chs',
  163. fallbackLocale: 'en',
  164. localeDir: 'locales',
  165. enableInSFC: true
  166. }
  167. }
  168. }