Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 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 @@ -86,12 +86,12 @@
"jsdom": "^27.4.0",
"prettier": "^3.5.3",
"rimraf": "^6.0.1",
"rollup": "^4.55.3",
"ses": "^1.14.0",
"turbo": "^2.5.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: [],
modules: {},
};
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: [],
modules: {},
}));

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
48 changes: 48 additions & 0 deletions packages/cli/src/vite/export-metadata-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { Plugin, RenderedModule } from 'rollup';

export type BundleMetadata = {
exports: string[];
modules: Record<
string,
{ renderedExports: string[]; removedExports: string[] }
>;
};

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

return {
name: 'export-metadata',
generateBundle(_, bundle) {
for (const chunk of Object.values(bundle)) {
if (chunk.type === 'chunk' && chunk.isEntry) {
const outputChunk = chunk;
metadata.exports = outputChunk.exports;
metadata.modules = Object.fromEntries(
Object.entries(outputChunk.modules).map(
([id, info]: [string, RenderedModule]) => [
id,
{
renderedExports: info.renderedExports,
removedExports: info.removedExports,
},
],
),
);
}
}
},
getMetadata: () => metadata,
};
}
51 changes: 51 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,51 @@
import { describe, it, expect } from 'vitest';

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

describe('stripCommentsPlugin', () => {
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 'rollup';

/**
* 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: 'module',
onComment: comments,
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong sourceType for parsing IIFE bundle output

Low Severity

The stripCommentsPlugin uses sourceType: 'module' to parse vite's IIFE output, but IIFE bundles are scripts, not modules. Module parsing implies strict mode, which rejects certain legacy JavaScript patterns (octal literals, with statements, etc.). If bundled dependencies contain non-strict-mode code that wasn't transpiled, parse() throws a SyntaxError, preventing comment stripping and causing the build to fail unexpectedly.

Fix in Cursor Fix in Web


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;
},
};
}
60 changes: 60 additions & 0 deletions packages/cli/src/vite/vat-bundler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { VatBundle } from '@metamask/kernel-utils';
import { build } from 'vite';
import type { Rollup, PluginOption } from 'vite';

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

export type { VatBundle };

/**
* 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 modules.
*
* @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() as unknown as PluginOption,
metadataPlugin as unknown as PluginOption,
],
},
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