rollup-plugin-keep-import

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

A Rollup plugin that preserves import statements in the output bundle, useful for dynamic imports or retaining external dependencies as imports. Current stable version: 1.0.3. Released via semantic-release, active development. Key differentiator: unlike Rollup's default behavior of bundling imports, this plugin keeps them as is, similar to renderDynamicImport, enabling partial bundling for micro-frontends or runtime module loading.

error Error [ERR_REQUIRE_ESM]: require() of ES Module
cause Using CommonJS require() to import the package (ESM-only).
fix
Change to import syntax or set type: 'module' in package.json.
error TypeError: keepImport is not a function
cause Destructuring import with named import (e.g., import { keepImport }).
fix
Use default import: import keepImport from 'rollup-plugin-keep-import'.
npm install rollup-plugin-keep-import
yarn add rollup-plugin-keep-import
pnpm add rollup-plugin-keep-import

Rollup config using keepImport plugin to preserve import statements for specific files, excluding node_modules.

// rollup.config.js
import keepImport from 'rollup-plugin-keep-import';

export default {
  input: 'src/index.js',
  output: {
    dir: 'dist',
    format: 'es',
  },
  plugins: [
    keepImport({
      // By default, all imports are kept. Use 'include' or 'exclude' to filter.
      include: ['**/*.js'],
      exclude: ['node_modules/**'],
    }),
  ],
};