Skip to content

Commit

Permalink
fix(static-module-record): cleaner Babel codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 18, 2021
1 parent dc9ccaa commit 6e22569
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
26 changes: 14 additions & 12 deletions packages/compartment-mapper/src/bundle.js
Expand Up @@ -231,7 +231,19 @@ export const makeBundle = async (read, moduleLocation, options) => {

const bundle = `\
'use strict';
(functors => {
(() => {
const functors = [
${''.concat(
...modules.map(
({ record: { __syncModuleProgram__ } }, i) =>
`\
// === functors[${i}] ===
${__syncModuleProgram__},
`,
),
)}\
]; // functors end
function cell(name, value = undefined) {
const observers = [];
function set(newValue) {
Expand Down Expand Up @@ -318,17 +330,7 @@ ${importsCellSetter(__fixedExportMap__, index)}\
`,
),
)}\
})([
${''.concat(
...modules.map(
({ record: { __syncModuleProgram__ } }) =>
`\
${__syncModuleProgram__}
,
`,
),
)}\
]);
})();
`;

return bundle;
Expand Down
36 changes: 26 additions & 10 deletions packages/static-module-record/src/babelPlugin.js
Expand Up @@ -38,6 +38,20 @@ const collectPatternIdentifiers = (path, pattern) => {
}
};

const prependReplacements = (replacements, node) => {
const lastReplace = replacements[replacements.length - 1];
if (lastReplace) {
lastReplace.trailingComments = node.trailingComments;
node.trailingComments = undefined;
}
replacements.unshift(node);
};

const displayAsExport = node => {
node.loc.prependText = '/*EX*/ ';
return node;
};

function makeModulePlugins(options) {
const {
sourceType,
Expand Down Expand Up @@ -252,11 +266,11 @@ function makeModulePlugins(options) {
case 'VariableDeclaration': {
// We rewrote the declaration.
rewrittenDecls.add(decl);
replace.unshift(decl);
prependReplacements(replace, decl);
break;
}
case 'FunctionDeclaration': {
replace.unshift(decl);
prependReplacements(replace, decl);
break;
}
default: {
Expand Down Expand Up @@ -390,20 +404,22 @@ function makeModulePlugins(options) {
if (decl.id) {
// Just keep the same declaration and mark it as the default.
path.replaceWithMultiple([
decl,
displayAsExport(decl),
t.expressionStatement(t.callExpression(callee, [decl.id])),
]);
return;
}

// const {default: $c_default} = {default: (XXX)}; $h_once.default($c_default);
path.replaceWithMultiple([
t.variableDeclaration('const', [
t.variableDeclarator(
t.objectPattern([t.objectProperty(id, cid)]),
t.objectExpression([t.objectProperty(id, expr)]),
),
]),
displayAsExport(
t.variableDeclaration('const', [
t.variableDeclarator(
t.objectPattern([t.objectProperty(id, cid)]),
t.objectExpression([t.objectProperty(id, expr)]),
),
]),
),
t.expressionStatement(t.callExpression(callee, [cid])),
]);
}
Expand Down Expand Up @@ -567,7 +583,7 @@ function makeModulePlugins(options) {
});
}
if (doTransform) {
path.replaceWithMultiple(decl ? [decl] : []);
path.replaceWithMultiple(decl ? [displayAsExport(decl)] : []);
}
},
});
Expand Down

0 comments on commit 6e22569

Please sign in to comment.