From 276a0641e84d5d6b9cfc6cbbb37b84a4bea444cb Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Tue, 29 Sep 2020 17:49:33 +0000 Subject: [PATCH] fix(core): update --- ts/smartenv.classes.smartenv.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/ts/smartenv.classes.smartenv.ts b/ts/smartenv.classes.smartenv.ts index 4c95afb..e1192c4 100644 --- a/ts/smartenv.classes.smartenv.ts +++ b/ts/smartenv.classes.smartenv.ts @@ -12,6 +12,10 @@ export interface IEnvObject { */ export class Smartenv { public getSafeNodeModule(moduleNameArg: string): T { + if (!this.isNode) { + console.error('You tried to load a node module in a wrong context'); + return; + } // tslint:disable-next-line: function-constructor return new Function( 'exports', @@ -23,6 +27,34 @@ export class Smartenv { )(exports, require, module, __filename, __dirname); } + public loadedScripts: string[] = []; + public async getSafeWebModule(urlArg: string) { + if (!this.isBrowser) { + console.error('You tried to load a web module in a wrong context'); + return; + } + + if (this.loadedScripts.includes(urlArg)) { + return; + } else { + this.loadedScripts.push(urlArg); + } + + const done = plugins.smartpromise.defer(); + if (globalThis.importScripts) { + globalThis.importScripts(urlArg); + done.resolve(); + } else { + const script = document.createElement('script'); + script.onload = () => { + done.resolve(); + }; + script.src = urlArg; + document.head.appendChild(script); + } + await done.promise; + } + public get runtimeEnv() { if (typeof window !== 'undefined') { return 'browser';