vue.config.js 4.4 KB

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