Skip to content

Commit d098d0e

Browse files
committed
Update readme
1 parent c6725f4 commit d098d0e

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

README.md

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# rollup-plugin-node-externals
2-
A Rollup plugin that automatically declares NodeJS built-in modules as `external`. Also handles npm dependencies, devDependencies, peerDependencies and optionalDependencies. Works in monorepos too!
2+
A Rollup plugin that automatically declares NodeJS built-in modules as `external`. Also handles npm dependencies, devDependencies, peerDependencies and optionalDependencies.
3+
4+
Works in monorepos too!
35

46
> ## Breaking changes in version 4
57
> - In previous versions, the `deps` option (see below) defaulted to `false`.<br>This was practical, but often wrong: when bundling for distribution, you want your own dependencies to be installed by the package manager alongside your package, so they should not be bundled in the code. Therefore, the `deps` option now defaults to `true`.
68
> - Now requires Node 14 (up from Node 12 for previous versions).
7-
> - Now has a _peer dependency_ on Rollup 2.60.0.
9+
> - Now has a _peer dependency_ on Rollup ^2.60.0.
810
9-
## Why do you need this?
11+
## Why you need this
1012
<details><summary>(click to expand)</summary>
11-
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` warning.
13+
14+
By default, Rollup doesn't know a thing about NodeJS, so trying to bundle simple things like `import path from 'path'` in your code generates an `Unresolved dependencies` warning.
1215

1316
The solution here is quite simple: you must 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).
1417

@@ -26,9 +29,8 @@ npm install --save-dev rollup-plugin-node-externals
2629
```
2730

