Skip to content

Commit b56cb90

Browse files
committed
Update readme
1 parent adc236d commit b56cb90

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

README.md

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,76 @@ A Rollup plugin that automatically declares NodeJS built-in modules as `external
33

44
Works in npm/yarn/pnpm/lerna monorepos too!
55

6+
67
## Why you need this
78
<details><summary>(click to expand)</summary>
89

910
By default, Rollup doesn't know a thing about NodeJS, so trying to bundle simple things like `import path from 'node:path'` in your code generates an `Unresolved dependencies` warning.
1011

1112
The solution here is quite simple: you must tell Rollup that the `node: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).
1213

13-
However, this must be done for each and every NodeJS built-in you happen to use in your program: `node:path`, `node:os`, `node:fs`, `node:url`, etc., which can quicky become cumbersome when done manually.
14+
However, this must be done for each and every NodeJS built-in you happen to use in your program: `node:path`, `node:os`, `node:fs`, `node:url`, etc., which can quickly become cumbersome when done manually.
1415

1516
So the primary goal of this plugin is simply to automatically declare all NodeJS built-in modules as external.
1617

1718
As an added bonus, this plugin will also allow you to declare your dependencies (as per your local or monorepo `package.json` file(s)) as external.
1819
</details>
1920

21+
2022
## Installation
2123
Use your favorite package manager. Mine is [npm](https://www.npmjs.com).
24+
2225
```sh
2326
npm install --save-dev rollup-plugin-node-externals
2427
```
2528

29+
2630
## Usage
27-
You generally want to have your **runtime dependencies** listed under `dependencies` in `package.json`, and your **development dependencies** listed under `devDependencies`.
2831

29-
If you follow this simple rule, then the defaults are just what you need:
32+
### Import
33+
The plugin is available both as the default export and as a named export:
34+
35+
```js
36+
import nodeExternals from 'rollup-plugin-node-externals'
37+
```
38+
39+
and
40+
3041
```js
42+
import { nodeExternals } from 'rollup-plugin-node-externals'
43+
```
44+
45+
will both work.
46+
47+
> Note: an undocumented named export `externals` also exists that is kept in v6.1 for backwards compatibility only and will be removed in the next major version.
48+
49+
### Options
50+
You generally want to have your **runtime dependencies** (those that will be imported/required at runtime) listed under `dependencies` in `package.json`, and your **development dependencies** (those that should be bundled in by Rollup) listed under `devDependencies`.
51+
52+
If you follow this simple rule, then the defaults settings are just what you need:
53+
54+
```js
55+
// rollup.config.js
56+
3157
export default {
3258
...
3359
plugins: [
34-
externals(),
60+
nodeExternals(),
3561
]
3662
}
3763
```
3864

3965
This will bundle your `devDependencies` in while leaving your `dependencies`, `peerDependencies` and `optionalDependencies` external.
4066

41-
### Options
42-
All options are, well, optional.
67+
Should the defaults not suit your case, here is the full list of options.
4368

4469
```typescript
45-
import externals from 'rollup-plugin-node-externals'
70+
import nodeExternals from 'rollup-plugin-node-externals'
4671

4772
export default {
4873
...
4974
plugins: [
50-
externals({
75+
nodeExternals({
5176

5277
// Make node builtins external. Default: true.
5378
builtins?: boolean
@@ -85,13 +110,13 @@ Set the `builtins` option to `false` if you'd like to use some shims/polyfills f
85110

86111
#### builtinsPrefix?: 'add' | 'strip' | 'ignore' = 'add'
87112
How to handle the `node:` scheme used in recent versions of Node (i.e., `import path from 'node:path'`).<br>
88-
- If `add` (the default), the `node:` prefix is always added. In effect, this homogenizes all your imports of node builtins to their prefixed version.
89-
- If `strip` (the default), the import is always resolved unprefixed. In effect, this homogenizes all your imports of node builtins to their unprefixed version.
90-
- `ignore` will simply leave all prefixes as written in your code.
91-
> _Note that prefix handling is independant of the `builtins` options being enabled or disabled._
113+
- If `add` (the default, recommended), the `node:` prefix is always added. In effect, this homogenizes all your imports of Node builtins to their prefixed version.
114+
- If `strip` (the default), the import is always resolved unprefixed. In effect, this homogenizes all your imports of Node builtins to their unprefixed version.
115+
- `ignore` will simply leave all builtins imports as written in your code.
116+
> _Note that prefix handling is always applied, regardless of the `builtins` options being enabled or disabled._
92117
93118
#### packagePath?: string | string[] = []
94-
If you're working with monorepos, the `packagePath` option 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.
119+
If you're working with monorepos, the `packagePath` option 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.
95120

96121
#### deps?: boolean = true
97122
#### devDeps?: boolean = false
@@ -101,23 +126,27 @@ Set the `deps`, `devDeps`, `peerDeps` and `optDeps` options to `false` to preven
101126

102127
#### include?: string | RegExp | (string | RegExp)[] = []
103128
Use the `include` option to force certain dependencies into the list of externals, regardless of other settings:
129+
104130
```js
105-
externals({
131+
nodeExternals({
106132
deps: false, // Deps will be bundled in
107133
include: /^fsevents/ // Except for fsevents
108134
})
109135
```
110136

111137
#### exclude?: string | RegExp | (string | RegExp)[] = []
112138
Conversely, use the `exclude` option to remove certain dependencies from the list of externals, regardless of other settings:
139+
113140
```js
114-
externals({
141+
nodeExternals({
115142
deps: true, // Deps are external
116143
exclude: 'electron-reload' // Yet we want `electron-reload` bundled in
117144
})
118145
```
119146

147+
120148
## Notes
149+
121150
### 1/ This plugin is smart
122151
- Falsy values in `include` and `exclude` are silently ignored. This allows for conditional constructs like `exclude: process.env.NODE_ENV === 'production' && 'my-prod-only-dep'`.
123152
- Subpath imports are supported with regexes, meaning that `include: /^lodash/` will externalize `lodash` and also `lodash/map`, `lodash/merge`, etc.
@@ -134,7 +163,7 @@ and you don't want `mylib` bundled in, then write:
134163

135164
```js
136165
// In rollup.config.js:
137-
externals({
166+
nodeExternals({
138167
include: '@/mylib'
139168
})
140169
```
@@ -143,14 +172,14 @@ externals({
143172
If you're also using [`@rollup/plugin-node-resolve`](https://github.com/rollup/plugins/tree/master/packages/node-resolve/#readme), make sure this plugin comes _before_ it in the `plugins` array:
144173

145174
```js
146-
import externals from 'rollup-plugin-node-externals'
147-
import resolve from '@rollup/plugin-node-resolve'
175+
import nodeExternals from 'rollup-plugin-node-externals'
176+
import nodeResolve from '@rollup/plugin-node-resolve'
148177

149178
export default {
150179
...
151180
plugins: [
152-
externals(),
153-
resolve(),
181+
nodeExternals(),
182+
nodeResolve(),
154183
]
155184
}
156185
```

0 commit comments

Comments
 (0)