allow export default non-functions mapping to module.exports in common module formatter - fixes #356

This commit is contained in:
Sebastian McKenzie 2015-01-01 22:58:46 +11:00
parent af412cae28
commit 52e23473ed
8 changed files with 45 additions and 14 deletions

View File

@ -126,7 +126,7 @@ DefaultFormatter.prototype._pushStatement = function (ref, nodes) {
DefaultFormatter.prototype._hoistExport = function (declar, assign, priority) {
if (t.isFunctionDeclaration(declar)) {
assign._blockHoist = priority || 1;
assign._blockHoist = priority || 2;
}
return assign;

View File

@ -57,6 +57,7 @@ CommonJSFormatter.prototype.importDeclaration = function (node, nodes) {
CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
if (node.default) {
var declar = node.declaration;
var assign;
// module.exports = VALUE;
var templateName = "exports-default-module";
@ -64,15 +65,29 @@ CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
// exports = module.exports = VALUE;
if (this.hasNonDefaultExports) templateName = "exports-default-module-override";
var assign = util.template(templateName, {
if (t.isFunction(declar) || !this.hasNonDefaultExports) {
assign = util.template(templateName, {
VALUE: this._pushStatement(declar, nodes)
}, true);
// hoist to the top if this default is a function
nodes.push(this._hoistExport(declar, assign, 2));
nodes.push(this._hoistExport(declar, assign, 3));
return;
} else {
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
// this export isn't a function so we can't hoist it to the top so we need to set it
// at the very end of the file with something like:
//
// module.exports = Object.assign(exports["default"], exports)
//
assign = util.template("common-export-default-assign", true);
assign._blockHoist = 0;
nodes.push(assign);
}
}
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
};
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {

View File

@ -0,0 +1 @@
module.exports = Object.assign(exports["default"], exports);

View File

@ -1,17 +1,27 @@
var _ = require("lodash");
// Priority:
//
// - 0 We want this to be at the **very** bottom
// - 1 Default node position
// - 2 Priority over normal nodes
// - 3 We want this to be at the **very** top
exports.BlockStatement =
exports.Program = {
exit: function (node) {
var hasChange = false;
for (var i in node.body) {
var bodyNode = node.body[i];
if (bodyNode && bodyNode._blockHoist) hasChange = true;
if (bodyNode && bodyNode._blockHoist != null) hasChange = true;
}
if (!hasChange) return;
var nodePriorities = _.groupBy(node.body, function (bodyNode) {
return bodyNode._blockHoist || 0;
var priority = bodyNode._blockHoist;
if (priority == null) priority = 1;
if (priority === true) priority = 2;
return priority;
});
node.body = _.flatten(_.values(nodePriorities).reverse());

View File

@ -0,0 +1,3 @@
export default new Cachier()
export function Cachier(databaseName) {}

View File

@ -0,0 +1,6 @@
"use strict";
exports.Cachier = Cachier;
exports["default"] = new Cachier();
function Cachier(databaseName) {}
module.exports = Object.assign(exports["default"], exports);

View File

@ -8,5 +8,3 @@ import {foo as bar} from "foo";
export {test};
export var test = 5;
export default test;

View File

@ -18,5 +18,3 @@ var bar = require("foo").bar;
var bar = require("foo").foo;
exports.test = test;
var test = exports.test = 5;
exports = module.exports = test;