vue.config.js 6.6 KB

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