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
commit fe2733dba65ebe471e4b1a9736a0e7106d0cd3c6
Author: Stephan Schreiber <[email protected]>
Date: Sat Feb 5 19:39:09 2022 +0100
Pre 4.0
commit a39cceea0bf6f6d0e6cf888416d277ce73da9792
Author: Stephan Schreiber <[email protected]>
Date: Sat Feb 5 18:20:25 2022 +0100
Updates tests
commit ce5d5a0be1703fe99ba79b259019b3532c5d7790
Author: Stephan Schreiber <[email protected]>
Date: Sat Feb 5 18:20:04 2022 +0100
Update dependencies
Copy file name to clipboardExpand all lines: README.md
+47-44Lines changed: 47 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,13 @@
1
1
# rollup-plugin-node-externals
2
-
A Rollup plugin that automatically declares NodeJS built-in modules as `external`. Can also handle 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. Works in monorepos too!
3
+
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.
3
8
4
9
## Why?
10
+
<details><summary>(click to expand)</summary>
5
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.
6
12
7
13
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).
@@ -10,17 +16,42 @@ However, this must be done for each and every NodeJS built-in you happen to use
10
16
11
17
So the primary goal of this plugin is simply to automatically declare all NodeJS built-in modules as `external`.
12
18
13
-
As an added bonus, this plugin will also allow you to declare your dependencies (as per in your local or monorepo `package.json` file) as external.
14
-
19
+
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.
20
+
</details>
15
21
16
22
## Installation
17
23
Use your favorite package manager. Mine is [npm](https://www.npmjs.com).
- 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:
31
+
```typescript
32
+
exportdefault {
33
+
...
34
+
plugins: [
35
+
externals(), // Make all Node builtins, deps, devDeps, peerDeps and optDeps external
36
+
]
37
+
}
38
+
```
39
+
40
+
- To bundle _a standalone app_ (such as a browser app):
41
+
```typescript
42
+
exportdefault {
43
+
...
44
+
plugins: [
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
48
+
}),
49
+
]
50
+
}
51
+
```
52
+
53
+
### Options
54
+
24
55
```typescript
25
56
importexternalsfrom'rollup-plugin-node-externals'
26
57
@@ -37,7 +68,7 @@ export default {
37
68
// Treat prefixed builtins as their unprefixed counterpart. Optional. Default: 'strip'
38
69
prefixedBuiltins?: boolean|'strip',
39
70
40
-
// Make pkg.dependencies external. Optional. Default: false
71
+
// Make pkg.dependencies external. Optional. Default: true
41
72
deps?: boolean,
42
73
43
74
// Make pkg.devDependencies external. Optional. Default: true
@@ -59,59 +90,42 @@ export default {
59
90
}
60
91
```
61
92
62
-
### Options
63
-
Most of the time, the built-in defaults are just what you need:
64
-
```typescript
65
-
importexternalsfrom'rollup-plugin-node-externals'
66
-
67
-
exportdefault {
68
-
...
69
-
plugins: [
70
-
externals(), // Bundle deps in; make all Node builtins, devDeps, peerDeps and optDeps external
71
-
]
72
-
}
73
-
```
74
-
75
93
#### packagePath?: string | string[] = []
76
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.
77
95
78
96
#### builtins?: boolean = true
79
97
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` (default), the prefix is also removed from the name and other plugins will never know it was there.
83
-
84
-
#### deps?: boolean = false
85
-
Set the `deps` option to `true` to externalize your normal dependencies, therefore preventing Rollup from bundling them with your code.
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.
86
101
102
+
#### deps?: boolean = true
87
103
#### devDeps?: boolean = true
88
104
#### peerDeps?: boolean = true
89
105
#### optDeps?: boolean = true
90
-
Set the `devDeps`, `peerDeps` and `optDeps` options to `false` to prevent the corresponding dependencies from being externalized, therefore letting Rollup bundle them with your code. Note that bundling these dependencies is quite meaningless but it might be useful as a transitional step before migrating them to `dependencies`.
106
+
Set the `deps`, `devDeps`, `peerDeps` and `optDeps` options to `false` to prevent the corresponding dependencies from being externalized, therefore letting Rollup bundle them with your code.
Use the `include` option to force certain dependencies into the list of externals:
95
111
```typescript
96
112
externals({
97
-
deps: false, // Deps will be bundled in
98
-
include:/^fsevents/// Except for fsevents
113
+
deps: false, // Deps will be bundled in
114
+
include:/^fsevents/// Except for fsevents
99
115
})
100
116
```
101
117
102
118
Conversely, use the `exclude` option to remove certain dependencies from the list of externals:
103
119
```typescript
104
120
externals({
105
-
deps: true, // Deps are external
106
-
exclude: [
107
-
'electron-reload'// Yet we want `electron-reload` bundled in
108
-
]
121
+
deps: true, // Deps are external
122
+
exclude: 'electron-reload'// Yet we want `electron-reload` bundled in
109
123
})
110
124
```
111
125
112
126
## Notes
113
127
### 1/ This plugin is smart
114
-
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/`.
128
+
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'`.
115
129
116
130
### 2/ This plugin is not _that_ smart
117
131
It uses an exact match against your imports, so if your are using some kind of path substitution in your code, eg.:
@@ -127,6 +141,9 @@ externals({
127
141
})
128
142
```
129
143
144
+
However, subpath imports are supported with regexes, meaning that `include: /^lodash/` will also externalize `loadash/map`, `lodash/merge`, etc.
145
+
146
+
130
147
### 3/ Order matters
131
148
If you're also using `@rollup/plugin-node-resolve`, make sure this plugin comes _before_ it in the `plugins` array:
132
149
```typescript
@@ -150,19 +167,5 @@ As a general rule of thumb, you might want to always make this plugin the first
150
167
Rollup's own `external` configuration option always takes precedence over this plugin. This is intentional.
151
168
152
169
153
-
## Migrating from version 1.x
154
-
- In 1.x, normal dependencies were externalized by default. This is no more true since 2.0, so you'll need to change:
155
-
```typescript
156
-
externals()
157
-
```
158
-
to:
159
-
```typescript
160
-
externals({ deps: true })
161
-
```
162
-
if you want the same behavior.
163
-
164
-
- The `except` option from 1.x has been deprecated in 2.0 and removed in 3.0. Use the Rollup-friendlier `exclude` option instead.
0 commit comments