Registry / web-framework / rollup-plugin-preserve-directives

rollup-plugin-preserve-directives

JSON →
library0.4.0jsnpmunverified

A Rollup plugin that preserves file-level directives (e.g., 'use client', 'use server') when using preserveModules: true. Version 0.4.0 is the current stable release, with a simple API and minimal configuration. Unlike bundling, where directives are meaningless per file, this plugin ensures each preserved module retains its directives, enabling patterns like React Server Components without separate entrypoints. Still considered early-stage (largely untested) but functional. Filters via include/exclude globs. Warning suppression options exist. Supports Rollup 2–4. Ships TypeScript types.

npm install rollup-plugin-preserve-directives
INSTALL
IMPORT
SIG · ROLLUP-PLUGIN-PRES
R
rollup-plugin-preserve-directives
web-frameworkjavascriptv0.4.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18223 runs
build_error
glibc
node 18223 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

preserveDirectives
import preserveDirectives from 'rollup-plugin-preserve-directives'
import { preserveDirectives } from 'rollup-plugin-preserve-directives'
Default export, not named.
preserveDirectives
const preserveDirectives = require('rollup-plugin-preserve-directives')
const { preserveDirectives } = require('rollup-plugin-preserve-directives')
CJS require of default export.
type Options
import type { Options } from 'rollup-plugin-preserve-directives'
TypeScript users can import the Options type for plugin configuration.

Configure Rollup with preserveModules: true and add the plugin to retain 'use client' directives in output modules.

// rollup.config.js import preserveDirectives from 'rollup-plugin-preserve-directives'; export default { input: 'src/index.js', output: { dir: 'dist', format: 'esm', preserveModules: true, }, plugins: [ preserveDirectives({ exclude: ['**/*.css'], }), ], };
Debug
Known issues
gotchapreserveModules must be true for the plugin to work; otherwise it warns and does nothing.
fix
Set output.preserveModules: true in your Rollup config.
affects: >=0.0.0
gotchaMinifiers like terser may remove directives; terser's compress.directives defaults to true.
fix
Set terser compress.directives: false to preserve directives.
affects: >=0.0.0
deprecatedsuppressPreserveModulesWarning option exists but will be removed in future versions.
fix
Avoid using suppressPreserveModulesWarning; use onwarn in Rollup to handle warnings.
affects: >=0.4.0
gotchaRollup still emits warnings about module-level directives; the plugin does not suppress them.
fix
Add an onwarn handler to ignore directive warnings, e.g., onwarn: (warning, defaultHandler) => { if (!warning.message.includes('use client')) defaultHandler(warning); }
affects: >=0.0.0
Errors
Common errors & fixes
Module level directives cause errors when bundled, 'use client' was ignored.
preserveModules is not enabled, or the plugin is not used.
fix
Set output.preserveModules: true and add preserveDirectives() to the plugins array.
preserveModules must be true for preserveDirectives to work.
The plugin is used but preserveModules is not set to true.
fix
Set output.preserveModules: true in your Rollup config.
Directive 'use client' was removed after minification.
Terser or other minifier removes directives by default.
fix
Configure minifier to preserve directives, e.g., terser({ compress: { directives: false } }).
Upgrade
Version history
0.4.0latest on npm
Audit
Dependencies
rollupoptionalPeer dependency – plugin works with Rollup 2.x, 3.x, or 4.x
Agent activity
6 hits · last 30 days
node
6
Resources
rollup-plugin-preserve-directives — npm install rollup-plugin-preserve-directives · libregistry