rollup-plugin-shim
raw JSON → 1.0.0 verified Mon Apr 27 auth: no javascript maintenance
A Rollup plugin that replaces module imports with a specified string shim, useful for stripping out dev-time dependencies like 'debug' in production builds. Stable at v1.0.0 since 2017, with minimal releases. Unlike rollup-plugin-alias, this plugin replaces the module content inline with a string rather than pointing to another file, making it ideal for small shims. Best for simple noop replacements; for complex aliasing, use rollup-plugin-alias instead. Active maintenance is low, but the plugin works with Rollup 0.x and 1.x.
Common errors
error Error: Unexpected token (Note that you need plugins to import files that are not JavaScript) ↓
cause Shim string contains syntax errors or invalid module format.
fix
Correct the shim string to be valid ES module syntax.
error TypeError: shim is not a function ↓
cause Importing the plugin incorrectly (e.g., using named import when default is needed).
fix
Use default import:
import shim from 'rollup-plugin-shim'. error Error: Cannot find module 'rollup-plugin-shim' ↓
cause Package not installed or not in node_modules.
fix
Run
npm install rollup-plugin-shim --save-dev or yarn add -D rollup-plugin-shim. Warnings
gotcha Plugin overwrites imports globally; if two modules use the same import name but expect different behavior, shimming may break one of them. ↓
fix Use distinct keys or handle per-module with custom resolve.
gotcha Shim values are strings that are evaluated as modules; syntax errors in the shim string will cause build failures. ↓
fix Ensure shim strings are valid JavaScript/ES module syntax.
gotcha The plugin may not work with Rollup >=2.0.0 due to API changes; it was developed for Rollup 0.x/1.x. ↓
fix Test with your Rollup version; consider using '@rollup/plugin-alias' or newer alternatives.
gotcha Keys in the shim object must match the import specifier exactly, including file extensions if used in the import. ↓
fix Use full paths or bare specifiers as they appear in import statements.
Install
npm install rollup-plugin-shim yarn add rollup-plugin-shim pnpm add rollup-plugin-shim Imports
- default (shim) wrong
import { shim } from 'rollup-plugin-shim'correctimport shim from 'rollup-plugin-shim' - shim (CommonJS)
const shim = require('rollup-plugin-shim') - shim (TypeScript) wrong
import shim from 'rollup-plugin-shim' // not typed, use require for TS compatibilitycorrectimport shim = require('rollup-plugin-shim')
Quickstart
import shim from 'rollup-plugin-shim';
export default {
input: 'src/main.js',
output: { format: 'es' },
plugins: [
shim({
'debug': `export default () => () => undefined`,
'fs': `export function writeFileSync() {}`
})
]
};