vue.config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. host: '127.0.0.1',
  32. port: '8081',
  33. https: false,
  34. open: false // 配置自动启动浏览器
  35. },
  36. chainWebpack: (config) => {
  37. config.resolve.symlinks(true);
  38. config.plugin("provide").use(webpack.ProvidePlugin, [
  39. {
  40. XE: "xe-utils",
  41. },
  42. ]);
  43. config.plugin("define").use(webpack.DefinePlugin, [
  44. {
  45. VE_ENV: {
  46. MODE: JSON.stringify(process.env.NODE_ENV),
  47. },
  48. },
  49. ]);
  50. },
  51. configureWebpack: () => {
  52. let baseConfig = {
  53. plugins: [
  54. Icons({ compiler: 'vue3' }),
  55. ],
  56. };
  57. let envConfig = {};
  58. if (process.env.NODE_ENV === "production") {
  59. // 为生产环境修改配置...
  60. envConfig = {
  61. optimization: {
  62. splitChunks: {
  63. chunks: "all",
  64. // enforceSizeThreshold: 20000,
  65. cacheGroups: {
  66. echarts: {
  67. name: "chunk-echarts",
  68. priority: 20,
  69. test: /[\\/]node_modules[\\/]_?echarts(.*)/,
  70. },
  71. elementPlus: {
  72. name: "chunk-elementPlus",
  73. priority: 20,
  74. test: /[\\/]node_modules[\\/]_?element-plus(.*)/,
  75. },
  76. elementPlusIcon: {
  77. name: "chunk-elementPlusIcon",
  78. priority: 20,
  79. test: /[\\/]node_modules[\\/]_?@element-plus[\\/]icons(.*)/,
  80. },
  81. mockjs: {
  82. name: "chunk-mockjs",
  83. priority: 20,
  84. test: /[\\/]node_modules[\\/]_?mockjs(.*)/,
  85. },
  86. },
  87. },
  88. },
  89. externals: {
  90. // lodash: "_"
  91. },
  92. plugins: [
  93. new TerserPlugin({
  94. terserOptions: {
  95. compress: {
  96. drop_console: false,
  97. drop_debugger: true,
  98. },
  99. },
  100. }),
  101. new CompressionWebpackPlugin({
  102. filename: "[path][base].gz",
  103. algorithm: "gzip",
  104. // test: /\.js$|\.html$|\.json$|\.css/,
  105. test: /\.js$|\.json$|\.css/,
  106. threshold: 10240, // 只有大小大于该值的资源会被处理
  107. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  108. // deleteOriginalAssets: true // 删除原文件
  109. }),
  110. Icons({ compiler: 'vue3' }),
  111. ],
  112. };
  113. }
  114. return Object.assign(baseConfig, envConfig);
  115. },
  116. css: {
  117. loaderOptions: {
  118. scss: {
  119. // 注意:在 sass-loader v8 中,这个选项名是 "prependData"
  120. // additionalData: `@import "~@/styles/imports.scss";`
  121. additionalData: Object.keys(scssVariables)
  122. .map((k) => `$${k.replace("_", "-")}: ${scssVariables[k]};`)
  123. .join("\n"),
  124. },
  125. },
  126. },
  127. };