ProvenPin Install

rollup virtual module bundle: working versions (tested 2026-10-17)

Bundle in-memory virtual modules with the rollup JS API, generate ESM output, assert tree-shaking drops an unused export, and execute the bundle through a data URL import offline.

passed3 passed · 0 failed2026-10-17 05:58 UTC
Status
passed
Last passed
2026-10-17 05:58 UTC
Tests
3 / 3
Language
TypeScript
Runtime
node v22.23.3
pinned versions3
rollup@4.64.7@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false
npm test
test environment
node v22.23.3
linux 6.17.0-1022-azure
x64
runner: github-actions 39008163431

The code

Exactly the files that passed. Your agent can pull them with get_recipe("rollup-virtual-module-bundle") (install), or see them on GitHub.

package.json
{
  "name": "rollup-virtual-module-bundle",
  "private": true,
  "type": "module",
  "scripts": {
    "test": "node --import tsx --test test/*.test.ts"
  },
  "dependencies": {
    "rollup": "4.64.7"
  },
  "devDependencies": {
    "@types/node": "26.6.5",
    "tsx": "4.23.15"
  }
}
src/bundle.ts
import { rollup, type Plugin } from "rollup";

export type VirtualModules = Record<string, string>;

export function virtualModulesPlugin(modules: VirtualModules): Plugin {
  return {
    name: "virtual-modules",
    resolveId(source) {
      if (Object.prototype.hasOwnProperty.call(modules, source)) {
        return source;
      }
      return null;
    },
    load(id) {
      if (Object.prototype.hasOwnProperty.call(modules, id)) {
        return modules[id];
      }
      return null;
    },
  };
}

export async function bundleVirtual(
  input: string,
  modules: VirtualModules,
): Promise<string> {
  const bundle = await rollup({
    input,
    plugins: [virtualModulesPlugin(modules)],
  });
  try {
    const { output } = await bundle.generate({ format: "es" });
    const chunk = output[0];
    if (chunk.type !== "chunk") {
      throw new Error("expected a single chunk");
    }
    return chunk.code;
  } finally {
    await bundle.close();
  }
}
test/bundle.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { bundleVirtual, type VirtualModules } from "../src/bundle.ts";

const modules: VirtualModules = {
  "virtual:entry": [
    'import { add } from "virtual:math";',
    'import { label } from "virtual:format";',
    'export const result = label() + ": " + add(2, 3);',
  ].join("\n"),
  "virtual:math": [
    "export function add(a, b) { return a + b; }",
    "export function subtract(a, b) { return a - b; }",
  ].join("\n"),
  "virtual:format": [
    'export function label() { return "sum"; }',
    'export function unusedFormatter() { return "never"; }',
  ].join("\n"),
};

function toDataUrl(code: string): string {
  return "data:text/javascript;base64," + Buffer.from(code, "utf8").toString("base64");
}

test("bundles virtual modules to esm", async () => {
  const code = await bundleVirtual("virtual:entry", modules);
  assert.match(code, /result/);
  assert.match(code, /export \{/);
});

test("tree-shaking drops unused exports", async () => {
  const code = await bundleVirtual("virtual:entry", modules);
  assert.doesNotMatch(code, /subtract/);
  assert.doesNotMatch(code, /unusedFormatter/);
});

test("generated bundle runs via a data url import", async () => {
  const code = await bundleVirtual("virtual:entry", modules);
  const mod = (await import(toDataUrl(code))) as { result: string };
  assert.equal(mod.result, "sum: 5");
});

Connect to ProvenPin

Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.

terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp

Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.