Skip to content

Commit

Permalink
export as module.exports if there are no other ExportDeclarations i…
Browse files Browse the repository at this point in the history
…n commonInterop module formatter @guybedford
  • Loading branch information
sebmck committed Nov 23, 2014
1 parent f4ce3a2 commit e3b8fa9
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 29 deletions.
63 changes: 63 additions & 0 deletions doc/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,69 @@ var test = 5; exports.test = test;
exports.default = test;
```

### Common interop

**In**

```javascript
import "foo";

import foo from "foo";
import * as foo from "foo";

import {bar} from "foo";
import {foo as bar} from "foo";

export {test};
export var test = 5;

export default test;
```

**Out**

```javascript
var _interopRequire = function (obj) {
return obj && (obj["default"] || obj);
};

require("foo");

var foo = _interopRequire(require("foo"));
var foo = require("foo");

var bar = require("foo").bar;
var bar = require("foo").foo;

exports.test = test;
var test = exports.test = 5;

exports["default"] = test;
```

#### module.exports behaviour

If there exist no other non-default `export`s then `default exports` are
exported as `module.exports` instead of `exports.default`.

**In**

```javascript
export default function foo() {

}
```

**Out**

```javascript
module.exports = foo;

function foo() {

}
```

### AMD

**In**
Expand Down
1 change: 1 addition & 0 deletions lib/6to5/templates/exports-default-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = VALUE;
28 changes: 27 additions & 1 deletion lib/6to5/transformation/modules/common-interop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
};
35 changes: 21 additions & 14 deletions lib/6to5/transformation/modules/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,32 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes)
}));
};

CommonJSFormatter.prototype.export = function (node, nodes) {
var declar = node.declaration;
CommonJSFormatter.prototype._hoistExport = function (declar, assign) {
if (t.isFunctionDeclaration(declar)) {
assign._blockHoist = true;
}

if (node.default) {
var ref = declar;
return assign;
};

if (t.isClass(ref) || t.isFunction(ref)) {
if (ref.id) {
nodes.push(t.toStatement(ref));
ref = ref.id;
}
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) {
nodes.push(util.template("exports-default", {
//inherits: node,

VALUE: ref
VALUE: this._pushStatement(declar, nodes)
}, true));
} else {
var assign;
Expand Down Expand Up @@ -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);
}
}
};
Expand Down Expand Up @@ -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);
};
8 changes: 4 additions & 4 deletions lib/6to5/transformation/transformers/es6-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict";

exports["default"] = 42;
exports["default"] = {};
exports["default"] = [];
exports["default"] = foo;
exports["default"] = function () {};
exports["default"] = function () {};
module.exports = foo;
module.exports = 42;
module.exports = {};
module.exports = [];
module.exports = foo;
module.exports = function () {};
module.exports = function () {};
function foo() {}
exports["default"] = foo;
var Foo = function Foo() {};

exports["default"] = Foo;
module.exports = Foo;
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

var _arguments = arguments;
var _argumentsToArray = function (args) {
var target = new Array(args.length);
for (var i = 0; i < args.length; i++) {
Expand All @@ -11,5 +10,5 @@ var _argumentsToArray = function (args) {
};

var concat = function () {
var arrs = _argumentsToArray(_arguments);
var arrs = _argumentsToArray(arguments);
};

0 comments on commit e3b8fa9

Please sign in to comment.