rollup-swc-preserve-directives
raw JSON → 0.7.0 verified Mon Apr 27 auth: no javascript
Rollup plugin that uses SWC to preserve directives such as shebang and string directives (e.g., 'use strict', 'use asm') in bundled output. Version 0.7.0 is the latest stable; updated on demand. It integrates with Rollup build pipeline to ensure important code-level annotations are not stripped during minification or transformation, unlike naive bundlers. Differentiates itself by being SWC-based (fast, Rust-powered) and designed specifically for directive preservation.
Common errors
error Error: Cannot find module '@swc/core' ↓
cause @swc/core is not installed or not in node_modules.
fix
Run
npm install @swc/core in your project directory. error TypeError: swcPreserveDirectives is not a function ↓
cause Using named import instead of default import.
fix
Use
import swcPreserveDirectives from 'rollup-swc-preserve-directives' (no curly braces). Warnings
breaking SWC version mismatch can cause parse errors or unexpected behavior. ↓
fix Use @swc/core version compatible with the SWC version used by this plugin; check package.json peerDependencies.
deprecated Rollup <4 support may be dropped in future versions. ↓
fix Upgrade Rollup to ^4.0.0 if encountering issues, or pin to older plugin version.
gotcha Plugin only works with Rollup's JavaScript plugins; if using TypeScript, ensure SWC is configured separately for type stripping. ↓
fix Use @rollup/plugin-typescript or @rollup/plugin-sucrase for TS before this plugin.
Install
npm install rollup-swc-preserve-directives yarn add rollup-swc-preserve-directives pnpm add rollup-swc-preserve-directives Imports
- default wrong
const swcPreserveDirectives = require('rollup-swc-preserve-directives')correctimport swcPreserveDirectives from 'rollup-swc-preserve-directives' - swcPreserveDirectives wrong
import { swcPreserveDirectives } from 'rollup-swc-preserve-directives'correctimport swcPreserveDirectives from 'rollup-swc-preserve-directives' - type definitions
import type { RollupSwcPreserveDirectivesOptions } from 'rollup-swc-preserve-directives'
Quickstart
import swcPreserveDirectives from 'rollup-swc-preserve-directives';
import { defineConfig } from 'rollup';
export default defineConfig({
input: 'src/index.js',
output: {
dir: 'dist',
format: 'esm'
},
plugins: [
swcPreserveDirectives()
]
});