@jay-js/system
Version:
A powerful and flexible TypeScript library for UI, state management, lazy loading, routing and managing draggable elements in modern web applications.
89 lines • 3.93 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { moduleCache } from "./configuration.js";
/**
* Loads a module from the cache and resets its usage counter.
*
* @param {TLazyModule} lazy - Configuration object for the lazy module
* @returns {HTMLElement} The instantiated module element
* @throws {Error} When the module is not found in cache
*/
export function loadFromCache(lazy) {
if (!lazy.module) {
throw new Error("Module name is required.");
}
const cached = moduleCache.get(lazy.module);
if (!cached) {
throw new Error(`Module ${lazy.module} not found in cache`);
}
const moduleSection = cached.module(Object.assign({}, lazy.params));
cached.lastUsed = 0;
return moduleSection;
}
/**
* Loads a module by dynamically importing it and caching the result.
* Handles both named exports and default exports automatically.
*
* @param {TLazyModule} lazy - Configuration object for the lazy module
* @param {HTMLElement} moduleSection - Element to replace with the loaded module
* @returns {Promise<HTMLElement|undefined>} The loaded module element or undefined if loading fails
*/
export function loadModule(lazy, moduleSection) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
try {
const moduleImported = yield lazy.import();
const moduleName = lazy.module || "";
const moduleToUse = isDefaultExportModule(lazy) ? moduleImported.default : moduleImported[moduleName];
if (!moduleToUse) {
if (moduleImported.default && !isDefaultExportModule(lazy)) {
console.warn(`Named export '${moduleName}' not found, using default export instead.`);
moduleCache.set(moduleName, {
module: moduleImported.default,
lastUsed: 0,
collect: (_a = lazy.collect) !== null && _a !== void 0 ? _a : true,
});
}
else {
throw new Error(`Module ${moduleName} not found in the imported file.`);
}
}
else {
moduleCache.set(moduleName, {
module: moduleToUse,
lastUsed: 0,
collect: (_b = lazy.collect) !== null && _b !== void 0 ? _b : true,
});
}
const cached = moduleCache.get(moduleName);
if (!cached) {
throw new Error(`Module ${lazy.module} not found in cache`);
}
const loadedModule = yield cached.module(lazy.params || {});
moduleSection.replaceWith(loadedModule);
return loadedModule;
}
catch (error) {
console.error(`Error importing module ${lazy.module}:`, error);
}
});
}
/**
* Checks if the module uses default export based on its name.
*
* @param {TLazyModule} lazy - Configuration object for the lazy module
* @returns {boolean} True if the module uses default export
* @private
*/
function isDefaultExportModule(lazy) {
var _a;
return ((_a = lazy.module) === null || _a === void 0 ? void 0 : _a.startsWith("default_")) || false;
}
//# sourceMappingURL=module-loader.js.map