vue.config.js 4.7 KB

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