Skip to content

Commit 3bc8d64

Browse files
author
Stephan Septh Schreiber
committed
Merge #8, update doc
1 parent bb7736f commit 3bc8d64

File tree

3 files changed

+56
-37
lines changed

3 files changed

+56
-37
lines changed

README.md

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# rollup-plugin-node-externals
22

3-
A Rollup plugin that automatically declares NodeJS built-in modules as `external`. Can also handle npm dependencies, devDependencies, peerDependencies and optionalDependencies.
3+
A Rollup plugin that automatically declares NodeJS built-in modules as `external`. Can also handle npm dependencies, devDependencies, peerDependencies and optionalDependencies. Works in monorepos too!
44

55
## Why?
66

7-
By default, Rollup doesn't know a thing about NodeJS, so trying to bundle simple things like `import * as path from 'path'` in your code generates an `Unresolved dependencies` error. The solution here is twofold, depending on what you're building:
8-
* If building a *standalone app*, e.g. for the browser, you'll need to use some kind of shim like those provided by [rollup-plugin-node-builtins](https://github.com/calvinmetcalf/rollup-plugin-node-builtins).
9-
* If building *an npm module or lib*, you'll need to tell Rollup that the `path` module is in fact `external`: this way, Rollup won't try to bundle the module in and rather leave the `import` statement as is (or translate it to a `require()` call if bundling for CommonJS).
7+
By default, Rollup doesn't know a thing about NodeJS, so trying to bundle simple things like `import * as path from 'path'` in your code generates an `Unresolved dependencies` error. The solution here is twofold:
8+
* Either use some kind of shim like those provided by [rollup-plugin-node-builtins](https://github.com/calvinmetcalf/rollup-plugin-node-builtins).
9+
* Or tell Rollup that the `path` module is in fact `external`: this way, Rollup won't try to bundle it in and rather leave the `import` statement as is (or translate it to a `require()` call if bundling for CommonJS).
1010

1111
However, this must be done for each and every NodeJS built-in: `path`, `os`, `fs`, etc., which can quicky become cumbersome when done manually. So the primary goal of this plugin is simply to automatically declare all NodeJS built-in modules as `external`.
12+
> Note: the list of builtins is obtained via [the builtin-modules package](https://github.com/sindresorhus/builtin-modules) by Sindre Sorhus and should be up-to-date with your current NodeJS version.
1213
1314
This plugin will also allow you to declare your dependencies (as declared in your `package.json` file) as `external`. This may come in handy when building an [Electron](https://electronjs.org) app, for example.
1415

@@ -26,13 +27,15 @@ npm install --save-dev rollup-plugin-node-externals
2627
import externals from 'rollup-plugin-node-externals'
2728

2829
export default {
29-
// ...
30+
input: { ... },
31+
output: { ... },
3032
plugins: [
3133
externals({
32-
// The path to your package.json. Optional. Default: process.cwd() which is usally the same dir where rollup.config.js stands
34+
// The path(s) to your package.json. Optional.
35+
// Can be a string or an array of strings for monorepos -- see below
3336
packagePath: 'path/to/package.json',
3437

35-
// Make node built-ins external. Optional. Default: true
38+
// Make node builtins external. Optional. Default: true
3639
builtins: true,
3740

3841
// Make pkg.dependencies external. Optional. Default: false
@@ -63,22 +66,19 @@ export default {
6366
Most of the time, the built-in defaults are just what you need:
6467
```js
6568
import externals from 'rollup-plugin-node-externals'
66-
// ...
6769

6870
export default {
6971
// ...
7072
plugins: [
71-
externals(), // Bundle deps in; make Node built-ins, devDeps, peerDeps and optDeps external
73+
externals(), // Bundle deps in; make all Node builtins, devDeps, peerDeps and optDeps external
7274
]
7375
}
7476
```
7577

76-
> Note: the list of builtins is obtained via [the builtin-modules package](https://github.com/sindresorhus/builtin-modules), by Sindre Sorhus and should be up-to-date with your current NodeJS version.
77-
78-
> Note: if you're also using `rollup-plugin-node-resolve`, make sure this plugin comes before it in the `plugins` array:
78+
> Note: if you're also using `@rollup/plugin-node-resolve`, make sure this plugin comes before it in the `plugins` array:
7979
```js
8080
import externals from 'rollup-plugin-node-externals'
81-
import resolve from 'rollup-plugin-node-resolve'
81+
import resolve from '@rollup/plugin-node-resolve'
8282
// ...
8383

8484
export default {
@@ -94,43 +94,54 @@ export default {
9494

9595
### Options
9696

97-
By default, the plugin will mark all Node built-in modules and _all_ your `dev-`, `peer-` and `optionalDependencies` as external. Normal `dependencies` are left unmarked so Rollup will still bundle them with your code as expected in most situations.
97+
By default, the plugin will mark all Node builtin modules and _all_ your `dev-`, `peer-` and `optionalDependencies` as external. Normal `dependencies` are left unmarked so Rollup will still bundle them with your code as expected in most situations.
98+
99+
- **packagePath?: string | string[] = []**
100+
101+
If you're working with monorepos, the `packagePath` is made for you. It can take a path, or an array of paths, to your package.json file(s). If not specified, the default is to start with the current directory's package.json then go up scan for all package.json files in parent directories recursively until either the root git directory is reached or until no other package.json can be found.
98102

99-
- Set the `deps`, `devDeps`, `peerDeps` and/or `optDeps` options to `false` to prevent the corresponding dependencies from being externalized, therefore letting Rollup bundle them with your code. Set them to `true` for Rollup to treat the corresponding dependencies as external.
103+
- **builtins?: boolean = true**
104+
- **deps?: boolean = false**
105+
- **devDeps?: boolean = true**
106+
- **peerDeps?: boolean = true**
107+
- **optDeps?: boolean = true**
100108

101-
- Use the `exclude` option to remove certain dependencies from the list of externals. `exclude` can be a string, a regex, or an array of those, for example:
109+
Set the `builtins`, `deps`, `devDeps`, `peerDeps` and/or `optDeps` options to `false` to prevent the corresponding dependencies from being externalized, therefore letting Rollup bundle them with your code. Set them to `true` for Rollup to treat the corresponding dependencies as external.
110+
111+
- **include?: string | RegExp | (string | RegExp)[] = []**
112+
- **exclude?: string | RegExp | (string | RegExp)[] = []**
113+
114+
Use the `exclude` option to remove certain dependencies from the list of externals, for example:
102115
```js
103116
externals({
104-
deps: true, // Don't bundle dependencies, we'll require() them at runtime instead
105-
exclude: [
106-
'electron-reload', // Yet we want `electron-reload` bundled in
107-
/^vuex?/ // as well as the VueJS family (vue, vuex, vue-router, etc.)
108-
]
117+
deps: true, // Don't bundle dependencies, we'll require() them at runtime instead
118+
exclude: [
119+
'electron-reload', // Yet we want `electron-reload` bundled in
120+
/^vuex?/ // as well as the VueJS family (vue, vuex, vue-router, etc.)
121+
]
109122
})
110123
```
111124

112-
- Use the `include` option to force certain dependencies into the list of externals, for example:
125+
Use the `include` option to force certain dependencies into the list of externals, for example:
113126
```js
114127
externals({
115-
peerDeps: false, // Bundle peerDependencies in
116-
include: /^lodash/ // Except for Lodash
128+
peerDeps: false, // Bundle peerDependencies in
129+
include: /^lodash/ // Except for Lodash
117130
})
118131
```
119-
Just like `exclude`, the `include` option can be a string, a regex or an array of those.
120132

121133
> Note 1 : falsy values in `include` and `exclude` are silently ignored. This allows for conditional constructs like so: `exclude: process.env.NODE_ENV === 'production' && /mydep/`.
122134
123135
> Note2 : this plugin uses an exact match against your imports, so if your are using some path substitution in your code, eg.:
124136
```js
125-
// in your code:
126-
import something from '@/mylib' // Say '@/' is mapped to some directory
127-
...
137+
// in your code, say '@/' is mapped to some directory:
138+
import something from '@/mylib'
128139
```
129140
and you don't want `mylib` bundled in, then write:
130141
```js
131142
// in rollup.config.js:
132143
externals({
133-
include: '@/mylib' // /^@\// or /mylib$/ would work too
144+
include: '@/mylib'
134145
})
135146
```
136147

@@ -145,8 +156,9 @@ to:
145156
externals({ deps: true })
146157
```
147158
if you want the same behavior.
148-
- For consistency with all other Rollup plugins out there, the `except` option from 1.x is now deprecated in favor of the Rollup-friendly `exclude` option.
149-
`except` is still accepted for backward compatibility and works exactly the same as `exclude` but it will issue a warning if used. To suppress this warning, just replace `except` with `exclude`.
159+
160+
161+
- For consistency with all other Rollup plugins out there, the `except` option from 1.x is now deprecated in favor of the Rollup-friendly `exclude` option. It will be removed in the next major release but is still accepted for backward compatibility and works exactly the same as `exclude` but it will issue a warning if used. To suppress this warning, just replace `except` with `exclude`.
150162

151163

152164
## Licence

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"version": "2.1.6",
44
"description": "Automatically declare NodeJS built-in modules and npm dependencies as 'external' in Rollup config",
55
"author": "Stephan Schreiber <[email protected]>",
6+
"contributors": [
7+
"Tomer Aberbach <[email protected]>",
8+
"Elad Ossadon <[email protected]>"
9+
],
610
"license": "MIT",
711
"keywords": [
812
"rollup",
@@ -15,7 +19,8 @@
1519
"bundle",
1620
"external",
1721
"dependencies",
18-
"package.json"
22+
"package.json",
23+
"monorepo"
1924
],
2025
"engines": {
2126
"node": ">=8.0.0"
@@ -45,7 +50,7 @@
4550
"scripts": {
4651
"build": "rollup -c",
4752
"watch": "rollup -cw",
48-
"types": "tsc src/index.ts --downlevelIteration --declaration --emitDeclarationOnly --outDir dist",
53+
"types": "tsc src/index.ts --downlevelIteration --declaration --emitDeclarationOnly --esModuleInterop --outDir dist",
4954
"test": "ava",
5055
"watch-tests": "ava -w",
5156
"clean": "trash dist",

src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ export default function externals(options: ExternalsOptions = {}): Plugin {
103103
},
104104

105105
async buildStart() {
106-
let msg: string | undefined
107-
while (msg = warnings.shift()) {
108-
this.warn(msg)
109-
}
110106

111107
// Find and filter dependencies
112108
const dependencies = (await findDependencies({
@@ -120,6 +116,12 @@ export default function externals(options: ExternalsOptions = {}): Plugin {
120116
warnings
121117
})).filter(f)
122118

119+
// Issue the warnings we may have collected
120+
let msg: string | undefined
121+
while (msg = warnings.shift()) {
122+
this.warn(msg)
123+
}
124+
123125
// Build regexes
124126
if (builtins.length > 0) {
125127
externals.push(new RegExp('^(?:' + builtins.join('|') + ')(\/.+)?$'))

0 commit comments

Comments
 (0)