API Reference
Programmatic API, plugin development, hooks, and package documentation for Packem
API Reference
Use Packem programmatically, write custom plugins, or hook into the build lifecycle. This section covers the public API for every package in the Packem ecosystem.
Packages
| Package | Description | Docs |
|---|---|---|
@visulima/packem | Bundler core, CLI, and configuration API | — |
@visulima/packem-rollup | Rollup plugins and transformer integrations | API |
@visulima/rollup-plugin-css | CSS processing and optimization plugin | — |
@visulima/css-style-inject | Runtime CSS injection utility | — |
@visulima/packem-share | Shared types, utilities, and constants | — |
Programmatic usage
Run Packem from your own scripts instead of the CLI:
import { build } from '@visulima/packem'
await build({
root: process.cwd(),
config: 'packem.config.ts',
})Plugin development
Packem uses Rollup under the hood, so any Rollup plugin works. Wrap yours in a function and add it to the plugins array:
import { defineConfig } from '@visulima/packem/config'
import transformer from '@visulima/packem/transformer/esbuild'
export default defineConfig({
transformer,
rollup: {
plugins: [myCustomPlugin()],
},
})See the plugins option for configuration details.
Specialized APIs
- packem-rollup — Rollup plugins and transformer integration APIs.
- require-cjs-transformer — Transform ESM imports of CJS packages for better performance.
- native-modules-plugin — Automatic handling of Node.js native addons.
Build hooks
Hook into the build lifecycle to run custom logic:
export default defineConfig({
hooks: {
'build:before': (context) => {
console.log('Build starting...')
},
'build:done': (context) => {
console.log(`Built in ${context.duration}ms`)
},
},
})See the hooks option for the full list of available hooks.
Environment variables
| Variable | Description |
|---|---|
PACKEM_CONFIG | Path to config file (overrides auto-detection) |
NODE_ENV | Set to production for optimized builds |
DEBUG | Set to packem:* for debug output |