From d6576490314b0369f68b43aaaf7b49d615108ff8 Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Tue, 19 Apr 2016 23:45:10 -0400 Subject: [PATCH] Use local add-module-exports --- package.json | 10 +++----- scripts/add-module-exports.js | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 scripts/add-module-exports.js diff --git a/package.json b/package.json index bb7a0ed4a2..4fd91f9fd2 100644 --- a/package.json +++ b/package.json @@ -46,12 +46,10 @@ "babel": { "presets": ["stage-0", "es2015-loose"], "plugins": [ - "transform-runtime", - "transform-class-properties", - "transform-flow-strip-types" - ], - "ignore": [ - "packages/babel-cli/src/babel-plugin/templates" + "./scripts/add-module-exports", + "transform-runtime", + "transform-class-properties", + "transform-flow-strip-types" ], "env": { "test": { diff --git a/scripts/add-module-exports.js b/scripts/add-module-exports.js new file mode 100644 index 0000000000..d0e89a17cf --- /dev/null +++ b/scripts/add-module-exports.js @@ -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; + } + } + } + } +}