Skip to content

Commit 44cbe98

Browse files
committed
Squashed commit of the following:
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
1 parent 589cd81 commit 44cbe98

File tree

16 files changed

+3733
-9184
lines changed

16 files changed

+3733
-9184
lines changed

.eslintrc

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@
1717
"no-unused-vars": "off",
1818
"prefer-const": "warn",
1919
"no-inner-declarations": "off",
20-
"@typescript-eslint/no-explicit-any": "off",
21-
"@typescript-eslint/no-unused-vars": "off",
22-
"@typescript-eslint/no-this-alias": [
23-
"error",
24-
{
25-
"allowDestructuring": true,
26-
"allowedNames": ["self"]
27-
}
28-
]
20+
"@typescript-eslint/no-unused-vars": "off"
2921
}
3022
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
22
dist
3+
out
4+
zz*

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Plugin",
9+
"type": "pwa-node",
10+
"request": "launch",
11+
"skipFiles": [
12+
"<node_internals>/**",
13+
"${workspaceFolder}/node_modules/**"
14+
],
15+
"cwd": "${workspaceFolder}",
16+
"program": "${workspaceFolder}/node_modules/rollup/dist/bin/rollup",
17+
"windows": {
18+
"program": "${workspaceFolder}\\node_modules\\rollup\\dist\\bin\\rollup"
19+
},
20+
"args": [ "-c", "zz_rollup.config.js" ],
21+
"outputCapture": "std"
22+
}
23+
]
24+
}

README.md

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# 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.
38
49
## Why?
10+
<details><summary>(click to expand)</summary>
511
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.
612

713
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
1016

1117
So the primary goal of this plugin is simply to automatically declare all NodeJS built-in modules as `external`.
1218

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>
1521

1622
## Installation
1723
Use your favorite package manager. Mine is [npm](https://www.npmjs.com).
1824
```sh
1925
npm install --save-dev rollup-plugin-node-externals
2026
```
2127

22-
2328
## Usage
29+
30+
- 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+
export default {
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+
export default {
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+
2455
```typescript
2556
import externals from 'rollup-plugin-node-externals'
2657

@@ -37,7 +68,7 @@ export default {
3768
// Treat prefixed builtins as their unprefixed counterpart. Optional. Default: 'strip'
3869
prefixedBuiltins?: boolean | 'strip',
3970

40-
// Make pkg.dependencies external. Optional. Default: false
71+
// Make pkg.dependencies external. Optional. Default: true
4172
deps?: boolean,
4273

4374
// Make pkg.devDependencies external. Optional. Default: true
@@ -59,59 +90,42 @@ export default {
5990
}
6091
```
6192

62-
### Options
63-
Most of the time, the built-in defaults are just what you need:
64-
```typescript
65-
import externals from 'rollup-plugin-node-externals'
66-
67-
export default {
68-
...
69-
plugins: [
70-
externals(), // Bundle deps in; make all Node builtins, devDeps, peerDeps and optDeps external
71-
]
72-
}
73-
```
74-
7593
#### packagePath?: string | string[] = []
7694
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.
7795

7896
#### builtins?: boolean = true
7997
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.
8098

8199
#### prefixedBuiltins?: boolean | 'strip' = 'strip'
82-
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.
86101

102+
#### deps?: boolean = true
87103
#### devDeps?: boolean = true
88104
#### peerDeps?: boolean = true
89105
#### 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.
91107

92108
#### include?: string | RegExp | (string | RegExp)[] = []
93109
#### exclude?: string | RegExp | (string | RegExp)[] = []
94110
Use the `include` option to force certain dependencies into the list of externals:
95111
```typescript
96112
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
99115
})
100116
```
101117

102118
Conversely, use the `exclude` option to remove certain dependencies from the list of externals:
103119
```typescript
104120
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
109123
})
110124
```
111125

112126
## Notes
113127
### 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'`.
115129

116130
### 2/ This plugin is not _that_ smart
117131
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({
127141
})
128142
```
129143

144+
However, subpath imports are supported with regexes, meaning that `include: /^lodash/` will also externalize `loadash/map`, `lodash/merge`, etc.
145+
146+
130147
### 3/ Order matters
131148
If you're also using `@rollup/plugin-node-resolve`, make sure this plugin comes _before_ it in the `plugins` array:
132149
```typescript
@@ -150,19 +167,5 @@ As a general rule of thumb, you might want to always make this plugin the first
150167
Rollup's own `external` configuration option always takes precedence over this plugin. This is intentional.
151168

152169

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.
165-
166-
167170
## Licence
168171
MIT

0 commit comments

Comments
 (0)