Skip to content

Commit 1f09187

Browse files
committed
Accept true as 'add' and false as 'strip' for options.builtinsPrefix
1 parent db71aaf commit 1f09187

3 files changed

Lines changed: 52 additions & 14 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default {
9191
builtins?: boolean
9292

9393
// node: prefix handing for importing NodeJS builtins. Default: 'add'.
94-
builtinsPrefix?: 'add' | 'strip' | 'ignore'
94+
builtinsPrefix?: boolean | 'add' | 'strip' | 'ignore'
9595

9696
// The path(s) to your package.json. Default: read below.
9797
packagePath?: string | string[]
@@ -121,10 +121,10 @@ export default {
121121
#### builtins?: boolean = true
122122
Set the `builtins` option to `false` if you'd like to use some shims/polyfills for those. You'll most certainly need [an other plugin](https://www.npmjs.com/package/rollup-plugin-node-polyfills) as well.
123123

124-
#### builtinsPrefix?: 'add' | 'strip' | 'ignore' = 'add'
124+
#### builtinsPrefix?: boolean | 'add' | 'strip' | 'ignore' = 'add'
125125
How to handle the `node:` scheme when importing builtins (i.e., `import path from 'node:path'`).
126-
- If `add` (the default, recommended), the `node:` scheme is always added if missing, so `path` becomes `node:path`. In effect, this dedupes your imports of Node builtins by homogenizing their names to their schemed version.
127-
- If `strip`, the scheme is always removed, so `node:path` becomes `path`. In effect, this dedupes your imports of Node builtins by homogenizing their names to their scheme-less version. Schemed-only builtins like `node:test` or `node:sqlite` are never stripped.
126+
- If `add` or `true` (the default, recommended), the `node:` scheme is always added if missing, so `path` becomes `node:path`. In effect, this dedupes your imports of Node builtins by homogenizing their names to their schemed version.
127+
- If `strip` or `false`, the scheme is always removed, so `node:path` becomes `path`. In effect, this dedupes your imports of Node builtins by homogenizing their names to their scheme-less version. Schemed-only builtins like `node:test` or `node:sqlite` are never stripped.
128128
- `ignore` will simply leave all builtins imports as written in your code. Caveat: if you write `node:path` but one of your bundled dependencies uses `path` (or the other way around), your bundle will end up with both `node:path` and `path` imports.
129129

130130
>[!NOTE]

source/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface ExternalsOptions {
2828
*
2929
* Defaults to `'add'`.
3030
*/
31-
builtinsPrefix?: 'add' | 'strip' | 'ignore'
31+
builtinsPrefix?: boolean | 'add' | 'strip' | 'ignore'
3232

3333
/**
3434
* Path/to/your/package.json file (or array of paths).
@@ -208,9 +208,9 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
208208
continue
209209

210210
case 'builtinsPrefix':
211-
if (value === 'add')
211+
if (value === 'add' || value === true)
212212
config.prefix = true
213-
else if (value === 'strip')
213+
else if (value === 'strip' || value === false)
214214
config.prefix = false
215215
else if (value === 'ignore')
216216
config.prefix = null

test/builtins.test.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@ test("Does NOT mark Node builtins external when builtins=false", async t => {
1919
}
2020
})
2121

22-
test("Does NOT mark Node builtins external when implicitly excluded", async t => {
23-
const context = await initPlugin({ exclude: [ 'path', 'node:fs' ]})
22+
test("Does NOT mark Node builtins external when builtins=true and implicitly excluded", async t => {
23+
const context = await initPlugin({ builtins: true, exclude: [ 'path', 'node:fs' ]})
24+
25+
// Implicitly excluded
2426
for (const builtin of [ 'path', 'node:fs' ]) {
2527
t.like(await context.resolveId(builtin, 'index.js'), {
2628
external: false
2729
})
2830
}
31+
32+
// not excluded
33+
for (const builtin of [ 'node:module' ]) {
34+
t.like(await context.resolveId(builtin, 'index.js'), {
35+
external: true
36+
})
37+
}
2938
})
3039

3140
test("Marks Node builtins external when builtins=false and implicitly included", async t => {
@@ -46,7 +55,25 @@ test("Adds 'node:' prefix to builtins by default", async t => {
4655
}
4756
})
4857

49-
test("Removes 'node:' prefix when using builtinsPrefix='strip'", async t => {
58+
test("Adds 'node:' prefix when builtinsPrefix is 'add'", async t => {
59+
const context = await initPlugin({ builtinsPrefix: 'add' })
60+
for (const builtin of [ 'node:path', 'path' ]) {
61+
t.like(await context.resolveId(builtin, 'index.js'), {
62+
id: 'node:path'
63+
})
64+
}
65+
})
66+
67+
test("Adds 'node:' prefix when builtinsPrefix is true", async t => {
68+
const context = await initPlugin({ builtinsPrefix: true })
69+
for (const builtin of [ 'node:path', 'path' ]) {
70+
t.like(await context.resolveId(builtin, 'index.js'), {
71+
id: 'node:path'
72+
})
73+
}
74+
})
75+
76+
test("Removes 'node:' prefix when builtinsPrefix is 'strip'", async t => {
5077
const context = await initPlugin({ builtinsPrefix: 'strip' })
5178
for (const builtin of [ 'node:path', 'path' ]) {
5279
t.like(await context.resolveId(builtin, 'index.js'), {
@@ -55,18 +82,29 @@ test("Removes 'node:' prefix when using builtinsPrefix='strip'", async t => {
5582
}
5683
})
5784

58-
test("Does NOT remove 'node:test' prefix even with builtinsPrefix='strip'", async t => {
85+
test("Removes 'node:' prefix when builtinsPrefix is false", async t => {
86+
const context = await initPlugin({ builtinsPrefix: false })
87+
for (const builtin of [ 'node:path', 'path' ]) {
88+
t.like(await context.resolveId(builtin, 'index.js'), {
89+
id: 'path'
90+
})
91+
}
92+
})
93+
94+
test("Does NOT remove 'node:' prefix on prefixed-only builtins even with builtinsPrefix='strip'", async t => {
5995
const context = await initPlugin({ builtinsPrefix: 'strip' })
60-
for (const builtin of [ 'node:test' ]) {
96+
for (const builtin of [ 'node:test', 'node:sqlite', 'node:sea' ]) {
6197
t.like(await context.resolveId(builtin, 'index.js'), {
6298
id: builtin
6399
})
64100
}
65101
})
66102

67-
test("Does not recognize 'test' as a Node builtin", async t => {
103+
test("Does not recognize unprefixed 'test' / 'sqlite' / 'sea' as a Node builtins", async t => {
68104
const context = await initPlugin()
69-
t.is(await context.resolveId('node', 'index.js'), IGNORED)
105+
t.is(await context.resolveId('node', 'index.js'), IGNORED)
106+
t.is(await context.resolveId('sqlite', 'index.js'), IGNORED)
107+
t.is(await context.resolveId('sea', 'index.js'), IGNORED)
70108
})
71109

72110
test("Ignores 'node:' prefix when using builtinsPrefix='ignore'", async t => {

0 commit comments

Comments
 (0)