vue.config.js 3.8 KB

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