vite.config.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { fileURLToPath } from 'node:url';
  2. import { defineConfig } from 'vite';
  3. import { svelte } from '@sveltejs/vite-plugin-svelte';
  4. // The viewer is emitted straight into the engine's `dist/` tree so it ships
  5. // with everything else: `build-bundle.sh` copies `dist` wholesale and
  6. // `pack-npm.sh` packs that bundle, so nothing extra has to be taught about it.
  7. //
  8. // NOT `dist/ui` — that name is already taken. `src/ui/` is the engine's
  9. // TERMINAL ui (shimmer progress + its worker) and tsc compiles it to
  10. // `dist/ui/`, so emitting here would both clobber it (emptyOutDir) and, worse,
  11. // leave the CLI serving compiled engine internals as static files.
  12. //
  13. // `fileURLToPath` (not a bare '../dist/viewer') keeps this a native path on
  14. // Windows, where Rollup resolves outDir against the platform separator.
  15. const outDir = fileURLToPath(new URL('../dist/viewer', import.meta.url));
  16. export default defineConfig(({ command }) => {
  17. // `vite build` does NOT override an ambient NODE_ENV, and Svelte compiles in
  18. // dev mode when it sees one — a shell (or a CI runner) with
  19. // NODE_ENV=development silently ships a viewer carrying Svelte's dev-only
  20. // runtime checks: ~13 kB larger, slower, and warning in the user's console.
  21. // A release artifact must not depend on the machine that built it.
  22. if (command === 'build') process.env.NODE_ENV = 'production';
  23. return {
  24. plugins: [svelte()],
  25. // Relative asset URLs: the CLI serves this at '/', but a relative base also
  26. // survives being opened from the filesystem or mounted under a sub-path.
  27. base: './',
  28. build: {
  29. // Scoped to dist/viewer — `emptyOutDir` must never be allowed to widen
  30. // to dist/, which holds the compiled engine tsc wrote moments earlier.
  31. outDir,
  32. emptyOutDir: true,
  33. target: 'es2022',
  34. // A localhost reader has the sources on disk already; sourcemaps would
  35. // double the bundle in every platform archive for no one's benefit.
  36. sourcemap: false,
  37. chunkSizeWarningLimit: 1024,
  38. },
  39. server: {
  40. host: '127.0.0.1',
  41. port: 5174,
  42. },
  43. };
  44. });