Skip to content

Commit

Permalink
feat(compartment-mapper): replace graph node exports with internal an…
Browse files Browse the repository at this point in the history
…d external aliases
  • Loading branch information
kumavis committed Dec 8, 2022
1 parent fdacf1b commit 1d52a8b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
15 changes: 14 additions & 1 deletion packages/compartment-mapper/src/infer-exports.js
Expand Up @@ -4,7 +4,7 @@

import { join, relativize } from './node-module-specifier.js';

const { entries, fromEntries } = Object;
const { entries, fromEntries, assign } = Object;
const { isArray } = Array;

/**
Expand Down Expand Up @@ -129,3 +129,16 @@ export const inferExportsEntries = function* inferExportsEntries(
*/
export const inferExports = (descriptor, tags, types) =>
fromEntries(inferExportsEntries(descriptor, tags, types));

export const inferExportsAndModules = (
descriptor,
externalAliases,
internalAliases,
tags,
types,
) => {
assign(
externalAliases,
fromEntries(inferExportsEntries(descriptor, tags, types)),
);
};
41 changes: 28 additions & 13 deletions packages/compartment-mapper/src/node-modules.js
Expand Up @@ -24,8 +24,9 @@
* @property {string} label
* @property {string} name
* @property {Array<string>} path
* @property {Record<string, string>} exports
* @property {boolean} explicitExports
* @property {Record<string, string>} internalAliases
* @property {Record<string, string>} externalAliases
* @property {Record<string, string>} dependencyLocations - from module name to
* location in storage.
* @property {Record<string, Language>} parsers - the parser for
Expand All @@ -34,7 +35,7 @@
* modules.
*/

import { inferExports } from './infer-exports.js';
import { inferExportsAndModules } from './infer-exports.js';
import { searchDescriptor } from './search.js';
import { parseLocatedJson } from './json.js';
import { unpackReadPowers } from './powers.js';
Expand Down Expand Up @@ -315,19 +316,33 @@ const graphPackage = async (
return data;
};

/** @type {Record<string, string>} */
const externalAliases = {};
/** @type {Record<string, string>} */
const internalAliases = {};

inferExportsAndModules(
packageDescriptor,
externalAliases,
internalAliases,
tags,
types,
);

Object.assign(result, {
name,
path: undefined,
label: `${name}${version ? `-v${version}` : ''}`,
exports: inferExports(packageDescriptor, tags, types),
explicitExports: exportsField !== undefined,
externalAliases,
internalAliases,
dependencyLocations,
types,
parsers: inferParsers(packageDescriptor, packageLocation),
});

await Promise.all(
values(result.exports).map(async item => {
values(result.externalAliases).map(async item => {
const descriptor = await readDescriptorUpwards(item);
if (descriptor && descriptor.type === 'module') {
types[item] = 'mjs';
Expand Down Expand Up @@ -510,21 +525,21 @@ const translateGraph = (
const { name, path, label, dependencyLocations, parsers, types } =
graph[dependeeLocation];
/** @type {Record<string, ModuleDescriptor>} */
const modules = Object.create(null);
const moduleDescriptors = Object.create(null);
/** @type {Record<string, ScopeDescriptor>} */
const scopes = Object.create(null);
/**
* @param {string} dependencyName
* @param {string} packageLocation
*/
const digest = (dependencyName, packageLocation) => {
const { exports, explicitExports } = graph[packageLocation];
for (const exportPath of keys(exports).sort()) {
const targetPath = exports[exportPath];
const digestExternalAliases = (dependencyName, packageLocation) => {
const { externalAliases, explicitExports } = graph[packageLocation];
for (const exportPath of keys(externalAliases).sort()) {
const targetPath = externalAliases[exportPath];
// dependency name may be different from package's name,
// as in the case of browser field dependency replacements
const localPath = join(dependencyName, exportPath);
modules[localPath] = {
moduleDescriptors[localPath] = {
compartment: packageLocation,
module: targetPath,
};
Expand All @@ -537,18 +552,18 @@ const translateGraph = (
}
};
// Support reflexive package imports.
digest(name, dependeeLocation);
digestExternalAliases(name, dependeeLocation);
// Support external package imports.
for (const dependencyName of keys(dependencyLocations).sort()) {
const dependencyLocation = dependencyLocations[dependencyName];
digest(dependencyName, dependencyLocation);
digestExternalAliases(dependencyName, dependencyLocation);
}
compartments[dependeeLocation] = {
label,
name,
path,
location: dependeeLocation,
modules,
modules: moduleDescriptors,
scopes,
parsers,
types,
Expand Down

0 comments on commit 1d52a8b

Please sign in to comment.