vue.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const baseURL = process.env.VUE_APP_URL
  2. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  3. const BundleAnalyzer = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  4. const path = require('path')
  5. const resolve = (dir) => path.join(__dirname, '.', dir)
  6. const productionGzipExtensions = ['js', 'css']
  7. // 导入全局scss
  8. function addStyleResource(rule) {
  9. rule.use('style-resource')
  10. .loader('style-resources-loader')
  11. .options({
  12. patterns: [
  13. path.resolve(__dirname, './src/static/styles/common.scss')
  14. ]
  15. })
  16. }
  17. // 开发的cdn
  18. const devCdn = {
  19. css: ['https://unpkg.com/vant@2.12.48/lib/index.css'],
  20. js: []
  21. }
  22. // 打包的cdn
  23. const proCdn = {
  24. css: [
  25. 'https://unpkg.com/vant@2.12.48/lib/index.css'
  26. ],
  27. js: [
  28. 'https://unpkg.com/vue@2.6.14/dist/vue.runtime.min.js',
  29. 'https://unpkg.com/vue-router@3.5.4/dist/vue-router.min.js',
  30. 'https://unpkg.com/vuex@3.6.2/dist/vuex.min.js',
  31. 'https://unpkg.com/axios@0.27.2/dist/axios.min.js',
  32. 'https://unpkg.com/vant@2.12.48/lib/vant.min.js'
  33. ]
  34. }
  35. // 打包忽略
  36. const objExternals = {
  37. vue: 'Vue',
  38. axios: 'axios',
  39. vuex: 'Vuex',
  40. 'vue-router': 'VueRouter',
  41. vant: 'vant'
  42. }
  43. module.exports = {
  44. publicPath: './',
  45. outputDir: 'dist',
  46. assetsDir: 'assets',
  47. lintOnSave: true,
  48. productionSourceMap: false, // 不需要生产环境的 source map
  49. chainWebpack: config => {
  50. const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
  51. types.forEach(type => addStyleResource(config.module.rule('scss').oneOf(type)))
  52. // 配置,将当前页定义的cdn值传到主页面(index.html)
  53. config.plugin('html').tap(args => {
  54. // 这里我是除本地环境,其余均使用CDN,可自己选择是否配置
  55. args[0].cdn = process.env.VUE_APP_STAGE === 'development' ? devCdn : proCdn
  56. return args
  57. })
  58. },
  59. pluginOptions: {
  60. windicss: {
  61. preflight: false,
  62. // 具体配置请查看 https://github.com/windicss/vite-plugin-windicss/blob/main/packages/plugin-utils/src/options.ts
  63. },
  64. },
  65. configureWebpack: {
  66. devServer: {
  67. client: {
  68. overlay: false, // 编译错误时,取消全屏覆盖(建议关掉)
  69. },
  70. open: false,
  71. host: '0.0.0.0',
  72. port: '8800',
  73. hot: true,
  74. proxy: {
  75. '/api': {
  76. target: baseURL,
  77. secure: false,
  78. changeOrigin: true, // 是否允许跨域
  79. pathRewrite: {
  80. '^/api': '/'
  81. }
  82. }
  83. }
  84. },
  85. resolve: {
  86. extensions: ['.js', '.vue', '.json'],
  87. // 别名
  88. alias: {
  89. '@': resolve('src')
  90. }
  91. },
  92. // 定义webpack打包配置
  93. externals: process.env.VUE_APP_STAGE === 'development' ? {} : objExternals,
  94. plugins: [
  95. // 配置compression-webpack-plugin压缩ZIP加速
  96. new CompressionWebpackPlugin({
  97. algorithm: 'gzip',
  98. // eslint-disable-next-line
  99. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  100. threshold: 10240,
  101. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  102. deleteOriginalAssets: false // 删除原文件
  103. }),
  104. // build时生成打包报告
  105. new BundleAnalyzer({
  106. analyzerMode: 'disabled', // 启用:server 不启用:disabled
  107. openAnalyzer: true, // 是否自动打开报告页面
  108. analyzerPort: 9999 // 报告页面端口
  109. })
  110. ]
  111. }
  112. }