# @rollup/plugin-esm-shim

> Generate minified bundle

Latest version **0.1.8** (published 2025-02-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @rollup/plugin-esm-shim
pnpm add @rollup/plugin-esm-shim
yarn add @rollup/plugin-esm-shim
bun add @rollup/plugin-esm-shim
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.1.8 |
| Published | 2025-02-27 |
| First published | 2023-04-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14.0.0 |
| Dependencies | 2 |
| Unpacked size | 11 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3758 |
| Author | Peter Placzek |
| Maintainers | shellscape, rich_harris, guybedford, lukastaegert |
| Keywords | rollup, plugin, esm-shim, npm, modules |

## Links

- npm: https://www.npmjs.com/package/@rollup/plugin-esm-shim
- Repository: https://github.com/rollup/plugins
- Homepage: https://github.com/rollup/plugins/tree/master/packages/esm-shim#readme
- Issues: https://github.com/rollup/plugins/issues
- npm.io page: https://npm.io/package/@rollup/plugin-esm-shim

## Dependencies (2)

- [mlly](https://npm.io/package/mlly.md) ^1.7.4
- [magic-string](https://npm.io/package/magic-string.md) ^0.30.3

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 0.1.8 (latest) — 2025-02-27
- 0.1.7 — 2024-06-05
- 0.1.6 — 2024-04-05
- 0.1.5 — 2023-11-05
- 0.1.4 — 2023-10-15
- 0.1.3 — 2023-10-05
- 0.1.2 — 2023-08-26
- 0.1.1 — 2023-05-17
- 0.1.0 — 2023-04-04

## README

[npm]: https://img.shields.io/npm/v/@rollup/plugin-esm-shim
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-esm-shim
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-esm-shim
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-esm-shim

[![npm][npm]][npm-url]
[![size][size]][size-url]
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)

# @rollup/plugin-esm-shim

🍣 A Rollup plugin to replace CJS global variables within esm output bundles.

The list of global variables that are shimmed are:

- `__filename`: This variable corresponds to the file path of the generated bundle file.
- `__dirname`: This variable corresponds to the folder path of the created bundle file.
- `require`: Corresponds to a function to import modules in a synchronous way.

## Requirements

This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v2.0+.

## Install

Using npm:

```console
npm install @rollup/plugin-esm-shim --save-dev
```

## Usage

Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:

```typescript
import esmShim from '@rollup/plugin-esm-shim';

export default {
  input: 'src/index.js',
  output: {
    dir: 'output',
    format: 'cjs'
  },
  plugins: [esmShim()]
};
```

Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).

## Meta

[CONTRIBUTING](/.github/CONTRIBUTING.md)

[LICENSE (MIT)](/LICENSE)

Portions of this code were used with permission from [unbuild](https://github.com/unjs/unbuild). [unbuild LICENSE](./unbuild.LICENSE)

---
_Source: https://npm.io/package/@rollup/plugin-esm-shim · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
