vite-plugin-config.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import path from 'path';
  2. import { ConfigEnv, Plugin, UserConfig } from 'vite';
  3. export function viteConfigPlugin({ packageRoot }: { packageRoot: string }): Plugin {
  4. return {
  5. name: 'vendure:vite-config-plugin',
  6. config: (config: UserConfig, env: ConfigEnv) => {
  7. // Only set the vite `root` to the dashboard package when running the dev server.
  8. // During a production build we still need to reference the dashboard source which
  9. // lives in `node_modules`, but we don't want the build output to be emitted in there.
  10. // Therefore, we set `root` only for `serve` and, for `build`, we instead make sure that
  11. // an `outDir` **outside** of `node_modules` is used (defaulting to the current working
  12. // directory if the user did not provide one already).
  13. config.root = packageRoot;
  14. config.publicDir = config.publicDir ?? path.join(packageRoot, 'public');
  15. // If we are building and no explicit outDir has been provided (or it is a relative path),
  16. // set it to an **absolute** path relative to the cwd so that the output never ends up in
  17. // `node_modules`.
  18. if (env.command === 'build') {
  19. const buildConfig = config.build ?? {};
  20. const outDir = buildConfig.outDir;
  21. const hasOutDir = typeof outDir === 'string' && outDir.length > 0;
  22. const outDirIsAbsolute = hasOutDir && path.isAbsolute(outDir);
  23. const normalizedOutDir = hasOutDir
  24. ? outDirIsAbsolute
  25. ? outDir
  26. : path.resolve(process.cwd(), outDir)
  27. : path.resolve(process.cwd(), 'dist');
  28. config.build = {
  29. ...buildConfig,
  30. outDir: normalizedOutDir,
  31. };
  32. }
  33. config.resolve = {
  34. alias: {
  35. ...(config.resolve?.alias ?? {}),
  36. // See the readme for an explanation of this alias.
  37. '@/vdb': path.resolve(packageRoot, './src/lib'),
  38. '@/graphql': path.resolve(packageRoot, './src/lib/graphql'),
  39. },
  40. };
  41. // This is required to prevent Vite from pre-bundling the
  42. // dashboard source when it resides in node_modules.
  43. config.optimizeDeps = {
  44. ...config.optimizeDeps,
  45. exclude: [
  46. ...(config.optimizeDeps?.exclude || []),
  47. '@vendure/dashboard',
  48. '@/vdb',
  49. 'virtual:vendure-ui-config',
  50. 'virtual:admin-api-schema',
  51. 'virtual:dashboard-extensions',
  52. ],
  53. // We however do want to pre-bundle recharts, as it depends
  54. // on lodash which is a CJS packages and _does_ require
  55. // pre-bundling.
  56. include: [
  57. ...(config.optimizeDeps?.include || []),
  58. '@/components > recharts',
  59. '@/components > react-dropzone',
  60. '@/components > @tiptap/react', // https://github.com/fawmi/vue-google-maps/issues/148#issuecomment-1235143844
  61. '@vendure/common/lib/generated-types',
  62. '@radix-ui/react-portal > react-dom',
  63. '@messageformat/parser',
  64. ],
  65. };
  66. return config;
  67. },
  68. };
  69. }