Skip to content

Commit

Permalink
feat(compartment-mapper): handle internalAliases including internal p…
Browse files Browse the repository at this point in the history
…ackage aliases
  • Loading branch information
kumavis committed Dec 8, 2022
1 parent 1d52a8b commit 979f9c1
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions packages/compartment-mapper/src/node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,26 @@ const graphPackage = async (
);

await Promise.all(children);

// handle internalAliases package aliases
for (const specifier of keys(internalAliases).sort()) {
const target = internalAliases[specifier];
// ignore all internalAliases where the specifier or target is relative
const specifierIsRelative = specifier.startsWith('./') || specifier === '.';
// eslint-disable-next-line no-continue
if (specifierIsRelative) continue;
const targetIsRelative = target.startsWith('./') || target === '.';
// eslint-disable-next-line no-continue
if (targetIsRelative) continue;
const targetLocation = dependencyLocations[target];
if (targetLocation === undefined) {
throw new Error(
`Cannot find dependency ${target} for ${packageLocation}`,
);
}
dependencyLocations[specifier] = targetLocation;
}

return undefined;
};

Expand Down Expand Up @@ -522,12 +542,20 @@ const translateGraph = (
// package and is a complete list of every external module that the
// corresponding compartment can import.
for (const dependeeLocation of keys(graph).sort()) {
const { name, path, label, dependencyLocations, parsers, types } =
graph[dependeeLocation];
const {
name,
path,
label,
dependencyLocations,
internalAliases,
parsers,
types,
} = graph[dependeeLocation];
/** @type {Record<string, ModuleDescriptor>} */
const moduleDescriptors = Object.create(null);
/** @type {Record<string, ScopeDescriptor>} */
const scopes = Object.create(null);

/**
* @param {string} dependencyName
* @param {string} packageLocation
Expand Down Expand Up @@ -558,6 +586,20 @@ const translateGraph = (
const dependencyLocation = dependencyLocations[dependencyName];
digestExternalAliases(dependencyName, dependencyLocation);
}
// digest own internal aliases
for (const modulePath of keys(internalAliases).sort()) {
const facetTarget = internalAliases[modulePath];
const targetIsRelative =
facetTarget.startsWith('./') || facetTarget === '.';
if (targetIsRelative) {
// add target to moduleDescriptors
moduleDescriptors[modulePath] = {
compartment: dependeeLocation,
module: facetTarget,
};
}
}

compartments[dependeeLocation] = {
label,
name,
Expand Down

0 comments on commit 979f9c1

Please sign in to comment.