fix(metro/polyfill): unproxy polyfill

This commit is contained in:
amsyarasyiq 2024-06-02 00:09:16 +08:00
parent 1c151745e2
commit e660447143
3 changed files with 42 additions and 45 deletions

View file

@ -1,7 +1,7 @@
import { ClientInfoManager, MMKVManager } from "@lib/api/native/modules"; import { ClientInfoManager, MMKVManager } from "@lib/api/native/modules";
import { throttle } from "@lib/utils/throttle"; import { throttle } from "@lib/utils/throttle";
const CACHE_VERSION = 29; const CACHE_VERSION = 31;
const BUNNY_METRO_CACHE_KEY = `__bunny_metro_cache_key_v${CACHE_VERSION}__`; const BUNNY_METRO_CACHE_KEY = `__bunny_metro_cache_key_v${CACHE_VERSION}__`;
export enum ExportsFlags { export enum ExportsFlags {
@ -115,6 +115,10 @@ export function getPolyfillModuleCacher(name: string) {
cacheId(moduleId: number) { cacheId(moduleId: number) {
indexObject[moduleId] = 1; indexObject[moduleId] = 1;
saveCache(); saveCache();
},
finish() {
indexObject[`_${ModulesMapInternal.FULL_LOOKUP}`] = 1;
saveCache();
} }
}; };
} }

View file

@ -212,7 +212,7 @@ export function* getModules(uniq: string, all = false) {
} }
export function* getCachedPolyfillModules(name: string) { export function* getCachedPolyfillModules(name: string) {
const cache = getMetroCache().polyfillIndex[name]; const cache = getMetroCache().polyfillIndex[name]!;
for (const id in cache) { for (const id in cache) {
const exports = requireModule(Number(id)); const exports = requireModule(Number(id));
@ -220,9 +220,11 @@ export function* getCachedPolyfillModules(name: string) {
yield [id, exports]; yield [id, exports];
} }
for (const id in metroModules) { if (!cache[`_${ModulesMapInternal.FULL_LOOKUP}`]) {
const exports = requireModule(Number(id)); for (const id in metroModules) {
if (isBadExports(exports)) continue; const exports = requireModule(Number(id));
yield [id, exports]; if (isBadExports(exports)) continue;
yield [id, exports];
}
} }
} }

View file

@ -102,46 +102,37 @@ type Keys = LiteralUnion<typeof redesignProps extends Set<infer U> ? U : string,
const cacher = getPolyfillModuleCacher("redesign_module"); const cacher = getPolyfillModuleCacher("redesign_module");
const _cache = {} as Record<Keys, any>; const _module = {} as Record<Keys, any>;
export default _module;
export default new Proxy({}, { for (const prop of redesignProps) {
get(_, p) { const filter = byProps(prop);
if (typeof p !== "string" || !redesignProps.has(p as any)) return; const candidates: any[] = [];
if (_cache[p]) return _cache[p];
const prop = p as Keys; for (const [id, moduleExports] of cacher.getModules()) {
const filter = byProps(prop); if (filter(moduleExports, id, false)) {
const candidates: any[] = []; cacher.cacheId(id);
candidates.push(moduleExports);
for (const [id, moduleExports] of cacher.getModules()) { } else if (
if (filter(moduleExports, id, false)) { moduleExports.__esModule
cacher.cacheId(id); && moduleExports.default
candidates.push(moduleExports); && filter(moduleExports.default, id, false)
} else if ( ) {
moduleExports.__esModule cacher.cacheId(id);
&& moduleExports.default candidates.push(moduleExports.default);
&& filter(moduleExports.default, id, false)
) {
cacher.cacheId(id);
candidates.push(moduleExports.default);
}
} }
if (candidates.length === 0) return undefined;
const bestCandidate = candidates.reduce(
(c1, c2) =>
(Object.keys(c2).length < Object.keys(c1).length)
? c2
: c1
);
return _cache[prop] = bestCandidate[prop];
},
ownKeys() {
redesignProps.forEach(prop => {
this.get!({}, prop, {});
});
return Reflect.ownKeys(_cache);
} }
});
if (candidates.length === 0) continue;
const bestCandidate = candidates.reduce(
(c1, c2) =>
(Object.keys(c2).length < Object.keys(c1).length)
? c2
: c1
);
_module[prop] = bestCandidate[prop];
}
cacher.finish();