vue.config.js 4.5 KB

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