Skip to content

Commit 5e9015b

Browse files
committed
Use order: 'pre' for the resolveId hook. Minor other code changes.
1 parent de3057e commit 5e9015b

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

source/index.ts

+43-33
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export interface ExternalsOptions {
7979
// Fields of interest in package.json
8080
interface PackageJson {
8181
version: string
82+
workspaces?: string[]
8283
dependencies?: Record<string, string>
8384
devDependencies?: Record<string, string>
8485
peerDependencies?: Record<string, string>
@@ -130,8 +131,8 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
130131
let include: RegExp[],
131132
exclude: RegExp[]
132133

133-
const isIncluded = (id: string) => include.length === 0 || include.some(rx => rx.test(id)),
134-
isExcluded = (id: string) => exclude.length > 0 && exclude.some(rx => rx.test(id))
134+
const isIncluded = (id: string) => include.length > 0 && include.some(rx => rx.test(id)),
135+
isExcluded = (id: string) => exclude.length > 0 && exclude.some(rx => rx.test(id))
135136

136137
return {
137138
name: 'node-externals',
@@ -169,15 +170,19 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
169170
previous = current, current = path.dirname(current)
170171
) {
171172
const entries = await fs.readdir(current, { withFileTypes: true }).catch(() => null)
172-
if (entries === null)
173-
this.error(`Could not read contents of directory ${JSON.stringify(current)}.`)
173+
if (entries === null) {
174+
return this.error({
175+
message: `Could not read contents of directory ${JSON.stringify(current)}.`,
176+
stack: undefined
177+
})
178+
}
174179

175180
// Gather package.json files.
176-
if (entries!.some(entry => entry.name === 'package.json' && entry.isFile()))
181+
if (entries.some(entry => entry.name === 'package.json' && entry.isFile()))
177182
packagePaths.push(path.join(current, 'package.json'))
178183

179184
// Break early if this is a git repo root or there is a known workspace root file.
180-
if (entries!.some(entry =>
185+
if (entries.some(entry =>
181186
(entry.name === '.git' && entry.isDirectory())
182187
|| (workspaceRootFiles.has(entry.name) && entry.isFile())
183188
)) {
@@ -189,9 +194,8 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
189194
// Gather dependencies names.
190195
const dependencies: Record<string, string> = {}
191196
for (const packagePath of packagePaths) {
192-
this.addWatchFile(packagePath)
193197
try {
194-
const json = (await fs.readFile(packagePath)).toString()
198+
const json = await fs.readFile(packagePath).then(buffer => buffer.toString())
195199
try {
196200
const pkg = JSON.parse(json) as PackageJson
197201
Object.assign(dependencies,
@@ -201,8 +205,11 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
201205
config.optDeps ? pkg.optionalDependencies : undefined
202206
)
203207

208+
// Watch this package.json
209+
this.addWatchFile(packagePath)
210+
204211
// Break early if this is a npm/yarn workspace root.
205-
if ('workspaces' in pkg)
212+
if (Array.isArray(pkg.workspaces) && pkg.workspaces.length > 0)
206213
break
207214
}
208215
catch {
@@ -226,33 +233,36 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
226233
include.push(new RegExp('^(?:' + names.join('|') + ')(?:/.+)?$'))
227234
},
228235

229-
async resolveId(specifier, importer) {
230-
if (
231-
!importer // Ignore entry points (they should always be resolved)
232-
|| path.isAbsolute(specifier) // Ignore already resolved ids
233-
|| /^(?:\0|\.{1,2}\/)/.test(specifier) // Ignore virtual modules and relative imports
234-
) {
235-
return null
236-
}
236+
resolveId: {
237+
order: 'pre',
238+
async handler(specifier, importer) {
239+
if (
240+
!importer // Ignore entry points (they should always be resolved)
241+
|| path.isAbsolute(specifier) // Ignore already resolved ids
242+
|| /^(?:\0|\.{1,2}\/)/.test(specifier) // Ignore virtual modules and relative imports
243+
) {
244+
return null
245+
}
237246

238-
// Handle node builtins.
239-
if (isBuiltin(specifier)) {
240-
const stripped = specifier.replace(nodePrefixRx, '')
241-
return {
242-
id: config.builtinsPrefix === 'ignore'
243-
? specifier
244-
: config.builtinsPrefix === 'add' || !isBuiltin(stripped)
245-
? nodePrefix + stripped
246-
: stripped,
247-
external: (config.builtins || isIncluded(specifier)) && !isExcluded(specifier),
248-
moduleSideEffects: false
247+
// Handle node builtins.
248+
if (isBuiltin(specifier)) {
249+
const stripped = specifier.replace(nodePrefixRx, '')
250+
return {
251+
id: config.builtinsPrefix === 'ignore'
252+
? specifier
253+
: config.builtinsPrefix === 'add' || !isBuiltin(stripped)
254+
? nodePrefix + stripped
255+
: stripped,
256+
external: (config.builtins || isIncluded(specifier)) && !isExcluded(specifier),
257+
moduleSideEffects: false
258+
}
249259
}
250-
}
251260

252-
// Handle npm dependencies.
253-
return isIncluded(specifier) && !isExcluded(specifier)
254-
? false // external
255-
: null // normal handling
261+
// Handle npm dependencies.
262+
return isIncluded(specifier) && !isExcluded(specifier)
263+
? false // external
264+
: null // normal handling
265+
}
256266
}
257267
}
258268
}

0 commit comments

Comments
 (0)