vue.config.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. const path = require('path')
  2. const defaultSettings = require('./src/config/index.js')
  3. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  4. const resolve = dir => path.join(__dirname, dir)
  5. // page titley
  6. const name = defaultSettings.title;
  7. // 生产环境,测试和正式
  8. const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV)
  9. const { defineConfig } = require('@vue/cli-service')
  10. require('events').EventEmitter.defaultMaxListeners = 0;
  11. // externals
  12. // const externals = {
  13. // vue: 'Vue',
  14. // 'vue-router': 'VueRouter',
  15. // vuex: 'Vuex',
  16. // vant: 'vant',
  17. // axios: 'axios'
  18. // }
  19. // CDN外链,会插入到index.html中
  20. // const cdn = {
  21. // // 开发环境
  22. // dev: {
  23. // css: [],
  24. // js: []
  25. // },
  26. // // 生产环境
  27. // build: {
  28. // css: ['https://cdn.jsdelivr.net/npm/vant@2.4.7/lib/index.css'],
  29. // js: [
  30. // 'https://cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.min.js',
  31. // 'https://cdn.jsdelivr.net/npm/vue-router@3.1.5/dist/vue-router.min.js',
  32. // 'https://cdn.jsdelivr.net/npm/axios@0.19.2/dist/axios.min.js',
  33. // 'https://cdn.jsdelivr.net/npm/vuex@3.1.2/dist/vuex.min.js',
  34. // 'https://cdn.jsdelivr.net/npm/vant@2.4.7/lib/index.min.js'
  35. // ]
  36. // }
  37. // }
  38. module.exports = defineConfig({
  39. publicPath: './', // 署应用包时的基本 URL。 vue-router hash 模式使用
  40. outputDir: 'app', // 生产环境构建文件的目录
  41. assetsDir: 'static', // outputDir的静态资源(js、css、img、fonts)目录
  42. lintOnSave: !IS_PROD,
  43. productionSourceMap: true, // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  44. devServer: {
  45. port: 8181, // 端口
  46. open: false, // 启动后打开浏览器
  47. client: {
  48. overlay: false
  49. },
  50. /*本地开发请求代理*/
  51. proxy: {
  52. '/online': {
  53. target: "http://47.92.229.224:8080",
  54. // ws:true,
  55. changOrigin:true,
  56. pathRewrite:{
  57. '^/online':'/'
  58. }
  59. },
  60. //配置跨域
  61. '/luojun': {
  62. target: "http://10.87.10.49:8080",
  63. // ws:true,
  64. changOrigin:true,
  65. pathRewrite:{
  66. '^/luojun':'/'
  67. }
  68. },
  69. // '/jingyuanchao': {
  70. // target: "http://10.87.10.109:8080",
  71. // // ws:true,
  72. // changOrigin:true,
  73. // pathRewrite:{
  74. // '^/jingyuanchao':'/'
  75. // }
  76. // },
  77. '/jingyuanchao': {
  78. target: "http://10.87.11.173:8080",
  79. // ws:true,
  80. changOrigin:true,
  81. pathRewrite:{
  82. '^/jingyuanchao':'/'
  83. }
  84. },
  85. '/luowei': {
  86. target: "http://10.87.10.47:8080",
  87. // ws:true,
  88. changOrigin:true,
  89. pathRewrite:{
  90. '^/luowei':'/'
  91. }
  92. },
  93. '/gaoxiong': {
  94. target: "http://10.87.11.94:8080",
  95. // ws:true,
  96. changOrigin:true,
  97. pathRewrite:{
  98. '^/gaoxiong':'/'
  99. }
  100. },
  101. '/jwx': {
  102. target: "http://10.87.10.42:8080",
  103. // ws:true,
  104. changOrigin:true,
  105. pathRewrite:{
  106. '^/jwx':'/'
  107. }
  108. },
  109. '/zl': {
  110. target: "http://10.87.10.73:8080",
  111. // ws:true,
  112. changOrigin:true,
  113. pathRewrite:{
  114. '^/zl':'/'
  115. }
  116. },
  117. '/dev': {
  118. target: "http://10.87.21.107:8080",
  119. // target: "http://10.87.21.103:8080",
  120. // ws:true,
  121. changOrigin:true,
  122. pathRewrite:{
  123. '^/dev':'/'
  124. }
  125. },
  126. '/test': {
  127. target: "http://10.87.21.107:8080",
  128. // ws:true,
  129. changOrigin:true,
  130. pathRewrite:{
  131. '^/test':'/'
  132. }
  133. },
  134. '/chao': {
  135. target: "http://10.87.10.34:8080",
  136. // ws:true,
  137. changOrigin:true,
  138. pathRewrite:{
  139. '^/chao':'/'
  140. }
  141. },
  142. }
  143. },
  144. css: {
  145. extract: IS_PROD, // 是否将组件中的 CSS 提取至一个独立的 CSS 文件中 (而不是动态注入到 JavaScript 中的 inline 代码)。
  146. sourceMap: false,
  147. loaderOptions: {
  148. scss: {
  149. // 向全局sass样式传入共享的全局变量, $src可以配置图片cdn前缀
  150. // 详情: https://cli.vuejs.org/guide/css.html#passing-options-to-pre-processor-loaders
  151. additionalData: `
  152. @import "assets/css/mixin.scss";
  153. @import "assets/css/variables.scss";
  154. $cdn: "${defaultSettings.$cdn}";
  155. `
  156. }
  157. }
  158. },
  159. configureWebpack: config => {
  160. config.name = name
  161. // 为生产环境修改配置...
  162. // if (IS_PROD) {
  163. // // externals
  164. // config.externals = externals
  165. // }
  166. },
  167. chainWebpack: config => {
  168. config.plugins.delete('preload') // TODO: need test
  169. config.plugins.delete('prefetch') // TODO: need test
  170. // 别名 alias
  171. config.resolve.alias
  172. .set('@', resolve('src'))
  173. .set('assets', resolve('src/assets'))
  174. .set('api', resolve('src/api'))
  175. .set('views', resolve('src/views'))
  176. .set('components', resolve('src/components'))
  177. /**
  178. * 添加CDN参数到htmlWebpackPlugin配置中
  179. */
  180. // config.plugin('html').tap(args => {
  181. // if (IS_PROD) {
  182. // args[0].cdn = cdn.build
  183. // } else {
  184. // args[0].cdn = cdn.dev
  185. // }
  186. // return args
  187. // })
  188. /**
  189. * 设置保留空格
  190. */
  191. config.module
  192. .rule('vue')
  193. .use('vue-loader')
  194. .loader('vue-loader')
  195. .tap(options => {
  196. options.compilerOptions.preserveWhitespace = true
  197. return options
  198. })
  199. .end()
  200. /**
  201. * 打包分析
  202. */
  203. if (IS_PROD) {
  204. config.plugin('webpack-report').use(BundleAnalyzerPlugin, [
  205. {
  206. analyzerMode: 'static'
  207. }
  208. ])
  209. }
  210. config
  211. // https://webpack.js.org/configuration/devtool/#development
  212. .when(!IS_PROD, config => config.devtool('cheap-source-map'))
  213. config.when(IS_PROD, config => {
  214. config.optimization.splitChunks({
  215. chunks: 'all',
  216. cacheGroups: {
  217. // cacheGroups 下可以可以配置多个组,每个组根据test设置条件,符合test条件的模块
  218. commons: {
  219. name: 'chunk-commons',
  220. test: resolve('src/components'),
  221. minChunks: 3, // 被至少用三次以上打包分离
  222. priority: 5, // 优先级
  223. reuseExistingChunk: true // 表示是否使用已有的 chunk,如果为 true 则表示如果当前的 chunk 包含的模块已经被抽取出去了,那么将不会重新生成新的。
  224. },
  225. node_vendors: {
  226. name: 'chunk-libs',
  227. chunks: 'initial', // 只打包初始时依赖的第三方
  228. test: /[\\/]node_modules[\\/]/,
  229. priority: 10
  230. },
  231. vantUI: {
  232. name: 'chunk-vantUI', // 单独将 vantUI 拆包
  233. priority: 20, // 数字大权重到,满足多个 cacheGroups 的条件时候分到权重高的
  234. test: /[\\/]node_modules[\\/]_?vant(.*)/
  235. }
  236. }
  237. })
  238. config.optimization.runtimeChunk('single')
  239. })
  240. }
  241. })