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
+37-44Lines changed: 37 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,25 @@
1
1
# rollup-plugin-node-externals
2
-
3
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!
4
3
5
4
## Why?
5
+
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
6
7
-
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` error. The solution here is twofold:
8
-
* Either use some kind of shim like those provided by [rollup-plugin-node-builtins](https://github.com/calvinmetcalf/rollup-plugin-node-builtins).
9
-
* Or 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).
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).
10
8
11
-
However, this must be done for each and every NodeJS built-in: `path`, `os`, `fs`, etc., which can quicky become cumbersome when done manually. So the primary goal of this plugin is simply to automatically declare all NodeJS built-in modules as `external`.
12
-
> Note: the list of builtins is obtained via [the builtin-modules package](https://github.com/sindresorhus/builtin-modules) by Sindre Sorhus and should be up-to-date with your current NodeJS version.
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.
13
10
14
-
This plugin will also allow you to declare your dependencies (as declared in your `package.json` file) as `external`. This may come in handy when building an [Electron](https://electronjs.org) app, for example.
11
+
So the primary goal of this plugin is simply to automatically declare all NodeJS built-in modules as `external`.
15
12
13
+
This plugin will also allow you to declare your dependencies (as declared in your `package.json` file) as `external`.
// Make pkg.devDependencies external. Optional. Default: true
51
48
devDeps:true,
52
49
53
-
// Modules to exclude from externals. Optional. Default: none
50
+
// Modules to exclude from externals. Optional. Default: []
54
51
exclude: [],
55
52
56
-
// Modules to include in externals. Optional. Default: all
57
-
include: [],
58
-
59
-
// Deprecated -- see below
60
-
except: []
53
+
// Modules to include in externals. Optional. Default: []
54
+
include: []
61
55
})
62
56
]
63
57
}
@@ -75,46 +69,28 @@ export default {
75
69
}
76
70
```
77
71
78
-
> Note: if you're also using `@rollup/plugin-node-resolve`, make sure this plugin comes before it in the `plugins` array:
79
-
```js
80
-
importexternalsfrom'rollup-plugin-node-externals'
81
-
importresolvefrom'@rollup/plugin-node-resolve'
82
-
// ...
83
-
84
-
exportdefault {
85
-
// ...
86
-
plugins: [
87
-
externals(),
88
-
resolve(),
89
-
// other plugins
90
-
]
91
-
}
92
-
```
93
-
94
-
95
72
### Options
96
-
97
73
By default, the plugin will mark all Node builtin 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.
98
74
99
-
-**packagePath?: string | string[] = []**
75
+
#### packagePath?: string | string[] = []
100
76
101
77
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.
102
78
103
-
-**builtins?: boolean = true**
104
-
-**deps?: boolean = false**
105
-
-**devDeps?: boolean = true**
106
-
-**peerDeps?: boolean = true**
107
-
-**optDeps?: boolean = true**
79
+
#### builtins?: boolean = true
80
+
#### deps?: boolean = false
81
+
#### devDeps?: boolean = true
82
+
#### peerDeps?: boolean = true
83
+
#### optDeps?: boolean = true
108
84
109
85
Set the `builtins`, `deps`, `devDeps`, `peerDeps` and/or `optDeps` options to `false` to prevent the corresponding dependencies from being externalized, therefore letting Rollup bundle them with your code. Set them to `true` for Rollup to treat the corresponding dependencies as external.
0 commit comments