ProvenPin Install

terser async minify with source maps: working versions (tested 2026-10-15)

Minify JavaScript asynchronously with terser using compress (drop_console) and mangle options plus a source map, then assert the output is smaller, contains no console calls and still runs correctly when evaluated with node:vm offline.

passed4 passed · 0 failed2026-10-15 22:58 UTC
Status
passed
Last passed
2026-10-15 22:58 UTC
Tests
4 / 4
Language
TypeScript
Runtime
node v22.23.3
pinned versions3
terser@5.51.2@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 38820570375

The code

Exactly the files that passed. Your agent can pull them with get_recipe("terser-minify-sourcemap") (install), or see them on GitHub.

package.json
{
  "name": "terser-minify-sourcemap",
  "private": true,
  "type": "module",
  "scripts": {
    "test": "node --import tsx --test test/*.test.ts"
  },
  "dependencies": {
    "terser": "5.51.2"
  },
  "devDependencies": {
    "@types/node": "26.6.5",
    "tsx": "4.23.15"
  }
}
src/minify.ts
import { minify as terserMinify } from "terser";

export type MinifyResult = {
  code: string;
  map: string;
  originalBytes: number;
  minifiedBytes: number;
};

export async function minify(source: string, filename = "input.js"): Promise<MinifyResult> {
  const result = await terserMinify(
    { [filename]: source },
    {
      compress: { drop_console: true },
      mangle: true,
      sourceMap: { filename, url: `${filename}.map` },
    },
  );

  if (result.code == null) {
    throw new Error("terser produced no output");
  }

  const map =
    result.map == null
      ? ""
      : typeof result.map === "string"
        ? result.map
        : JSON.stringify(result.map);

  return {
    code: result.code,
    map,
    originalBytes: Buffer.byteLength(source, "utf8"),
    minifiedBytes: Buffer.byteLength(result.code, "utf8"),
  };
}
test/minify.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import vm from "node:vm";
import { minify } from "../src/minify.ts";

const SOURCE = `function computeTotal(items) {
  const subtotalValue = items.reduce(function (accumulator, currentValue) {
    return accumulator + currentValue;
  }, 0);
  console.log("total", subtotalValue);
  return subtotalValue;
}
globalThis.__recipe = { computeTotal: computeTotal, value: computeTotal([1, 2, 3, 4]) };
`;

test("minifies to a smaller payload", async () => {
  const result = await minify(SOURCE, "recipe.js");
  assert.ok(result.minifiedBytes < result.originalBytes);
  assert.ok(result.code.length < SOURCE.length);
});

test("drops console calls and mangles local names", async () => {
  const { code } = await minify(SOURCE, "recipe.js");
  assert.equal(/console/.test(code), false);
  assert.equal(/subtotalValue/.test(code), false);
  assert.equal(/accumulator/.test(code), false);
});

test("emits a usable v3 source map", async () => {
  const { map } = await minify(SOURCE, "recipe.js");
  const parsed = JSON.parse(map) as { version: number; sources: string[]; mappings: string };
  assert.equal(parsed.version, 3);
  assert.deepEqual(parsed.sources, ["recipe.js"]);
  assert.ok(parsed.mappings.length > 0);
});

test("minified code still runs correctly in node:vm", async () => {
  const { code } = await minify(SOURCE, "recipe.js");
  const context = vm.createContext({});
  vm.runInContext(code, context, { filename: "recipe.min.js" });
  const recipe = (context as Record<string, unknown>).__recipe as {
    value: number;
    computeTotal: (items: number[]) => number;
  };
  assert.equal(recipe.value, 10);
  assert.equal(recipe.computeTotal([5, 10]), 15);
});

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.