vue.config.js 7.4 KB

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