vue.config.js 7.2 KB

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