Use local add-module-exports

This commit is contained in:
Henry Zhu 2016-04-19 23:45:10 -04:00
parent ecd294b55e
commit d657649031
2 changed files with 51 additions and 6 deletions

View File

@ -46,13 +46,11 @@
"babel": { "babel": {
"presets": ["stage-0", "es2015-loose"], "presets": ["stage-0", "es2015-loose"],
"plugins": [ "plugins": [
"./scripts/add-module-exports",
"transform-runtime", "transform-runtime",
"transform-class-properties", "transform-class-properties",
"transform-flow-strip-types" "transform-flow-strip-types"
], ],
"ignore": [
"packages/babel-cli/src/babel-plugin/templates"
],
"env": { "env": {
"test": { "test": {
"auxiliaryCommentBefore": "istanbul ignore next" "auxiliaryCommentBefore": "istanbul ignore next"

View File

@ -0,0 +1,47 @@
// "add-module-exports"
module.exports = function (babel) {
var t = babel.types;
return {
visitor: {
Program: {
exit: function(path) {
if (path.BABEL_PLUGIN_ADD_MODULE_EXPORTS) {
return;
}
var hasExportDefault = false;
var hasExportNamed = false;
var body = path.get("body");
path.get('body').forEach(function (path) {
if (path.isExportDefaultDeclaration()) {
hasExportDefault = true;
return;
}
if (path.isExportNamedDeclaration()) {
if (path.node.specifiers.length === 1 && path.node.specifiers[0].exported.name === "default") {
hasExportDefault = true;
} else {
hasExportNamed = true;
}
return;
}
});
if (hasExportDefault && !hasExportNamed) {
path.pushContainer("body", [
t.expressionStatement(t.assignmentExpression(
"=",
t.memberExpression(t.identifier("module"), t.identifier("exports")),
t.memberExpression(t.identifier("exports"), t.stringLiteral("default"), true)
))
]);
}
path.BABEL_PLUGIN_ADD_MODULE_EXPORTS = true;
}
}
}
}
}