An esbuild plugin that ensures directives like 'use client' and 'use strict' are preserved at the top of output files. Current stable version is 0.0.11. The plugin scans source files for specified directives before bundling and reinserts them into the corresponding output chunks. It supports regex-based include/exclude patterns and works with esbuild's metafile option for improved accuracy. Updated frequently with community contributions, it is primarily used with React Server Components and Node.js ESM/CJS builds. Key differentiator: it handles directives that esbuild would otherwise strip, with minimal overhead.
npm install esbuild-plugin-preserve-directivesNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows how to use the plugin with esbuild, including metafile option for accuracy.
Add metafile: true to esbuild options.
Upgrade esbuild to ^0.21.0 or later.
Use Node.js >=18.0.0.
Run 'npm install esbuild@^0.21.0' or update esbuild to compatible version.
Run 'npm install -D esbuild-plugin-preserve-directives'.
Use 'import { preserveDirectivesPlugin } from 'esbuild-plugin-preserve-directives''.