Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rollup-plugin-inject #19

Merged
merged 14 commits into from
Oct 31, 2019
Merged
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 <[email protected]>",
"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"
btd marked this conversation as resolved.
Show resolved Hide resolved
],
"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";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need 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