vue.config.js 6.9 KB

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