Skip to content

Commit

Permalink
Merge pull request #1650 from endojs/kris-bundle-aliases
Browse files Browse the repository at this point in the history
feat(compartment-mapper): Bundler support for aliases
  • Loading branch information
kriskowal authored Jun 23, 2023
2 parents a783f67 + ab02c2c commit e0bfe94
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions packages/compartment-mapper/src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import cjsSupport from './bundle-cjs.js';

const textEncoder = new TextEncoder();

const { quote: q } = assert;

/** @type {Record<string, ParserImplementation>} */
const parserForLanguage = {
mjs: parserArchiveMjs,
Expand All @@ -55,6 +57,7 @@ const sortedModules = (
entryModuleSpecifier,
) => {
const modules = [];
const aliases = new Map();
const seen = new Set();

/**
Expand Down Expand Up @@ -112,7 +115,9 @@ const sortedModules = (
aliasCompartmentName !== undefined &&
aliasModuleSpecifier !== undefined
) {
return recur(aliasCompartmentName, aliasModuleSpecifier);
const aliasKey = recur(aliasCompartmentName, aliasModuleSpecifier);
aliases.set(key, aliasKey);
return aliasKey;
}
}
}
Expand All @@ -124,7 +129,7 @@ const sortedModules = (

recur(entryCompartmentName, entryModuleSpecifier);

return modules;
return { modules, aliases };
};

const implementationPerParser = {
Expand All @@ -149,7 +154,7 @@ function getBundlerKitForModule(module) {
getFunctorCall: warning,
};
}
const getBundlerKit = implementationPerParser[parser].getBundlerKit;
const { getBundlerKit } = implementationPerParser[parser];
return getBundlerKit(module);
}

Expand Down Expand Up @@ -220,7 +225,7 @@ export const makeBundle = async (read, moduleLocation, options) => {
});
await compartment.load(entryModuleSpecifier);

const modules = sortedModules(
const { modules, aliases } = sortedModules(
compartmentMap.compartments,
sources,
resolvers,
Expand All @@ -239,10 +244,21 @@ export const makeBundle = async (read, moduleLocation, options) => {
const parsersInUse = new Set();
for (const module of modules) {
module.indexedImports = Object.fromEntries(
Object.entries(module.resolvedImports).map(([importSpecifier, key]) => [
importSpecifier,
modulesByKey[key].index,
]),
Object.entries(module.resolvedImports).map(([importSpecifier, key]) => {
key = aliases.get(key) ?? key;
const module = modulesByKey[key];
if (module === undefined) {
throw new Error(
`Unable to locate module for key ${q(key)} import specifier ${q(
importSpecifier,
)} in ${q(module.moduleSpecifier)} of compartment ${q(
module.compartmentName,
)}`,
);
}
const { index } = module;
return [importSpecifier, index];
}),
);
parsersInUse.add(module.parser);
module.bundlerKit = getBundlerKitForModule(module);
Expand Down

0 comments on commit e0bfe94

Please sign in to comment.