vue.config.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const path = require('path')
  2. const webpack = require('webpack')
  3. const createThemeColorReplacerPlugin = require('./config/plugin.config')
  4. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  5. const productionGzipExtensions = ['js', 'css']
  6. function resolve (dir) {
  7. return path.join(__dirname, dir)
  8. }
  9. /**
  10. * check production or preview(pro.loacg.com only)
  11. * @returns {boolean}
  12. */
  13. function isProd () {
  14. return process.env.NODE_ENV === 'production'
  15. }
  16. const assetsCDN = {
  17. css: [],
  18. // https://unpkg.com/browse/vue@2.6.10/
  19. js: [
  20. '//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js',
  21. '//cdn.jsdelivr.net/npm/vue-router@3.1.3/dist/vue-router.min.js',
  22. '//cdn.jsdelivr.net/npm/vuex@3.1.1/dist/vuex.min.js',
  23. '//cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js'
  24. ]
  25. }
  26. // webpack build externals
  27. const prodExternals = {
  28. vue: 'Vue',
  29. 'vue-router': 'VueRouter',
  30. vuex: 'Vuex',
  31. axios: 'axios'
  32. }
  33. // vue.config.js
  34. const vueConfig = {
  35. configureWebpack: {
  36. // webpack plugins
  37. plugins: [
  38. // Ignore all locale files of moment.js
  39. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
  40. ],
  41. // if prod is on, add externals
  42. externals: isProd() ? prodExternals : {}
  43. },
  44. chainWebpack: (config) => {
  45. config.resolve.alias
  46. .set('@$', resolve('src'))
  47. config.plugins.delete('preload')
  48. config.plugins.delete('prefetch')
  49. const svgRule = config.module.rule('svg')
  50. svgRule.uses.clear()
  51. svgRule
  52. .oneOf('inline')
  53. .resourceQuery(/inline/)
  54. .use('vue-svg-icon-loader')
  55. .loader('vue-svg-icon-loader')
  56. .end()
  57. .end()
  58. .oneOf('external')
  59. .use('file-loader')
  60. .loader('file-loader')
  61. .options({
  62. name: 'assets/[name].[hash:8].[ext]'
  63. })
  64. // if prod is on
  65. // assets require on cdn
  66. if (isProd()) {
  67. config.plugin('html').tap(args => {
  68. args[0].cdn = assetsCDN
  69. return args
  70. })
  71. }
  72. },
  73. css: {
  74. loaderOptions: {
  75. less: {
  76. modifyVars: {
  77. // less vars,customize ant design theme
  78. // 'primary-color': '#F5222D',
  79. // 'link-color': '#F5222D',
  80. // 'border-radius-base': '4px'
  81. },
  82. // do not remove this line
  83. javascriptEnabled: true
  84. }
  85. }
  86. },
  87. devServer: {
  88. // development server port 8000
  89. port: 8000,
  90. // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
  91. proxy: {
  92. '/api': {
  93. target: 'http://localhost:5000',
  94. ws: false,
  95. changeOrigin: true,
  96. pathRewrite: {
  97. '^/api': ''
  98. }
  99. },
  100. '/files': {
  101. target: 'http://localhost:5000',
  102. ws: false,
  103. changeOrigin: true
  104. }
  105. }
  106. },
  107. // disable source map in production
  108. productionSourceMap: false,
  109. lintOnSave: undefined,
  110. // babel-loader no-ignore node_modules/*
  111. transpileDependencies: []
  112. }
  113. // preview.pro.loacg.com only do not use in your production;
  114. // if (process.env.VUE_APP_PREVIEW === 'true') {
  115. // console.log('VUE_APP_PREVIEW', true)
  116. // // add `ThemeColorReplacer` plugin to webpack plugins
  117. // vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
  118. // }
  119. vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
  120. if (isProd()) {
  121. vueConfig.configureWebpack.plugins.push(new CompressionWebpackPlugin({
  122. filename: '[path].gz[query]', // 提示 compression-webpack-plugin@3.0.0以上的话asset改为filename
  123. algorithm: 'gzip',
  124. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  125. threshold: 1024,
  126. minRatio: 0.8
  127. }))
  128. }
  129. module.exports = vueConfig