vue.config.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. const CopyWebpackPlugin = require('copy-webpack-plugin')
  13. function resolve(dir) {
  14. return path.join(__dirname, '.', dir)
  15. }
  16. const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
  17. const TerserPlugin = require("terser-webpack-plugin");
  18. const CompressionWebpackPlugin = require("compression-webpack-plugin");
  19. let scssVariables = require("./src/styles/variables.scss.js");
  20. const Icons = require("unplugin-icons/webpack")
  21. const IconsResolver = require("unplugin-icons/resolver")
  22. module.exports = {
  23. publicPath: "",
  24. productionSourceMap: false,
  25. devServer: {
  26. setupMiddlewares: (middlewares, devServer) => {
  27. if (config.dev_mock) {
  28. const mock_server = require("./src/api/mock-server.js");
  29. mock_server(devServer.app);
  30. }
  31. return middlewares;
  32. },
  33. proxy: {
  34. '/ws': {
  35. target: 'https://apis.map.qq.com',
  36. changeOrigin: true,
  37. ws: false,
  38. },
  39. '/v3/elevation': {
  40. target: 'https://restapi.amap.com',
  41. changeOrigin: true,
  42. ws: false,
  43. }
  44. },
  45. //这里的ip和端口是前端项目的;下面为需要跨域访问后端项目
  46. host:"127.0.0.1",
  47. port: '8081',
  48. https: false,
  49. open: false // 配置自动启动浏览器
  50. },
  51. chainWebpack: (config) => {
  52. config.resolve.symlinks(true);
  53. config.plugin("provide").use(webpack.ProvidePlugin, [
  54. {
  55. XE: "xe-utils",
  56. },
  57. ]);
  58. config.plugin("define").use(webpack.DefinePlugin, [
  59. {
  60. VE_ENV: {
  61. MODE: JSON.stringify(process.env.NODE_ENV),
  62. SERVER: JSON.stringify(process.env.SERVER),
  63. PYSERVER: JSON.stringify(process.env.PYSERVER)
  64. },
  65. },
  66. ]);
  67. },
  68. configureWebpack: (config) => {
  69. const cesiumSourcePath = 'node_modules/mars3d-cesium/Build/Cesium/' // cesium库安装目录
  70. const cesiumRunPath = './mars3d-cesium/' // cesium运行时路径
  71. const plugins = [
  72. // 标识cesium资源所在的主目录,cesium内部资源加载、多线程等处理时需要用到
  73. new webpack.DefinePlugin({
  74. CESIUM_BASE_URL: JSON.stringify(path.join(config.output.publicPath, cesiumRunPath))
  75. }),
  76. new webpack.DefinePlugin({
  77. '__VUE_PROD_HYDRATION_MISMATCH_DETAILS__': JSON.stringify(false)
  78. }),
  79. // Cesium相关资源目录需要拷贝到系统目录下面(部分CopyWebpackPlugin版本的语法可能没有patterns)
  80. new CopyWebpackPlugin({
  81. patterns: [
  82. { from: 'node_modules/@liveqing/liveplayer-v3/dist/component/crossdomain.xml'},
  83. { from: 'node_modules/@liveqing/liveplayer-v3/dist/component/liveplayer-lib.min.js', to: 'public/js'},
  84. { from: 'node_modules/@liveqing/liveplayer-v3/dist/component/liveplayer.swf'},
  85. { from: path.join(cesiumSourcePath, 'Workers'), to: path.join(config.output.path, cesiumRunPath, 'Workers') },
  86. { from: path.join(cesiumSourcePath, 'Assets'), to: path.join(config.output.path, cesiumRunPath, 'Assets') },
  87. { from: path.join(cesiumSourcePath, 'ThirdParty'), to: path.join(config.output.path, cesiumRunPath, 'ThirdParty') },
  88. { from: path.join(cesiumSourcePath, 'Widgets'), to: path.join(config.output.path, cesiumRunPath, 'Widgets') }
  89. ],
  90. })
  91. ,
  92. Icons({ compiler: 'vue3' }),
  93. new NodePolyfillPlugin({})
  94. ]
  95. let baseConfig = {
  96. plugins: plugins,
  97. module: { unknownContextCritical: false }
  98. };
  99. let envConfig = {};
  100. if (process.env.NODE_ENV === "production") {
  101. // 为生产环境修改配置...
  102. envConfig = {
  103. optimization: {
  104. splitChunks: {
  105. chunks: "all",
  106. // enforceSizeThreshold: 20000,
  107. cacheGroups: {
  108. echarts: {
  109. name: "chunk-echarts",
  110. priority: 20,
  111. test: /[\\/]node_modules[\\/]_?echarts(.*)/,
  112. },
  113. elementPlus: {
  114. name: "chunk-elementPlus",
  115. priority: 20,
  116. test: /[\\/]node_modules[\\/]_?element-plus(.*)/,
  117. },
  118. elementPlusIcon: {
  119. name: "chunk-elementPlusIcon",
  120. priority: 20,
  121. test: /[\\/]node_modules[\\/]_?@element-plus[\\/]icons(.*)/,
  122. },
  123. mockjs: {
  124. name: "chunk-mockjs",
  125. priority: 20,
  126. test: /[\\/]node_modules[\\/]_?mockjs(.*)/,
  127. },
  128. },
  129. },
  130. },
  131. externals: {
  132. // lodash: "_"
  133. },
  134. plugins: [
  135. new TerserPlugin({
  136. terserOptions: {
  137. compress: {
  138. drop_console: false,
  139. drop_debugger: true,
  140. },
  141. },
  142. }),
  143. new CompressionWebpackPlugin({
  144. filename: "[path][base].gz",
  145. algorithm: "gzip",
  146. // test: /\.js$|\.html$|\.json$|\.css/,
  147. test: /\.js$|\.json$|\.css/,
  148. threshold: 10240, // 只有大小大于该值的资源会被处理
  149. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  150. // deleteOriginalAssets: true // 删除原文件
  151. }),
  152. Icons({ compiler: 'vue3' }),
  153. ],
  154. };
  155. }
  156. return Object.assign(baseConfig, envConfig);
  157. },
  158. css: {
  159. loaderOptions: {
  160. scss: {
  161. // 注意:在 sass-loader v8 中,这个选项名是 "prependData"
  162. // additionalData: `@import "~@/styles/imports.scss";`
  163. additionalData: Object.keys(scssVariables)
  164. .map((k) => `$${k.replace("_", "-")}: ${scssVariables[k]};`)
  165. .join("\n"),
  166. },
  167. },
  168. },
  169. };