Catalogue / universal-bundler-plugin
universal-bundler-plugin
Older skill: source not recordedSignedAuthor one compiler plugin and ship it as a single npm package that works across many build tools through unplugin — a shared typed factory, thin per-bundler adapter entries, subpath exports with optional peer dependencies, Nuxt and Astro wrappers, id-normalized transforms with accurate source maps,…--- name: universal-bundler-plugin description: >- Author one compiler plugin and ship it as a single npm package that works across many build tools through unplugin — a shared typed factory, thin per-bundler adapter entries, subpath exports with optional peer dependencies, Nuxt and Astro wrappers, id-normalized transforms with accurate source maps, a playground with unit tests, packed-archive consumer verification in at least two bundlers, and read-only CI that runs those consumer builds. Use when creating, editing, reviewing, or verifying a cross-bundler build-plugin package, its adapters, exports, wrappers, types, tests, or CI. Not for configuring a Vite, Webpack, or Nuxt application that merely installs such plugins, and not for plugin logic that cannot share one factory. ---
Ship One Plugin to Every Bundler
You are building a published package, not an application: one factory that the unplugin library wraps into native plugins for several build tools, plus the exports, wrappers, tests and CI that keep it trustworthy. If the request is actually about wiring plugins into an app's bundler config, stop — this skill only covers authoring the plugin package itself.
Get the decisions first
Ask before touching any file; the answers shape everything after:
- Package name — it drives the manifest, export map, docs and the plugin's
namestring. - Which targets to ship: any of Vite, Rollup, Webpack, esbuild, Farm, Rspack, plus whether Nuxt and Astro wrappers are wanted.
- Whether the plugin is callable with no arguments; if so, type the factory as
UnpluginFactory<Options | undefined>and apply defaults internally. - Which bundlers get packed-archive consumer fixtures — Vite plus at least one other.
- The repo's package manager and script names. Preserve them; add missing scripts rather than renaming ones contributors rely on.
Scaffold, pin, freeze
- Before fetching or installing anything, show the user the exact commands and wait for an explicit yes — installs can trigger lifecycle scripts written by third parties.
- Copy the starter at an immutable revision: pass the full commit hash of the template repository's head to the degit tool after a hash sign, and record that hash in the README so the scaffold is reproducible. Never copy from the moving default branch.
- Replace the template's placeholder name everywhere it occurs.
- Pin every dependency and devDependency to exact versions — no floating ranges. If the scaffold has no lockfile, or one for a different manager, generate it with a scripts-disabled, lockfile-only install, review the diff, and commit the file. With pnpm that means one install invocation carrying both the scripts-disabled and lockfile-only flags; npm has equivalents for both. Only after a lockfile is committed may anything install with a frozen-lockfile flag.
- Inspect the result: confirm the template actually generated (a source directory and a manifest exist), open the installed unplugin package and verify it exports every adapter helper you intend to call (installed code, not docs of a newer release), and read how each adapter maps your hooks. Hook semantics differ per bundler and parity is never guaranteed — verify per target instead of assuming.
The factory — write it once
All transform logic lives in one place; every entry and wrapper reuses it.
// src/index.ts
import { createUnplugin } from 'unplugin'
import type { UnpluginFactory } from 'unplugin'
import MagicString from 'magic-string'
import type { Options } from './types'
const TOKEN = '__BUILD_STAMP__'
// Bundler ids arrive dirty: ?query suffixes, NUL-prefixed virtual modules,
// Windows backslashes. Clean before filtering on them.
function clean(id: string): string {
return id.split('?')[0].replace(/^\0/, '').replace(/\\/g, '/')
}
export const unpluginFactory: UnpluginFactory<Options | undefined> = userOptions => {
const opts = { stamp: 'local', ...userOptions }
return {
name: 'unplugin-buildstamp',
transformInclude(id) {
return /\/version\.[cm]?[jt]s$/.test(clean(id))
},
transform(code, id) {
const start = code.indexOf(TOKEN)
if (start < 0) return null
const editor = new MagicString(code)
editor.overwrite(start, start + TOKEN.length, JSON.stringify(opts.stamp))
return {
code: editor.toString(),
map: editor.generateMap({ hires: true, source: clean(id) }),
}
},
}
}
export const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)
export default unpluginFilter aggressively in transformInclude on the cleaned id so unrelated files never reach transform; keep the pure annotation so unused adapters tree-shake; export both the factory (wrappers need it) and the default instance. Define Options once in src/types.ts and import it everywhere.
The literal splice above is a placeholder demonstration only. A production transform must parse the file, escape every interpolated value, edit precise ranges, and always return a correct high-resolution map. You must not ship raw string substitution.
Entries, peers, export map
Each bundler entry is three lines:
// src/vite.ts
import { createVitePlugin } from 'unplugin'
import { unpluginFactory } from './index'
export default createVitePlugin(unpluginFactory)| Subpath | Entry | Helper from installed unplugin | Optional peer | |---|---|---|---| | . | index.ts | createUnplugin | — | | ./vite | vite.ts | createVitePlugin | vite | | ./rollup | rollup.ts | createRollupPlugin | rollup | | ./webpack | webpack.ts | createWebpackPlugin | webpack | | ./esbuild | esbuild.ts | createEsbuildPlugin | esbuild | | ./farm | farm.ts | createFarmPlugin | @farmfe/core | | ./rspack | rspack.ts | createRspackPlugin | @rspack/core | | ./nuxt | nuxt.ts | see wrappers below | — | | ./astro | astro.ts | uses unplugin.vite() | astro | | ./types | types.ts | — | — |
Only ship entries whose helper you confirmed in the installed unplugin release.
Peer rules: every bundler engine is an optional peer dependency (mark each optional: true under peerDependenciesMeta) under its real published identifier — Farm and Rspack publish scoped packages, so list @farmfe/core and @rspack/core, and verify the identifiers each adapter actually resolves against rather than copying names from memory. Import bundlers for types only: an entry must never import a bundler engine at runtime, because consumers on other targets never installed it. Framework wrappers are the deliberate exception pattern — a wrapper may take a required runtime dependency on its own framework kit, which the Nuxt entry does with @nuxt/kit.
Map every subpath, types first, both module kinds where either can occur:
{
"files": ["dist"],
"exports": {
".": { "types": "./dist/index.d.ts", "import": "./dist/index.js", "require": "./dist/index.cjs" },
"./vite": { "types": "./dist/vite.d.ts", "import": "./dist/vite.js", "require": "./dist/vite.cjs" },
"./types": { "types": "./dist/types.d.ts", "import": "./dist/types.js", "require": "./dist/types.cjs" }
}
}Repeat the ./vite shape for the other bundlers and both wrappers. ./types carries runtime conditions deliberately: configure the build to compile src/types.ts alongside the entries so dist/types.d.ts plus its intentionally value-empty ESM and CJS modules are emitted, and verify all three landed in the packed archive. Document the subpath as type-only; consumers should use import type.
Framework wrappers
Nuxt — the kit functions run when the module loads, so @nuxt/kit is a pinned entry under dependencies, never an optional peer:
// src/nuxt.ts
import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'
import buildstampVite from './vite'
import buildstampWebpack from './webpack'
import type { Options } from './types'
export interface ModuleOptions extends Options {}
export default defineNuxtModule<ModuleOptions>({
meta: { name: 'unplugin-buildstamp', configKey: 'buildstamp' },
defaults: {},
setup(options) {
addVitePlugin(() => buildstampVite(options))
addWebpackPlugin(() => buildstampWebpack(options))
},
})Registering both adapters covers whichever bundler Nuxt builds with, and ModuleOptions keeps nuxt.config typed. Claim compatibility only for Nuxt majors you exercised in a fixture; do not inherit a compatibility range from the template untested.
Astro — it builds through Vite, so hand it the Vite form via the hook's typed updater:
// src/astro.ts
import type { AstroIntegration } from 'astro'
import unplugin from './index'
import type { Options } from './types'
export default function buildstampIntegration(options: Options = {}): AstroIntegration {
return {
name: 'unplugin-buildstamp',
hooks: {
'astro:config:setup': ({ updateConfig }) => {
updateConfig({ vite: { plugins: [unplugin.vite(options)] } })
},
},
}
}Call the typed updateConfig the hook provides; never cast its argument to any and never push into Astro's plugin array directly. astro remains an optional peer imported for types only.
Iterate with a playground
Run the watch build in one terminal and the playground dev server in another. The playground config imports an entry straight from source (for instance ../src/vite), so behavior is checked before any publish; confirm the transformed result in the served app or built bundle. Add a Vitest unit test that calls the factory's transform on a sample and asserts both emitted code and generated map. Keep build, lint, typecheck, test, a watch build and a playground launcher available; extras such as dead-export or publish-lint checks can join as further scripts.
Prove the packed archive
Inspecting dist/ proves nothing; consuming the real tarball does.
- Build, then pack.
- Create two fixtures installing the archive by file path — e.g. a Vite fixture importing
pkg/vitewith an ESM import, and a Rollup or webpack fixture loading its subpath withrequire. - In each, run a real build of fixture code and assert the transform applied.
- In each, run a TypeScript check resolving
import type { Options }frompkg/types, exercising the declarations andtypesconditions. - On failure, fix the conditions or build output, repack, rerun both. Two bundlers is the floor, not the ceiling.
Wrap steps 1–4 in a single repo script (conventionally test:pack) so CI runs the identical proof.
CI
One workflow, on pushes and pull requests to the default branch. Pin the manager through the manifest's packageManager field, fix one Node major, and install from the committed lockfile — the frozen flag presupposes the lockfile created in the scaffold section — with scripts disabled. Wire the packed-consumer proof in as an explicit step:
name: CI
on:
push: { branches: [main] }
pull_request: { branches: [main] }
permissions:
contents: read
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.2.2
- uses: pnpm/action-setup@v4.0.0
- uses: actions/setup-node@v4.1.0
with: { node-version: 20, cache: pnpm }
- run: pnpm install --frozen-lockfile --ignore-scripts
- run: pnpm run lint
- run: pnpm run typecheck
build-test-consume:
needs: check
strategy:
fail-fast: false
matrix: { os: [ubuntu-latest, windows-latest, macos-latest] }
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4.2.2
- uses: pnpm/action-setup@v4.0.0
- uses: actions/setup-node@v4.1.0
with: { node-version: 20, cache: pnpm }
- run: pnpm install --frozen-lockfile --ignore-scripts
- run: pnpm run build
- run: pnpm run test
- run: pnpm run test:packPreview shortened. The full SKILL.md is in the pack.
| File | Size | SHA-256 |
|---|---|---|
| SKILL.md | 14.8 kB | c436c34f29dd |
| examples.json | 2.1 kB | 9cd43ee3c3c2 |
Published verbatim from the pinned commit. Nothing is rewritten.
| Version | Pinned commit | Signed | |
|---|---|---|---|
| 1.0.0 Current | Not recorded | 10 Oct 2026 |
Locking a skill to one version needs Pro.