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
+49-20Lines changed: 49 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,51 +3,76 @@ A Rollup plugin that automatically declares NodeJS built-in modules as `external
3
3
4
4
Works in npm/yarn/pnpm/lerna monorepos too!
5
5
6
+
6
7
## Why you need this
7
8
<details><summary>(click to expand)</summary>
8
9
9
10
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.
10
11
11
12
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).
12
13
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.
14
15
15
16
So the primary goal of this plugin is simply to automatically declare all NodeJS built-in modules as external.
16
17
17
18
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.
18
19
</details>
19
20
21
+
20
22
## Installation
21
23
Use your favorite package manager. Mine is [npm](https://www.npmjs.com).
You generally want to have your **runtime dependencies** listed under `dependencies` in `package.json`, and your **development dependencies** listed under `devDependencies`.
28
31
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:
> 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
+
31
57
exportdefault {
32
58
...
33
59
plugins: [
34
-
externals(),
60
+
nodeExternals(),
35
61
]
36
62
}
37
63
```
38
64
39
65
This will bundle your `devDependencies` in while leaving your `dependencies`, `peerDependencies` and `optionalDependencies` external.
40
66
41
-
### Options
42
-
All options are, well, optional.
67
+
Should the defaults not suit your case, here is the full list of options.
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._
92
117
93
118
#### 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.
95
120
96
121
#### deps?: boolean = true
97
122
#### devDeps?: boolean = false
@@ -101,23 +126,27 @@ Set the `deps`, `devDeps`, `peerDeps` and `optDeps` options to `false` to preven
Conversely, use the `exclude` option to remove certain dependencies from the list of externals, regardless of other settings:
139
+
113
140
```js
114
-
externals({
141
+
nodeExternals({
115
142
deps:true, // Deps are external
116
143
exclude:'electron-reload'// Yet we want `electron-reload` bundled in
117
144
})
118
145
```
119
146
147
+
120
148
## Notes
149
+
121
150
### 1/ This plugin is smart
122
151
- 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'`.
123
152
- 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:
134
163
135
164
```js
136
165
// In rollup.config.js:
137
-
externals({
166
+
nodeExternals({
138
167
include:'@/mylib'
139
168
})
140
169
```
@@ -143,14 +172,14 @@ externals({
143
172
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:
0 commit comments