ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/shtukbase-ui-react/test/setup.ts

test/setup.ts

41 líneas, 2.0 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // Test preload (bunfig.toml): where `react`, `react-dom/server`, `radix-ui` and the other npm packages of the components
  2. // come from. The package has no npm dependencies and the repository has no node_modules, so the tests carry minified
  3. // bundles of the few packages they need (test/vendor, built by tools/vendor.ts). When the packages ARE installed (a
  4. // project that placed the library, or a `bun install` in the package), the installed ones are used and nothing is mapped.
  5. import { existsSync } from 'node:fs';
  6. import { dirname, join } from 'node:path';
  7. const VENDOR = join(import.meta.dir, 'vendor');
  8. /** specifier of the library or the test -> vendored bundle */
  9. const MAP: Record<string, string> = {
  10. react: 'react.entry.js',
  11. 'react/jsx-runtime': 'react-jsx-runtime.entry.js',
  12. 'react/jsx-dev-runtime': 'react-jsx-runtime.entry.js',
  13. 'react-dom/server': 'react-dom-server.entry.js',
  14. 'radix-ui': 'radix-ui.entry.js',
  15. 'class-variance-authority': 'class-variance-authority.entry.js',
  16. cn: 'cn.entry.js',
  17. 'lucide-react': 'lucide-react.entry.js',
  18. };
  19. /** Installed means a node_modules folder up the tree holds it (Bun alone would also find it in its global cache and auto-install). */
  20. const installed = (spec: string): boolean => {
  21. const name = spec.startsWith('@') ? spec.split('/').slice(0, 2).join('/') : spec.split('/')[0];
  22. for (let dir = import.meta.dir; ; dir = dirname(dir)) {
  23. if (existsSync(join(dir, 'node_modules', name))) return true;
  24. if (dirname(dir) === dir) return false;
  25. }
  26. };
  27. /** True when the tests run on the vendored copies (nothing installed), false on installed packages. */
  28. export const VENDORED = !Object.keys(MAP).every(installed);
  29. if (VENDORED) {
  30. Bun.plugin({
  31. name: 'vendored-npm',
  32. setup(build) {
  33. // virtual modules: Bun reads their named exports from the bundle
  34. for (const [spec, file] of Object.entries(MAP)) build.module(spec, async () => ({ exports: await import(join(VENDOR, file)), loader: 'object' }));
  35. },
  36. });
  37. }