2831
## Usage
29-
30-
- To bundle _a package that depends on other packages **at runtime**_ (e.g., a libray or a NodeJS CLI), the built-in defaults are just what you need:
31-
```typescript
32+
- To bundle a package that depends on other packages **at runtime**, the built-in defaults are just what you need:
33+
```js
3234
export default {
3335
...
3436
plugins: [
@@ -37,8 +39,8 @@ export default {
3739
}
3840
```
3941

40-
- To bundle _a standalone app_ (such as a browser app):
41-
```typescript
42+
- To bundle _a standalone package_:
43+
```js
4244
export default {
4345
...
4446
plugins: [
@@ -50,7 +52,7 @@ export default {
5052
```
5153

5254
- You may also want to bundle some libraries with your code but still `import`/`require` others at runtime. In that case, you could use something like:
53-
```typescript
55+
```js
5456
export default {
5557
...
5658
plugins: [
@@ -64,6 +66,7 @@ export default {
6466

6567

6668
### Options
69+
All options are, well, optional.
6770

6871
```typescript
6972
import externals from 'rollup-plugin-node-externals'
@@ -72,46 +75,58 @@ export default {
7275
...
7376
plugins: [
7477
externals({
75-
// Make node builtins external. Optional. Default: true.
78+
// Make node builtins external. Default: true.
7679
builtins?: boolean,
7780

78-
// Treat prefixed builtins as their unprefixed counterpart. Optional. Default: 'strip' (will be 'add' in next major).
81+
// node: prefix handing for importing Node builtins.
82+
// Default: 'strip' (will be 'add' in next major).
83+
builtinsPrefix?: 'add' | 'strip',
84+
85+
// DEPRECATED. Will be removed in next major.
86+
// Please use builtinsPrefix instead (see above).
87+
// Default: 'strip'
7988
prefixedBuiltins?: boolean | 'strip' | 'add',
8089

81-
// The path(s) to your package.json. Optional. See below for default.
90+
// The path(s) to your package.json. See below for default.
8291
packagePath?: string | string[],
8392

84-
// Make pkg.dependencies external. Optional. Default: true.
93+
// Make pkg.dependencies external. Default: true.
8594
deps?: boolean,
8695

87-
// Make pkg.devDependencies external. Optional. Default: true.
96+
// Make pkg.devDependencies external. Default: true.
8897
devDeps?: boolean,
8998

90-
// Make pkg.peerDependencies external. Optional. Default: true.
99+
// Make pkg.peerDependencies external. Default: true.
91100
peerDeps?: boolean,
92101

93-
// Make pkg.optionalDependencies external. Optional. Default: true.
102+
// Make pkg.optionalDependencies external. Default: true.
94103
optDeps?: boolean,
95104

96-
// Modules to force include in externals. Optional. Default: [].
105+
// Modules to force include in externals. Default: [].
97106
include?: string | RegExp | (string | RegExp)[],
98107

99-
// Modules to force exclude from externals. Optional. Default: [].
108+
// Modules to force exclude from externals. Default: [].
100109
exclude?: string | RegExp | (string | RegExp)[]
101110
})
102111
]
103112
}
104113
```
105114

106115
#### builtins?: boolean = true
107-
Set the `builtins` option to `false` if you'd like to use some shims for those. You'll most certainly need [an other plugin](https://github.com/rollup/plugins/tree/master/packages/node-resolve/#resolving-built-ins-like-fs) for this.
116+
Set the `builtins` option to `false` if you'd like to use some shims/polyfills for those. You'll most certainly need [an other plugin](https://github.com/ionic-team/rollup-plugin-node-polyfills) for this.
108117

109-
#### prefixedBuiltins?: boolean | 'strip' | 'add' = 'strip'
110-
How to handle the `node:` (or the legacy `nodejs:`) prefix some authors use in their code (i.e., `import path from 'node:path'`).
111-
- If `false`, the import is used as-is, meaning that `'node:path'` and `'path'` are considered two distincts imports. **This may cause redundant imports in your final code if you (or your dependencies's) are mixing prefixed and unprefixed imports.**
112-
- If `true`, prefixed builtins are simply treated as their unprefixed equivalent. _Note: This value is now deprecated and will be removed in the next major release of this plugin._
113-
- If `strip` (the default), the import is resolved unprefixed. In effect, this homogenizes all your node imports to their unprefixed version.
114-
- If `add`, the `node:` prefix is added. In effect, this homogenizes all your node imports to their prefixed version. _Note: `'add'` will be the default for this option in the next major release of this plugin._
118+
#### builtinsPrefix?: 'add' | 'strip' = 'strip'
119+
How to handle the `node:` scheme used in recent versions of Node (i.e., `import path from 'node:path'`).
120+
- If `strip` (the default), the import is always resolved unprefixed. In effect, this homogenizes all your imports of node builtins to their unprefixed version.
121+
- If `add`, the `node:` prefix is always added. In effect, this homogenizes all your imports of node builtins to their prefixed version.<br>
122+
_Note: `'add'` will be the default for this option in the next major release of this plugin._
123+
124+
#### [DEPRECATED] prefixedBuiltins?: boolean | 'add' | 'strip' = 'strip'
125+
How to handle the `node:` scheme used in recent versions of Node (i.e., `import path from 'node:path'`).
126+
- If `strip` (the default), the import is always resolved unprefixed. In effect, this homogenizes all your imports of node builtins to their unprefixed version.
127+
- If `add`, the `node:` prefix is always added. In effect, this homogenizes all your imports of node builtins to their prefixed version.<br>
128+
- If `false`, the import is used as-is, meaning that `'node:path'` and `'path'` are considered two distincts imports. **This may cause redundant imports in your final code if you (or your dependencies) are mixing prefixed and unprefixed imports.**<br>
129+
- `true` is the same as `add`.<br>
115130

116131
#### packagePath?: string | string[] = []
117132
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.
@@ -125,15 +140,15 @@ Set the `deps`, `devDeps`, `peerDeps` and `optDeps` options to `false` to preven
125140
#### include?: string | RegExp | (string | RegExp)[] = []
126141
#### exclude?: string | RegExp | (string | RegExp)[] = []
127142
Use the `include` option to force certain dependencies into the list of externals:
128-
```typescript
143+
```js
129144
externals({
130145
deps: false, // Deps will be bundled in
131146
include: /^fsevents/ // Except for fsevents
132147
})
133148
```
134149

135150
Conversely, use the `exclude` option to remove certain dependencies from the list of externals:
136-
```typescript
151+
```js
137152
externals({
138153
deps: true, // Deps are external
139154
exclude: 'electron-reload' // Yet we want `electron-reload` bundled in
@@ -142,11 +157,11 @@ externals({
142157

143158
## Notes
144159
### 1/ This plugin is smart
145-
Falsy values in `include` and `exclude` are silently ignored. This allows for conditional constructs like so: `exclude: process.env.NODE_ENV === 'production' && 'my-prod-only-dep'`.
160+
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'`.
146161

147162
### 2/ This plugin is not _that_ smart
148163
It uses an exact match against your imports, so if your are using some kind of path substitution in your code, eg.:
149-
```typescript
164+
```js
150165
// In your code, say '@/' is mapped to some directory:
151166
import something from '@/mylib'
152167
```
@@ -163,18 +178,15 @@ However, subpath imports are supported with regexes, meaning that `include: /^lo
163178

164179
### 3/ Order matters
165180
If you're also using `@rollup/plugin-node-resolve`, make sure this plugin comes _before_ it in the `plugins` array:
166-
```typescript
181+
```js
167182
import externals from 'rollup-plugin-node-externals'
168183
import resolve from '@rollup/plugin-node-resolve'
169184

170-
...
171-
172185
export default {
173186
...
174187
plugins: [
175188
externals(),
176189
resolve(),
177-
...
178190
]
179191
}
180192
```

0 commit comments

Comments
 (0)