vue.config.js 5.5 KB

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