UNPKG

@tripsnek/tmf

Version:

TypeScript Modeling Framework - A TypeScript port of the Eclipse Modeling Framework (EMF)

91 lines 3.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConditionalImports = exports.Environment = void 0; exports.safeDynamicImport = safeDynamicImport; /** * Enables enviromental checks for conditional imports, ensuring * that Node functionality in bundle that is not used does not cause * issues in browsers or WebView */ class Environment { static _isNode = null; static _isBrowser = null; static get isNode() { if (this._isNode === null) { try { this._isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null; } catch { this._isNode = false; } } return this._isNode; } static get isBrowser() { if (this._isBrowser === null) { try { this._isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined'; } catch { this._isBrowser = false; } } return this._isBrowser; } static get hasDOMParser() { try { return typeof DOMParser !== 'undefined'; } catch { return false; } } static requireNodeEnvironment(operation) { if (!this.isNode) { throw new Error(`${operation} is only available in Node.js environment`); } } } exports.Environment = Environment; // Conditional imports helper - now redundant but kept for backward compatibility class ConditionalImports { static nodeModules = new Map(); /** * This method is kept for backward compatibility but direct imports are preferred */ static async getNodeModule(moduleName) { Environment.requireNodeEnvironment(`Loading ${moduleName}`); if (!this.nodeModules.has(moduleName)) { try { const module = await safeDynamicImport(moduleName); this.nodeModules.set(moduleName, module); return module; } catch (error) { throw new Error(`Failed to load Node.js module '${moduleName}': ${error.message}`); } } return this.nodeModules.get(moduleName); } /** * Helper method for safe dynamic imports with better error messages */ static async safeImport(moduleName, operation) { Environment.requireNodeEnvironment(operation); try { return await safeDynamicImport(moduleName); } catch (error) { throw new Error(`${operation} failed: Cannot load '${moduleName}' module. This operation requires Node.js environment. ${error.message}`); } } } exports.ConditionalImports = ConditionalImports; // Or use Function constructor (more robust): function safeDynamicImport(moduleName) { return new Function('moduleName', 'return import(moduleName)')(moduleName); } //# sourceMappingURL=environment.js.map