vue.config.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. proxy: {
  43. '^/testServer/datareport': {
  44. target: 'http://192.168.20.241:92',
  45. changeOrigin: true,
  46. logLevel: 'debug',
  47. pathRewrite: {
  48. '^/testServer/datareport': '/datareport'
  49. }
  50. },
  51. '^/testServer/jypay': {
  52. target: 'http://192.168.20.241:86',
  53. changeOrigin: true,
  54. logLevel: 'debug',
  55. pathRewrite: {
  56. '^/testServer/jypay': '/jypay'
  57. }
  58. }
  59. }
  60. },
  61. css: {
  62. loaderOptions: {
  63. sass: {
  64. prependData: '@import "@/style/_mixin.scss";@import "@/style/_variables.scss";@import "@/style/base.scss";@import "@/style/common.scss";' // 全局引入
  65. },
  66. postcss: {
  67. plugins: [
  68. autoprefixer(),
  69. pxtoviewport(({
  70. unitToConvert: 'px',
  71. viewportWidth: 375,
  72. unitPrecision: 5,
  73. propList: [
  74. '*'
  75. ],
  76. viewportUnit: 'vw',
  77. fontViewportUnit: 'vw',
  78. selectorBlackList: [],
  79. minPixelValue: 1,
  80. mediaQuery: false,
  81. replace: true,
  82. exclude: /(\/|\\)(node_modules)(\/|\\)/
  83. }))
  84. // pxtorem({
  85. // rootValue: 37.5,
  86. // propList: ['*']
  87. // })
  88. ]
  89. }
  90. }
  91. },
  92. chainWebpack: config => {
  93. // 防止多页面打包卡顿
  94. // eslint-disable-next-line no-unused-expressions
  95. // config.plugins.delete('named-chunks')
  96. if (process.env.NODE_ENV === 'production') {
  97. // 打包时需要执行,开发环境运行不需要执行
  98. config.externals(externals)
  99. // 如果使用多页面打包,使用vue inspect --plugins查看html是否在结果数组中
  100. config.plugin('html').tap(args => {
  101. // html中添加cdn
  102. args[0].cdn = cdn
  103. // console.log(JSON.stringify(args))
  104. // 修复 Lazy loading routes Error
  105. // args[0].chunksSortMode = 'none'
  106. return args
  107. })
  108. } else {
  109. config.plugin('html').tap(args => {
  110. // html中添加wxjsssk的cdn
  111. args[0].cdn = {
  112. js: [
  113. cdn.js[cdn.js.length - 1]
  114. ]
  115. }
  116. return args
  117. })
  118. }
  119. // 分析静态资源
  120. if (process.env.use_analyzer) {
  121. config
  122. .plugin('webpack-bundle-analyzer')
  123. .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  124. }
  125. // 修复HMR
  126. config.resolve.symlinks(true)
  127. config.module
  128. .rule('ts')
  129. .use('ts-loader')
  130. .tap(options => {
  131. options = merge(options, {
  132. transpileOnly: true,
  133. getCustomTransformers: () => ({
  134. before: [
  135. tsImportPluginFactory({
  136. libraryName: 'vant',
  137. libraryDirectory: 'es',
  138. style: true
  139. })
  140. ]
  141. }),
  142. compilerOptions: {
  143. module: 'es2015'
  144. }
  145. })
  146. return options
  147. })
  148. // return config
  149. }
  150. }