Skip to content

Commit

Permalink
perf: use babel/scripts/add-module-exports as reference implementation(
Browse files Browse the repository at this point in the history
…#34)

- use `babel-types` instead of `babel-template`
- use `path.node.specifiers` instead of `path.get('declaration').container.specifiers`

refs: https://github.com/babel/babel/blob/v6.8.1/scripts/add-module-exports.js
  • Loading branch information
59naga committed May 12, 2016
1 parent cf8cedd commit 77cc52a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = ({template}) => ({
module.exports = ({types}) => ({
visitor: {
Program: {
exit (path) {
Expand All @@ -13,11 +13,9 @@ module.exports = ({template}) => ({
hasExportDefault = true
return
}

if (path.isExportNamedDeclaration()) {
// HACK detect export-from statements for default
const specifiers = path.get('declaration').container.specifiers
const isDefaultExportDeclaration = specifiers.length === 1 && specifiers[0].exported.name === 'default'
if (isDefaultExportDeclaration) {
if (path.node.specifiers.length === 1 && path.node.specifiers[0].exported.name === 'default') {
hasExportDefault = true
} else {
hasExportNamed = true
Expand All @@ -27,10 +25,13 @@ module.exports = ({template}) => ({
})

if (hasExportDefault && !hasExportNamed) {
const topNodes = []
topNodes.push(template("module.exports = exports['default']")())

path.pushContainer('body', topNodes)
path.pushContainer('body', [
types.expressionStatement(types.assignmentExpression(
'=',
types.memberExpression(types.identifier('module'), types.identifier('exports')),
types.memberExpression(types.identifier('exports'), types.stringLiteral('default'), true)
))
])
}

path.BABEL_PLUGIN_ADD_MODULE_EXPORTS = true
Expand Down

0 comments on commit 77cc52a

Please sign in to comment.