Skip to content

Commit ce18b3e

Browse files
replace module.parent (#17)
1 parent d104929 commit ce18b3e

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ function rewriteModule (row, i, rows) {
164164
// rewrite `typeof module` to `"object"`
165165
if (node.parent.type === 'UnaryExpression' && node.parent.operator === 'typeof') {
166166
node.parent.edit.update('"object"')
167+
} else if (isModuleParent(node.parent)) {
168+
if (row.entry) {
169+
node.parent.edit.update('null')
170+
} else {
171+
node.parent.edit.update('({})')
172+
}
167173
} else {
168174
renameIdentifier(node, moduleBaseName)
169175
}
@@ -399,6 +405,12 @@ function isModuleExports (node) {
399405
(node.property.type === 'Identifier' && node.property.name === 'exports' ||
400406
node.property.type === 'Literal' && node.property.value === 'exports')
401407
}
408+
function isModuleParent (node) {
409+
return node.type === 'MemberExpression' &&
410+
node.object.type === 'Identifier' && node.object.name === 'module' &&
411+
(node.property.type === 'Identifier' && node.property.name === 'parent' ||
412+
node.property.type === 'Literal' && node.property.value === 'parent')
413+
}
402414
function isRequire (node) {
403415
return node.type === 'CallExpression' &&
404416
node.callee.type === 'Identifier' && node.callee.name === 'require'

test/module-parent/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require('./child')
2+
3+
if (module.parent) {
4+
throw Error('should be entry point')
5+
}

test/module-parent/child.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if (module.parent) {
2+
module.exports = 'required'
3+
} else {
4+
module.exports = 'entry point'
5+
}

test/module-parent/expected.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(function(){
2+
var _$child_2 = { exports: {} };
3+
if (({})) {
4+
_$child_2.exports = 'required'
5+
} else {
6+
_$child_2.exports = 'entry point'
7+
}
8+
9+
_$child_2 = _$child_2.exports
10+
var _$app_1 = { exports: {} };
11+
_$child_2
12+
13+
if (null) {
14+
throw Error('should be entry point')
15+
}
16+
17+
_$app_1 = _$app_1.exports
18+
}());

0 commit comments

Comments
 (0)