vue.config.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 pxtoviewport = require('postcss-px-to-viewport')
  9. const externals = {
  10. vue: 'Vue',
  11. 'vue-router': 'VueRouter',
  12. vuex: 'Vuex',
  13. axios: 'axios',
  14. 'js-cookie': 'Cookies',
  15. vant: 'vant',
  16. moment: 'moment'
  17. }
  18. // cdn地址获取访问(国外): https://www.jsdelivr.com/
  19. // cdn地址获取访问(国内): https://www.bootcdn.cn/
  20. const cdn = {
  21. css: [
  22. // '//unpkg.com/element-ui@2.10.1/lib/theme-chalk/index.css'
  23. ],
  24. js: [
  25. '//cdn.bootcss.com/axios/0.19.2/axios.min.js',
  26. '//cdn.bootcss.com/js-cookie/2.2.1/js.cookie.min.js',
  27. '//cdn.bootcdn.net/ajax/libs/moment.js/2.24.0/moment.min.js',
  28. '//cdn.bootcss.com/vue/2.6.11/vue.min.js',
  29. '//cdn.jsdelivr.net/npm/vue-router@3.1.5/dist/vue-router.min.js',
  30. '//cdn.jsdelivr.net/npm/vuex@3.4.0/dist/vuex.min.js',
  31. '//cdn.jsdelivr.net/npm/vant@2.8.2/lib/vant.min.js',
  32. '//res.wx.qq.com/open/js/jweixin-1.6.0.js'
  33. ]
  34. }
  35. module.exports = {
  36. publicPath: process.env.BASE_URL,
  37. parallel: false,
  38. productionSourceMap: false,
  39. devServer: {
  40. port: '8080',
  41. open: false,
  42. disableHostCheck: true,
  43. proxy: {
  44. '^/jypoints/api': {
  45. target: 'http://web2-jytest.jianyu360.cn',
  46. changeOrigin: true,
  47. logLevel: 'debug',
  48. pathRewrite: {
  49. '^/jypoints/api': '/jypoints/api'
  50. }
  51. },
  52. '^/jypay': {
  53. target: 'http://web2-jytest.jianyu360.cn',
  54. changeOrigin: true,
  55. logLevel: 'debug',
  56. pathRewrite: {
  57. '^/jypay': '/jypay'
  58. }
  59. },
  60. '^/subscribepay': {
  61. target: 'http://web2-jytest.jianyu360.cn',
  62. changeOrigin: true,
  63. logLevel: 'debug',
  64. pathRewrite: {
  65. '^/subscribepay': '/subscribepay'
  66. }
  67. }
  68. }
  69. },
  70. css: {
  71. loaderOptions: {
  72. sass: {
  73. prependData: '@import "@/style/_mixin.scss";@import "@/style/_variables.scss";@import "@/style/base.scss";@import "@/style/common.scss";' // 全局引入
  74. },
  75. postcss: {
  76. plugins: [
  77. autoprefixer(),
  78. pxtoviewport(({
  79. unitToConvert: 'px',
  80. viewportWidth: 375,
  81. unitPrecision: 5,
  82. propList: [
  83. '*'
  84. ],
  85. viewportUnit: 'vw',
  86. fontViewportUnit: 'vw',
  87. selectorBlackList: [],
  88. minPixelValue: 1,
  89. mediaQuery: false,
  90. replace: true,
  91. exclude: /(\/|\\)(node_modules)(\/|\\)/
  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. return args
  109. })
  110. } else {
  111. config.plugin('html').tap(args => {
  112. // html中添加wxjsssk的cdn
  113. args[0].cdn = {
  114. js: [
  115. cdn.js[cdn.js.length - 1]
  116. ]
  117. }
  118. return args
  119. })
  120. }
  121. // 分析静态资源
  122. if (process.env.use_analyzer) {
  123. config
  124. .plugin('webpack-bundle-analyzer')
  125. .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  126. }
  127. // 修复HMR
  128. config.resolve.symlinks(true)
  129. config.module
  130. .rule('ts')
  131. .use('ts-loader')
  132. .tap(options => {
  133. options = merge(options, {
  134. transpileOnly: true,
  135. getCustomTransformers: () => ({
  136. before: [
  137. tsImportPluginFactory({
  138. libraryName: 'vant',
  139. libraryDirectory: 'es',
  140. style: true
  141. })
  142. ]
  143. }),
  144. compilerOptions: {
  145. module: 'es2015'
  146. }
  147. })
  148. return options
  149. })
  150. }
  151. }