# @rollup/plugin-inject

> Scan modules for global variables and injects `import` statements where necessary

Latest version **5.0.5** (published 2023-10-15) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 40/100 (D)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 5.0.5 |
| Published | 2023-10-15 |
| First published | 2019-11-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14.0.0 |
| Dependencies | 3 |
| Unpacked size | 18.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3757 |
| Author | Rich Harris |
| Maintainers | shellscape, rich_harris, guybedford, lukastaegert |
| Keywords | rollup, plugin, inject, es2015, npm, modules |

## Links

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

## Dependencies (3)

- [magic-string](https://npm.io/package/magic-string.md) ^0.30.3
- [estree-walker](https://npm.io/package/estree-walker.md) ^2.0.2
- [@rollup/pluginutils](https://npm.io/package/@rollup/pluginutils.md) ^5.0.1

## 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

- 5.0.5 (latest) — 2023-10-15
- 5.0.4 — 2023-10-05
- 5.0.3 — 2022-12-17
- 5.0.2 — 2022-10-21
- 5.0.1 — 2022-10-15
- 5.0.0 — 2022-10-09
- 4.0.4 — 2021-12-28
- 4.0.3 — 2021-10-19
- 4.0.2 — 2020-05-11
- 4.0.1 — 2020-02-01
- 4.0.0 — 2019-11-20

## README

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

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

# @rollup/plugin-inject

🍣 A Rollup plugin which scans modules for global variables and injects `import` statements where necessary.

## Requirements

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

## Install

Using npm:

```console
npm install @rollup/plugin-inject --save-dev
```

## Usage

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

```js
import inject from '@rollup/plugin-inject';

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

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).

This configuration above will scan all your files for global Promise usage and plugin will add import to desired module (`import { Promise } from 'es6-promise'` in this case).

Examples:

```js
{
  // import { Promise } from 'es6-promise'
  Promise: [ 'es6-promise', 'Promise' ],

  // import { Promise as P } from 'es6-promise'
  P: [ 'es6-promise', 'Promise' ],

  // import $ from 'jquery'
  $: 'jquery',

  // import * as fs from 'fs'
  fs: [ 'fs', '*' ],

  // use a local module instead of a third-party one
  'Object.assign': path.resolve( 'src/helpers/object-assign.js' ),
}
```

Typically, `@rollup/plugin-inject` should be placed in `plugins` _before_ other plugins so that they may apply optimizations, such as dead code removal.

## Options

In addition to the properties and values specified for injecting, users may also specify the options below.

### `exclude`

Type: `String` | `Array[...String]`<br>
Default: `null`

A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.

### `include`

Type: `String` | `Array[...String]`<br>
Default: `null`

A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.

## Meta

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

[LICENSE (MIT)](/LICENSE)

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