withPageConfig.mjs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { defineConfig } from 'vite';
  2. import { watchRebuildPlugin } from '@extension/hmr';
  3. import react from '@vitejs/plugin-react-swc';
  4. import deepmerge from 'deepmerge';
  5. import { isDev, isProduction } from './env.mjs';
  6. export const watchOption = isDev ? {
  7. buildDelay: 100,
  8. chokidar: {
  9. ignored:[
  10. /\/packages\/.*\.(ts|tsx|map)$/,
  11. ]
  12. }
  13. }: undefined;
  14. /**
  15. * @typedef {import('vite').UserConfig} UserConfig
  16. * @param {UserConfig} config
  17. * @returns {UserConfig}
  18. */
  19. export function withPageConfig(config) {
  20. return defineConfig(
  21. deepmerge(
  22. {
  23. base: '',
  24. plugins: [react(), isDev && watchRebuildPlugin({ refresh: true })],
  25. build: {
  26. sourcemap: isDev,
  27. minify: isProduction,
  28. reportCompressedSize: isProduction,
  29. emptyOutDir: isProduction,
  30. watch: watchOption,
  31. rollupOptions: {
  32. external: ['chrome'],
  33. },
  34. },
  35. define: {
  36. 'process.env.NODE_ENV': isDev ? `"development"` : `"production"`,
  37. },
  38. envDir: '../..'
  39. },
  40. config,
  41. ),
  42. );
  43. }