Registry / devops / esbuild-plugin-preserve-directives

esbuild-plugin-preserve-directives

JSON →
library0.0.11jsnpmunverified

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.

devops
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

This package only exports a named export, not a default export.

import { preserveDirectivesPlugin } from 'esbuild-plugin-preserve-directives'

Options type is exported as a type-only export; use 'import type' for type-checking.

import type { PreserveDirectivesPluginOptions } from 'esbuild-plugin-preserve-directives'

CommonJS require must destructure the named export.

const { preserveDirectivesPlugin } = require('esbuild-plugin-preserve-directives')

Shows how to use the plugin with esbuild, including metafile option for accuracy.

import { build } from 'esbuild'; import { preserveDirectivesPlugin } from 'esbuild-plugin-preserve-directives'; await build({ entryPoints: ['src/index.js'], bundle: true, outdir: 'dist', metafile: true, plugins: [ preserveDirectivesPlugin({ directives: ['use client', 'use strict'], include: /\.(js|ts|jsx|tsx)$/, exclude: /node_modules/, }), ], });
Debug
Known footguns
gotchametafile option is not required but strongly recommended for accuracy; without it, directive preservation may be incomplete.
breakingPlugin requires esbuild ^0.21.0; older esbuild versions are incompatible.
deprecatedNode.js <18.0.0 is not supported.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
9 hits · last 30 days
gptbot
3
ahrefsbot
3
claudebot
3
Resources