fix(ClassDeclaration): properly handle as a live export
MODIFY
src/babelPlugin.js
src/babelPlugin.js
@@ -125,8 +125,7 @@ function makeModulePlugins(options) {
return;
}
const decl = path.node;
- const declarations =
- decl.type === 'FunctionDeclaration' ? [decl] : decl.declarations;
+ const declarations = decl.declarations || [decl];
const vids = declarations.reduce((prior, { id: pat }) => {
prior.push(...collectPatternIdentifiers(path, pat));
return prior;
@@ -269,6 +268,34 @@ function makeModulePlugins(options) {
path.replaceWith(t.callExpression(callee, [path.node.declaration]));
}
},
+ ClassDeclaration(path) {
+ const ptype = path.parent.type;
+ if (ptype !== 'Program' && ptype !== 'ExportNamedDeclaration') {
+ return;
+ }
+
+ const { name } = path.node.id;
+ if (doAnalyze) {
+ topLevelIsOnce[name] = path.scope.getBinding(name).constant;
+ }
+ if (doTransform) {
+ if (topLevelExported[name]) {
+ const callee = t.memberExpression(
+ hiddenIdentifier(h.HIDDEN_LIVE),
+ path.node.id,
+ );
+ path.replaceWith(
+ t.blockStatement([
+ path.node,
+ t.expressionStatement(t.callExpression(callee, [path.node.id])),
+ ]),
+ );
+ for (const importTo of topLevelExported[name]) {
+ liveExportMap[importTo] = [name, true];
+ }
+ }
+ }
+ },
FunctionDeclaration(path) {
const ptype = path.parent.type;
if (ptype !== 'Program' && ptype !== 'ExportNamedDeclaration') {
@@ -349,8 +376,7 @@ function makeModulePlugins(options) {
}
if (decl) {
- const declarations =
- decl.type === 'FunctionDeclaration' ? [decl] : decl.declarations;
+ const declarations = decl.declarations || [decl];
const vids = declarations.reduce((prior, { id: pat }) => {
prior.push(...collectPatternIdentifiers(path, pat));
return prior;

MODIFY
test/test-export-named.js
test/test-export-named.js
@@ -97,7 +97,7 @@ export const { def, nest: [, ghi, ...nestrest], ...rest } = { def: 456, nest: [
test(`export hoisting`, async t => {
try {
- const importer = makeImporter(['abc', 'fn']);
+ const importer = makeImporter(['abc', 'fn', 'C', 'count']);
const transforms = [makeModuleTransformer(babelCore, importer)];
const { evaluateModule } = makeEvaluators({
transforms,
@@ -140,6 +140,13 @@ export const fn3 = fn;
t.equal(fn2, fn, `function hoisting`);
t.equal(fn, fn3, `function exports with hoisting`);
t.equal(fn(), 'foo', `fn evaluates`);
+
+ const { C, count } = await evaluateModule(`\
+export let count = 0;
+export class C {} if (C) { count += 1; }
+`);
+ t.assert(new C(), `class exports`);
+ t.equal(count, 1, `class C is global`);
} catch (e) {
console.log('unexpected exception', e);
t.assert(false, e);

fix(ClassDeclaration): properly handle as a live export