rollup-preserve-directives

raw JSON →
1.1.3 verified Mon Apr 27 auth: no javascript

Rollup plugin to preserve shebang and string directives (e.g., 'use strict') in bundled output. Current stable version 1.1.3, maintained actively. No external dependencies, ships TypeScript types. Key differentiator: handles shebang and directives that Rollup's tree-shaking may remove, and exposes metadata for inter-plugin communication. Renamed from rollup-swc-preserve-directives in v1.0.0.

error SyntaxError: Unexpected token 'export'
cause Using require() to import an ES module package.
fix
Use import syntax or rename config file to .mjs.
error Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'rollup-preserve-directives'
cause Package not installed or mismatched version.
fix
Run npm install rollup-preserve-directives.
error TypeError: preserveDirectives is not a function
cause Default import error, possibly using named import instead of default.
fix
Use import preserveDirectives from 'rollup-preserve-directives' (default import).
breaking Package renamed from rollup-swc-preserve-directives to rollup-preserve-directives in v1.0.0. Old package deprecated.
fix Uninstall old package: npm uninstall rollup-swc-preserve-directives. Install new: npm install rollup-preserve-directives. Update import.
breaking v1.0.0 dropped dependency on swc/core. Plugin now uses Rollup's built-in parser only.
fix No change needed unless you relied on swc-specific behavior. Remove any swc options from plugin call.
deprecated v0.x versions (under old name rollup-swc-preserve-directives) are deprecated and receive no updates.
fix Upgrade to v1.0.0+ and rename package.
gotcha Plugin only works with Rollup's ESM configuration files (rollup.config.mjs or type: module).
fix Ensure your config file uses ES module syntax (export default) and has .mjs extension or package.json type: module.
gotcha May not preserve directives in all cases; if you rely on specific directives being kept, verify output.
fix Use this.getModuleInfo or moduleParsed hook to check directives metadata.
npm install rollup-preserve-directives
yarn add rollup-preserve-directives
pnpm add rollup-preserve-directives

Basic rollup.config.js configuration showing how to use the plugin to preserve shebang and directives.

import preserveDirectives from 'rollup-preserve-directives';

export default {
  input: './src/index.js',
  output: {
    file: './dist/index.js',
    format: 'cjs',
  },
  plugins: [
    preserveDirectives(),
  ],
};