vite.config.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import url from 'node:url';
  2. import path from 'node:path';
  3. import fs from 'node:fs';
  4. import { defineConfig } from 'vitest/config';
  5. import react from '@vitejs/plugin-react';
  6. import wasm from 'vite-plugin-wasm';
  7. import replace from '@rollup/plugin-replace';
  8. import topLevelAwait from 'vite-plugin-top-level-await';
  9. import { VitePWA } from 'vite-plugin-pwa';
  10. import { tryCommand } from './support/command';
  11. const projectRoot = url.fileURLToPath(new URL('.', import.meta.url));
  12. const pkg = JSON.parse(fs.readFileSync(projectRoot + '/package.json', 'utf-8'));
  13. const COMMAND_GIT_VERSION = 'git describe --long --dirty --tags --always';
  14. const shortCommit = tryCommand(COMMAND_GIT_VERSION, __dirname, 'unknown');
  15. const version = `${pkg.version}-${shortCommit}`;
  16. // https://vitejs.dev/config/
  17. export default defineConfig({
  18. worker: {
  19. format: 'es',
  20. },
  21. server: {
  22. fs: {
  23. // Note:
  24. // This is _insecure_, but is required to get pnpm link to work.
  25. // strict: false,
  26. allow: [
  27. 'src',
  28. 'node_modules',
  29. // Allow pnpm to link.
  30. process.env.LIB_UM_WASM_LOADER_DIR || '../lib_um_crypto_rust/um_wasm_loader',
  31. ],
  32. },
  33. },
  34. base: './',
  35. optimizeDeps: {
  36. exclude: ['@unlock-music/crypto', 'sql.js'],
  37. },
  38. plugins: [
  39. replace({
  40. preventAssignment: true,
  41. values: {
  42. __APP_VERSION_SHORT__: pkg.version,
  43. __APP_VERSION__: version,
  44. },
  45. }),
  46. react(),
  47. wasm(),
  48. topLevelAwait(),
  49. VitePWA({
  50. registerType: 'prompt',
  51. workbox: {
  52. // Cache everything from dist
  53. globPatterns: ['**/*.{js,css,html,ico,png,svg,wasm,webp}'],
  54. },
  55. manifest: {
  56. display: 'standalone',
  57. name: '音乐解锁 (Unlock Music)',
  58. short_name: '音乐解锁',
  59. lang: 'zh-cmn-Hans-CN',
  60. description: '在现代浏览器解锁已购的加密音乐!',
  61. theme_color: '#ffffff',
  62. icons: [
  63. {
  64. src: 'pwa-192x192.png',
  65. sizes: '192x192',
  66. type: 'image/png',
  67. purpose: 'maskable',
  68. },
  69. {
  70. src: 'pwa-512x512.png',
  71. sizes: '512x512',
  72. type: 'image/png',
  73. },
  74. ],
  75. },
  76. }),
  77. ],
  78. resolve: {
  79. alias: {
  80. '~': path.resolve(__dirname, 'src'),
  81. '@nm': path.resolve(__dirname, 'node_modules'),
  82. // workaround for vite, workbox (PWA)
  83. module: path.resolve(__dirname, 'src', 'dummy.mjs'),
  84. },
  85. },
  86. build: {
  87. rollupOptions: {
  88. output: {
  89. manualChunks: {
  90. reacts: ['react', 'react-dom', 'react-dropzone', 'react-promise-suspense', 'react-redux', '@reduxjs/toolkit'],
  91. chakra: ['@chakra-ui/react', '@emotion/react', '@emotion/styled', 'framer-motion'],
  92. icons: ['react-icons', '@chakra-ui/icons'],
  93. utility: ['radash', 'nanoid', 'react-syntax-highlighter'],
  94. },
  95. },
  96. },
  97. },
  98. test: {
  99. globals: true,
  100. mockReset: true,
  101. environment: 'jsdom',
  102. setupFiles: ['src/test-utils/setup-jest.ts'],
  103. // workaround: sql.js is not ESModule friendly, yet...
  104. deps: {
  105. optimizer: {
  106. web: {
  107. include: ['sql.js'],
  108. },
  109. },
  110. },
  111. api: {
  112. port: 5174, // vite port + 1
  113. },
  114. coverage: {
  115. provider: 'v8',
  116. exclude: [
  117. // default rules
  118. 'coverage/**',
  119. 'dist/**',
  120. 'packages/*/test{,s}/**',
  121. '**/*.d.ts',
  122. 'cypress/**',
  123. 'test{,s}/**',
  124. 'test{,-*}.{js,cjs,mjs,ts,tsx,jsx}',
  125. '**/*{.,-}test.{js,cjs,mjs,ts,tsx,jsx}',
  126. '**/*{.,-}spec.{js,cjs,mjs,ts,tsx,jsx}',
  127. '**/__tests__/**',
  128. '**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*',
  129. '**/.{eslint,mocha,prettier}rc.{js,cjs,yml}',
  130. // custom ones
  131. 'src/test-utils/**',
  132. ],
  133. },
  134. },
  135. });