All files / repo-tools/src/vite-plugins/bundle-vats index.ts

100% Statements 8/8
100% Branches 0/0
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                        2x       1x 2x         1x   2x 2x       1x 2x                  
import type { Plugin } from 'vite';
 
import { bundleVat } from './bundle-vat.ts';
 
export { bundleVat } from './bundle-vat.ts';
export type { VatBundle } from './bundle-vat.ts';
 
type VatEntry = {
  /** Absolute path to the vat source file */
  source: string;
  /** Output path relative to build outDir (e.g., 'echo/echo-caplet.bundle') */
  output: string;
};
 
type BundleVatsOptions = {
  vats: VatEntry[];
};
 
/**
 * Vite plugin that bundles vat source files as part of the build pipeline.
 *
 * Registers vat sources for watch mode and emits bundled assets during
 * `generateBundle`.
 *
 * @param options - Plugin options specifying which vats to bundle.
 * @returns A Vite plugin.
 */
export function bundleVats(options: BundleVatsOptions): Plugin {
  return {
    name: 'ocap-kernel:bundle-vats',
 
    buildStart() {
      for (const vat of options.vats) {
        this.addWatchFile(vat.source);
      }
    },
 
    async generateBundle() {
      const results = await Promise.all(
        options.vats.map(async (vat) => {
          const bundle = await bundleVat(vat.source);
          return { fileName: vat.output, bundle };
        }),
      );
 
      for (const { fileName, bundle } of results) {
        this.emitFile({
          type: 'asset',
          fileName,
          source: JSON.stringify(bundle),
        });
      }
    },
  };
}