From aead555b7b022326373c0fc55984da9f707947aa Mon Sep 17 00:00:00 2001 From: Marine Dunstetter Date: Thu, 27 Jun 2024 11:19:53 +0200 Subject: [PATCH] stop relying on require, pass the modules to loadInitializers instead --- addon/index.ts | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/addon/index.ts b/addon/index.ts index 99a6ef4..161f746 100644 --- a/addon/index.ts +++ b/addon/index.ts @@ -1,18 +1,11 @@ -declare global { - var requirejs: { - _eak_seen: Object; - }; -} - import Engine from '@ember/engine'; -import require from 'require'; -function resolveInitializer(moduleName: string) { - var module = require(moduleName, null, null, true); +function resolveInitializer(moduleName: string, compatModules: any) { + let module = compatModules[moduleName]; if (!module) { throw new Error(moduleName + ' must export an initializer.'); } - var initializer = module['default']; + let initializer = module['default']; if (!initializer) { throw new Error(moduleName + ' must have a default export'); } @@ -22,15 +15,15 @@ function resolveInitializer(moduleName: string) { return initializer; } -function registerInitializers(app: typeof Engine, moduleNames: string[]) { - for (var i = 0; i < moduleNames.length; i++) { - app.initializer(resolveInitializer(moduleNames[i])); +function registerInitializers(app: typeof Engine, moduleNames: string[], compatModules: any) { + for (let i = 0; i < moduleNames.length; i++) { + app.initializer(resolveInitializer(moduleNames[i], compatModules)); } } -function registerInstanceInitializers(app: typeof Engine, moduleNames: string[]) { - for (var i = 0; i < moduleNames.length; i++) { - app.instanceInitializer(resolveInitializer(moduleNames[i])); +function registerInstanceInitializers(app: typeof Engine, moduleNames: string[], compatModules: any) { + for (let i = 0; i < moduleNames.length; i++) { + app.instanceInitializer(resolveInitializer(moduleNames[i], compatModules)); } } @@ -41,16 +34,16 @@ function _endsWith(str: string, suffix: string): boolean { /** * Configure your application as it boots */ -export default function loadInitializers(app: typeof Engine, prefix: string): void { - var initializerPrefix = prefix + '/initializers/'; - var instanceInitializerPrefix = prefix + '/instance-initializers/'; - var initializers = []; - var instanceInitializers = []; +export default function loadInitializers(app: typeof Engine, prefix: string, compatModules: any): void { + const initializerPrefix = prefix + '/initializers/'; + const instanceInitializerPrefix = prefix + '/instance-initializers/'; + let initializers = []; + let instanceInitializers = []; // this is 2 pass because generally the first pass is the problem // and is reduced, and resolveInitializer has potential to deopt - var moduleNames = Object.keys(requirejs._eak_seen); - for (var i = 0; i < moduleNames.length; i++) { - var moduleName = moduleNames[i]; + const moduleNames = Object.keys(compatModules); + for (let i = 0; i < moduleNames.length; i++) { + const moduleName = moduleNames[i]; if (moduleName.lastIndexOf(initializerPrefix, 0) === 0) { if (!_endsWith(moduleName, '-test')) { initializers.push(moduleName); @@ -61,6 +54,6 @@ export default function loadInitializers(app: typeof Engine, prefix: string): vo } } } - registerInitializers(app, initializers); - registerInstanceInitializers(app, instanceInitializers); + registerInitializers(app, initializers, compatModules); + registerInstanceInitializers(app, instanceInitializers, compatModules); }