vue.config.js 4.7 KB

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