allow export default non-functions mapping to module.exports in common module formatter - fixes #356
This commit is contained in:
parent
af412cae28
commit
52e23473ed
@ -126,7 +126,7 @@ DefaultFormatter.prototype._pushStatement = function (ref, nodes) {
|
|||||||
|
|
||||||
DefaultFormatter.prototype._hoistExport = function (declar, assign, priority) {
|
DefaultFormatter.prototype._hoistExport = function (declar, assign, priority) {
|
||||||
if (t.isFunctionDeclaration(declar)) {
|
if (t.isFunctionDeclaration(declar)) {
|
||||||
assign._blockHoist = priority || 1;
|
assign._blockHoist = priority || 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return assign;
|
return assign;
|
||||||
|
|||||||
@ -57,6 +57,7 @@ CommonJSFormatter.prototype.importDeclaration = function (node, nodes) {
|
|||||||
CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
|
CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||||
if (node.default) {
|
if (node.default) {
|
||||||
var declar = node.declaration;
|
var declar = node.declaration;
|
||||||
|
var assign;
|
||||||
|
|
||||||
// module.exports = VALUE;
|
// module.exports = VALUE;
|
||||||
var templateName = "exports-default-module";
|
var templateName = "exports-default-module";
|
||||||
@ -64,15 +65,29 @@ CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
|
|||||||
// exports = module.exports = VALUE;
|
// exports = module.exports = VALUE;
|
||||||
if (this.hasNonDefaultExports) templateName = "exports-default-module-override";
|
if (this.hasNonDefaultExports) templateName = "exports-default-module-override";
|
||||||
|
|
||||||
var assign = util.template(templateName, {
|
if (t.isFunction(declar) || !this.hasNonDefaultExports) {
|
||||||
VALUE: this._pushStatement(declar, nodes)
|
assign = util.template(templateName, {
|
||||||
}, true);
|
VALUE: this._pushStatement(declar, nodes)
|
||||||
|
}, true);
|
||||||
|
|
||||||
// hoist to the top if this default is a function
|
// hoist to the top if this default is a function
|
||||||
nodes.push(this._hoistExport(declar, assign, 2));
|
nodes.push(this._hoistExport(declar, assign, 3));
|
||||||
} else {
|
return;
|
||||||
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
|
} else {
|
||||||
|
// 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) {
|
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||||
|
|||||||
@ -0,0 +1 @@
|
|||||||
|
module.exports = Object.assign(exports["default"], exports);
|
||||||
@ -1,17 +1,27 @@
|
|||||||
var _ = require("lodash");
|
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.BlockStatement =
|
||||||
exports.Program = {
|
exports.Program = {
|
||||||
exit: function (node) {
|
exit: function (node) {
|
||||||
var hasChange = false;
|
var hasChange = false;
|
||||||
for (var i in node.body) {
|
for (var i in node.body) {
|
||||||
var bodyNode = node.body[i];
|
var bodyNode = node.body[i];
|
||||||
if (bodyNode && bodyNode._blockHoist) hasChange = true;
|
if (bodyNode && bodyNode._blockHoist != null) hasChange = true;
|
||||||
}
|
}
|
||||||
if (!hasChange) return;
|
if (!hasChange) return;
|
||||||
|
|
||||||
var nodePriorities = _.groupBy(node.body, function (bodyNode) {
|
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());
|
node.body = _.flatten(_.values(nodePriorities).reverse());
|
||||||
|
|||||||
3
test/fixtures/transformation/es6-modules-common/exports-default-non-function/actual.js
vendored
Normal file
3
test/fixtures/transformation/es6-modules-common/exports-default-non-function/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export default new Cachier()
|
||||||
|
|
||||||
|
export function Cachier(databaseName) {}
|
||||||
6
test/fixtures/transformation/es6-modules-common/exports-default-non-function/expected.js
vendored
Normal file
6
test/fixtures/transformation/es6-modules-common/exports-default-non-function/expected.js
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
exports.Cachier = Cachier;
|
||||||
|
exports["default"] = new Cachier();
|
||||||
|
function Cachier(databaseName) {}
|
||||||
|
module.exports = Object.assign(exports["default"], exports);
|
||||||
@ -8,5 +8,3 @@ import {foo as bar} from "foo";
|
|||||||
|
|
||||||
export {test};
|
export {test};
|
||||||
export var test = 5;
|
export var test = 5;
|
||||||
|
|
||||||
export default test;
|
|
||||||
|
|||||||
@ -18,5 +18,3 @@ var bar = require("foo").bar;
|
|||||||
var bar = require("foo").foo;
|
var bar = require("foo").foo;
|
||||||
exports.test = test;
|
exports.test = test;
|
||||||
var test = exports.test = 5;
|
var test = exports.test = 5;
|
||||||
|
|
||||||
exports = module.exports = test;
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user