Skip to content

Commit a1b82d1

Browse files
committed
fix: handle as named export, Object.defineProperty of exports other than __esModule
closes #59
1 parent c78dccb commit a1b82d1

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/index.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,20 @@ class ExportsFinder {
5656
.get('body')
5757
.forEach(path => {
5858
if (path.isVariableDeclaration()) {
59-
this.findExport(path.get('declarations.0'), 'init')
59+
this.findExports(path.get('declarations.0'), 'init')
6060
} else if (
6161
path.isExpressionStatement() &&
6262
path.get('expression').isAssignmentExpression()
6363
) {
64-
this.findExport(path)
64+
this.findExports(path)
65+
} else {
66+
this.findExportsInCallExpression(path)
6567
}
6668
})
6769
return this.hasExportsDefault && !this.hasExportsNamed && !this.hasModuleExports
6870
}
6971

70-
findExport(path, property = 'expression') {
72+
findExports(path, property = 'expression') {
7173
// Not `exports.anything`, skip
7274
if (!path.get(`${property}.left`).node || !path.get(`${property}.left.object`).node) {
7375
return
@@ -87,6 +89,24 @@ class ExportsFinder {
8789
}
8890
}
8991

92+
findExportsInCallExpression(path) {
93+
const self = this
94+
path.traverse({
95+
CallExpression(path) {
96+
if (!path.get('callee').matchesPattern('Object.defineProperty')) {
97+
return
98+
}
99+
100+
const [identifier, prop] = path.get('arguments')
101+
const objectName = identifier.get('name').node
102+
const propertyName = prop.get('value').node
103+
if (objectName === 'exports' && propertyName !== '__esModule') {
104+
self.hasExportsNamed = true
105+
}
106+
}
107+
})
108+
}
109+
90110
isAmd() {
91111
const rootPath = this.getRootPath()
92112
const hasntAmdRoot = !(rootPath.parentPath && rootPath.parentPath.parentPath)

0 commit comments

Comments
 (0)