Skip to content

Commit

Permalink
chore: add rollup-plugin-inject (#19)
Browse files Browse the repository at this point in the history
* initial version

* Update test to snapshot testing

* Disable extra eslint rules

* Replace for-of to forEach

* Update packages/inject/CHANGELOG.md

Co-Authored-By: Andrew Powell <shellscape@users.noreply.github.com>

* Update packages/inject/package.json

Co-Authored-By: Andrew Powell <shellscape@users.noreply.github.com>

* Update packages/inject/README.md

Co-Authored-By: Andrew Powell <shellscape@users.noreply.github.com>

* Update packages/inject/package.json

Co-Authored-By: Andrew Powell <shellscape@users.noreply.github.com>

* Update packages/inject/README.md

Co-Authored-By: Andrew Powell <shellscape@users.noreply.github.com>

* chore: Update packages/inject/package.json

* chore: Update packages/inject/package.json

* chore: Apply suggestions from code review

* chore: bump package major
  • Loading branch information
btd authored and shellscape committed Oct 31, 2019
1 parent 27d70c1 commit 866e5dc
Show file tree
Hide file tree
Showing 24 changed files with 799 additions and 24 deletions.
54 changes: 54 additions & 0 deletions packages/inject/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# @rollup/plugin-inject Changelog

## 3.0.2

* Fix bug with sourcemap usage

## 3.0.1

* Generate sourcemap when sourcemap enabled

## 3.0.0

* Remove node v6 from support
* Use modern js

## 2.1.0

* Update all dependencies ([#15](https://github.com/rollup/rollup-plugin-inject/pull/15))

## 2.0.0

* Work with all file extensions, not just `.js` (unless otherwise specified via `options.include` and `options.exclude`) ([#6](https://github.com/rollup/rollup-plugin-inject/pull/6))
* Allow `*` imports ([#9](https://github.com/rollup/rollup-plugin-inject/pull/9))
* Ignore replacements that are superseded (e.g. if `Buffer.isBuffer` is replaced, ignore `Buffer` replacement) ([#10](https://github.com/rollup/rollup-plugin-inject/pull/10))

## 1.4.1

* Return a `name`

## 1.4.0

* Use `string.search` instead of `regex.test` to avoid state-related mishaps ([#5](https://github.com/rollup/rollup-plugin-inject/issues/5))
* Prevent self-importing module bug

## 1.3.0

* Windows support ([#2](https://github.com/rollup/rollup-plugin-inject/issues/2))
* Node 0.12 support

## 1.2.0

* Generate sourcemaps by default

## 1.1.1

* Use `modules` option

## 1.1.0

* Handle shorthand properties

## 1.0.0

* First release
99 changes: 99 additions & 0 deletions packages/inject/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
[cover]: https://codecov.io/gh/rollup/plugins/inject/branch/master/graph/badge.svg
[cover-url]: https://codecov.io/gh/rollup/plugins
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-inject
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-inject
[tests]: https://img.shields.io/circleci/project/github/rollup/plugins.svg
[tests-url]: https://circleci.com/gh/rollup/plugins

[![tests][tests]][tests-url]
[![cover][cover]][cover-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 (v8.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]`
Default: `null`

A [minimatch pattern](https://github.com/isaacs/minimatch), 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)`
Default: `null`

A [minimatch pattern](https://github.com/isaacs/minimatch), 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)
32 changes: 32 additions & 0 deletions packages/inject/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Plugin } from "rollup";

type Injectment = string | [string, string];

interface RollupInjectOptions {
/**
* A minimatch pattern, or array of patterns, of files that should be
* processed by this plugin (if omitted, all files are included by default)
*/
include?: string | RegExp | ReadonlyArray<string | RegExp> | null;

/**
* Files that should be excluded, if `include` is otherwise too permissive.
*/
exclude?: string | RegExp | ReadonlyArray<string | RegExp> | null;

/**
* You can separate values to inject from other options.
*/
modules?: { [str: string]: Injectment };

/**
* All other options are treated as `string: injectment` injectrs,
* or `string: (id) => injectment` functions.
*/
[str: string]: Injectment | RollupInjectOptions["include"] | RollupInjectOptions["modules"];
}

/**
* inject strings in files while bundling them.
*/
export default function inject(options?: RollupInjectOptions): Plugin;
71 changes: 71 additions & 0 deletions packages/inject/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "@rollup/plugin-inject",
"version": "4.0.0",
"publishConfig": {
"access": "public"
},
"license": "MIT",
"repository": "rollup/plugins",
"author": "Rich Harris <richard.a.harris@gmail.com>",
"homepage": "https://github.com/rollup/plugins",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "dist/index.js",
"scripts": {
"build": "rollup -c",
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov",
"ci:coverage:submit": "curl -s https://codecov.io/bash | bash -s - -F inject",
"ci:lint": "pnpm run build && pnpm run lint && pnpm run security",
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
"ci:test": "pnpm run test -- --verbose && pnpm run test:ts",
"lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package",
"lint:docs": "prettier --single-quote --write README.md",
"lint:js": "eslint --fix --cache src test",
"lint:package": "prettier --write package.json --plugin=prettier-plugin-package",
"prebuild": "del-cli dist",
"prepare": "npm run build",
"prepublishOnly": "npm run lint && npm run test",
"pretest": "npm run build",
"security": "echo 'pnpm needs `npm audit` support'",
"test": "ava",
"test:ts": "tsc index.d.ts test/types.ts --noEmit"
},
"files": [
"dist",
"index.d.ts",
"LICENSE",
"README.md"
],
"keywords": [
"rollup",
"plugin",
"inject",
"es2015",
"npm",
"modules"
],
"peerDependencies": {
"rollup": "^1.20.0"
},
"dependencies": {
"estree-walker": "^0.9.0",
"magic-string": "^0.25.2",
"rollup-pluginutils": "^2.6.0"
},
"devDependencies": {
"del-cli": "^3.0.0",
"locate-character": "^2.0.5",
"rollup": "^1.20.0",
"rollup-plugin-buble": "^0.19.6",
"source-map": "^0.7.3",
"typescript": "^3.4.3"
},
"ava": {
"files": [
"!**/fixtures/**",
"!**/helpers/**",
"!**/recipes/**",
"!**/types.ts"
]
},
"module": "dist/index.es.js"
}
12 changes: 12 additions & 0 deletions packages/inject/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import buble from "rollup-plugin-buble";

import pkg from "./package.json";

const external = Object.keys(pkg.dependencies).concat("path");

export default {
input: "src/index.js",
plugins: [buble()],
external,
output: [{ file: pkg.main, format: "cjs" }, { file: pkg.module, format: "es" }]
};
Loading

0 comments on commit 866e5dc

Please sign in to comment.