vue.config.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * @Author: your name
  3. * @Date: 2020-10-14 15:24:16
  4. * @LastEditTime: 2022-01-20 11:38:21
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: \vue3-element-sysu-map\vue.config.js
  8. */
  9. const config = require("./src/config");
  10. const webpack = require("webpack");
  11. const path = require('path')
  12. function resolve(dir) {
  13. return path.join(__dirname, '.', dir)
  14. }
  15. const TerserPlugin = require("terser-webpack-plugin");
  16. const CompressionWebpackPlugin = require("compression-webpack-plugin");
  17. let scssVariables = require("./src/styles/variables.scss.js");
  18. const Icons = require("unplugin-icons/webpack")
  19. const IconsResolver = require("unplugin-icons/resolver")
  20. module.exports = {
  21. publicPath: "",
  22. productionSourceMap: false,
  23. devServer: {
  24. setupMiddlewares: (middlewares, devServer) => {
  25. if (config.dev_mock) {
  26. const mock_server = require("./src/api/mock-server.js");
  27. mock_server(devServer.app);
  28. }
  29. return middlewares;
  30. },
  31. proxy: {
  32. '/ws': {
  33. target: 'https://apis.map.qq.com',
  34. changeOrigin: true,
  35. ws: false,
  36. }
  37. },
  38. host: '127.0.0.1',
  39. port: '8081',
  40. https: false,
  41. open: false // 配置自动启动浏览器
  42. },
  43. chainWebpack: (config) => {
  44. config.resolve.symlinks(true);
  45. config.plugin("provide").use(webpack.ProvidePlugin, [
  46. {
  47. XE: "xe-utils",
  48. },
  49. ]);
  50. config.plugin("define").use(webpack.DefinePlugin, [
  51. {
  52. VE_ENV: {
  53. MODE: JSON.stringify(process.env.NODE_ENV),
  54. SERVER: JSON.stringify(process.env.SERVER),
  55. PYSERVER: JSON.stringify(process.env.PYSERVER),
  56. MOCK: JSON.stringify(process.env.MOCK)
  57. },
  58. },
  59. ]);
  60. },
  61. configureWebpack: () => {
  62. let baseConfig = {
  63. plugins: [
  64. Icons({ compiler: 'vue3' }),
  65. ],
  66. };
  67. let envConfig = {};
  68. if (process.env.NODE_ENV === "production") {
  69. // 为生产环境修改配置...
  70. envConfig = {
  71. optimization: {
  72. splitChunks: {
  73. chunks: "all",
  74. // enforceSizeThreshold: 20000,
  75. cacheGroups: {
  76. echarts: {
  77. name: "chunk-echarts",
  78. priority: 20,
  79. test: /[\\/]node_modules[\\/]_?echarts(.*)/,
  80. },
  81. elementPlus: {
  82. name: "chunk-elementPlus",
  83. priority: 20,
  84. test: /[\\/]node_modules[\\/]_?element-plus(.*)/,
  85. },
  86. elementPlusIcon: {
  87. name: "chunk-elementPlusIcon",
  88. priority: 20,
  89. test: /[\\/]node_modules[\\/]_?@element-plus[\\/]icons(.*)/,
  90. },
  91. mockjs: {
  92. name: "chunk-mockjs",
  93. priority: 20,
  94. test: /[\\/]node_modules[\\/]_?mockjs(.*)/,
  95. },
  96. },
  97. },
  98. },
  99. externals: {
  100. // lodash: "_"
  101. },
  102. plugins: [
  103. new TerserPlugin({
  104. terserOptions: {
  105. compress: {
  106. drop_console: false,
  107. drop_debugger: true,
  108. },
  109. },
  110. }),
  111. new CompressionWebpackPlugin({
  112. filename: "[path][base].gz",
  113. algorithm: "gzip",
  114. // test: /\.js$|\.html$|\.json$|\.css/,
  115. test: /\.js$|\.json$|\.css/,
  116. threshold: 10240, // 只有大小大于该值的资源会被处理
  117. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  118. // deleteOriginalAssets: true // 删除原文件
  119. }),
  120. Icons({ compiler: 'vue3' }),
  121. ],
  122. };
  123. }
  124. return Object.assign(baseConfig, envConfig);
  125. },
  126. css: {
  127. loaderOptions: {
  128. scss: {
  129. // 注意:在 sass-loader v8 中,这个选项名是 "prependData"
  130. // additionalData: `@import "~@/styles/imports.scss";`
  131. additionalData: Object.keys(scssVariables)
  132. .map((k) => `$${k.replace("_", "-")}: ${scssVariables[k]};`)
  133. .join("\n"),
  134. },
  135. },
  136. },
  137. };