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
+46-34Lines changed: 46 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,17 @@
1
1
# 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!
3
5
4
6
> ## Breaking changes in version 4
5
7
> - 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
8
> - 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.
8
10
9
-
## Why do you need this?
11
+
## Why you need this
10
12
<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.
12
15
13
16
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).
- 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
32
34
exportdefault {
33
35
...
34
36
plugins: [
@@ -37,8 +39,8 @@ export default {
37
39
}
38
40
```
39
41
40
-
- To bundle _a standalone app_ (such as a browser app):
41
-
```typescript
42
+
- To bundle _a standalone package_:
43
+
```js
42
44
exportdefault {
43
45
...
44
46
plugins: [
@@ -50,7 +52,7 @@ export default {
50
52
```
51
53
52
54
- 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
54
56
exportdefault {
55
57
...
56
58
plugins: [
@@ -64,6 +66,7 @@ export default {
64
66
65
67
66
68
### Options
69
+
All options are, well, optional.
67
70
68
71
```typescript
69
72
importexternalsfrom'rollup-plugin-node-externals'
@@ -72,46 +75,58 @@ export default {
72
75
...
73
76
plugins: [
74
77
externals({
75
-
// Make node builtins external. Optional. Default: true.
78
+
// Make node builtins external. Default: true.
76
79
builtins?: boolean,
77
80
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'
79
88
prefixedBuiltins?: boolean|'strip'|'add',
80
89
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.
82
91
packagePath?: string|string[],
83
92
84
-
// Make pkg.dependencies external. Optional. Default: true.
93
+
// Make pkg.dependencies external. Default: true.
85
94
deps?: boolean,
86
95
87
-
// Make pkg.devDependencies external. Optional. Default: true.
96
+
// Make pkg.devDependencies external. Default: true.
88
97
devDeps?: boolean,
89
98
90
-
// Make pkg.peerDependencies external. Optional. Default: true.
99
+
// Make pkg.peerDependencies external. Default: true.
91
100
peerDeps?: boolean,
92
101
93
-
// Make pkg.optionalDependencies external. Optional. Default: true.
102
+
// Make pkg.optionalDependencies external. Default: true.
94
103
optDeps?: boolean,
95
104
96
-
// Modules to force include in externals. Optional. Default: [].
105
+
// Modules to force include in externals. Default: [].
97
106
include?: string|RegExp| (string|RegExp)[],
98
107
99
-
// Modules to force exclude from externals. Optional. Default: [].
108
+
// Modules to force exclude from externals. Default: [].
100
109
exclude?: string|RegExp| (string|RegExp)[]
101
110
})
102
111
]
103
112
}
104
113
```
105
114
106
115
#### 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.
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._
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>
115
130
116
131
#### packagePath?: string | string[] = []
117
132
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
Use the `include` option to force certain dependencies into the list of externals:
128
-
```typescript
143
+
```js
129
144
externals({
130
145
deps:false, // Deps will be bundled in
131
146
include:/^fsevents/// Except for fsevents
132
147
})
133
148
```
134
149
135
150
Conversely, use the `exclude` option to remove certain dependencies from the list of externals:
136
-
```typescript
151
+
```js
137
152
externals({
138
153
deps:true, // Deps are external
139
154
exclude:'electron-reload'// Yet we want `electron-reload` bundled in
@@ -142,11 +157,11 @@ externals({
142
157
143
158
## Notes
144
159
### 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'`.
146
161
147
162
### 2/ This plugin is not _that_ smart
148
163
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
150
165
// In your code, say '@/' is mapped to some directory:
151
166
importsomethingfrom'@/mylib'
152
167
```
@@ -163,18 +178,15 @@ However, subpath imports are supported with regexes, meaning that `include: /^lo
163
178
164
179
### 3/ Order matters
165
180
If you're also using `@rollup/plugin-node-resolve`, make sure this plugin comes _before_ it in the `plugins` array:
0 commit comments