vue.config.js 7.2 KB

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