vite.config.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { fileURLToPath, URL } from 'node:url'
  2. import { defineConfig, loadEnv } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import viteImagemin from 'vite-plugin-imagemin'
  5. import { VitePWA } from 'vite-plugin-pwa'
  6. import legacy from '@vitejs/plugin-legacy'
  7. import tailwindcss from 'tailwindcss'
  8. import autoprefixer from 'autoprefixer'
  9. import archiver from 'archiver'
  10. import fs from 'fs'
  11. import OSS from 'ali-oss'
  12. import { format } from 'date-fns'
  13. import { Buffer } from 'node:buffer'
  14. // https://vitejs.dev/config/
  15. export default defineConfig(({ command, mode }) => {
  16. process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }
  17. const plugins = [
  18. {
  19. name: 'build-index',
  20. async buildStart(options) {
  21. if (mode !== 'app') return
  22. const meta = JSON.parse(fs.readFileSync('./src/plugins/updater_meta.json'))
  23. meta.version++
  24. fs.writeFileSync('./src/plugins/updater_meta.json', JSON.stringify(meta))
  25. },
  26. async closeBundle(options) {
  27. if (mode !== 'app') return
  28. const meta = JSON.parse(fs.readFileSync('./src/plugins/updater_meta.json'))
  29. const sourceDir = 'dist'
  30. const outputFilePath = 'dist.zip'
  31. await new Promise((resolve, reject) => {
  32. const output = fs.createWriteStream(outputFilePath)
  33. output.on('close', function () {
  34. resolve()
  35. })
  36. const archive = archiver('zip')
  37. archive.pipe(output)
  38. archive.directory(sourceDir, false)
  39. archive.finalize()
  40. }).catch(e => {
  41. console.log(e)
  42. })
  43. let client = new OSS({
  44. accessKeyId: 'PXzJyah5rZfWHIIH',
  45. accessKeySecret: 'e1MS6j0wypXJrw8CM0hObZu8qKbfah',
  46. region: 'oss-cn-hangzhou',
  47. bucket: 'zm-shorts'
  48. })
  49. let { url } = await client.put(`packages/${format(new Date(), 'yyyyMMddHHmmss')}.zip`, outputFilePath, {
  50. headers: {
  51. 'x-oss-object-acl': 'public-read'
  52. }
  53. })
  54. url = url.replace('http://', 'https://')
  55. console.log('package uploaded: %s', url.replace('http://', 'https://'))
  56. await client.put('packages/meta.json', Buffer.from(JSON.stringify({ url, version: meta.version })), {
  57. headers: { 'x-oss-object-acl': 'public-read' }
  58. })
  59. }
  60. },
  61. vue(),
  62. viteImagemin({
  63. gifsicle: {
  64. optimizationLevel: 7,
  65. interlaced: false
  66. },
  67. optipng: false,
  68. mozjpeg: {
  69. quality: 80
  70. },
  71. pngquant: {
  72. quality: [0.5, 0.9],
  73. speed: 1
  74. },
  75. svgo: {
  76. plugins: [
  77. {
  78. name: 'removeViewBox'
  79. },
  80. {
  81. name: 'removeEmptyAttrs',
  82. active: false
  83. }
  84. ]
  85. },
  86. webp: false
  87. }),
  88. // For production build environments only
  89. legacy({
  90. targets: ['chrome >= 64', 'edge >= 79', 'safari >= 11.1', 'firefox >= 67'],
  91. ignoreBrowserslistConfig: true,
  92. renderLegacyChunks: false,
  93. /**
  94. * Polyfills required by modern browsers
  95. *
  96. * Since some low-version modern browsers do not support the new syntax
  97. * You need to load polyfills corresponding to the syntax to be compatible
  98. * At build, all required polyfills are packaged according to the target browser version range
  99. * But when the page is accessed, only the required part is loaded depending on the browser version
  100. *
  101. * Two configuration methods:
  102. *
  103. * 1. true
  104. * - Automatically load all required polyfills based on the target browser version range
  105. * - Demerit: will introduce polyfills that are not needed by modern browsers in higher versions,
  106. * as well as more aggressive polyfills.
  107. *
  108. * 2、string[]
  109. * - Add low-version browser polyfills as needed
  110. * - Demerit: It needs to be added manually, which is inflexible;
  111. * it will be discovered after the production is deployed, resulting in production failure! ! !
  112. */
  113. modernPolyfills: ['es/global-this']
  114. // or
  115. // modernPolyfills: true,
  116. })
  117. ]
  118. return {
  119. base: process.env.VITE_BASE_URL,
  120. server: {
  121. host: '0.0.0.0',
  122. port: 3000,
  123. fs: {
  124. strict: false
  125. }
  126. },
  127. plugins,
  128. resolve: {
  129. alias: {
  130. '@': fileURLToPath(new URL('./src', import.meta.url))
  131. }
  132. },
  133. css: {
  134. postcss: {
  135. plugins: [tailwindcss, autoprefixer]
  136. },
  137. preprocessorOptions: {
  138. less: {
  139. javascriptEnabled: true,
  140. additionalData: '@import "@/styles/common.less";'
  141. }
  142. }
  143. },
  144. logLevel: mode === 'app' || mode === 'test' ? 'error' : 'info'
  145. }
  146. })