-
-
Notifications
You must be signed in to change notification settings - Fork 602
add rollup-plugin-inject #19
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3e5571f
initial version
btd b931ae8
Merge branch 'master' of github.com:rollup/plugins into add/inject
btd 1360dcb
Update test to snapshot testing
btd 41c3471
Disable extra eslint rules
btd 02155b9
Replace for-of to forEach
btd c62847e
Update packages/inject/CHANGELOG.md
btd e5a6f2d
Update packages/inject/package.json
btd fe9c73f
Update packages/inject/README.md
btd b579b1a
Update packages/inject/package.json
btd ba49645
Update packages/inject/README.md
btd ec3cff9
chore: Update packages/inject/package.json
shellscape 098e6ec
chore: Update packages/inject/package.json
shellscape d4a49f2
chore: Apply suggestions from code review
shellscape 32555b4
chore: bump package major
shellscape File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
[](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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import buble from "rollup-plugin-buble"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" }] | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.