vue.config.js 7.0 KB

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