rollup-plugin-import-manager
raw JSON → 0.6.4 verified Mon Apr 27 auth: no javascript
A Rollup plugin for programmatically manipulating import statements in ES6, CommonJS, and dynamic imports. Version 0.6.4 is the latest stable release, with weekly updates. Key features include adding, removing, renaming, and modifying imports, members, and modules. Supports regex selection, cut-and-paste, and debug output. Differentiates from other import plugins by supporting all three module systems and offering granular control via units and actions.
Common errors
error Error: Plugin could not be resolved. Ensure rollup-plugin-import-manager is installed. ↓
cause Missing package or wrong version (CJS/ESM mismatch).
fix
Install via npm: npm install rollup-plugin-import-manager --save-dev, and use ESM import.
error TypeError: importManager is not a function ↓
cause Using named import instead of default import.
fix
Use default import: import importManager from 'rollup-plugin-import-manager'
error Error: Cannot find module 'magic-string' ↓
cause Missing peer dependency magic-string.
fix
npm install magic-string --save-dev
Warnings
breaking In v0.6.0, the plugin changed from CJS to ESM-only. Node <12 or projects using require() will break. ↓
fix Update to Node >=12 and use ESM imports (import statement).
deprecated The 'shorthand' action for removing imports is deprecated since v0.5.0; use 'remove: true' in actions array instead. ↓
fix Replace shorthand remove with { remove: true } inside actions.
gotcha When using 'add' action, the member is added even if it already exists, potentially causing duplicate imports. ↓
fix Check for existing members before adding, or use 'rename' to modify an existing member.
gotcha Dynamic imports are parsed as strings; the module path must be exactly as written in code, including quotes. ↓
fix Ensure module strings match source exactly (e.g., 'import('./foo')' needs module: './foo').
breaking In v0.5.0, the 'modType' option required different values; 'es6' changed to 'esm'. ↓
fix Use 'esm' for ES6 imports instead of 'es6'.
Install
npm install rollup-plugin-import-manager yarn add rollup-plugin-import-manager pnpm add rollup-plugin-import-manager Imports
- importManager wrong
const importManager = require('rollup-plugin-import-manager')correctimport importManager from 'rollup-plugin-import-manager' - importManager wrong
import importManager from 'rollup-plugin-import-manager'correctimport { importManager } from 'rollup-plugin-import-manager' - Unit wrong
import { Unit } from 'rollup-plugin-import-manager'correctimport type { Unit } from 'rollup-plugin-import-manager'
Quickstart
import importManager from 'rollup-plugin-import-manager';
export default {
input: 'src/index.js',
output: {
dir: 'dist',
format: 'esm'
},
plugins: [
importManager({
units: [
{
module: './old-module',
actions: [
{ remove: true }
]
},
{
createModule: './new-module',
insert: { after: './existing-module' },
actions: [
{ add: { name: 'newExport' } }
]
}
]
})
]
};