@@ -6,6 +6,10 @@ import resolveExportDeclaration from './resolveExportDeclaration'
66// tslint:disable-next-line:no-var-requires
77import recast = require( 'recast' )
88
9+ function ignore ( ) : boolean {
10+ return false
11+ }
12+
913function isComponentDefinition ( path : NodePath ) : boolean {
1014 return (
1115 // export default {}
@@ -54,8 +58,9 @@ export default function resolveExportedComponent(ast: bt.File): NodePath[] {
5458 // in extenso export default or export myvar
5559 function exportDeclaration ( path : NodePath ) {
5660 const definitions = resolveExportDeclaration ( path ) . reduce ( ( acc : NodePath [ ] , definition ) => {
57- if ( isComponentDefinition ( definition ) ) {
58- acc . push ( definition )
61+ const realDef = resolveIdentifier ( ast , definition )
62+ if ( realDef && isComponentDefinition ( realDef ) ) {
63+ acc . push ( realDef )
5964 }
6065 return acc
6166 } , [ ] )
@@ -67,6 +72,20 @@ export default function resolveExportedComponent(ast: bt.File): NodePath[] {
6772 }
6873
6974 recast . visit ( ast . program , {
75+ // to look only at the root we ignore all traversing
76+ visitFunctionDeclaration : ignore ,
77+ visitFunctionExpression : ignore ,
78+ visitClassDeclaration : ignore ,
79+ visitClassExpression : ignore ,
80+ visitIfStatement : ignore ,
81+ visitWithStatement : ignore ,
82+ visitSwitchStatement : ignore ,
83+ visitCatchCause : ignore ,
84+ visitWhileStatement : ignore ,
85+ visitDoWhileStatement : ignore ,
86+ visitForStatement : ignore ,
87+ visitForInStatement : ignore ,
88+
7089 visitDeclareExportDeclaration : exportDeclaration ,
7190 visitExportNamedDeclaration : exportDeclaration ,
7291 visitExportDefaultDeclaration : exportDeclaration ,
@@ -83,11 +102,12 @@ export default function resolveExportedComponent(ast: bt.File): NodePath[] {
83102 // Resolve the value of the right hand side. It should resolve to a call
84103 // expression, something like Vue.extend({})
85104 const pathRight = path . get ( 'right' )
86- if ( ! isComponentDefinition ( pathRight ) ) {
105+ const realComp = resolveIdentifier ( ast , pathRight )
106+ if ( ! realComp || ! isComponentDefinition ( realComp ) ) {
87107 return false
88108 }
89109
90- setComponent ( pathRight )
110+ setComponent ( realComp )
91111 return false
92112 } ,
93113 } )
@@ -105,3 +125,48 @@ function normalizeComponentPath(path: NodePath): NodePath {
105125 }
106126 return path
107127}
128+
129+ function resolveIdentifier ( ast : bt . File , path : NodePath ) : NodePath | null {
130+ if ( ! bt . isIdentifier ( path . node ) ) {
131+ return path
132+ }
133+
134+ const varName = path . node . name
135+ let comp : NodePath | null = null
136+
137+ recast . visit ( ast . program , {
138+ // to look only at the root we ignore all traversing
139+ visitFunctionDeclaration : ignore ,
140+ visitFunctionExpression : ignore ,
141+ visitClassExpression : ignore ,
142+ visitIfStatement : ignore ,
143+ visitWithStatement : ignore ,
144+ visitSwitchStatement : ignore ,
145+ visitCatchCause : ignore ,
146+ visitWhileStatement : ignore ,
147+ visitDoWhileStatement : ignore ,
148+ visitForStatement : ignore ,
149+ visitForInStatement : ignore ,
150+
151+ visitVariableDeclaration ( variablePath : NodePath < bt . VariableDeclaration > ) {
152+ const varID = variablePath . node . declarations [ 0 ] . id
153+ if ( ! varID || ! bt . isIdentifier ( varID ) || varID . name !== varName ) {
154+ return false
155+ }
156+
157+ comp = variablePath . get ( 'declarations' , 0 ) . get ( 'init' )
158+ return false
159+ } ,
160+
161+ visitClassDeclaration ( classPath : NodePath < bt . ClassDeclaration > ) {
162+ const classID = classPath . node . id
163+ if ( ! classID || ! bt . isIdentifier ( classID ) || classID . name !== varName ) {
164+ return false
165+ }
166+
167+ comp = classPath
168+ return false
169+ } ,
170+ } )
171+ return comp
172+ }
0 commit comments