add default live bindings to common module formatter
This commit is contained in:
parent
5f91ee8a1a
commit
b7a08100a6
@ -61,7 +61,6 @@ var importsVisitor = {
|
||||
enter(node, parent, scope, formatter) {
|
||||
formatter.hasLocalImports = true;
|
||||
extend(formatter.localImports, this.getBindingIdentifiers());
|
||||
formatter.bumpImportOccurences(node);
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -69,7 +68,7 @@ var importsVisitor = {
|
||||
var exportsVisitor = traverse.explode({
|
||||
ExportDeclaration: {
|
||||
enter(node, parent, scope, formatter) {
|
||||
formatter.hasLocalImports = true;
|
||||
formatter.hasLocalExports = true;
|
||||
|
||||
var declar = this.get("declaration");
|
||||
if (declar.isStatement()) {
|
||||
@ -96,10 +95,6 @@ var exportsVisitor = traverse.explode({
|
||||
formatter.hasNonDefaultExports = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (node.source) {
|
||||
formatter.bumpImportOccurences(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -117,7 +112,6 @@ export default class DefaultFormatter {
|
||||
this.hasLocalExports = false;
|
||||
this.hasLocalImports = false;
|
||||
|
||||
this.localImportOccurences = object();
|
||||
this.localExports = object();
|
||||
this.localImports = object();
|
||||
|
||||
@ -133,15 +127,6 @@ export default class DefaultFormatter {
|
||||
return (t.isExportDefaultDeclaration(node) || t.isSpecifierDefault(node)) && !this.noInteropRequireExport && !this.hasNonDefaultExports;
|
||||
}
|
||||
|
||||
bumpImportOccurences(node) {
|
||||
var source = node.source.value;
|
||||
var occurs = this.localImportOccurences;
|
||||
occurs[source] ||= 0;
|
||||
if (node.specifiers) {
|
||||
occurs[source] += node.specifiers.length;
|
||||
}
|
||||
}
|
||||
|
||||
getLocalExports() {
|
||||
this.file.path.traverse(exportsVisitor, this);
|
||||
}
|
||||
@ -151,7 +136,7 @@ export default class DefaultFormatter {
|
||||
}
|
||||
|
||||
remapAssignments() {
|
||||
if (this.hasLocalImports) {
|
||||
if (this.hasLocalExports || this.hasLocalImports) {
|
||||
this.file.path.traverse(remapVisitor, this);
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,7 +6,9 @@ import * as util from "../../util";
|
||||
import * as t from "../../types";
|
||||
|
||||
export default class AMDFormatter extends DefaultFormatter {
|
||||
init = CommonFormatter.prototype.init;
|
||||
init() {
|
||||
CommonFormatter.prototype._init.call(this, this.hasNonDefaultExports);
|
||||
}
|
||||
|
||||
buildDependencyLiterals() {
|
||||
var names = [];
|
||||
@ -100,15 +102,34 @@ export default class AMDFormatter extends DefaultFormatter {
|
||||
this.internalRemap[specifier.local.name] = ref;
|
||||
}
|
||||
|
||||
exportSpecifier() {
|
||||
CommonFormatter.prototype.exportSpecifier.apply(this, arguments);
|
||||
exportSpecifier(specifier, node, nodes) {
|
||||
if (this.doDefaultExportInterop(specifier)) {
|
||||
nodes.push(util.template("exports-default-assign", {
|
||||
VALUE: specifier.local
|
||||
}, true));
|
||||
} else {
|
||||
CommonFormatter.prototype.exportSpecifier.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
exportDeclaration(node) {
|
||||
exportDeclaration(node, nodes) {
|
||||
if (this.doDefaultExportInterop(node)) {
|
||||
this.passModuleArg = true;
|
||||
|
||||
var declar = node.declaration;
|
||||
var assign = util.template("exports-default-assign", {
|
||||
VALUE: this._pushStatement(declar, nodes)
|
||||
}, true);
|
||||
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
// we can hoist this assignment to the top of the file
|
||||
assign._blockHoist = 3;
|
||||
}
|
||||
|
||||
nodes.push(assign);
|
||||
return;
|
||||
}
|
||||
|
||||
CommonFormatter.prototype.exportDeclaration.apply(this, arguments);
|
||||
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,13 +5,17 @@ import * as t from "../../types";
|
||||
|
||||
export default class CommonJSFormatter extends DefaultFormatter {
|
||||
init() {
|
||||
this._init(this.hasLocalExports);
|
||||
}
|
||||
|
||||
_init(conditional) {
|
||||
var file = this.file;
|
||||
var scope = file.scope;
|
||||
|
||||
scope.rename("module");
|
||||
scope.rename("exports");
|
||||
|
||||
if (!this.noInteropRequireImport && this.hasNonDefaultExports) {
|
||||
if (!this.noInteropRequireImport && conditional) {
|
||||
var templateName = "exports-module-declaration";
|
||||
if (this.file.isLoose("es6.modules")) templateName += "-loose";
|
||||
var declar = util.template(templateName, true);
|
||||
@ -20,6 +24,20 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
}
|
||||
}
|
||||
|
||||
transform(program) {
|
||||
DefaultFormatter.prototype.transform.apply(this, arguments);
|
||||
|
||||
if (this.hasDefaultOnlyExport) {
|
||||
program.body.push(
|
||||
t.expressionStatement(t.assignmentExpression(
|
||||
"=",
|
||||
t.memberExpression(t.identifier("module"), t.identifier("exports")),
|
||||
t.memberExpression(t.identifier("exports"), t.identifier("default"))
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
importSpecifier(specifier, node, nodes) {
|
||||
var variableName = specifier.local;
|
||||
|
||||
@ -31,11 +49,15 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
this.internalRemap[variableName.name] = ref;
|
||||
} else {
|
||||
if (this.noInteropRequireImport) {
|
||||
this.internalRemap[variableName.name] = t.memberExpression(ref, t.identifier("default"))
|
||||
this.internalRemap[variableName.name] = t.memberExpression(ref, t.identifier("default"));
|
||||
} else if (!includes(this.file.dynamicImported, node)) {
|
||||
var uid = this.scope.generateUidBasedOnNode(node, "import");
|
||||
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(variableName, t.callExpression(this.file.addHelper("interop-require"), [ref]))
|
||||
t.variableDeclarator(uid, t.callExpression(this.file.addHelper("interop-require-wildcard"), [ref]))
|
||||
]));
|
||||
|
||||
this.internalRemap[variableName.name] = t.memberExpression(uid, t.identifier("default"));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -64,29 +86,15 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
|
||||
exportSpecifier(specifier, node, nodes) {
|
||||
if (this.doDefaultExportInterop(specifier)) {
|
||||
nodes.push(util.template("exports-default-assign", {
|
||||
VALUE: specifier.local
|
||||
}, true));
|
||||
return;
|
||||
} else {
|
||||
DefaultFormatter.prototype.exportSpecifier.apply(this, arguments);
|
||||
this.hasDefaultOnlyExport = true;
|
||||
}
|
||||
|
||||
DefaultFormatter.prototype.exportSpecifier.apply(this, arguments);
|
||||
}
|
||||
|
||||
exportDeclaration(node, nodes) {
|
||||
if (this.doDefaultExportInterop(node)) {
|
||||
var declar = node.declaration;
|
||||
var assign = util.template("exports-default-assign", {
|
||||
VALUE: this._pushStatement(declar, nodes)
|
||||
}, true);
|
||||
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
// we can hoist this assignment to the top of the file
|
||||
assign._blockHoist = 3;
|
||||
}
|
||||
|
||||
nodes.push(assign);
|
||||
return;
|
||||
this.hasDefaultOnlyExport = true;
|
||||
}
|
||||
|
||||
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
|
||||
|
||||
@ -2,13 +2,16 @@
|
||||
|
||||
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
|
||||
|
||||
module.exports = foo;
|
||||
module.exports = 42;
|
||||
module.exports = {};
|
||||
module.exports = [];
|
||||
module.exports = foo;
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports["default"] = foo;
|
||||
exports["default"] = 42;
|
||||
exports["default"] = {};
|
||||
exports["default"] = [];
|
||||
exports["default"] = foo;
|
||||
|
||||
module.exports = function () {};
|
||||
exports["default"] = function () {};
|
||||
|
||||
var _default = (function () {
|
||||
var _class = function _default() {
|
||||
@ -18,7 +21,7 @@ var _default = (function () {
|
||||
return _class;
|
||||
})();
|
||||
|
||||
module.exports = _default;
|
||||
exports["default"] = _default;
|
||||
|
||||
function foo() {}
|
||||
|
||||
@ -26,5 +29,6 @@ var Foo = function Foo() {
|
||||
_classCallCheck(this, Foo);
|
||||
};
|
||||
|
||||
module.exports = Foo;
|
||||
module.exports = foo;
|
||||
exports["default"] = Foo;
|
||||
exports["default"] = foo;
|
||||
module.exports = exports["default"];
|
||||
@ -1,12 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
|
||||
var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { "default": obj }; };
|
||||
|
||||
var _foo = require("foo");
|
||||
|
||||
var foo = _interopRequire(_foo);
|
||||
var _foo2 = _interopRequireWildcard(_foo);
|
||||
|
||||
var foo2 = _interopRequire(_foo);
|
||||
var _foo22 = _interopRequireWildcard(_foo);
|
||||
|
||||
foo;
|
||||
foo2;
|
||||
_foo2["default"];
|
||||
_foo22["default"];
|
||||
@ -1,10 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
|
||||
var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { "default": obj }; };
|
||||
|
||||
var _foo$xyz = require("foo");
|
||||
|
||||
var foo = _interopRequire(_foo$xyz);
|
||||
var _foo$xyz2 = _interopRequireWildcard(_foo$xyz);
|
||||
|
||||
foo;
|
||||
_foo$xyz.baz;
|
||||
_foo$xyz2["default"];
|
||||
_foo$xyz.baz;
|
||||
@ -2,8 +2,6 @@
|
||||
|
||||
var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { "default": obj }; };
|
||||
|
||||
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
@ -16,7 +14,7 @@ require("./directory/foo-bar");
|
||||
|
||||
var _foo = require("foo2");
|
||||
|
||||
var foo = _interopRequire(_foo);
|
||||
var _foo2 = _interopRequireWildcard(_foo);
|
||||
|
||||
var _import = require("foo3");
|
||||
|
||||
@ -32,4 +30,4 @@ var test = 5;
|
||||
exports.test = test;
|
||||
_bar.bar;
|
||||
_bar2.foo;
|
||||
foo;
|
||||
_foo2["default"];
|
||||
@ -39,4 +39,4 @@ System.register([], function (_export) {
|
||||
_export("f", _export("e", d = 4));
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
@ -1,7 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _foo2 = require("bar");
|
||||
|
||||
var _foo = babelHelpers.interopRequire(_foo2);
|
||||
var _foo3 = babelHelpers.interopRequireWildcard(_foo2);
|
||||
|
||||
exports.foo = _foo;
|
||||
exports.foo = _foo3["default"];
|
||||
@ -1,9 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
|
||||
var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { "default": obj }; };
|
||||
|
||||
var _toString = require("foo");
|
||||
|
||||
var toString = _interopRequire(_toString);
|
||||
var _toString2 = _interopRequireWildcard(_toString);
|
||||
|
||||
toString;
|
||||
_toString2["default"];
|
||||
@ -1,7 +1,11 @@
|
||||
module.exports = React.createClass({
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports["default"] = React.createClass({
|
||||
displayName: "actual",
|
||||
|
||||
render: function render() {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
module.exports = exports["default"];
|
||||
@ -4,8 +4,6 @@ var _core = require("babel-runtime/core-js")["default"];
|
||||
|
||||
var _regeneratorRuntime = require("babel-runtime/regenerator")["default"];
|
||||
|
||||
var _interopRequire = require("babel-runtime/helpers/interop-require")["default"];
|
||||
|
||||
var _interopRequireWildcard = require("babel-runtime/helpers/interop-require-wildcard")["default"];
|
||||
|
||||
_core.Object.defineProperty(exports, "__esModule", {
|
||||
@ -30,12 +28,12 @@ exports.giveWord = giveWord;
|
||||
|
||||
var _foo = require("someModule");
|
||||
|
||||
var foo = _interopRequire(_foo);
|
||||
var _foo2 = _interopRequireWildcard(_foo);
|
||||
|
||||
var bar = _interopRequireWildcard(_foo);
|
||||
|
||||
var myWord = _core.Symbol("abc");
|
||||
exports.myWord = myWord;
|
||||
|
||||
foo;
|
||||
bar;
|
||||
_foo2["default"];
|
||||
bar;
|
||||
@ -1,9 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequire = require("babel-runtime/helpers/interop-require")["default"];
|
||||
var _interopRequireWildcard = require("babel-runtime/helpers/interop-require-wildcard")["default"];
|
||||
|
||||
var _foo = require("foo");
|
||||
|
||||
var foo = _interopRequire(_foo);
|
||||
var _foo2 = _interopRequireWildcard(_foo);
|
||||
|
||||
foo;
|
||||
_foo2["default"];
|
||||
Loading…
x
Reference in New Issue
Block a user