vue.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. },
  57. },
  58. ]);
  59. },
  60. configureWebpack: () => {
  61. let baseConfig = {
  62. plugins: [
  63. Icons({ compiler: 'vue3' }),
  64. ],
  65. };
  66. let envConfig = {};
  67. if (process.env.NODE_ENV === "production") {
  68. // 为生产环境修改配置...
  69. envConfig = {
  70. optimization: {
  71. splitChunks: {
  72. chunks: "all",
  73. // enforceSizeThreshold: 20000,
  74. cacheGroups: {
  75. echarts: {
  76. name: "chunk-echarts",
  77. priority: 20,
  78. test: /[\\/]node_modules[\\/]_?echarts(.*)/,
  79. },
  80. elementPlus: {
  81. name: "chunk-elementPlus",
  82. priority: 20,
  83. test: /[\\/]node_modules[\\/]_?element-plus(.*)/,
  84. },
  85. elementPlusIcon: {
  86. name: "chunk-elementPlusIcon",
  87. priority: 20,
  88. test: /[\\/]node_modules[\\/]_?@element-plus[\\/]icons(.*)/,
  89. },
  90. mockjs: {
  91. name: "chunk-mockjs",
  92. priority: 20,
  93. test: /[\\/]node_modules[\\/]_?mockjs(.*)/,
  94. },
  95. },
  96. },
  97. },
  98. externals: {
  99. // lodash: "_"
  100. },
  101. plugins: [
  102. new TerserPlugin({
  103. terserOptions: {
  104. compress: {
  105. drop_console: false,
  106. drop_debugger: true,
  107. },
  108. },
  109. }),
  110. new CompressionWebpackPlugin({
  111. filename: "[path][base].gz",
  112. algorithm: "gzip",
  113. // test: /\.js$|\.html$|\.json$|\.css/,
  114. test: /\.js$|\.json$|\.css/,
  115. threshold: 10240, // 只有大小大于该值的资源会被处理
  116. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  117. // deleteOriginalAssets: true // 删除原文件
  118. }),
  119. Icons({ compiler: 'vue3' }),
  120. ],
  121. };
  122. }
  123. return Object.assign(baseConfig, envConfig);
  124. },
  125. css: {
  126. loaderOptions: {
  127. scss: {
  128. // 注意:在 sass-loader v8 中,这个选项名是 "prependData"
  129. // additionalData: `@import "~@/styles/imports.scss";`
  130. additionalData: Object.keys(scssVariables)
  131. .map((k) => `$${k.replace("_", "-")}: ${scssVariables[k]};`)
  132. .join("\n"),
  133. },
  134. },
  135. },
  136. };