fix(compartment-mapper): prettier bundle code, with some reduction
MODIFY
packages/compartment-mapper/src/bundle.js
packages/compartment-mapper/src/bundle.js
@@ -212,6 +212,23 @@ export const makeBundle = async (read, moduleLocation, options) => {
);
}
+ const exportsCellRecord = exportMap =>
+ ''.concat(
+ ...Object.keys(exportMap).map(
+ exportName => `\
+ ${exportName}: cell(${q(exportName)}),
+`,
+ ),
+ );
+ const importsCellSetter = (exportMap, index) =>
+ ''.concat(
+ ...Object.entries(exportMap).map(
+ ([exportName, [importName]]) => `\
+ ${importName}: cells[${index}].${exportName}.set,
+`,
+ ),
+ );
+
const bundle = `\
'use strict';
(functors => {
@@ -233,32 +250,27 @@ export const makeBundle = async (read, moduleLocation, options) => {
return { get, set, observe, enumerable: true };
}
- const cells = [${''.concat(
- ...modules.map(
- ({ record: { __fixedExportMap__, __liveExportMap__ } }) => `{
- ${''.concat(
- ...Object.keys(__fixedExportMap__).map(
- exportName => `${exportName}: cell(${q(exportName)}),\n`,
- ),
- )}
+ const cells = [
${''.concat(
- ...Object.keys(__liveExportMap__).map(
- exportName => `${exportName}: cell(${q(exportName)}),\n`,
- ),
- )}
- },`,
+ ...modules.map(
+ ({ record: { __fixedExportMap__, __liveExportMap__ } }) => `\
+ {
+${exportsCellRecord(__fixedExportMap__)}${exportsCellRecord(__liveExportMap__)}\
+ },
+`,
),
- )}];
+)}\
+ ];
${''.concat(
...modules.flatMap(({ index, indexedImports, record: { reexports } }) =>
reexports.map(
- (/* @type {string} */ importSpecifier) => `\
+ importSpecifier => `\
Object.defineProperties(cells[${index}], Object.getOwnPropertyDescriptors(cells[${indexedImports[importSpecifier]}]));
`,
),
),
- )}
+)}\
const namespaces = cells.map(cells => Object.create(null, cells));
@@ -266,6 +278,18 @@ export const makeBundle = async (read, moduleLocation, options) => {
cells[index]['*'] = cell('*', namespaces[index]);
}
+ function observeImports(map, importName, importIndex) {
+ for (const [name, observers] of map.get(importName)) {
+ const cell = cells[importIndex][name];
+ if (cell === undefined) {
+ throw new ReferenceError(\`Cannot import name \${name}\`);
+ }
+ for (const observer of observers) {
+ cell.observe(observer);
+ }
+ }
+ }
+
${''.concat(
...modules.map(
({
@@ -279,49 +303,31 @@ export const makeBundle = async (read, moduleLocation, options) => {
${''.concat(
...Object.entries(indexedImports).map(
([importName, importIndex]) => `\
- for (const [name, observers] of map.get(${q(importName)})) {
- const cell = cells[${importIndex}][name];
- if (cell === undefined) {
- throw new ReferenceError(\`Cannot import name \${name}\`);
- }
- for (const observer of observers) {
- cell.observe(observer);
- }
- }
+ observeImports(map, ${q(importName)}, ${importIndex});
`,
),
- )}
+)}\
},
liveVar: {
- ${''.concat(
- ...Object.entries(__liveExportMap__).map(
- ([exportName, [importName]]) => `\
- ${importName}: cells[${index}].${exportName}.set,
- `,
- ),
- )}
+${importsCellSetter(__liveExportMap__, index)}\
},
onceVar: {
- ${''.concat(
- ...Object.entries(__fixedExportMap__).map(
- ([exportName, [importName]]) => `\
- ${importName}: cells[${index}].${exportName}.set,
- `,
- ),
- )}
+${importsCellSetter(__fixedExportMap__, index)}\
},
});
`,
),
- )}
-
+)}\
})([
${''.concat(
...modules.map(
({ record: { __syncModuleProgram__ } }) =>
- `${__syncModuleProgram__}\n,\n`,
+ `\
+${__syncModuleProgram__}
+,
+`,
),
- )}
+)}\
]);
`;

fix(compartment-mapper): prettier bundle code, with some reduction