fix(core): update
This commit is contained in:
parent
b166269cb4
commit
276a0641e8
@ -12,6 +12,10 @@ export interface IEnvObject {
|
|||||||
*/
|
*/
|
||||||
export class Smartenv {
|
export class Smartenv {
|
||||||
public getSafeNodeModule<T = any>(moduleNameArg: string): T {
|
public getSafeNodeModule<T = any>(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
|
// tslint:disable-next-line: function-constructor
|
||||||
return new Function(
|
return new Function(
|
||||||
'exports',
|
'exports',
|
||||||
@ -23,6 +27,34 @@ export class Smartenv {
|
|||||||
)(exports, require, module, __filename, __dirname);
|
)(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() {
|
public get runtimeEnv() {
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
return 'browser';
|
return 'browser';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user