feat(lib/modules/finders)!: check namespace before default exports, improve performance marginally

This commit is contained in:
PalmDevs 2025-05-01 00:18:42 +07:00
parent 2de47ff046
commit 384f669c68
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA
2 changed files with 14 additions and 37 deletions

View file

@ -35,21 +35,13 @@ const FilterResultFlags = {
*/
Found: 1,
/**
* A module was found, and it is an ES module, and the filter matched the module namespace.
* A module was found, and the filter matched the default export.
*/
ESModuleNamespace: 2,
Default: 2,
/**
* A module was found, and it is an ES module, and the filter matched the default export.
* A module was found, and the filter matched the module namespace.
*/
ESModuleDefault: 3,
/**
* A module was found, and it is a CommonJS module, and the filter matched the module namespace.
*/
CJSModuleNamespace: 4,
/**
* A module was found, and it is a CommonJS module, and the filter matched the default export.
*/
CJSModuleDefault: 5,
Namespace: 3,
}
export type FilterResultFlag = (typeof FilterResultFlags)[keyof typeof FilterResultFlags]
@ -74,7 +66,7 @@ export function runFilter(
exports?: Metro.ModuleExports,
options?: RunFilterOptions,
): FilterResultFlag | undefined {
if (exports == null) {
if (exports === undefined) {
if ((filter as Filter<any, false>)(id)) {
// TODO(modules/finders/caches)
return FilterResultFlags.Found
@ -83,25 +75,15 @@ export function runFilter(
return
}
const defaultExport = exports.default
if (!options?.skipDefault && !isModuleExportBad(defaultExport) && filter(id, defaultExport)) {
if (exports.__esModule) {
// TODO(modules/finders/caches::esm::default)
return FilterResultFlags.ESModuleDefault
}
// TODO(modules/finders/caches::cjs::default)
return FilterResultFlags.CJSModuleDefault
if (filter(id, exports)) {
// TODO(modules/finders/caches::namespace)
return FilterResultFlags.Namespace
}
if (filter(id, exports)) {
if (exports.__esModule) {
// TODO(modules/finders/caches::esm::namespace)
return FilterResultFlags.ESModuleNamespace
}
// TODO(modules/finders/caches::cjs::namespace)
return FilterResultFlags.CJSModuleNamespace
const defaultExport = exports.default
if (!options?.skipDefault && !isModuleExportBad(defaultExport) && filter(id, defaultExport)) {
// TODO(modules/finders/caches::default)
return FilterResultFlags.Default
}
}
@ -110,11 +92,6 @@ export function exportsFromFilterResultFlag(
exports: Metro.ModuleExports,
options?: RunFilterReturnExportsOptions,
) {
if (
(flag === FilterResultFlags.ESModuleDefault || flag === FilterResultFlags.CJSModuleDefault) &&
!options?.returnNamespace
)
return exports.default
if (flag === FilterResultFlags.Default && !options?.returnNamespace) return exports.default
return exports
}

View file

@ -217,7 +217,7 @@ function depsToKey(deps: ComparableDependencyMap): string {
else if (Array.isArray(dep)) {
if (dep.loose) key += '#'
key += `[${depsToKey(dep)}],`
} else key += `${-dep & RootRelativeDepBit ? `r${dep}` : dep},`
} else key += `${dep},`
return key.substring(0, key.length - 1)
}