vue.config.js 5.4 KB

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