Skip to content

Commit 06884dc

Browse files
committed
Merge branch 'next'
2 parents 8a8ddaf + 7d0f416 commit 06884dc

File tree

4 files changed

+55
-56
lines changed

4 files changed

+55
-56
lines changed

package.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"Tomer Aberbach <[email protected]>",
88
"Elad Ossadon <[email protected]>"
99
],
10-
"license": "MIT",
1110
"keywords": [
1211
"rollup",
1312
"rollup-plugin",
@@ -22,14 +21,15 @@
2221
"package.json",
2322
"monorepo"
2423
],
25-
"engines": {
26-
"node": ">=14.0.0"
27-
},
2824
"repository": {
2925
"type": "git",
3026
"url": "https://github.com/Septh/rollup-plugin-node-externals"
3127
},
3228
"homepage": "https://github.com/Septh/rollup-plugin-node-externals",
29+
"license": "MIT",
30+
"engines": {
31+
"node": ">=14.0.0"
32+
},
3333
"type": "commonjs",
3434
"main": "./dist/cjs/index.js",
3535
"module": "./dist/esm/index.js",
@@ -61,9 +61,6 @@
6161
"dependencies": {
6262
"find-up": "^6.2.0"
6363
},
64-
"peerDependencies": {
65-
"rollup": "^2.60.1"
66-
},
6764
"devDependencies": {
6865
"@rollup/plugin-commonjs": "^21.0.1",
6966
"@rollup/plugin-node-resolve": "^13.0.6",
@@ -72,6 +69,7 @@
7269
"fast-check": "^2.20.0",
7370
"rimraf": "^3.0.2",
7471
"rollup-plugin-ts": "^2.0.4",
72+
"rollup": "^2.60.1",
7573
"ts-node": "^10.4.0",
7674
"tslib": "^2.3.1",
7775
"tslint": "^6.1.2",

rollup.config.mjs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22
import { dirname } from 'path'
3-
import { builtinModules as external, createRequire } from 'module'
3+
import { builtinModules, createRequire } from 'module'
44
import resolve from '@rollup/plugin-node-resolve'
55
import commonjs from '@rollup/plugin-commonjs'
66
import ts from 'rollup-plugin-ts'
@@ -9,13 +9,12 @@ import ts from 'rollup-plugin-ts'
99
const pkg = createRequire(import.meta.url)('./package.json')
1010

1111
/**
12-
* A mini-plugin that resolves `node:` imports to their unprefixed equivalent.
12+
* A mini-plugin that resolves `node:` and `nodejs:` imports to their unprefixed equivalent.
1313
* @type { import('rollup').PluginImpl }
1414
*/
1515
function nodeColon() {
1616
return {
1717
name: 'node-colon',
18-
1918
resolveId(id) {
2019
for (const scheme of [ 'node:', 'nodejs:' ]) {
2120
if (id.startsWith(scheme)) {
@@ -28,16 +27,16 @@ function nodeColon() {
2827

2928
/**
3029
* A mini-plugin that generates a package.json file next to the bundle.
31-
* @type { import('rollup').PluginImpl }
30+
* @type { import('rollup').PluginImpl<'module' | 'commonjs'> }
3231
*/
33-
function emitModulePackageFile() {
32+
function emitPkg(type) {
3433
return {
3534
name: 'emit-module-package-file',
3635
generateBundle() {
3736
this.emitFile({
3837
type: 'asset',
3938
fileName: 'package.json',
40-
source: JSON.stringify({ type: 'module' }, undefined, 2)
39+
source: JSON.stringify({ type }, undefined, 2)
4140
})
4241
}
4342
}
@@ -51,14 +50,15 @@ const config = {
5150
format: 'commonjs',
5251
file: pkg.main,
5352
exports: 'named',
54-
sourcemap: false
53+
plugins: [
54+
emitPkg('commonjs')
55+
],
5556
},
5657
{
5758
format: 'module',
5859
file: pkg.module,
59-
sourcemap: false,
6060
plugins: [
61-
emitModulePackageFile()
61+
emitPkg('module')
6262
]
6363
},
6464
],
@@ -73,7 +73,7 @@ const config = {
7373
})
7474
})
7575
],
76-
external
76+
external: builtinModules.concat(Object.keys(pkg.dependencies))
7777
}
7878

7979
export default config

src/index.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ export interface ExternalsOptions {
3434
*/
3535
export default function externals(options: ExternalsOptions = {}): Plugin {
3636

37-
// Store eventual warnings until we can display them
38-
const warnings: string[] = []
39-
4037
// Consolidate options
41-
const opts: Required<ExternalsOptions> = {
38+
const consolidatedOptions: Required<ExternalsOptions> = {
4239
packagePath: [],
4340
builtins: true,
4441
deps: false,
@@ -50,9 +47,12 @@ export interface ExternalsOptions {
5047
...options
5148
}
5249

50+
// This will store all eventual warnings until we can display them
51+
const warnings: string[] = []
52+
5353
// Map the include and exclude options to arrays of regexes
54-
const [ include, exclude ] = [ 'include', 'exclude' ].map(option => new Array()
55-
.concat((opts as any)[option])
54+
const [ include, exclude ] = [ 'include', 'exclude' ].map(optionName => new Array()
55+
.concat((consolidatedOptions as any)[optionName])
5656
.map((entry: string | RegExp, index: number): RegExp => {
5757
if (entry instanceof RegExp) {
5858
return entry
@@ -62,68 +62,69 @@ export interface ExternalsOptions {
6262
}
6363
else {
6464
if (!!entry) {
65-
warnings.push(`Ignoring wrong entry type #${index} in '${option}' option: '${entry}'`)
65+
warnings.push(`Ignoring wrong entry type #${index} in '${optionName}' option: '${entry}'`)
6666
}
6767
return /(?=no)match/
6868
}
6969
})
7070
)
7171

72-
// Build a function to filter out unwanted dependencies
73-
const filterFn = (dep: string) => !exclude.some(rx => rx.test(dep))
72+
// Build a function to keep only non excluded dependencies
73+
const isNotExcluded = (id: string) => !exclude.some(rx => rx.test(id))
7474

75-
// Filter NodeJS builtins
76-
const builtins = (opts.builtins ? builtinModules : []).filter(filterFn)
77-
78-
// Normalize package paths
79-
let packagePaths: string[] = ([] as string[]).concat(opts.packagePath)
80-
81-
// Array of the final regexes, include potential import from a sub directory (e.g. 'lodash/map')
75+
// Array of the final regexes
8276
const externals: RegExp[] = []
77+
const isExternal = (id: string) => externals.some(deps => deps.test(id))
8378

8479
return {
8580
name: 'node-externals',
8681

8782
async buildStart() {
8883

89-
// Find and filter dependencies
84+
// 1) Filter NodeJS builtins, supporting potential import from a sub directory (e.g. 'fs/promises')
85+
const builtins = (consolidatedOptions.builtins ? builtinModules : []).filter(isNotExcluded)
86+
if (builtins.length > 0) {
87+
externals.push(new RegExp('^(?:' + builtins.join('|') + ')(\/.+)?$'))
88+
}
89+
90+
// 2) Find and filter dependencies, supporting potential import from a sub directory (e.g. 'lodash/map')
91+
const packagePaths: string[] = ([] as string[]).concat(consolidatedOptions.packagePath)
9092
const dependencies = (await findDependencies({
9193
packagePaths: packagePaths.length > 0 ? packagePaths : findPackagePaths(),
9294
keys: [
93-
opts.deps && 'dependencies',
94-
opts.devDeps && 'devDependencies',
95-
opts.peerDeps && 'peerDependencies',
96-
opts.optDeps && 'optionalDependencies'
95+
consolidatedOptions.deps && 'dependencies',
96+
consolidatedOptions.devDeps && 'devDependencies',
97+
consolidatedOptions.peerDeps && 'peerDependencies',
98+
consolidatedOptions.optDeps && 'optionalDependencies'
9799
].filter(Boolean) as string[],
98100
warnings
99-
})).filter(filterFn)
100-
101-
// Issue the warnings we may have collected
102-
let msg: string | undefined
103-
while (msg = warnings.shift()) {
104-
this.warn(msg)
105-
}
106-
107-
// Build regexes
108-
if (builtins.length > 0) {
109-
externals.push(new RegExp('^(?:' + builtins.join('|') + ')(\/.+)?$'))
110-
}
101+
})).filter(isNotExcluded)
111102

112103
if (dependencies.length > 0) {
113104
externals.push(new RegExp('^(?:' + dependencies.join('|') + ')(\/.+)?$'))
114105
}
115106

107+
// 3) Add the include option
116108
if (include.length > 0) {
117109
externals.push(...include)
118110
}
111+
112+
// All done. Issue the warnings we may have collected
113+
let msg: string | undefined
114+
while (msg = warnings.shift()) {
115+
this.warn(msg)
116+
}
119117
},
120118

121119
resolveId(id, importer) {
120+
// Ignore entry chunks & don't mess with other plugins
121+
if (!importer?.charCodeAt(0) || !id.charCodeAt(0)) {
122+
return null
123+
}
124+
122125
// Return `false` if importee should be treated as an external module,
123-
// otherwise return `null` to let Rollup and other plugins handle it.
124-
return importer && !/\0/.test(id) && externals.some(deps => deps.test(id)) && filterFn(id)
125-
? false
126-
: null
126+
// otherwise return `null` to let Rollup and other plugins handle it
127+
return isExternal(id) && isNotExcluded(id) ? false : null
127128
}
128129
}
129130
}

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
// "strictFunctionTypes": true,
3535
// "strictPropertyInitialization": true,
3636
"noImplicitReturns": true,
37-
"noUnusedLocals": true,
38-
"noUnusedParameters": true,
37+
"noUnusedLocals": false,
38+
"noUnusedParameters": false,
3939
},
4040
"files": [
4141
"src/index.ts"

0 commit comments

Comments
 (0)