vue.config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 baseUrl = '//cdn.jsdelivr.net/npm'
  23. const baseUrl = 'res://app2-jytest.jianyu360.cn'
  24. // const baseUrl = 'res://res.jianyu360.com'
  25. const cdn = {
  26. css: [
  27. // '//unpkg.com/element-ui@2.10.1/lib/theme-chalk/index.css'
  28. ],
  29. js: [
  30. '//cdn.jsdelivr.net/npm/vue@2.6.11/dist/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. '//cdn.jsdelivr.net/npm/axios@0.19.2/index.min.js',
  35. '//cdn.jsdelivr.net/npm/js-cookie@2.2.1/src/js.cookie.min.js',
  36. '//cdn.jsdelivr.net/npm/moment@2.26.0/moment.min.js',
  37. '//res.wx.qq.com/open/js/jweixin-1.6.0.js'
  38. ]
  39. // js: [
  40. // `${baseUrl}/jyapp/local/vue.min.js`,
  41. // `${baseUrl}/jyapp/local/vue-router.min.js`,
  42. // `${baseUrl}/jyapp/local/vuex.min.js`,
  43. // `${baseUrl}/jyapp/local/vant.min.js`,
  44. // `${baseUrl}/jyapp/local/axios.min.js`,
  45. // `${baseUrl}/jyapp/local/js.cookie.min.js`,
  46. // `${baseUrl}/jyapp/local/moment.min.js`,
  47. // '//res.wx.qq.com/open/js/jweixin-1.6.0.js'
  48. // ]
  49. }
  50. const s_version = process.env.npm_package_version.replace(/\./g, '')
  51. module.exports = {
  52. publicPath: process.env.BASE_URL,
  53. parallel: false,
  54. productionSourceMap: false,
  55. devServer: {
  56. port: '8080',
  57. open: false,
  58. disableHostCheck: true,
  59. proxy: {
  60. '^/datareport/api': {
  61. target: 'http://web2-jytest.jianyu360.cn',
  62. changeOrigin: true,
  63. logLevel: 'debug',
  64. pathRewrite: {
  65. '^/datareport/api': '/datareport/api'
  66. }
  67. },
  68. '^/jypay': {
  69. target: 'http://web2-jytest.jianyu360.cn',
  70. changeOrigin: true,
  71. logLevel: 'debug',
  72. pathRewrite: {
  73. '^/jypay': '/jypay'
  74. }
  75. },
  76. '^/subscribepay': {
  77. target: 'http://web2-jytest.jianyu360.cn',
  78. changeOrigin: true,
  79. logLevel: 'debug',
  80. pathRewrite: {
  81. '^/subscribepay': '/subscribepay'
  82. }
  83. }
  84. }
  85. },
  86. css: {
  87. extract: {
  88. filename: `css/[name].css?v=${s_version}`,
  89. chunkFilename: `css/[name].css?v=${s_version}`
  90. },
  91. loaderOptions: {
  92. sass: {
  93. prependData: '@import "@/style/_mixin.scss";@import "@/style/_variables.scss";@import "@/style/base.scss";@import "@/style/common.scss";' // 全局引入
  94. },
  95. postcss: {
  96. plugins: [
  97. autoprefixer(),
  98. pxtoviewport(({
  99. unitToConvert: 'px',
  100. viewportWidth: 375,
  101. unitPrecision: 5,
  102. propList: [
  103. '*'
  104. ],
  105. viewportUnit: 'vw',
  106. fontViewportUnit: 'vw',
  107. selectorBlackList: [],
  108. minPixelValue: 1,
  109. mediaQuery: false,
  110. replace: true,
  111. exclude: /(\/|\\)(node_modules)(\/|\\)/
  112. }))
  113. // pxtorem({
  114. // rootValue: 37.5,
  115. // propList: ['*']
  116. // })
  117. ]
  118. }
  119. }
  120. },
  121. chainWebpack: config => {
  122. // 防止多页面打包卡顿
  123. // eslint-disable-next-line no-unused-expressions
  124. // config.plugins.delete('named-chunks')
  125. if (process.env.NODE_ENV === 'production') {
  126. // 打包时需要执行,开发环境运行不需要执行
  127. config.externals(externals)
  128. // 如果使用多页面打包,使用vue inspect --plugins查看html是否在结果数组中
  129. config.plugin('html').tap(args => {
  130. // html中添加cdn
  131. args[0].cdn = cdn
  132. return args
  133. })
  134. } else {
  135. config.plugin('html').tap(args => {
  136. // html中添加wxjsssk的cdn
  137. args[0].cdn = {
  138. js: [
  139. cdn.js[cdn.js.length - 1]
  140. ]
  141. }
  142. return args
  143. })
  144. }
  145. // 分析静态资源
  146. if (process.env.use_analyzer) {
  147. config
  148. .plugin('webpack-bundle-analyzer')
  149. .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  150. }
  151. // 修复HMR
  152. config.resolve.symlinks(true)
  153. config.module
  154. .rule('ts')
  155. .use('ts-loader')
  156. .tap(options => {
  157. options = merge(options, {
  158. transpileOnly: true,
  159. getCustomTransformers: () => ({
  160. before: [
  161. tsImportPluginFactory({
  162. libraryName: 'vant',
  163. libraryDirectory: 'es',
  164. style: true
  165. })
  166. ]
  167. }),
  168. compilerOptions: {
  169. module: 'es2015'
  170. }
  171. })
  172. return options
  173. })
  174. },
  175. configureWebpack: {
  176. // 修改打包后js文件名
  177. output: {
  178. filename: `js/[name].js?v=${s_version}`,
  179. chunkFilename: `js/[name].js?v=${s_version}`
  180. }
  181. }
  182. }