Skip to content

Commit c6725f4

Browse files
committed
Update readme
1 parent c7134b2 commit c6725f4

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

README.md

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
A Rollup plugin that automatically declares NodeJS built-in modules as `external`. Also handles npm dependencies, devDependencies, peerDependencies and optionalDependencies. Works in monorepos too!
33

44
> ## Breaking changes in version 4
5-
> - In previous versions, the `deps` option (see below) defaulted to `false`. This was practial, 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`.
6-
> - `rollup-plugin-node-externals` now requires Node 14 (up from Node 12 for previous versions).
7-
> - `rollup-plugin-node-externals` now has a _peer dependency_ on Rollup 2.60.0.
5+
> - 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`.
6+
> - Now requires Node 14 (up from Node 12 for previous versions).
7+
> - Now has a _peer dependency_ on Rollup 2.60.0.
88
9-
## Why?
9+
## Why do you need this?
1010
<details><summary>(click to expand)</summary>
1111
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.
1212

@@ -27,12 +27,12 @@ npm install --save-dev rollup-plugin-node-externals
2727

2828
## Usage
2929

30-
- To bundle _a package that depends on other packages **at runtime**_ (e.g., a library or a NodeJS CLI), install your own dependencies with `--save`. Then, the built-in defaults are just what you need:
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:
3131
```typescript
3232
export default {
3333
...
3434
plugins: [
35-
externals(), // Make all Node builtins, deps, devDeps, peerDeps and optDeps external
35+
externals(), // Make all Node builtins and all dependencies external
3636
]
3737
}
3838
```
@@ -43,13 +43,26 @@ export default {
4343
...
4444
plugins: [
4545
externals({
46-
deps: false, // If you installed your own deps with --save
47-
devDeps: false // If you installed your own deps with --save-dev
46+
deps: false, // Dependencies will be bundled in
4847
}),
4948
]
5049
}
5150
```
5251

52+
- 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
54+
export default {
55+
...
56+
plugins: [
57+
externals({
58+
deps: true, // Regular dependencies are external
59+
devDeps: false // devDependencies will be bundled in
60+
}),
61+
]
62+
}
63+
```
64+
65+
5366
### Options
5467

5568
```typescript
@@ -59,45 +72,49 @@ export default {
5972
...
6073
plugins: [
6174
externals({
62-
// The path(s) to your package.json. Optional. See below for default.
63-
packagePath?: string | string[],
64-
65-
// Make node builtins external. Optional. Default: true
75+
// Make node builtins external. Optional. Default: true.
6676
builtins?: boolean,
6777

68-
// Treat prefixed builtins as their unprefixed counterpart. Optional. Default: 'strip'
69-
prefixedBuiltins?: boolean | 'strip',
78+
// Treat prefixed builtins as their unprefixed counterpart. Optional. Default: 'strip' (will be 'add' in next major).
79+
prefixedBuiltins?: boolean | 'strip' | 'add',
80+
81+
// The path(s) to your package.json. Optional. See below for default.
82+
packagePath?: string | string[],
7083

71-
// Make pkg.dependencies external. Optional. Default: true
84+
// Make pkg.dependencies external. Optional. Default: true.
7285
deps?: boolean,
7386

74-
// Make pkg.devDependencies external. Optional. Default: true
87+
// Make pkg.devDependencies external. Optional. Default: true.
7588
devDeps?: boolean,
7689

77-
// Make pkg.peerDependencies external. Optional. Default: true
90+
// Make pkg.peerDependencies external. Optional. Default: true.
7891
peerDeps?: boolean,
7992

80-
// Make pkg.optionalDependencies external. Optional. Default: true
93+
// Make pkg.optionalDependencies external. Optional. Default: true.
8194
optDeps?: boolean,
8295

83-
// Modules to force include in externals. Optional. Default: []
96+
// Modules to force include in externals. Optional. Default: [].
8497
include?: string | RegExp | (string | RegExp)[],
8598

86-
// Modules to force exclude from externals. Optional. Default: []
99+
// Modules to force exclude from externals. Optional. Default: [].
87100
exclude?: string | RegExp | (string | RegExp)[]
88101
})
89102
]
90103
}
91104
```
92105

93-
#### packagePath?: string | string[] = []
94-
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.
95-
96106
#### builtins?: boolean = true
97107
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.
98108

99-
#### prefixedBuiltins?: boolean | 'strip' = 'strip'
100-
How to handle the `node:` (or sometimes `nodejs:`) prefix some authors use in their code (i.e., `import path from 'node:path'`). If `false`, the import is used as-is to determine if it is external, meaning that `'node:path'` and `'path'` are considered two distincts imports. If `true`, prefixed builtins are treated as their unprefixed equivalent. If `strip` (the default), the prefix is also removed from the name and other plugins will never know it was there.
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._
115+
116+
#### packagePath?: string | string[] = []
117+
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.
101118

102119
#### deps?: boolean = true
103120
#### devDeps?: boolean = true

0 commit comments

Comments
 (0)