vue.config.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.bootcss.com/axios/0.19.2/axios.min.js',
  28. '//cdn.bootcss.com/js-cookie/2.2.1/js.cookie.min.js',
  29. '//cdn.bootcdn.net/ajax/libs/moment.js/2.24.0/moment.min.js',
  30. '//cdn.bootcss.com/vue/2.6.11/vue.min.js',
  31. '//cdn.jsdelivr.net/npm/vue-router@3.1.5/dist/vue-router.min.js',
  32. '//cdn.jsdelivr.net/npm/vuex@3.4.0/dist/vuex.min.js',
  33. '//cdn.jsdelivr.net/npm/vant@2.8.2/lib/vant.min.js',
  34. '//res.wx.qq.com/open/js/jweixin-1.6.0.js'
  35. ]
  36. }
  37. module.exports = {
  38. publicPath: process.env.BASE_URL,
  39. parallel: false,
  40. productionSourceMap: false,
  41. devServer: {
  42. proxy: {
  43. '^/testServer': {
  44. target: 'http://192.168.20.241:92/',
  45. // target: 'http://app2-jytest.jianyu360.cn',
  46. changeOrigin: true,
  47. logLevel: 'debug',
  48. pathRewrite: {
  49. '/testServer': ''
  50. }
  51. }
  52. }
  53. },
  54. css: {
  55. loaderOptions: {
  56. sass: {
  57. prependData: '@import "@/style/_mixin.scss";@import "@/style/_variables.scss";@import "@/style/base.scss";@import "@/style/common.scss";' // 全局引入
  58. },
  59. postcss: {
  60. plugins: [
  61. autoprefixer(),
  62. pxtoviewport(({
  63. unitToConvert: 'px',
  64. viewportWidth: 375,
  65. unitPrecision: 5,
  66. propList: [
  67. '*'
  68. ],
  69. viewportUnit: 'vw',
  70. fontViewportUnit: 'vw',
  71. selectorBlackList: [],
  72. minPixelValue: 1,
  73. mediaQuery: false,
  74. replace: true,
  75. exclude: /(\/|\\)(node_modules)(\/|\\)/
  76. }))
  77. // pxtorem({
  78. // rootValue: 37.5,
  79. // propList: ['*']
  80. // })
  81. ]
  82. }
  83. }
  84. },
  85. chainWebpack: config => {
  86. // 防止多页面打包卡顿
  87. // eslint-disable-next-line no-unused-expressions
  88. // config.plugins.delete('named-chunks')
  89. if (process.env.NODE_ENV === 'production') {
  90. // 打包时需要执行,开发环境运行不需要执行
  91. config.externals(externals)
  92. // 如果使用多页面打包,使用vue inspect --plugins查看html是否在结果数组中
  93. config.plugin('html').tap(args => {
  94. // html中添加cdn
  95. args[0].cdn = cdn
  96. // console.log(JSON.stringify(args))
  97. // 修复 Lazy loading routes Error
  98. // args[0].chunksSortMode = 'none'
  99. return args
  100. })
  101. } else {
  102. config.plugin('html').tap(args => {
  103. // html中添加wxjsssk的cdn
  104. args[0].cdn = {
  105. js: [
  106. cdn.js[cdn.js.length - 1]
  107. ]
  108. }
  109. return args
  110. })
  111. }
  112. // 分析静态资源
  113. if (process.env.use_analyzer) {
  114. config
  115. .plugin('webpack-bundle-analyzer')
  116. .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  117. }
  118. // 修复HMR
  119. config.resolve.symlinks(true)
  120. config.module
  121. .rule('ts')
  122. .use('ts-loader')
  123. .tap(options => {
  124. options = merge(options, {
  125. transpileOnly: true,
  126. getCustomTransformers: () => ({
  127. before: [
  128. tsImportPluginFactory({
  129. libraryName: 'vant',
  130. libraryDirectory: 'es',
  131. style: true
  132. })
  133. ]
  134. }),
  135. compilerOptions: {
  136. module: 'es2015'
  137. }
  138. })
  139. return options
  140. })
  141. // return config
  142. }
  143. }