rolldown-plugin-strip
v0.1.0
Published
Rolldown plugin to remove debug-only code paths.
Readme
rolldown-plugin-strip
rolldown-plugin-strip removes debug-only code from Rolldown build output.
It currently supports:
- stripping configured function-call statements (for example
console.log(...)); - stripping standalone
debuggerstatements; - stripping configured labeled statements and labeled blocks;
- safe handling for chained/non-statement call matches via
chainedCalls.
Install
npm install rolldown-plugin-stripUsage (production build)
The plugin is build-only (apply: "build") and runs as a pre transform (enforce: "pre").
import { defineConfig } from "rolldown";
import { strip } from "rolldown-plugin-strip";
export default defineConfig({
plugins: [
strip({
debugger: true,
functions: ["console.log", "console.debug"],
labels: ["dev", "debug"],
chainedCalls: "warn"
})
]
});Options
type ChainedCallsPolicy = "skip" | "warn" | "error";
interface StripOptions {
functions?: string[];
debugger?: boolean;
labels?: string[];
chainedCalls?: ChainedCallsPolicy; // default: "skip"
}functions- A list of function names to strip when they appear as a full statement on a line.
- Example removable line:
console.log("debug");
debugger- When
true, removes full lines containing onlydebugger(with optional semicolon).
- When
labels- A list of labels to remove, including single-line labeled statements and labeled blocks.
- Examples:
dev: doThing();,dev: { doThing(); },dev: while (cond) { ... }
chainedCalls- Controls behavior when a configured function matches but cannot be safely removed as a whole statement.
"skip"(default): keep the line unchanged."warn": keep the line and emit a warning toconsole.warn."error": throw and fail the build.
Chained-call behavior
Given:
strip({ functions: ["debug"], chainedCalls: "skip" });Safe removable line (always stripped):
debug("trace");Chained/non-statement line (not safely removable):
debug("trace").trim();Outcomes for the chained/non-statement line:
chainedCalls: "skip"-> line is left as-is.chainedCalls: "warn"-> line is left as-is and a warning is logged.chainedCalls: "error"-> an error is thrown and the build fails.
Notes
- When the transform changes the module source,
transformreturns a non-null source map (VLQ v3) so debuggers and error stacks can map generated output back to the original file. Unchanged modules keepmap: null. - When
functionsis enabled, line endings are normalized the same way as the stripper (split(/\r?\n/).join("\n")); if that step runs, the map is composed from the removal map and the newline-normalization map. - Call-expression matching is line-based and intentionally conservative to avoid unsafe removals.
Local development and test
npm install
npm run check
npm run buildnpm run checkruns type-check/lint (tsc --noEmit) and tests (vitest run).npm run buildemitsdist/usingtsconfig.build.json.
