Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a765ca7
build: Support bundling with vite
grypez Jan 22, 2026
ff56480
build: Remove @endo/import-bundle support
grypez Jan 22, 2026
d8b0178
respin yarn
grypez Jan 22, 2026
dd54c25
fix test mock
grypez Jan 22, 2026
7d1ffcd
Apply some suggestions from code review
grypez Jan 22, 2026
69039b0
address remaining review comments
grypez Jan 23, 2026
46caccd
small fixes
grypez Jan 23, 2026
0fa7ea3
add isVatBundle to index test
grypez Jan 23, 2026
20165fa
build: fix bundle import scrubber (#772)
grypez Jan 23, 2026
ef51f72
repro(4:1): isVatBundle type guard missing property validation
grypez Jan 26, 2026
1802892
repro(2:2): loadBundle missing code property validation
grypez Jan 26, 2026
8390606
repro(4:2): stripCommentsPlugin nonstrict parsing
grypez Jan 26, 2026
ca925e5
fix(2:2): validate code property in loadBundle
grypez Jan 26, 2026
9ca966b
fix(4:1): validate all VatBundle properties in isVatBundle
grypez Jan 26, 2026
1da5def
fix(4:2): use script sourceType for IIFE bundle parsing
grypez Jan 26, 2026
0d4fbdf
fix(5:4): remove redundant assert from loadBundle
grypez Jan 28, 2026
4f7d795
fix(5:1): remove unused VatBundle re-export
grypez Jan 28, 2026
0aba19e
fix(5:2,5:3): remove unused BundleMetadata export and modules metadata
grypez Jan 28, 2026
c4ba419
fix: update test.bundle fixture to use external field
grypez Jan 28, 2026
685b9af
fix(6:2): remove unnecessary VatBundle type cast
grypez Jan 28, 2026
2082e18
fix(7:1): use external field in test bundle mocks
grypez Jan 29, 2026
831a2dc
fix(7:2): remove unnecessary export from LoadBundleOptions
grypez Jan 29, 2026
c0cf139
fix(8:1,8:2): use superstruct for VatBundle and fix plugin types
grypez Jan 29, 2026
acce346
repro(8:1): add tests for null/non-object bundle content
grypez Jan 29, 2026
4d3ef97
fix(8:1): add null check validation to loadBundle
grypez Jan 29, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Ocap Kernel cli.

### `ocap bundle <targets..>`

Bundle the supplied file or directory targets. Expects each target to be a `.js` file or a directory containing `.js` files. Each `<file>.js` file will be bundled using `@endo/bundle-source` and written to an associated `<file>.bundle`.
Bundle the supplied file or directory targets. Expects each target to be a `.js` file or a directory containing `.js` files. Each `<file>.js` file will be bundled using `vite` and written to an associated `<file>.bundle`.

### `ocap watch <dir>`

Expand Down
5 changes: 2 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
"dependencies": {
"@chainsafe/libp2p-noise": "^16.1.3",
"@chainsafe/libp2p-yamux": "patch:@chainsafe/libp2p-yamux@npm%3A7.0.4#~/.yarn/patches/@chainsafe-libp2p-yamux-npm-7.0.4-284c2f6812.patch",
"@endo/bundle-source": "^4.1.2",
"@endo/init": "^1.1.12",
"@endo/promise-kit": "^1.1.13",
"@libp2p/autonat": "2.0.38",
"@libp2p/circuit-relay-v2": "3.2.24",
Expand All @@ -53,10 +51,12 @@
"@metamask/snaps-utils": "^11.7.1",
"@metamask/utils": "^11.9.0",
"@types/node": "^22.13.1",
"acorn": "^8.15.0",
"chokidar": "^4.0.1",
"glob": "^11.0.0",
"libp2p": "2.10.0",
"serve-handler": "^6.1.6",
"vite": "^7.3.0",
"yargs": "^17.7.2"
},
"devDependencies": {
Expand Down Expand Up @@ -91,7 +91,6 @@
"typedoc": "^0.28.1",
"typescript": "~5.8.2",
"typescript-eslint": "^8.29.0",
"vite": "^7.3.0",
"vitest": "^4.0.16"
},
"engines": {
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import '@endo/init';

import '@metamask/kernel-shims/endoify';
import { Logger } from '@metamask/logger';
import path from 'node:path';
import yargs from 'yargs';
Expand Down
30 changes: 18 additions & 12 deletions packages/cli/src/commands/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { fileExists } from '../file.ts';

const mocks = vi.hoisted(() => {
return {
endoBundleSource: vi.fn(),
bundleVat: vi.fn(),
Logger: vi.fn(
() =>
({
Expand All @@ -25,12 +25,10 @@ const mocks = vi.hoisted(() => {
};
});

vi.mock('@endo/bundle-source', () => ({
default: mocks.endoBundleSource,
vi.mock('../vite/vat-bundler.ts', () => ({
bundleVat: mocks.bundleVat,
}));

vi.mock('@endo/init', () => ({}));

vi.mock('@metamask/logger', () => ({
Logger: mocks.Logger,
}));
Expand Down Expand Up @@ -68,8 +66,13 @@ describe('bundle', async () => {
async ({ source, bundle }) => {
expect(await fileExists(bundle)).toBe(false);

const testContent = { source: 'test-content' };
mocks.endoBundleSource.mockImplementationOnce(() => testContent);
const testContent = {
moduleFormat: 'iife',
code: 'test-code',
exports: [],
external: [],
};
mocks.bundleVat.mockImplementationOnce(() => testContent);

await bundleFile(source, { logger });

Expand All @@ -84,7 +87,7 @@ describe('bundle', async () => {
);

it('throws if bundling fails', async () => {
mocks.endoBundleSource.mockImplementationOnce(() => {
mocks.bundleVat.mockImplementationOnce(() => {
throw new Error('test error');
});
await expect(
Expand All @@ -97,9 +100,12 @@ describe('bundle', async () => {
it('bundles a directory', async () => {
expect(await globBundles()).toStrictEqual([]);

mocks.endoBundleSource.mockImplementation(() => {
return 'test content';
});
mocks.bundleVat.mockImplementation(() => ({
moduleFormat: 'iife',
code: 'test content',
exports: [],
external: [],
}));

await bundleDir(testBundleRoot, { logger });

Expand All @@ -111,7 +117,7 @@ describe('bundle', async () => {
});

it('throws if bundling fails', async () => {
mocks.endoBundleSource.mockImplementationOnce(() => {
mocks.bundleVat.mockImplementationOnce(() => {
throw new Error('test error');
});
await expect(bundleDir(testBundleRoot, { logger })).rejects.toThrow(
Expand Down
7 changes: 3 additions & 4 deletions packages/cli/src/commands/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import '@endo/init';
import endoBundleSource from '@endo/bundle-source';
import { Logger } from '@metamask/logger';
import type { Logger } from '@metamask/logger';
import { glob } from 'glob';
import { writeFile } from 'node:fs/promises';
import { resolve, join } from 'node:path';

import { isDirectory } from '../file.ts';
import { resolveBundlePath } from '../path.ts';
import { bundleVat } from '../vite/vat-bundler.ts';

type BundleFileOptions = {
logger: Logger;
Expand All @@ -30,7 +29,7 @@ export async function bundleFile(
const { logger, targetPath } = options;
const sourceFullPath = resolve(sourcePath);
const bundlePath = targetPath ?? resolveBundlePath(sourceFullPath);
const bundle = await endoBundleSource(sourceFullPath);
const bundle = await bundleVat(sourceFullPath);
const bundleContent = JSON.stringify(bundle);
await writeFile(bundlePath, bundleContent);
logger.info(`Wrote ${bundlePath}: ${new Blob([bundleContent]).size} bytes`);
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/commands/watch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { makePromiseKit } from '@endo/promise-kit';
import type { PromiseKit } from '@endo/promise-kit';
import { Logger } from '@metamask/logger';
import { watch } from 'chokidar';
import type { FSWatcher, MatchFunction } from 'chokidar';
Expand All @@ -17,8 +18,8 @@ type WatchDirReturn = {

export const makeWatchEvents = (
watcher: FSWatcher,
readyResolve: ReturnType<typeof makePromiseKit<CloseWatcher>>['resolve'],
throwError: ReturnType<typeof makePromiseKit<never>>['reject'],
readyResolve: PromiseKit<CloseWatcher>['resolve'],
throwError: PromiseKit<never>['reject'],
logger: Logger,
): {
ready: () => void;
Expand Down
32 changes: 32 additions & 0 deletions packages/cli/src/vite/export-metadata-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Plugin } from 'vite';

type BundleMetadata = {
exports: string[];
external: string[];
};

/**
* Rollup plugin that captures export metadata from the bundle.
*
* Uses the `generateBundle` hook to extract the exports array from the
* entry chunk.
*
* @returns A plugin with an additional `getMetadata()` method.
*/
export function exportMetadataPlugin(): Plugin & {
getMetadata: () => BundleMetadata;
} {
const metadata: BundleMetadata = { exports: [], external: [] };

return {
name: 'export-metadata',
generateBundle(_, bundle) {
for (const chunk of Object.values(bundle)) {
if (chunk.type === 'chunk' && chunk.isEntry) {
metadata.exports = chunk.exports;
}
}
},
getMetadata: () => metadata,
};
}
69 changes: 69 additions & 0 deletions packages/cli/src/vite/strip-comments-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { describe, it, expect } from 'vitest';

import { stripCommentsPlugin } from './strip-comments-plugin.ts';

describe('stripCommentsPlugin', () => {
describe('parsing non-strict-mode code', () => {
const plugin = stripCommentsPlugin();
const renderChunk = plugin.renderChunk as (code: string) => string | null;

it('handles octal literals in bundled IIFE code', () => {
// Octal literals like 010 are valid in non-strict mode (scripts)
// but invalid in strict mode (modules). IIFE bundles are scripts.
const iifeWithOctal = '(function() { var x = 010; /* comment */ })();';
expect(() => renderChunk(iifeWithOctal)).not.toThrow();
});

it('handles with statements in bundled IIFE code', () => {
// 'with' statements are valid in non-strict mode (scripts)
// but invalid in strict mode (modules). IIFE bundles are scripts.
const iifeWithWith = '(function() { with(obj) { /* comment */ x; } })();';
expect(() => renderChunk(iifeWithWith)).not.toThrow();
});
});
const plugin = stripCommentsPlugin();
const renderChunk = plugin.renderChunk as (code: string) => string | null;

it.each([
[
'single-line comment',
'const x = 1; // comment\nconst y = 2;',
'const x = 1; \nconst y = 2;',
],
[
'multi-line comment',
'const x = 1; /* comment */ const y = 2;',
'const x = 1; const y = 2;',
],
[
'multiple comments',
'/* a */ const x = 1; // b\n/* c */',
' const x = 1; \n',
],
[
'comment containing import()',
'const x = 1; // import("module")\nconst y = 2;',
'const x = 1; \nconst y = 2;',
],
[
'comment with string content preserved',
'const x = "// in string"; // real comment',
'const x = "// in string"; ',
],
['code that is only a comment', '// just a comment', ''],
])('removes %s', (_name, code, expected) => {
expect(renderChunk(code)).toBe(expected);
});

it.each([
['string with // pattern', 'const x = "// not a comment";'],
['string with /* */ pattern', 'const x = "/* not a comment */";'],
['regex literal like //', 'const re = /\\/\\//;'],
['template literal with // pattern', 'const x = `// not a comment`;'],
['nested quotes in string', 'const x = "a \\"// not comment\\" b";'],
['no comments', 'const x = 1;'],
['empty code', ''],
])('returns null for %s', (_name, code) => {
expect(renderChunk(code)).toBeNull();
});
});
46 changes: 46 additions & 0 deletions packages/cli/src/vite/strip-comments-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { Comment } from 'acorn';
import { parse } from 'acorn';
import type { Plugin } from 'vite';

/**
* Rollup plugin that strips comments from bundled code using AST parsing.
*
* SES rejects code containing `import(` patterns, even when they appear
* in comments. This plugin uses Acorn to definitively identify comment nodes
* and removes them to avoid triggering that detection.
*
* Uses the `renderChunk` hook to process the final output.
*
* @returns A Rollup plugin.
*/
export function stripCommentsPlugin(): Plugin {
return {
name: 'strip-comments',
renderChunk(code) {
const comments: Comment[] = [];

parse(code, {
ecmaVersion: 'latest',
sourceType: 'script',
onComment: comments,
});

if (comments.length === 0) {
return null;
}

// Build result by copying non-comment ranges.
// Comments are sorted by position since acorn parses linearly.
let result = '';
let position = 0;

for (const comment of comments) {
result += code.slice(position, comment.start);
position = comment.end;
}

result += code.slice(position);
return result;
},
};
}
55 changes: 55 additions & 0 deletions packages/cli/src/vite/vat-bundler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { VatBundle } from '@metamask/kernel-utils';
import { build } from 'vite';
import type { Rollup } from 'vite';

import { exportMetadataPlugin } from './export-metadata-plugin.ts';
import { stripCommentsPlugin } from './strip-comments-plugin.ts';

/**
* Bundle a vat source file using vite.
*
* Produces an IIFE bundle that assigns exports to a `__vatExports__` global,
* along with metadata about the bundle's exports and external dependencies.
*
* @param sourcePath - Absolute path to the vat entry point.
* @returns The bundle object containing code and metadata.
*/
export async function bundleVat(sourcePath: string): Promise<VatBundle> {
const metadataPlugin = exportMetadataPlugin();

const result = await build({
configFile: false,
logLevel: 'silent',
build: {
write: false,
lib: {
entry: sourcePath,
formats: ['iife'],
name: '__vatExports__',
},
rollupOptions: {
output: {
exports: 'named',
inlineDynamicImports: true,
},
plugins: [stripCommentsPlugin(), metadataPlugin],
},
minify: false,
},
});

const output = Array.isArray(result) ? result[0] : result;
const chunk = (output as Rollup.RollupOutput).output.find(
(item): item is Rollup.OutputChunk => item.type === 'chunk' && item.isEntry,
);

if (!chunk) {
throw new Error(`Failed to produce bundle for ${sourcePath}`);
}

return {
moduleFormat: 'iife',
code: chunk.code,
...metadataPlugin.getMetadata(),
};
}
Loading
Loading