Bundlemonkey Userscript Bundler
Bundlemonkey is a userscript bundler designed for fast and efficient development of browser userscripts, leveraging esbuild for lightning-fast compilation. Currently stable at version 0.7.6, the package sees a moderate release cadence, primarily focusing on dependency updates, with occasional feature enhancements and breaking changes between minor versions (e.g., v0.7.0, v0.5.0). Its key differentiators include robust TypeScript support, module bundling capabilities, and a unique feature for type-safe header comments, which streamlines metadata management and reduces errors. It integrates seamlessly with popular userscript managers like Tampermonkey, Violentmonkey, and Greasemonkey, providing a 'watch' mode for continuous development feedback. The project structure encourages modularity, allowing users to define scripts within dedicated source directories.
Common errors
-
TypeError: bundlemonkey.bundle is not a function
cause Attempting to use the deprecated `bundle` API after upgrading to Bundlemonkey v0.7.0 or later.fixReplace calls to `bundle()` with `build()` or `watch()`. Example: `import { build } from 'bundlemonkey'; build(...)`. -
Error: 'defineUserScript' is not defined
cause `defineUserScript` was not imported from the `bundlemonkey` package.fixAdd `import { defineUserScript } from 'bundlemonkey';` to the top of your userscript file. -
Userscript does not include expected @grant headers in the output.
cause Since v0.5.0, `bundlemonkey` only outputs `@grant` headers if the `grant` property is explicitly provided in `defineUserScript`.fixEnsure you specify the `grant` property in your `defineUserScript` call, e.g., `grant: ['GM_setValue', 'GM_getValue']` or `grant: ['none']`. -
SyntaxError: Cannot use import statement outside a module
cause This error occurs when trying to use ESM `import` syntax (which Bundlemonkey uses) in a CommonJS (`require`) environment without proper configuration.fixEnsure your `package.json` contains `"type": "module"` if you intend to use ESM syntax throughout your project, or convert `import` statements to dynamic `import()` if you must remain in a CommonJS context.
Warnings
- breaking The `bundle` export was removed and replaced by `build` and `watch` functions. Additionally, the `build` function now returns the bundled outputs directly.
- breaking If the `grant` property is omitted from `defineUserScript`, no `@grant` header will be output in the bundled userscript. Previously, omitting it might have resulted in a default or different behavior.
- breaking The main programmatic export of the library was renamed from `main` to `bundle`.
- gotcha Bundlemonkey is designed for modern JavaScript environments and uses ESM (ECMAScript Modules) for its exports. Attempting to use `require()` in a CommonJS context without proper configuration can lead to import errors.
Install
-
npm install bundlemonkey -
yarn add bundlemonkey -
pnpm add bundlemonkey
Imports
- defineUserScript
const { defineUserScript } = require('bundlemonkey');import { defineUserScript } from 'bundlemonkey'; - build
import { bundle } from 'bundlemonkey';import { build } from 'bundlemonkey'; - watch
import { bundle } from 'bundlemonkey';import { watch } from 'bundlemonkey';
Quickstart
npx bundlemonkey --create
# Follow prompts to initialize your project
# Then, open src/your-script-name/index.user.ts and modify it:
// src/your-script-name/index.user.ts
import { defineUserScript } from "bundlemonkey";
export default defineUserScript({
name: "My New Userscript",
version: "1.0.0",
description: "A quickstart script.",
match: ["https://example.com/*"],
grant: ["none"], // Explicitly define grants if needed
main: () => {
console.log("Hello from Bundlemonkey!");
document.body.innerHTML += '<h1>Bundlemonkey Quickstart!</h1>';
},
});
# Build the project (output will be in ./dist):
npx bundlemonkey