feat(compartment-mapper): Archives retain compartments only for retained modules
MODIFY
packages/compartment-mapper/src/archive.js
packages/compartment-mapper/src/archive.js
@@ -75,8 +75,8 @@ const translateCompartmentMap = (compartments, sources, renames) => {
const result = Object.create(null);
for (const compartmentName of keys(compartments).sort()) {
const compartment = compartments[compartmentName];
- const { name, label } = compartment;
-
+ const { name, label, retained } = compartment;
+ if (retained) {
// rename module compartments
/** @type {Record<string, ModuleDescriptor>} */
const modules = Object.create(null);
@@ -128,6 +128,7 @@ const translateCompartmentMap = (compartments, sources, renames) => {
// loadable module is captured in `modules`.
};
}
+ }
return result;
};

MODIFY
packages/compartment-mapper/src/import-hook.js
packages/compartment-mapper/src/import-hook.js
@@ -74,8 +74,8 @@ export const makeImportHookMaker = (
packageLocation = resolveLocation(packageLocation, baseLocation);
const packageSources = sources[packageLocation] || Object.create(null);
sources[packageLocation] = packageSources;
- const { modules = Object.create(null) } =
- compartments[packageLocation] || {};
+ const compartment = compartments[packageLocation] || {};
+ const { modules = Object.create(null) } = compartment;
/**
* @param {string} specifier
@@ -111,6 +111,8 @@ export const makeImportHookMaker = (
/** @type {ImportHook} */
const importHook = async moduleSpecifier => {
+ compartment.retained = true;
+
// per-module:
// In Node.js, an absolute specifier always indicates a built-in or

MODIFY
packages/compartment-mapper/src/link.js
packages/compartment-mapper/src/link.js
/** @typedef {import('./types.js').ModuleTransforms} ModuleTransforms */
/** @typedef {import('./types.js').Language} Language */
/** @typedef {import('./types.js').ModuleDescriptor} ModuleDescriptor */
+/** @typedef {import('./types.js').CompartmentDescriptor} CompartmentDescriptor */
/** @typedef {import('./types.js').CompartmentMapDescriptor} CompartmentMapDescriptor */
/** @typedef {import('./types.js').LinkOptions} LinkOptions */
@@ -173,6 +174,7 @@ const trimModuleSpecifierPrefix = (moduleSpecifier, prefix) => {
* Any module specifier with an absolute prefix should be captured by
* the `moduleMap` or `moduleMapHook`.
*
+ * @param {CompartmentDescriptor} compartmentDescriptor
* @param {Record<string, Compartment>} compartments
* @param {string} compartmentName
* @param {Record<string, ModuleDescriptor>} moduleDescriptors
@@ -182,6 +184,7 @@ const trimModuleSpecifierPrefix = (moduleSpecifier, prefix) => {
* @returns {ModuleMapHook | undefined}
*/
const makeModuleMapHook = (
+ compartmentDescriptor,
compartments,
compartmentName,
moduleDescriptors,
@@ -194,6 +197,8 @@ const makeModuleMapHook = (
* @returns {string | Object | undefined}
*/
const moduleMapHook = moduleSpecifier => {
+ compartmentDescriptor.retained = true;
+
const moduleDescriptor = moduleDescriptors[moduleSpecifier];
if (moduleDescriptor !== undefined) {
const {
@@ -366,6 +371,7 @@ export const link = (
const importHook = makeImportHook(location, name, parse, shouldDeferError);
const moduleMapHook = makeModuleMapHook(
+ compartmentDescriptor,
compartments,
compartmentName,
modules,

MODIFY
packages/compartment-mapper/src/types.js
packages/compartment-mapper/src/types.js
@@ -42,6 +42,9 @@ export {};
* constructing a sourceURL prefix that will match it to files in a developer
* workspace.
* @property {string} location
+ * @property {boolean} [retained] - whether this compartment was retained by
+ * any module in the solution. This property should never appear in an archived
+ * compartment map.
* @property {Record<string, ModuleDescriptor>} modules
* @property {Record<string, ScopeDescriptor>} scopes
* @property {Record<string, Language>} parsers - language for extension

feat(compartment-mapper): Archives retain compartments only for retained modules