vue.config.js 4.9 KB

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