A Rollup plugin to preserve the 'use client' directive in React 18 components during bundling, ensuring proper separation between server and client components. Current stable version is 3.0.1, released under MIT, with ESM and CJS support and TypeScript type definitions. Unlike alternatives like rollup-plugin-preserve-directives, this plugin does not require 'preserveModules: true', avoiding conflicts with CSS module imports. The plugin uses Rollup's moduleParsed hook (v3.0.0+) and targets Rollup ^4.0.0 as a peer dependency. It solves a specific pain point for library authors distributing npm packages with both server and client components.
npm install rollup-plugin-preserve-use-clientVerified import paths — ran on the pinned version, not inferred.
Basic Rollup configuration using ESM imports. Adds the plugin to preserve 'use client' directives in bundled output.
Update to v3.0.0+ and verify no reliance on previous transform-based internals.
Upgrade to v2.0.0 or later. If using ES modules, ensure import syntax is correct.
Use only one directive-preserving plugin, or ensure they do not interfere.
Ensure 'use client' is a literal string at the top of each client component module.
Place 'use client' as the first statement in the file, before any imports or other code.
Use default import: import preserveUseClientDirective from 'rollup-plugin-preserve-use-client'
Ensure you call the imported default as a function: preserveUseClientDirective()
Run: npm install rollup-plugin-preserve-use-client --save-dev
Update Rollup to v4.x: npm install rollup@^4.0.0