export as module.exports if there are no other ExportDeclarations in commonInterop module formatter @guybedford

This commit is contained in:
Sebastian McKenzie
2014-11-24 10:04:24 +11:00
parent f4ce3a23ad
commit e3b8fa93e2
7 changed files with 125 additions and 29 deletions

View File

@@ -0,0 +1 @@
module.exports = VALUE;

View File

@@ -3,9 +3,11 @@ module.exports = CommonJSInteropFormatter;
var CommonJSFormatter = require("./common");
var util = require("../../util");
var t = require("../../types");
var _ = require("lodash");
function CommonJSInteropFormatter(file) {
this.file = file;
this.has = false;
CommonJSFormatter.apply(this, arguments);
}
util.inherits(CommonJSInteropFormatter, CommonJSFormatter);
@@ -26,3 +28,27 @@ CommonJSInteropFormatter.prototype.importSpecifier = function (specifier, node,
CommonJSFormatter.prototype.importSpecifier.apply(this, arguments);
}
};
CommonJSInteropFormatter.prototype.export = function (node, nodes, parent) {
if (node.default && !this.has) {
var declar = node.declaration;
// module.exports = VALUE;
var assign = util.template("exports-default-module", {
VALUE: this._pushStatement(declar, nodes)
}, true);
// hoist to the top if this default is a function
nodes.push(this._hoistExport(declar, assign));
return;
} else {
this.has = true;
}
CommonJSFormatter.prototype.export.apply(this, arguments);
};
CommonJSInteropFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
this.has = true;
CommonJSFormatter.prototype.exportSpecifier.apply(this, arguments);
};

View File

@@ -38,23 +38,32 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes)
}));
};
CommonJSFormatter.prototype._hoistExport = function (declar, assign) {
if (t.isFunctionDeclaration(declar)) {
assign._blockHoist = true;
}
return assign;
};
CommonJSFormatter.prototype._pushStatement = function (ref, nodes) {
if (t.isClass(ref) || t.isFunction(ref)) {
if (ref.id) {
nodes.push(t.toStatement(ref));
ref = ref.id;
}
}
return ref;
};
CommonJSFormatter.prototype.export = function (node, nodes) {
var declar = node.declaration;
if (node.default) {
var ref = declar;
if (t.isClass(ref) || t.isFunction(ref)) {
if (ref.id) {
nodes.push(t.toStatement(ref));
ref = ref.id;
}
}
nodes.push(util.template("exports-default", {
//inherits: node,
VALUE: ref
VALUE: this._pushStatement(declar, nodes)
}, true));
} else {
var assign;
@@ -83,9 +92,7 @@ CommonJSFormatter.prototype.export = function (node, nodes) {
nodes.push(t.toStatement(declar));
nodes.push(assign);
if (t.isFunctionDeclaration(declar)) {
assign._blockHoist = true;
}
this._hoistExport(declar, assign);
}
}
};
@@ -126,7 +133,7 @@ CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node
};
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
return this._exportSpecifier(function () {
this._exportSpecifier(function () {
return t.callExpression(t.identifier("require"), [node.source]);
}, specifier, node, nodes);
};

View File

@@ -5,10 +5,10 @@ exports.ImportDeclaration = function (node, parent, file) {
if (node.specifiers.length) {
_.each(node.specifiers, function (specifier) {
file.moduleFormatter.importSpecifier(specifier, node, nodes);
file.moduleFormatter.importSpecifier(specifier, node, nodes, parent);
});
} else {
file.moduleFormatter.import(node, nodes);
file.moduleFormatter.import(node, nodes, parent);
}
return nodes;
@@ -18,10 +18,10 @@ exports.ExportDeclaration = function (node, parent, file) {
var nodes = [];
if (node.declaration) {
file.moduleFormatter.export(node, nodes);
file.moduleFormatter.export(node, nodes, parent);
} else {
_.each(node.specifiers, function (specifier) {
file.moduleFormatter.exportSpecifier(specifier, node, nodes);
file.moduleFormatter.exportSpecifier(specifier, node, nodes, parent);
});
}