vue.config.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. if (process.env.NODE_ENV === 'production') {
  133. const TerserPlugin = require('terser-webpack-plugin')
  134. config.optimization.minimizer = [
  135. new TerserPlugin({
  136. parallel: true,
  137. terserOptions: {
  138. ecma: 5, // 指定为 ES5
  139. compress: {
  140. drop_console: true,
  141. drop_debugger: true,
  142. pure_funcs: ['console.log'] // 移除特定的 console 方法
  143. },
  144. output: {
  145. comments: false,
  146. beautify: false,
  147. // 确保不生成 ES6+ 语法
  148. ecma: 5
  149. },
  150. // 特别针对 IE11 的兼容性设置
  151. ie8: true
  152. }
  153. })
  154. ]
  155. // 添加 ES5 输出的额外配置
  156. // config.output.environment = {
  157. // arrowFunction: false, // 不生成箭头函数
  158. // const: false, // 不生成 const
  159. // destructuring: false, // 不生成解构赋值
  160. // forOf: false // 不生成 for...of
  161. // }
  162. // // 压缩 JS 文件
  163. // config.plugins.push(
  164. // new UglifyJsPlugin({
  165. // uglifyOptions: {
  166. // compress: {
  167. // drop_console: true,
  168. // drop_debugger: true
  169. // },
  170. // output: {
  171. // comments: false // 移除注释
  172. // }
  173. // },
  174. // sourceMap: false,
  175. // parallel: true
  176. // }))
  177. }
  178. },
  179. chainWebpack: config => {
  180. config.when(process.env.NODE_ENV !== 'development', config => {
  181. config.optimization.minimizer('terser').tap(options => {
  182. options[0].terserOptions.compress.drop_console = true
  183. options[0].terserOptions.output = {
  184. comments: false
  185. }
  186. return options
  187. })
  188. })
  189. },
  190. // babel-loader 是否处理 node_modules 中的依赖包,处理哪些依赖包,参数类型: boolean | Array<string | RegExp>
  191. transpileDependencies: [
  192. /[\\/]node_modules[\\/]/ // 转译所有 node_modules 中的依赖
  193. // Vue 相关
  194. // 'vue',
  195. // 'vuex',
  196. // 'vue-router',
  197. // '@vue/composition-api',
  198. // // UI 组件库
  199. // 'element-ui',
  200. // // 编辑器
  201. // '@wangeditor/editor',
  202. // '@wangeditor/editor-for-vue',
  203. // '@mdi/font',
  204. // 'scrolling-element',
  205. // // 其他可能包含 ES6 代码的依赖
  206. // 'fs',
  207. // 'qs',
  208. // 'axios',
  209. // 'lodash',
  210. // 'echarts',
  211. // 'decimal.js',
  212. // 'nprogress',
  213. // 'element-resize-detector',
  214. // // 使用正则匹配更广泛的包
  215. // /node_modules\/@wangeditor/,
  216. // /node_modules\/vue-/,
  217. // /node_modules\/element-ui/,
  218. // /node_modules\/echarts/
  219. ],
  220. // 移除console
  221. terser: {
  222. terserOptions: {
  223. compress: {
  224. drop_console: true,
  225. drop_debugger: true
  226. }
  227. }
  228. },
  229. css: {
  230. loaderOptions: {
  231. sass: {
  232. additionalData: `
  233. @import "@/styles/config.scss";
  234. `
  235. }
  236. }
  237. }
  238. })