Skip to content

Commit f0da19b

Browse files
committed
Merge branch 'next'
2 parents 5690c59 + 9812b88 commit f0da19b

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default {
3434
// Make node builtins external. Optional. Default: true
3535
builtins?: boolean,
3636

37-
// Treat prefixed builtins as their unprefixed counterpart. Optional. Default: true
37+
// Treat prefixed builtins as their unprefixed counterpart. Optional. Default: 'strip'
3838
prefixedBuiltins?: boolean | 'strip',
3939

4040
// Make pkg.dependencies external. Optional. Default: false
@@ -78,8 +78,8 @@ If you're working with monorepos, the `packagePath` is made for you. It can take
7878
#### builtins?: boolean = true
7979
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.
8080

81-
#### prefixedBuiltins?: boolean | 'strip' = true
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.
81+
#### 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.
8383

8484
#### deps?: boolean = false
8585
Set the `deps` option to `true` to externalize your normal dependencies, therefore preventing Rollup from bundling them with your code.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@
6060
"clean": "rimraf dist",
6161
"prepublishOnly": "npm run lint && npm run clean && npm run build"
6262
},
63+
"dependencies": {
64+
"find-up": "^6.2.0"
65+
},
6366
"devDependencies": {
67+
"@rollup/plugin-alias": "^3.1.8",
6468
"@rollup/plugin-commonjs": "^21.0.1",
6569
"@rollup/plugin-node-resolve": "^13.0.6",
6670
"@typescript-eslint/eslint-plugin": "^5.6.0",
@@ -69,15 +73,11 @@
6973
"ava-fast-check": "^4.0.2",
7074
"eslint": "^8.4.1",
7175
"fast-check": "^2.20.0",
72-
"find-up": "^6.2.0",
7376
"rimraf": "^3.0.2",
7477
"rollup": "^2.61.1",
7578
"rollup-plugin-ts": "^2.0.4",
7679
"ts-node": "^10.4.0",
7780
"tslib": "^2.3.1",
7881
"typescript": "^4.5.3"
79-
},
80-
"dependencies": {
81-
"@rollup/plugin-alias": "^3.1.8"
8282
}
8383
}

src/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default function externals(options: ExternalsOptions = {}): Plugin {
4040
const config: Required<ExternalsOptions> = {
4141
packagePath: [],
4242
builtins: true,
43-
prefixedBuiltins: true,
43+
prefixedBuiltins: 'strip',
4444
deps: false,
4545
devDeps: true,
4646
peerDeps: true,
@@ -54,9 +54,9 @@ export default function externals(options: ExternalsOptions = {}): Plugin {
5454
const warnings: string[] = []
5555

5656
// Map the include and exclude options to arrays of regexes
57-
const [ include, exclude ] = [ 'include', 'exclude' ].map(optionName => []
57+
const [ include, exclude ] = [ 'include', 'exclude' ].map(optionName => ([] as (string | RegExp)[])
5858
.concat((config as any)[optionName])
59-
.map((entry: string | RegExp, index: number): RegExp => {
59+
.map((entry, index) => {
6060
if (entry instanceof RegExp) {
6161
return entry
6262
}
@@ -72,22 +72,22 @@ export default function externals(options: ExternalsOptions = {}): Plugin {
7272
})
7373
)
7474

75-
// Build a function to keep only non excluded dependencies
75+
// Filter function to keep only non excluded dependencies
7676
const isNotExcluded = (id: string) => !exclude.some(rx => rx.test(id))
7777

7878
// Array of the final regexes
7979
const externals: RegExp[] = []
80-
const isExternal = (id: string) => externals.some(deps => deps.test(id))
80+
const isExternal = (id: string) => externals.some(rx => rx.test(id))
8181

8282
return {
8383
name: 'node-externals',
8484

8585
async buildStart() {
8686

8787
// 1) Filter NodeJS builtins, supporting potential import from a sub directory (e.g. 'fs/promises')
88-
const node = (config.builtins ? builtinModules : []).filter(isNotExcluded)
89-
if (node.length > 0) {
90-
externals.push(new RegExp('^(?:' + node.join('|') + ')(?:/.+)?$'))
88+
const builtins = (config.builtins ? builtinModules : []).filter(isNotExcluded)
89+
if (builtins.length > 0) {
90+
externals.push(new RegExp('^(?:' + builtins.join('|') + ')(?:/.+)?$'))
9191
}
9292

9393
// 2) Find and filter dependencies, supporting potential import from a sub directory (e.g. 'lodash/map')
@@ -119,13 +119,13 @@ export default function externals(options: ExternalsOptions = {}): Plugin {
119119
}
120120
},
121121

122-
resolveId(importee, importer) {
122+
resolveId(importee, importer, { isEntry }) {
123123
// Ignore entry chunks & don't mess with other plugins
124-
if (!importer?.charCodeAt(0) || !importee.charCodeAt(0)) {
124+
if (isEntry || !importee.charCodeAt(0) || !importer?.charCodeAt(0)) {
125125
return null
126126
}
127127

128-
// Remove node:/nodejs: prefix so builtins resolve to their unprefixed equivalent
128+
// Remove node: prefix so builtins resolve to their unprefixed equivalent
129129
let stripped = importee
130130
if (config.prefixedBuiltins) {
131131
if (stripped.startsWith('node:')) {

0 commit comments

Comments
 (0)