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
+13-14Lines changed: 13 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,15 +6,15 @@ By default, Rollup doesn't know a thing about NodeJS, so trying to bundle simple
6
6
7
7
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).
8
8
9
-
However, this must be done for each and every NodeJS built-in that you happen to use in your program: `path`, `os`, `fs`, `url`, etc., which can quicky become cumbersome when done manually.
9
+
However, this must be done for each and every NodeJS built-in you happen to use in your program: `path`, `os`, `fs`, `url`, etc., which can quicky become cumbersome when done manually.
10
10
11
11
So the primary goal of this plugin is simply to automatically declare all NodeJS built-in modules as `external`.
12
12
13
-
As an added bonus, this plugin will also allow you to declare your dependencies (as declared in your local or monorepo `package.json` file) as external.
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
14
15
15
16
16
## Installation
17
-
Use your favorite package manager. Mine is npm.
17
+
Use your favorite package manager. Mine is [npm](https://www.npmjs.com).
Most of the time, the built-in defaults are just what you need:
63
64
```typescript
64
65
importexternalsfrom'rollup-plugin-node-externals'
@@ -71,40 +72,37 @@ export default {
71
72
}
72
73
```
73
74
74
-
### Options
75
-
By default, this plugin will mark all Node built-in modules and _all_ your `dev-`, `peer-` and `optionalDependencies` as external. Normal `dependencies` are left unmarked so Rollup will still bundle them with your code as expected in most situations.
76
-
77
75
#### packagePath?: string | string[] = []
78
76
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.
79
77
80
78
#### builtins?: boolean = true
81
-
Set the `builtins` option to `false` if you'd like to use some shims for those.
79
+
Set the `builtins` option to `false` if you'd like to use some shims for those. You'll most certainly need an other plugin for this.
82
80
83
81
#### prefixedBuiltins?: boolean | 'strip' = true
84
82
How to handle the `node:` (or sometimes `nodejs:`) prefix some authors use in their code (i.e., `import path from 'node:path'`). If `true` (default), prefixed builtins are treated as their unprefixed equivalent. If `strip`, the prefix is removed from the name and other plugins will never know it was there.
85
83
86
84
#### deps?: boolean = false
87
85
Set the `deps` option to `true` to externalize your normal dependencies, therefore preventing Rollup from bundling them with your code.
88
86
89
-
#### devDeps?: boolean = true,
87
+
#### devDeps?: boolean = true
90
88
#### peerDeps?: boolean = true
91
89
#### optDeps?: boolean = true
92
-
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 `devDependencies`.
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`.
Use the `include` option to force certain dependencies into the list of externals:
97
95
```typescript
98
96
externals({
99
-
deps: false, //Bundle dependencies in
97
+
deps: false, //Deps will be bundled in
100
98
include:/^fsevents/// Except for fsevents
101
99
})
102
100
```
103
101
104
102
Conversely, use the `exclude` option to remove certain dependencies from the list of externals:
105
103
```typescript
106
104
externals({
107
-
deps: true, //Don't bundle dependencies, we'll import/require them at runtime instead
105
+
deps: true, //Deps are external
108
106
exclude: [
109
107
'electron-reload'// Yet we want `electron-reload` bundled in
110
108
]
@@ -116,7 +114,7 @@ externals({
116
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/`.
117
115
118
116
### 2/ This plugin is not _that_ smart
119
-
It uses an exact match against your imports, so if your are using some path substitution in your code, eg.:
117
+
It uses an exact match against your imports, so if your are using some kind of path substitution in your code, eg.:
120
118
```typescript
121
119
// In your code, say '@/' is mapped to some directory:
122
120
importsomethingfrom'@/mylib'
@@ -138,14 +136,15 @@ import resolve from '@rollup/plugin-node-resolve'
138
136
...
139
137
140
138
exportdefault {
141
-
//...
139
+
...
142
140
plugins: [
143
141
externals(),
144
142
resolve(),
145
-
// other plugins
143
+
...
146
144
]
147
145
}
148
146
```
147
+
As a general rule of thumb, you might want to always make this plugin the first one in the `plugins` array.
149
148
150
149
### 4/ Rollup rules
151
150
Rollup's own `external` configuration option always takes precedence over this plugin. This is intentional.
0 commit comments