vue.config.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. open: false,
  68. host: '0.0.0.0',
  69. port: '8800',
  70. hot: true,
  71. proxy: {
  72. '/api': {
  73. target: baseURL,
  74. secure: false,
  75. changeOrigin: true, // 是否允许跨域
  76. pathRewrite: {
  77. '^/api': '/'
  78. }
  79. }
  80. }
  81. },
  82. resolve: {
  83. extensions: ['.js', '.vue', '.json'],
  84. // 别名
  85. alias: {
  86. '@': resolve('src')
  87. }
  88. },
  89. // 定义webpack打包配置
  90. externals: process.env.VUE_APP_STAGE === 'development' ? {} : objExternals,
  91. plugins: [
  92. // 配置compression-webpack-plugin压缩ZIP加速
  93. new CompressionWebpackPlugin({
  94. algorithm: 'gzip',
  95. // eslint-disable-next-line
  96. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  97. threshold: 10240,
  98. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  99. deleteOriginalAssets: false // 删除原文件
  100. }),
  101. // build时生成打包报告
  102. new BundleAnalyzer({
  103. analyzerMode: 'disabled', // 启用:server 不启用:disabled
  104. openAnalyzer: true, // 是否自动打开报告页面
  105. analyzerPort: 9999 // 报告页面端口
  106. })
  107. ]
  108. }
  109. }