vue.config.js 7.1 KB

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