Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
ed78c0becf | |||
63c103fde5 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartenv",
|
"name": "@pushrocks/smartenv",
|
||||||
"version": "4.0.12",
|
"version": "4.0.13",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartenv",
|
"name": "@pushrocks/smartenv",
|
||||||
"version": "4.0.12",
|
"version": "4.0.13",
|
||||||
"description": "store things about your environment and let them travel across modules",
|
"description": "store things about your environment and let them travel across modules",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
@ -11,6 +11,21 @@ export interface IEnvObject {
|
|||||||
* Smartenv class that makes it easy
|
* Smartenv class that makes it easy
|
||||||
*/
|
*/
|
||||||
export class Smartenv {
|
export class Smartenv {
|
||||||
|
public async getEnvAwareModule(optionsArg: {
|
||||||
|
nodeModuleName: string;
|
||||||
|
webUrlArg: string;
|
||||||
|
getFunction: () => any;
|
||||||
|
}) {
|
||||||
|
if (this.isNode) {
|
||||||
|
const moduleResult = await this.getSafeNodeModule(optionsArg.nodeModuleName);
|
||||||
|
return moduleResult;
|
||||||
|
} else if (this.isBrowser) {
|
||||||
|
const moduleResult = await this.getSafeWebModule(optionsArg.webUrlArg, optionsArg.getFunction);
|
||||||
|
} else {
|
||||||
|
console.error('platform for loading not supported by smartenv');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public getSafeNodeModule<T = any>(moduleNameArg: string): T {
|
public getSafeNodeModule<T = any>(moduleNameArg: string): T {
|
||||||
if (!this.isNode) {
|
if (!this.isNode) {
|
||||||
console.error('You tried to load a node module in a wrong context');
|
console.error('You tried to load a node module in a wrong context');
|
||||||
@ -28,7 +43,7 @@ export class Smartenv {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public loadedScripts: string[] = [];
|
public loadedScripts: string[] = [];
|
||||||
public async getSafeWebModule(urlArg: string) {
|
public async getSafeWebModule(urlArg: string, getFunctionArg: () => any) {
|
||||||
if (!this.isBrowser) {
|
if (!this.isBrowser) {
|
||||||
console.error('You tried to load a web module in a wrong context');
|
console.error('You tried to load a web module in a wrong context');
|
||||||
return;
|
return;
|
||||||
@ -53,6 +68,7 @@ export class Smartenv {
|
|||||||
document.head.appendChild(script);
|
document.head.appendChild(script);
|
||||||
}
|
}
|
||||||
await done.promise;
|
await done.promise;
|
||||||
|
return getFunctionArg();
|
||||||
}
|
}
|
||||||
|
|
||||||
public get runtimeEnv() {
|
public get runtimeEnv() {
|
||||||
|
Reference in New Issue
Block a user