2025-04-10 02:03:56 +07:00
|
|
|
import { isModuleExportBad } from '../metro'
|
2025-03-30 23:32:17 +07:00
|
|
|
|
2025-04-05 20:52:09 +07:00
|
|
|
import type { If } from '@revenge-mod/utils/types'
|
2025-04-05 23:35:01 +07:00
|
|
|
import type { Metro } from '../../types/metro'
|
2025-03-30 23:32:17 +07:00
|
|
|
import type { Filter } from './filters'
|
|
|
|
|
|
2025-04-05 20:52:09 +07:00
|
|
|
export type RunFilterOptions = {
|
2025-03-30 23:32:17 +07:00
|
|
|
/**
|
2025-04-05 20:52:09 +07:00
|
|
|
* For ES modules, whether to skip checking the default export.
|
|
|
|
|
*
|
|
|
|
|
* @default false
|
|
|
|
|
*/
|
|
|
|
|
esmSkipDefault?: boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type RunFilterReturnExportsOptions<ReturnNamespace extends boolean = boolean> = RunFilterOptions &
|
|
|
|
|
If<
|
|
|
|
|
ReturnNamespace,
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* For ES modules, whether to return the whole module with all exports instead of just the default export **if the default export matches**.
|
|
|
|
|
*
|
|
|
|
|
* CommonJS modules will always return all exports.
|
|
|
|
|
*
|
|
|
|
|
* @default false
|
|
|
|
|
*/
|
|
|
|
|
esmReturnNamespace: true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
esmReturnNamespace?: false
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
const FilterResultFlags = {
|
|
|
|
|
/**
|
|
|
|
|
* A module was found.
|
2025-03-30 23:32:17 +07:00
|
|
|
*/
|
|
|
|
|
Found: 1,
|
|
|
|
|
/**
|
2025-04-05 20:52:09 +07:00
|
|
|
* A module was found, and it is an ES module, and the filter matched the module namespace.
|
2025-03-30 23:32:17 +07:00
|
|
|
*/
|
|
|
|
|
ESModuleNamespace: 2,
|
|
|
|
|
/**
|
2025-04-05 20:52:09 +07:00
|
|
|
* A module was found, and it is an ES module, and the filter matched the default export.
|
2025-03-30 23:32:17 +07:00
|
|
|
*/
|
|
|
|
|
ESModuleDefault: 3,
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-05 20:52:09 +07:00
|
|
|
export type FilterResultFlag = (typeof FilterResultFlags)[keyof typeof FilterResultFlags]
|
2025-03-30 23:32:17 +07:00
|
|
|
|
2025-04-05 20:52:09 +07:00
|
|
|
// The reason this returns a flag is because flags are never falsy, while exports may be falsy when using ID-only filters (eg. `byDependencies`).
|
2025-04-09 03:42:06 +07:00
|
|
|
|
|
|
|
|
// Currently, we only have options that are relevant for checking exports
|
|
|
|
|
// If we add more options later on, do NOT forget about
|
|
|
|
|
// adding them here, and passing them in the lookup* functions
|
|
|
|
|
export function runFilter(filter: Filter<any, false>, id: Metro.ModuleID): FilterResultFlag | false
|
|
|
|
|
|
|
|
|
|
export function runFilter(
|
|
|
|
|
filter: Filter,
|
|
|
|
|
id: Metro.ModuleID,
|
|
|
|
|
exports: Metro.ModuleExports,
|
|
|
|
|
options?: RunFilterOptions,
|
|
|
|
|
): FilterResultFlag | false
|
|
|
|
|
|
|
|
|
|
export function runFilter(
|
|
|
|
|
filter: Filter,
|
2025-03-30 23:32:17 +07:00
|
|
|
id: Metro.ModuleID,
|
2025-04-09 03:42:06 +07:00
|
|
|
exports?: Metro.ModuleExports,
|
2025-04-05 20:52:09 +07:00
|
|
|
options?: RunFilterOptions,
|
|
|
|
|
): FilterResultFlag | false {
|
|
|
|
|
if (exports?.__esModule) {
|
2025-03-30 23:32:17 +07:00
|
|
|
const { default: defaultExport } = exports
|
|
|
|
|
|
2025-04-05 20:52:09 +07:00
|
|
|
// TODO(modules/caches/flags): could cache here that default export is bad
|
2025-04-10 02:03:56 +07:00
|
|
|
if (!options?.esmSkipDefault && !isModuleExportBad(defaultExport) && filter(id, defaultExport)) {
|
2025-03-30 23:32:17 +07:00
|
|
|
// TODO(modules/finders/caches::esm::default)
|
2025-04-05 20:52:09 +07:00
|
|
|
return FilterResultFlags.ESModuleDefault
|
2025-03-30 23:32:17 +07:00
|
|
|
}
|
|
|
|
|
|
2025-04-05 20:52:09 +07:00
|
|
|
if (filter(id, exports)) {
|
2025-03-30 23:32:17 +07:00
|
|
|
// TODO(modules/finders/caches::esm::namespace)
|
2025-04-05 20:52:09 +07:00
|
|
|
return FilterResultFlags.ESModuleNamespace
|
2025-03-30 23:32:17 +07:00
|
|
|
}
|
2025-04-05 20:52:09 +07:00
|
|
|
} else if (filter(id, exports)) {
|
2025-03-30 23:32:17 +07:00
|
|
|
// TODO(modules/finders/caches)
|
2025-04-05 20:52:09 +07:00
|
|
|
return FilterResultFlags.Found
|
2025-03-30 23:32:17 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
2025-04-05 20:52:09 +07:00
|
|
|
|
|
|
|
|
export function runFilterReturnExports(
|
|
|
|
|
filter: Filter,
|
|
|
|
|
id: Metro.ModuleID,
|
|
|
|
|
exports: Metro.ModuleExports,
|
|
|
|
|
options?: RunFilterReturnExportsOptions,
|
|
|
|
|
) {
|
|
|
|
|
const flag = runFilter(filter, id, exports, options)
|
|
|
|
|
|
|
|
|
|
if (flag) {
|
|
|
|
|
if (flag === FilterResultFlags.ESModuleDefault && !options?.esmReturnNamespace) return exports.default
|
|
|
|
|
return exports
|
|
|
|
|
}
|
|
|
|
|
}
|