vue.config.js 4.9 KB

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