vue.config.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. const svgRule = config.module.rule('svg')
  48. svgRule.uses.clear()
  49. svgRule
  50. .oneOf('inline')
  51. .resourceQuery(/inline/)
  52. .use('vue-svg-icon-loader')
  53. .loader('vue-svg-icon-loader')
  54. .end()
  55. .end()
  56. .oneOf('external')
  57. .use('file-loader')
  58. .loader('file-loader')
  59. .options({
  60. name: 'assets/[name].[hash:8].[ext]'
  61. })
  62. // if prod is on
  63. // assets require on cdn
  64. /* if (isProd()) {
  65. config.plugin('html').tap(args => {
  66. args[0].cdn = assetsCDN
  67. return args
  68. })
  69. } */
  70. },
  71. css: {
  72. loaderOptions: {
  73. less: {
  74. modifyVars: {
  75. // less vars,customize ant design theme
  76. // 'primary-color': '#F5222D',
  77. // 'link-color': '#F5222D',
  78. // 'border-radius-base': '4px'
  79. },
  80. // do not remove this line
  81. javascriptEnabled: true
  82. }
  83. }
  84. },
  85. devServer: {
  86. // development server port 8000
  87. port: 8000,
  88. // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
  89. proxy: {
  90. '/api': {
  91. target: 'http://localhost:5000',
  92. ws: false,
  93. changeOrigin: true,
  94. pathRewrite: {
  95. '^/api': ''
  96. }
  97. },
  98. '/files': {
  99. target: 'http://localhost:5000',
  100. ws: false,
  101. changeOrigin: true
  102. }
  103. }
  104. },
  105. // disable source map in production
  106. productionSourceMap: false,
  107. lintOnSave: undefined,
  108. // babel-loader no-ignore node_modules/*
  109. transpileDependencies: []
  110. }
  111. // preview.pro.loacg.com only do not use in your production;
  112. // if (process.env.VUE_APP_PREVIEW === 'true') {
  113. // console.log('VUE_APP_PREVIEW', true)
  114. // // add `ThemeColorReplacer` plugin to webpack plugins
  115. // vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
  116. // }
  117. vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
  118. if (isProd()) {
  119. vueConfig.configureWebpack.plugins.push(new CompressionWebpackPlugin({
  120. filename: '[path].gz[query]', // 提示 compression-webpack-plugin@3.0.0以上的话asset改为filename
  121. algorithm: 'gzip',
  122. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  123. threshold: 10240,
  124. minRatio: 0.8
  125. }))
  126. }
  127. module.exports = vueConfig