@esmx/core
Version:
A high-performance microfrontend framework supporting Vue, React, Preact, Solid, and Svelte with SSR and Module Federation capabilities.
51 lines (50 loc) • 1.55 kB
JavaScript
import { pathWithoutIndex } from "./path-without-index.mjs";
export function buildImportsMap(manifests, getFile) {
const imports = {};
manifests.forEach((manifest) => {
Object.entries(manifest.exports).forEach(([, exportItem]) => {
const file = getFile(manifest.name, exportItem.file);
imports[exportItem.identifier] = file;
});
});
manifests.forEach((manifest) => {
Object.entries(manifest.imports).forEach(([name, identifier]) => {
const fullName = `${manifest.name}/${name}`;
imports[fullName] = imports[identifier] ?? identifier;
});
});
pathWithoutIndex(imports);
return imports;
}
export function buildScopesMap(imports, manifests, getScope) {
const scopes = {};
manifests.forEach((manifest) => {
if (!manifest.scopes) {
return;
}
Object.entries(manifest.scopes).forEach(([scopeName, specifierMap]) => {
const scopedImports = {};
Object.entries(specifierMap).forEach(
([specifierName, identifier]) => {
scopedImports[specifierName] = imports[identifier] ?? identifier;
}
);
const scopePath = imports[`${manifest.name}/${scopeName}`] ?? `/${scopeName}`;
const scopeKey = getScope(manifest.name, scopePath);
scopes[scopeKey] = scopedImports;
});
});
return scopes;
}
export function getImportMap({
manifests,
getFile,
getScope
}) {
const imports = buildImportsMap(manifests, getFile);
const scopes = buildScopesMap(imports, manifests, getScope);
return {
imports,
scopes
};
}