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-directivesVerified import paths — ran on the pinned version, not inferred.
Configure Rollup with preserveModules: true and add the plugin to retain 'use client' directives in output modules.
Set output.preserveModules: true in your Rollup config.
Set terser compress.directives: false to preserve directives.
Avoid using suppressPreserveModulesWarning; use onwarn in Rollup to handle warnings.
Add an onwarn handler to ignore directive warnings, e.g., onwarn: (warning, defaultHandler) => { if (!warning.message.includes('use client')) defaultHandler(warning); }Set output.preserveModules: true and add preserveDirectives() to the plugins array.
Set output.preserveModules: true in your Rollup config.
Configure minifier to preserve directives, e.g., terser({ compress: { directives: false } }).