vue.config.js 4.8 KB

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