From 1d2ecd9e02527363ac8124915d5774e1c65bdedc Mon Sep 17 00:00:00 2001 From: Nicolas Martinos Date: Thu, 15 Jun 2023 11:28:21 +0200 Subject: [PATCH] fix: hardcodes export-application-global --- addon/initializers/embedded.ts | 3 +- .../initializers/export-application-global.ts | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 addon/initializers/export-application-global.ts diff --git a/addon/initializers/embedded.ts b/addon/initializers/embedded.ts index eb9350f4..4e41d7d0 100644 --- a/addon/initializers/embedded.ts +++ b/addon/initializers/embedded.ts @@ -97,6 +97,7 @@ function normalizeConfig(userConfig: GivenConfig): ObjectConfig { } export function initialize(application: Application): void { + const env = application.resolveRegistration('config:environment') as { embedded?: GivenConfig } const embeddedConfig: ObjectConfig = normalizeConfig(env.embedded) @@ -126,5 +127,5 @@ export function initialize(application: Application): void { export default { name: 'ember-cli-embedded', after: 'export-application-global', - initialize, + initialize } diff --git a/addon/initializers/export-application-global.ts b/addon/initializers/export-application-global.ts new file mode 100644 index 00000000..a3bd1807 --- /dev/null +++ b/addon/initializers/export-application-global.ts @@ -0,0 +1,42 @@ +import Application from '@ember/application' +import { classify } from '@ember/string' + +// @ts-ignore: because TS doesn't like this import +import config from "../config/environment" + +export function initialize(application: Application): void { + if (config.exportApplicationGlobal !== false) { + let theGlobal + + if (typeof window !== 'undefined') { + theGlobal = window + } else if (typeof global !== 'undefined') { + theGlobal = global + } else if (typeof self !== 'undefined') { + theGlobal = self + } else { + return + } + + const value = config.exportApplicationGlobal + + let globalName + + if (typeof value === 'string') { + globalName = value + } else { + globalName = classify(config.modulePrefix) + } + + // @ts-ignore: until there's a way to access a dynamic propertyName of window in TS + if (!theGlobal[globalName]) { + // @ts-ignore: until there's a way to set a dynamic propertyName to the window in TS + theGlobal[globalName] = application + } + } +} + +export default { + name: 'export-application-global', + initialize +} \ No newline at end of file