@@ -12,14 +12,6 @@ function removeSourceMap(ast) {
12
12
} ) ;
13
13
}
14
14
15
- function getRenderFunctionExpression ( ast ) {
16
- return ast . program . body [ 0 ] . declarations [ 0 ] . init ;
17
- }
18
-
19
- function getStaticRenderFunctionExpressions ( ast ) {
20
- return ast . program . body [ 1 ] . declarations [ 0 ] . init ;
21
- }
22
-
23
15
const {
24
16
ArrayExpression,
25
17
ObjectProperty,
@@ -29,10 +21,21 @@ const {
29
21
ExpressionStatement,
30
22
AssignmentExpression,
31
23
MemberExpression,
32
- ExportDefaultDeclaration
24
+ ExportDefaultDeclaration,
25
+ LogicalExpression,
26
+ FunctionDeclaration
33
27
} = babel . types ;
34
28
35
- const getPlugin = ( renderFunctionExpr , staticRenderArrayExpr , isFunctional ) => {
29
+ function getRenderFunctionDeclaration ( ast ) {
30
+ const expr = ast . program . body [ 0 ] . declarations [ 0 ] . init ;
31
+ return FunctionDeclaration ( Identifier ( 'render' ) , expr . params , expr . body ) ;
32
+ }
33
+
34
+ function getStaticRenderFunctionExpressions ( ast ) {
35
+ return ast . program . body [ 1 ] . declarations [ 0 ] . init ;
36
+ }
37
+
38
+ const getPlugin = ( renderFunctionDeclr , staticRenderArrayExpr , isFunctional ) => {
36
39
return function AddFunctionPlugin ( ) {
37
40
return {
38
41
visitor : {
@@ -47,13 +50,17 @@ const getPlugin = (renderFunctionExpr, staticRenderArrayExpr, isFunctional) => {
47
50
ExportDefaultDeclaration ( Identifier ( '__export__' ) )
48
51
] ;
49
52
50
- if ( renderFunctionExpr ) {
53
+ if ( renderFunctionDeclr ) {
51
54
statements . splice ( 1 , 0 , ExpressionStatement (
52
55
AssignmentExpression ( '=' ,
53
56
MemberExpression ( Identifier ( '__export__' ) , Identifier ( 'render' ) ) ,
54
- renderFunctionExpr
57
+ LogicalExpression ( '||' ,
58
+ MemberExpression ( Identifier ( '__export__' ) , Identifier ( 'render' ) ) ,
59
+ Identifier ( 'render' )
60
+ )
55
61
)
56
62
) ) ;
63
+ statements . splice ( 1 , 0 , renderFunctionDeclr ) ;
57
64
}
58
65
59
66
if ( staticRenderArrayExpr ) {
@@ -102,16 +109,16 @@ module.exports = function (vueSource, vueFilename, extraPlugins) {
102
109
compilerOptions : { outputSourceRange : true }
103
110
} ) : { } ;
104
111
105
- let renderFunctionExpr , staticRenderArrayExpr ;
112
+ let renderFunctionDeclr , staticRenderArrayExpr ;
106
113
107
114
if ( code ) {
108
115
const ast = babel . parse ( code ) ;
109
116
removeSourceMap ( ast ) ;
110
- renderFunctionExpr = getRenderFunctionExpression ( ast ) ;
117
+ renderFunctionDeclr = getRenderFunctionDeclaration ( ast ) ;
111
118
staticRenderArrayExpr = getStaticRenderFunctionExpressions ( ast ) ;
112
119
}
113
120
114
- plugins . unshift ( getPlugin ( renderFunctionExpr , staticRenderArrayExpr , template && template . attrs . functional ) ) ;
121
+ plugins . unshift ( getPlugin ( renderFunctionDeclr , staticRenderArrayExpr , template && template . attrs . functional ) ) ;
115
122
116
123
const ast = babel . transformSync ( script ? script . content : 'export default {};' , { plugins, ast : true } ) . ast ;
117
124
0 commit comments