forked from violentmonkey/rollup-plugin-userscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Using Minification alongside this
Seized and Desisted edited this page Apr 15, 2026
·
1 revision
Minification in Rolldown breaks this plugin (it removes comments), but you can work around it. This is how I do it in Vape Rewrite while still using Oxc to get near same performance as if it was enabled:
First of all, we need to disable the built-in minification from Rolldown
output: {
format: "iife",
file: `dist/vape-rewrite.user.js`,
globals: {
"@violentmonkey/dom": "VM",
},
- minify: true, // or something else
+ minify: false,
sourcemap: "inline",
},you could call it a day and leave it there (or just limit it to dce-only), or use oxc-minify (what Rolldown uses for minification) and make a light plugin wrapper thing around it:
// from Vape Rewrite.
import { MinifyOptions, minifySync } from "oxc-minify";
import type { Plugin } from "rolldown";
import { withFilter } from "rolldown/filter";
type Opts = MinifyOptions | undefined | null;
function minifyPlugin(opts?: Opts): Plugin {
return withFilter(
{
name: "oxc-minify",
renderChunk: {
order: "pre",
handler(code, chunk) {
return minifySync(chunk.fileName, code, opts).code;
},
}
},
{
load: { id: ["*.js", "*.jsx", "*.ts", "*.tsx"] },
},
);
}
export default minifyPlugin;then add it to rolldown.config.js (import the default export from that named minify):
plugins: [
// ... plugins that aren't the UserScript (they run before the minifier and get minified), if you need other plugins that add comments that are required at runtime, you can put them above the minifier() line.
// this MUST be before UserScript, so the comments from it won't be removed.
+ process.env.NODE_ENV === "production" ? minify() : undefined,
// avoid putting things above here, because they won't be minified. the UserScript plugin only adds comments that are **required at runtime**, so we MUST make it not minified here.
userscript((meta: string) => {
const newMeta = meta
.replace("process.env.AUTHOR", packageJson.author?.name ?? "Unspecified")
.replace("process.env.VERSION", packageJson.version)
.replace("process.env.NAME", REAL_CLIENT_NAME);
return newMeta;
}, {
threadNumber: 8 // I don't even use multi-threading, why am I doing this?
}),
]