vue.config.js 6.9 KB

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