From b4579d1d2fe52359bfb1654e5e3e804b2c615786 Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Tue, 22 Jul 2025 12:57:23 -0700 Subject: [PATCH] chore: global handling --- packages/core/global-types.d.ts | 3 +- packages/core/globals/index.ts | 121 ++++++++++++++++++++++++-------- 2 files changed, 93 insertions(+), 31 deletions(-) diff --git a/packages/core/global-types.d.ts b/packages/core/global-types.d.ts index b53ac6ba8..7b74a3999 100644 --- a/packages/core/global-types.d.ts +++ b/packages/core/global-types.d.ts @@ -100,8 +100,6 @@ declare module globalThis { function _unregisterModule(name: string): void; - function _isModuleLoadedForUI(moduleName: string): boolean; - var onGlobalLayoutListener: any; function zonedCallback(callback: T): T; var Reflect: any; @@ -132,6 +130,7 @@ declare const __CSS_PARSER__: string; declare const __NS_WEBPACK__: boolean; declare const __UI_USE_EXTERNAL_RENDERER__: boolean; declare const __UI_USE_XML_PARSER__: boolean; +declare const __COMMONJS__: boolean; declare const __ANDROID__: boolean; declare const __IOS__: boolean; declare const __VISIONOS__: boolean; diff --git a/packages/core/globals/index.ts b/packages/core/globals/index.ts index 56ea35c8b..a2993299e 100644 --- a/packages/core/globals/index.ts +++ b/packages/core/globals/index.ts @@ -103,10 +103,6 @@ global._unregisterModule = function _unregisterModule(name: string): void { modules.delete(name); }; -global._isModuleLoadedForUI = function _isModuleLoadedForUI(moduleName: string): boolean { - return modulesLoadedForUI.has(moduleName); -}; - global.registerWebpackModules = function registerWebpackModules(context: Context, extensionMap: ExtensionMap = {}) { context.keys().forEach((moduleId) => { const extDotIndex = moduleId.lastIndexOf('.'); @@ -245,15 +241,16 @@ global.Experimental = function (target: Object, key?: string | symbol, descripto } }; const modules: Map = new Map(); -const modulesLoadedForUI = new Set(); -global.loadModule = function loadModule(name: string, isUIModule = false): any { +async function dynamicResolveModule(name: string) { + const result = await import(name); + return result.default || result; +} + +global.loadModule = function loadModule(name: string): any { + console.log(`@@@ loadModule: ${name}, __COMMONJS__:`, __COMMONJS__); const moduleInfo = modules.get(name); if (moduleInfo) { - if (isUIModule) { - modulesLoadedForUI.add(moduleInfo.moduleId); - } - const result = moduleInfo.loader(name); if (result.enableAutoAccept) { @@ -305,34 +302,100 @@ if (!global.NativeScriptHasPolyfilled) { global.NativeScriptHasPolyfilled = true; console.log('Installing polyfills...'); // DOM api polyfills - global.registerModule('timer', () => timer); - installPolyfills('timer', ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']); + if (__COMMONJS__) { + global.registerModule('timer', () => timer); + installPolyfills('timer', ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']); - global.registerModule('animation', () => animationFrame); - installPolyfills('animation', ['requestAnimationFrame', 'cancelAnimationFrame']); + global.registerModule('animation', () => animationFrame); + installPolyfills('animation', ['requestAnimationFrame', 'cancelAnimationFrame']); - global.registerModule('media-query-list', () => mediaQueryList); - installPolyfills('media-query-list', ['matchMedia', 'MediaQueryList']); + global.registerModule('media-query-list', () => mediaQueryList); + installPolyfills('media-query-list', ['matchMedia', 'MediaQueryList']); - global.registerModule('text', () => text); - installPolyfills('text', ['TextDecoder', 'TextEncoder']); + global.registerModule('text', () => text); + installPolyfills('text', ['TextDecoder', 'TextEncoder']); - global.registerModule('xhr', () => xhrImpl); - installPolyfills('xhr', ['XMLHttpRequest', 'FormData', 'Blob', 'File', 'FileReader']); + global.registerModule('xhr', () => xhrImpl); + installPolyfills('xhr', ['XMLHttpRequest', 'FormData', 'Blob', 'File', 'FileReader']); - global.registerModule('fetch', () => fetchPolyfill); - installPolyfills('fetch', ['fetch', 'Headers', 'Request', 'Response']); + global.registerModule('fetch', () => fetchPolyfill); + installPolyfills('fetch', ['fetch', 'Headers', 'Request', 'Response']); - global.registerModule('wgc', () => wgc); - installPolyfills('wgc', ['atob', 'btoa']); + global.registerModule('wgc', () => wgc); + installPolyfills('wgc', ['atob', 'btoa']); - global.registerModule('crypto', () => cryptoImpl); - installPolyfills('crypto', ['Crypto']); + global.registerModule('crypto', () => cryptoImpl); + installPolyfills('crypto', ['Crypto']); - global.registerModule('subtle', () => subtleCryptoImpl); - installPolyfills('subtle-crypto', ['Subtle']); + global.registerModule('subtle', () => subtleCryptoImpl); + installPolyfills('subtle-crypto', ['Subtle']); + } else { + // @ts-expect-error + global.setTimeout = timer.setTimeout; + global.clearTimeout = timer.clearTimeout; + // @ts-expect-error + global.setInterval = timer.setInterval; + global.clearInterval = timer.clearInterval; + // global.registerModule('timer', () => timer); + // installPolyfills('timer', ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']); - global.crypto = new global.Crypto(); + // global.registerModule('animation', () => animationFrame); + // installPolyfills('animation', ['requestAnimationFrame', 'cancelAnimationFrame']); + global.requestAnimationFrame = animationFrame.requestAnimationFrame; + global.cancelAnimationFrame = animationFrame.cancelAnimationFrame; + + // global.registerModule('media-query-list', () => mediaQueryList); + // installPolyfills('media-query-list', ['matchMedia', 'MediaQueryList']); + // @ts-expect-error + global.matchMedia = mediaQueryList.matchMedia; + // @ts-expect-error + global.MediaQueryList = mediaQueryList.MediaQueryList; + + // global.registerModule('text', () => text); + // installPolyfills('text', ['TextDecoder', 'TextEncoder']); + // @ts-expect-error + global.TextDecoder = text.TextDecoder; + // @ts-expect-error + global.TextEncoder = text.TextEncoder; + + // global.registerModule('xhr', () => xhrImpl); + // installPolyfills('xhr', ['XMLHttpRequest', 'FormData', 'Blob', 'File', 'FileReader']); + // @ts-expect-error + global.XMLHttpRequest = xhrImpl.XMLHttpRequest; + // @ts-expect-error + global.FormData = xhrImpl.FormData; + // @ts-expect-error + global.Blob = xhrImpl.Blob; + // @ts-expect-error + global.File = xhrImpl.File; + + // global.registerModule('fetch', () => fetchPolyfill); + // installPolyfills('fetch', ['fetch', 'Headers', 'Request', 'Response']); + // @ts-expect-error + global.fetch = fetchPolyfill.fetch; + // @ts-expect-error + global.Headers = fetchPolyfill.Headers; + // @ts-expect-error + global.Request = fetchPolyfill.Request; + // @ts-expect-error + global.Response = fetchPolyfill.Response; + + // global.registerModule('wgc', () => wgc); + // installPolyfills('wgc', ['atob', 'btoa']); + global.atob = wgc.atob; + global.btoa = wgc.btoa; + + // global.registerModule('crypto', () => cryptoImpl); + // installPolyfills('crypto', ['Crypto']); + + // global.registerModule('subtle', () => subtleCryptoImpl); + // installPolyfills('subtle-crypto', ['Subtle']); + // @ts-expect-error + global.SubtleCrypto = subtleCryptoImpl.SubtleCrypto; + } + + // @ts-expect-error + global.crypto = new cryptoImpl.Crypto(); // global.registerModule('abortcontroller', () => require('../abortcontroller')); // installPolyfills('abortcontroller', ['AbortController', 'AbortSignal']);