vue.config.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. 'use strict'
  2. const { defineConfig } = require('@vue/cli-service')
  3. // const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  4. // const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  5. // 配置编辑器
  6. const Timestamp = new Date().getTime()
  7. module.exports = defineConfig({
  8. // 项目部署的基本路径,默认 '/'
  9. publicPath: '/',
  10. assetsDir: 'static',
  11. // 项目打包是否生成js的 source map 调试包,默认 true,生产部署设置为false
  12. productionSourceMap: process.env.NODE_ENV !== 'production',
  13. // devServer 支持 webpack-dev-server 所有选项
  14. devServer: {
  15. open: true,
  16. // host: 'localhost',
  17. port: 9000,
  18. hot: true,
  19. // https: false,
  20. proxy: {
  21. '/op/base': {
  22. // target: 'http://192.168.3.162:7654',
  23. target: 'https://company.citupro.com:18182/op/base',
  24. secure: false, // 是否支持 https,默认 false
  25. changeOrigin: true, // 是否支持跨域
  26. headers: {
  27. // 确保包含这些头部以兼容IE11
  28. Accept: 'application/json',
  29. 'Content-Type': 'application/json; charset=utf-8'
  30. },
  31. onProxyReq (proxyReq) {
  32. // 对于IE11的特殊处理
  33. if (proxyReq.getHeader('origin')) {
  34. proxyReq.setHeader('origin', 'https://company.citupro.com:18182/op/base')
  35. }
  36. },
  37. pathRewrite: {
  38. '^/op/base': ''
  39. }
  40. },
  41. '/op/base/api': {
  42. // target: 'http://192.168.3.162:7654',
  43. target: 'https://company.citupro.com:18183',
  44. secure: false, // 是否支持 https,默认 false
  45. changeOrigin: true, // 是否支持跨域
  46. headers: {
  47. // 确保包含这些头部以兼容IE11
  48. Accept: 'application/json',
  49. 'Content-Type': 'application/json; charset=utf-8'
  50. },
  51. onProxyReq (proxyReq) {
  52. // 对于IE11的特殊处理
  53. if (proxyReq.getHeader('origin')) {
  54. proxyReq.setHeader('origin', 'https://company.citupro.com:18183')
  55. }
  56. },
  57. pathRewrite: {
  58. '^/op/base/api': '/op/base/api'
  59. }
  60. }
  61. }
  62. },
  63. configureWebpack: config => {
  64. config.devtool = 'source-map'
  65. // 代码拆分
  66. // config.optimization.splitChunks.chunks = 'all'
  67. // 代码拆分配置
  68. config.optimization.splitChunks = {
  69. chunks: 'all',
  70. cacheGroups: {
  71. vendors: {
  72. test: /[\\/]node_modules[\\/]/,
  73. priority: -10,
  74. name: 'vendors'
  75. },
  76. common: {
  77. minChunks: 2,
  78. priority: -20,
  79. reuseExistingChunk: true
  80. }
  81. }
  82. }
  83. config.output.filename = `static/js/[name].${Timestamp}.js`
  84. config.output.chunkFilename = `static/js/[name].${Timestamp}.js`
  85. // 确保 polyfill 最先加载
  86. config.entry = {
  87. app: [
  88. 'core-js/stable',
  89. 'regenerator-runtime/runtime',
  90. 'whatwg-fetch', // 添加 fetch polyfill
  91. './src/main.js'
  92. ]
  93. }
  94. config.module.rules.push(
  95. {
  96. test: /\.geojson$/,
  97. loader: 'json-loader',
  98. type: 'javascript/auto'
  99. },
  100. // 其他加载器规则...
  101. {
  102. test: /\.jade$/,
  103. loader: 'jade'
  104. },
  105. {
  106. test: /\.pug$/,
  107. loader: 'pug-plain-loader'
  108. }
  109. // 确保所有 JS 文件都经过 Babel 转译
  110. // {
  111. // test: /\.js$/,
  112. // include: [
  113. // // 特别包含可能有问题的 node_modules
  114. // /node_modules\/element-ui/,
  115. // /node_modules\/vue/,
  116. // /node_modules\/axios/,
  117. // /node_modules\/@wangeditor/
  118. // ],
  119. // use: {
  120. // loader: 'babel-loader',
  121. // options: {
  122. // presets: [
  123. // ['@babel/preset-env', {
  124. // targets: { ie: '11' },
  125. // useBuiltIns: 'usage',
  126. // corejs: 3
  127. // }]
  128. // ]
  129. // }
  130. // }
  131. // }
  132. )
  133. if (process.env.NODE_ENV === 'production') {
  134. const TerserPlugin = require('terser-webpack-plugin')
  135. config.optimization.minimizer = [
  136. new TerserPlugin({
  137. parallel: true,
  138. terserOptions: {
  139. ecma: 5, // 指定为 ES5
  140. compress: {
  141. drop_console: true,
  142. drop_debugger: true,
  143. pure_funcs: ['console.log'] // 移除特定的 console 方法
  144. },
  145. output: {
  146. comments: false,
  147. beautify: false,
  148. // 确保不生成 ES6+ 语法
  149. ecma: 5
  150. },
  151. // 特别针对 IE11 的兼容性设置
  152. ie8: true
  153. }
  154. })
  155. ]
  156. // 添加 ES5 输出的额外配置
  157. // config.output.environment = {
  158. // arrowFunction: false, // 不生成箭头函数
  159. // const: false, // 不生成 const
  160. // destructuring: false, // 不生成解构赋值
  161. // forOf: false // 不生成 for...of
  162. // }
  163. // // 压缩 JS 文件
  164. // config.plugins.push(
  165. // new UglifyJsPlugin({
  166. // uglifyOptions: {
  167. // compress: {
  168. // drop_console: true,
  169. // drop_debugger: true
  170. // },
  171. // output: {
  172. // comments: false // 移除注释
  173. // }
  174. // },
  175. // sourceMap: false,
  176. // parallel: true
  177. // }))
  178. }
  179. },
  180. chainWebpack: config => {
  181. config.when(process.env.NODE_ENV !== 'development', config => {
  182. config.optimization.minimizer('terser').tap(options => {
  183. options[0].terserOptions.compress.drop_console = true
  184. options[0].terserOptions.output = {
  185. comments: false
  186. }
  187. return options
  188. })
  189. })
  190. },
  191. // babel-loader 是否处理 node_modules 中的依赖包,处理哪些依赖包,参数类型: boolean | Array<string | RegExp>
  192. transpileDependencies: [
  193. // /[\\/]node_modules[\\/]/ // 转译所有 node_modules 中的依赖
  194. // Vue 相关
  195. // 'vue',
  196. // 'vuex',
  197. // 'vue-router',
  198. '@vue/composition-api',
  199. // UI 组件库
  200. 'element-ui',
  201. // 编辑器
  202. '@wangeditor/editor',
  203. '@wangeditor/editor-for-vue',
  204. '@mdi/font',
  205. 'scrolling-element',
  206. // // 其他可能包含 ES6 代码的依赖
  207. 'fs',
  208. 'qs',
  209. 'axios',
  210. 'lodash',
  211. 'echarts',
  212. 'decimal.js',
  213. 'nprogress',
  214. 'element-resize-detector',
  215. // // 使用正则匹配更广泛的包
  216. /node_modules\/@wangeditor/
  217. // /node_modules\/vue-/,
  218. // /node_modules\/element-ui/,
  219. // /node_modules\/echarts/
  220. ],
  221. // 移除console
  222. terser: {
  223. terserOptions: {
  224. compress: {
  225. drop_console: true,
  226. drop_debugger: true
  227. }
  228. }
  229. },
  230. css: {
  231. loaderOptions: {
  232. sass: {
  233. additionalData: `
  234. @import "@/styles/config.scss";
  235. `
  236. }
  237. }
  238. }
  239. })