You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+42-25Lines changed: 42 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@
2
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!
3
3
4
4
> ## 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.
8
8
9
-
## Why?
9
+
## Why do you need this?
10
10
<details><summary>(click to expand)</summary>
11
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.
- 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:
31
31
```typescript
32
32
exportdefault {
33
33
...
34
34
plugins: [
35
-
externals(), // Make all Node builtins, deps, devDeps, peerDeps and optDeps external
35
+
externals(), // Make all Node builtinsand all dependencies external
36
36
]
37
37
}
38
38
```
@@ -43,13 +43,26 @@ export default {
43
43
...
44
44
plugins: [
45
45
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
48
47
}),
49
48
]
50
49
}
51
50
```
52
51
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
+
exportdefault {
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
+
53
66
### Options
54
67
55
68
```typescript
@@ -59,45 +72,49 @@ export default {
59
72
...
60
73
plugins: [
61
74
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.
66
76
builtins?: boolean,
67
77
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[],
70
83
71
-
// Make pkg.dependencies external. Optional. Default: true
84
+
// Make pkg.dependencies external. Optional. Default: true.
72
85
deps?: boolean,
73
86
74
-
// Make pkg.devDependencies external. Optional. Default: true
87
+
// Make pkg.devDependencies external. Optional. Default: true.
75
88
devDeps?: boolean,
76
89
77
-
// Make pkg.peerDependencies external. Optional. Default: true
90
+
// Make pkg.peerDependencies external. Optional. Default: true.
78
91
peerDeps?: boolean,
79
92
80
-
// Make pkg.optionalDependencies external. Optional. Default: true
93
+
// Make pkg.optionalDependencies external. Optional. Default: true.
81
94
optDeps?: boolean,
82
95
83
-
// Modules to force include in externals. Optional. Default: []
96
+
// Modules to force include in externals. Optional. Default: [].
84
97
include?: string|RegExp| (string|RegExp)[],
85
98
86
-
// Modules to force exclude from externals. Optional. Default: []
99
+
// Modules to force exclude from externals. Optional. Default: [].
87
100
exclude?: string|RegExp| (string|RegExp)[]
88
101
})
89
102
]
90
103
}
91
104
```
92
105
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
-
96
106
#### builtins?: boolean = true
97
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.
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.
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.
0 commit comments