Skip to content

Commit 1af7921

Browse files
committed
Add prefixedBuiltins option
1 parent 8ff8010 commit 1af7921

File tree

7 files changed

+123
-86
lines changed

7 files changed

+123
-86
lines changed

.eslintrc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
"eslint:recommended",
99
"plugin:@typescript-eslint/recommended"
1010
],
11+
"env": {
12+
"node": true,
13+
"es2021": true
14+
},
1115
"rules": {
1216
"no-debugger": "off",
1317
"no-unused-vars": "off",
1418
"prefer-const": "warn",
1519
"no-inner-declarations": "off",
1620
"@typescript-eslint/no-explicit-any": "off",
17-
// "@typescript-eslint/no-unused-vars": "off",
18-
// "@typescript-eslint/no-non-null-assertion": "off",
21+
"@typescript-eslint/no-unused-vars": "off",
1922
"@typescript-eslint/no-this-alias": [
2023
"error",
2124
{

README.md

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,119 +10,132 @@ However, this must be done for each and every NodeJS built-in that you happen to
1010

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

13-
This plugin will also allow you to declare your dependencies (as declared in your `package.json` file) as `external`.
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.
1414

1515

16-
## Install
16+
## Installation
17+
Use your favorite package manager. Mine is npm.
1718
```sh
1819
npm install --save-dev rollup-plugin-node-externals
1920
```
2021

2122

2223
## Usage
23-
```js
24+
```typescript
2425
import externals from 'rollup-plugin-node-externals'
2526

2627
export default {
27-
input: { ... },
28-
output: { ... },
28+
...
2929
plugins: [
3030
externals({
31-
// The path(s) to your package.json. Optional.
32-
// Can be a string or an array of strings for monorepos -- see below
33-
packagePath: 'path/to/package.json',
31+
// The path(s) to your package.json. Optional. See below for default.
32+
packagePath?: string | string[],
3433

3534
// Make node builtins external. Optional. Default: true
36-
builtins: true,
35+
builtins?: boolean,
36+
37+
// Treat prefixed builtins as their unprefixed counterpart. Optional. Default: true
38+
prefixedBuiltins?: boolean | 'strip',
3739

3840
// Make pkg.dependencies external. Optional. Default: false
39-
deps: false,
41+
deps?: boolean,
42+
43+
// Make pkg.devDependencies external. Optional. Default: true
44+
devDeps?: boolean,
4045

4146
// Make pkg.peerDependencies external. Optional. Default: true
42-
peerDeps: true,
47+
peerDeps?: boolean,
4348

4449
// Make pkg.optionalDependencies external. Optional. Default: true
45-
optDeps: true,
46-
47-
// Make pkg.devDependencies external. Optional. Default: true
48-
devDeps: true,
50+
optDeps?: boolean,
4951

50-
// Modules to exclude from externals. Optional. Default: []
51-
exclude: [],
52+
// Modules to force include in externals. Optional. Default: []
53+
include?: string | RegExp | (string | RegExp)[],
5254

53-
// Modules to include in externals. Optional. Default: []
54-
include: []
55+
// Modules to force exclude from externals. Optional. Default: []
56+
exclude?: string | RegExp | (string | RegExp)[]
5557
})
5658
]
5759
}
5860
```
5961

6062
Most of the time, the built-in defaults are just what you need:
61-
```js
63+
```typescript
6264
import externals from 'rollup-plugin-node-externals'
6365

6466
export default {
65-
// ...
67+
...
6668
plugins: [
6769
externals(), // Bundle deps in; make all Node builtins, devDeps, peerDeps and optDeps external
6870
]
6971
}
7072
```
7173

7274
### Options
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.
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.
7476

7577
#### packagePath?: string | string[] = []
7678
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.
7779

7880
#### builtins?: boolean = true
81+
Set the `builtins` option to `false` if you'd like to use some shims for those.
82+
83+
#### prefixedBuiltins?: boolean | 'strip' = true
84+
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+
7986
#### deps?: boolean = false
80-
#### devDeps?: boolean = true
87+
Set the `deps` option to `true` to externalize your normal dependencies, therefore preventing Rollup from bundling them with your code.
88+
89+
#### devDeps?: boolean = true,
8190
#### peerDeps?: boolean = true
8291
#### optDeps?: boolean = true
83-
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.
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`.
8493

8594
#### include?: string | RegExp | (string | RegExp)[] = []
8695
#### exclude?: string | RegExp | (string | RegExp)[] = []
87-
Use the `include` option to force certain dependencies into the list of externals, for example:
88-
```js
96+
Use the `include` option to force certain dependencies into the list of externals:
97+
```typescript
8998
externals({
9099
deps: false, // Bundle dependencies in
91100
include: /^fsevents/ // Except for fsevents
92101
})
93102
```
94103

95-
Use the `exclude` option to remove certain dependencies from the list of externals, for example:
96-
```js
104+
Conversely, use the `exclude` option to remove certain dependencies from the list of externals:
105+
```typescript
97106
externals({
98107
deps: true, // Don't bundle dependencies, we'll import/require them at runtime instead
99108
exclude: [
100-
'electron-reload', // Yet we want `electron-reload` bundled in
101-
/^vue/ // as well as the VueJS family (vue, vuex, vue-router, etc.)
109+
'electron-reload' // Yet we want `electron-reload` bundled in
102110
]
103111
})
104112
```
105113

106-
> Note 1 : falsy values in `include` and `exclude` are silently ignored. This allows for conditional constructs like so: `exclude: process.env.NODE_ENV === 'production' && /mydep/`.
114+
## Notes
115+
### 1/ This plugin is smart
116+
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/`.
107117

108-
> Note2 : this plugin uses an exact match against your imports, so if your are using some path substitution in your code, eg.:
109-
```js
110-
// in your code, say '@/' is mapped to some directory:
118+
### 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.:
120+
```typescript
121+
// In your code, say '@/' is mapped to some directory:
111122
import something from '@/mylib'
112123
```
113-
> and you don't want `mylib` bundled in, then write:
124+
and you don't want `mylib` bundled in, then write:
114125
```js
115-
// in rollup.config.js:
126+
// In rollup.config.js:
116127
externals({
117128
include: '@/mylib'
118129
})
119130
```
120131

121-
> Note3: if you're also using `@rollup/plugin-node-resolve`, make sure this plugin comes before it in the `plugins` array:
122-
```js
132+
### 3/ Order matters
133+
If you're also using `@rollup/plugin-node-resolve`, make sure this plugin comes _before_ it in the `plugins` array:
134+
```typescript
123135
import externals from 'rollup-plugin-node-externals'
124136
import resolve from '@rollup/plugin-node-resolve'
125-
// ...
137+
138+
...
126139

127140
export default {
128141
// ...
@@ -134,20 +147,22 @@ export default {
134147
}
135148
```
136149

150+
### 4/ Rollup rules
151+
Rollup's own `external` configuration option always takes precedence over this plugin. This is intentional.
137152

138-
### Migrating from version 1.x
139-
- In 1.x, normal dependencies were externalized by default. This is no more true, so you'll need to change:
140-
```js
153+
154+
## Migrating from version 1.x
155+
- In 1.x, normal dependencies were externalized by default. This is no more true since 2.0, so you'll need to change:
156+
```typescript
141157
externals()
142158
```
143159
to:
144-
```js
160+
```typescript
145161
externals({ deps: true })
146162
```
147163
if you want the same behavior.
148164

149-
150-
- For consistency with all other Rollup plugins out there, the `except` option from 1.x has been deprecated in 2.0 and removed in 3.0. Use the Rollup-friendly `exclude` option instead.
165+
- The `except` option from 1.x has been deprecated in 2.0 and removed in 3.0. Use the Rollup-friendlier `exclude` option instead.
151166

152167

153168
## Licence

package.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./package.json')

rollup.config.mjs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// @ts-check
22
import { dirname } from 'path'
3-
import { builtinModules, createRequire } from 'module'
3+
import { builtinModules } from 'module'
44
import resolve from '@rollup/plugin-node-resolve'
55
import commonjs from '@rollup/plugin-commonjs'
66
import ts from 'rollup-plugin-ts'
77

8-
// @ts-ignore
9-
const pkg = createRequire(import.meta.url)('./package.json')
8+
// https://rollupjs.org/guide/en/#using-untranspiled-config-files
9+
import pkg from './package.cjs'
1010

1111
/**
1212
* A mini-plugin that resolves `node:` and `nodejs:` imports to their unprefixed equivalent.
@@ -18,7 +18,7 @@ function nodeColon() {
1818
resolveId(id) {
1919
for (const scheme of [ 'node:', 'nodejs:' ]) {
2020
if (id.startsWith(scheme)) {
21-
return id.slice(scheme.length)
21+
return { id: id.slice(scheme.length), external: true}
2222
}
2323
}
2424
}
@@ -31,7 +31,7 @@ function nodeColon() {
3131
*/
3232
function emitPkg(type) {
3333
return {
34-
name: 'emit-module-package-file',
34+
name: 'emit-pkg',
3535
generateBundle() {
3636
this.emitFile({
3737
type: 'asset',
@@ -42,21 +42,31 @@ function emitPkg(type) {
4242
}
4343
}
4444

45+
/** @type {import('rollup').OutputOptions} */
46+
const commonOutput = {
47+
sourcemap: true,
48+
generatedCode: 'es2015'
49+
}
50+
4551
/** @type {import('rollup').RollupOptions} */
4652
const config = {
4753
input: 'src/index.ts',
4854
output: [
4955
{
56+
...commonOutput,
5057
format: 'commonjs',
5158
file: pkg.main,
52-
exports: 'named',
59+
exports: 'auto',
60+
sourcemap: true,
5361
plugins: [
5462
emitPkg('commonjs')
55-
],
63+
]
5664
},
5765
{
66+
...commonOutput,
5867
format: 'module',
5968
file: pkg.module,
69+
sourcemap: true,
6070
plugins: [
6171
emitPkg('module')
6272
]
@@ -69,7 +79,7 @@ const config = {
6979
ts({
7080
tsconfig: cfg => ({
7181
...cfg,
72-
declarationDir: dirname(pkg.types),
82+
declarationDir: dirname(pkg.types)
7383
})
7484
})
7585
],

src/dependencies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ function isInDirectory(parent: string, child: string): boolean {
1111
}
1212

1313
/**
14-
* @internal
1514
* Iterates over package.json file paths recursively found in parent directories, starting from the
1615
* current working directory. If the current working directory is in a git repository, then package.json
1716
* files outside of the git repository will not be yielded.
17+
* @internal
1818
*/
1919
export async function* findPackagePaths(): AsyncGenerator<string> {
2020
// Find git root if in git repository

0 commit comments

Comments
 (0)