vue.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // eslint-disable-next-line @typescript-eslint/no-var-requires
  2. const merge = require('webpack-merge')
  3. // eslint-disable-next-line @typescript-eslint/no-var-requires
  4. const tsImportPluginFactory = require('ts-import-plugin')
  5. // eslint-disable-next-line @typescript-eslint/no-var-requires
  6. const autoprefixer = require('autoprefixer')
  7. // eslint-disable-next-line @typescript-eslint/no-var-requires
  8. const pxtorem = require('postcss-pxtorem')
  9. // eslint-disable-next-line @typescript-eslint/no-var-requires
  10. const pxtoviewport = require('postcss-px-to-viewport')
  11. const externals = {
  12. vue: 'Vue',
  13. 'vue-router': 'VueRouter',
  14. vuex: 'Vuex',
  15. axios: 'axios',
  16. 'js-cookie': 'Cookies',
  17. vant: 'vant',
  18. moment: 'moment'
  19. }
  20. // cdn地址获取访问(国外): https://www.jsdelivr.com/
  21. // cdn地址获取访问(国内): https://www.bootcdn.cn/
  22. const cdn = {
  23. css: [
  24. // '//unpkg.com/element-ui@2.10.1/lib/theme-chalk/index.css'
  25. ],
  26. js: [
  27. '//cdn.bootcss.com/axios/0.19.2/axios.min.js',
  28. '//cdn.bootcss.com/js-cookie/2.2.1/js.cookie.min.js',
  29. '//cdn.bootcdn.net/ajax/libs/moment.js/2.24.0/moment.min.js',
  30. '//cdn.bootcss.com/vue/2.6.11/vue.min.js',
  31. '//cdn.jsdelivr.net/npm/vue-router@3.1.5/dist/vue-router.min.js',
  32. '//cdn.jsdelivr.net/npm/vuex@3.4.0/dist/vuex.min.js',
  33. '//cdn.jsdelivr.net/npm/vant@2.8.2/lib/vant.min.js',
  34. '//res.wx.qq.com/open/js/jweixin-1.6.0.js'
  35. ]
  36. }
  37. module.exports = {
  38. publicPath: process.env.BASE_URL,
  39. parallel: false,
  40. productionSourceMap: false,
  41. devServer: {
  42. port: '8080',
  43. open: false,
  44. disableHostCheck: true,
  45. proxy: {
  46. '^/datareport/api': {
  47. // target: 'http://192.168.20.241:92',
  48. target: 'http://192.168.20.135:92',
  49. changeOrigin: true,
  50. logLevel: 'debug',
  51. pathRewrite: {
  52. '^/datareport/api': '/datareport/api'
  53. }
  54. },
  55. '^/jypay': {
  56. // target: 'http://192.168.20.241:86',
  57. target: 'http://192.168.20.135:86',
  58. changeOrigin: true,
  59. logLevel: 'debug',
  60. pathRewrite: {
  61. '^/jypay': '/jypay'
  62. }
  63. }
  64. }
  65. },
  66. css: {
  67. loaderOptions: {
  68. sass: {
  69. prependData: '@import "@/style/_mixin.scss";@import "@/style/_variables.scss";@import "@/style/base.scss";@import "@/style/common.scss";' // 全局引入
  70. },
  71. postcss: {
  72. plugins: [
  73. autoprefixer(),
  74. pxtoviewport(({
  75. unitToConvert: 'px',
  76. viewportWidth: 375,
  77. unitPrecision: 5,
  78. propList: [
  79. '*'
  80. ],
  81. viewportUnit: 'vw',
  82. fontViewportUnit: 'vw',
  83. selectorBlackList: [],
  84. minPixelValue: 1,
  85. mediaQuery: false,
  86. replace: true,
  87. exclude: /(\/|\\)(node_modules)(\/|\\)/
  88. }))
  89. // pxtorem({
  90. // rootValue: 37.5,
  91. // propList: ['*']
  92. // })
  93. ]
  94. }
  95. }
  96. },
  97. chainWebpack: config => {
  98. // 防止多页面打包卡顿
  99. // eslint-disable-next-line no-unused-expressions
  100. // config.plugins.delete('named-chunks')
  101. if (process.env.NODE_ENV === 'production') {
  102. // 打包时需要执行,开发环境运行不需要执行
  103. config.externals(externals)
  104. // 如果使用多页面打包,使用vue inspect --plugins查看html是否在结果数组中
  105. config.plugin('html').tap(args => {
  106. // html中添加cdn
  107. args[0].cdn = cdn
  108. // console.log(JSON.stringify(args))
  109. // 修复 Lazy loading routes Error
  110. // args[0].chunksSortMode = 'none'
  111. return args
  112. })
  113. } else {
  114. config.plugin('html').tap(args => {
  115. // html中添加wxjsssk的cdn
  116. args[0].cdn = {
  117. js: [
  118. cdn.js[cdn.js.length - 1]
  119. ]
  120. }
  121. return args
  122. })
  123. }
  124. // 分析静态资源
  125. if (process.env.use_analyzer) {
  126. config
  127. .plugin('webpack-bundle-analyzer')
  128. .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  129. }
  130. // 修复HMR
  131. config.resolve.symlinks(true)
  132. config.module
  133. .rule('ts')
  134. .use('ts-loader')
  135. .tap(options => {
  136. options = merge(options, {
  137. transpileOnly: true,
  138. getCustomTransformers: () => ({
  139. before: [
  140. tsImportPluginFactory({
  141. libraryName: 'vant',
  142. libraryDirectory: 'es',
  143. style: true
  144. })
  145. ]
  146. }),
  147. compilerOptions: {
  148. module: 'es2015'
  149. }
  150. })
  151. return options
  152. })
  153. // return config
  154. }
  155. }