2
2
3
3
const generate = require ( 'babel-generator' ) . default
4
4
const babelon = require ( 'babelon' )
5
+ const t = require ( 'babel-types' )
5
6
6
7
function getImportsMap ( metadata ) {
7
8
let { importsMap } = metadata
@@ -59,7 +60,6 @@ const configVisitor = {
59
60
if ( ! arg ) {
60
61
return
61
62
}
62
-
63
63
const v = arg . type === 'Identifier' ? importsMap [ arg . name ] : importsMap [ 'App' ]
64
64
metadata . rootComponent = v || importsMap [ 'index' ] || importsMap [ 'main' ]
65
65
}
@@ -68,6 +68,78 @@ function parseConfig (babel) {
68
68
return { visitor : configVisitor }
69
69
}
70
70
71
+ function createMPParser ( ) {
72
+ return {
73
+ visitor : {
74
+ ImportDeclaration : function ( path ) {
75
+ if ( path . node . specifiers && path . node . source && path . node . source . value === 'vue' ) {
76
+ const specifiersValue = path . node . specifiers [ 0 ] . local . name
77
+ path . node . specifiers = [
78
+ t . importSpecifier ( t . identifier ( specifiersValue ) , t . identifier ( specifiersValue ) ) ,
79
+ t . importSpecifier ( t . identifier ( 'createMP' ) , t . identifier ( 'createMP' ) )
80
+ ]
81
+ }
82
+ } ,
83
+ VariableDeclaration : function ( path ) {
84
+ if ( path . node . declarations &&
85
+ t . isVariableDeclarator ( path . node . declarations [ 0 ] ) &&
86
+ t . isNewExpression ( path . node . declarations [ 0 ] . init ) &&
87
+ t . isIdentifier ( path . node . declarations [ 0 ] . init . callee , { name : 'Vue' } )
88
+ ) {
89
+ const fnExpression = t . functionExpression (
90
+ null ,
91
+ [ ] ,
92
+ t . blockStatement (
93
+ [
94
+ t . returnStatement (
95
+ t . newExpression (
96
+ path . node . declarations [ 0 ] . init . callee ,
97
+ path . node . declarations [ 0 ] . init . arguments
98
+ )
99
+ )
100
+ ]
101
+ )
102
+ )
103
+ // 构造createMP的options
104
+ const objExpression = t . objectExpression (
105
+ [
106
+ t . objectProperty (
107
+ t . identifier ( 'mpType' ) ,
108
+ t . MemberExpression (
109
+ t . identifier ( path . node . declarations [ 0 ] . init . arguments [ 0 ] . name ) ,
110
+ t . identifier ( 'mpType' )
111
+ )
112
+ ) ,
113
+ t . objectProperty (
114
+ t . identifier ( 'init' ) ,
115
+ fnExpression
116
+ )
117
+ ]
118
+ )
119
+ path . replaceWith ( t . expressionStatement (
120
+ t . callExpression (
121
+ t . identifier ( 'createMP' ) ,
122
+ [
123
+ objExpression
124
+ ]
125
+ )
126
+ ) )
127
+ }
128
+ } ,
129
+ ExpressionStatement : function ( path ) {
130
+ if (
131
+ path . parentPath . node . type === 'Program' &&
132
+ path . node . expression . callee &&
133
+ path . node . expression . callee . property &&
134
+ path . node . expression . callee . property . name === '$mount'
135
+ ) {
136
+ path . remove ( )
137
+ }
138
+ }
139
+ }
140
+ }
141
+ }
142
+
71
143
// 解析 components
72
144
const traverseComponentsVisitor = {
73
145
Property : function ( path ) {
@@ -133,4 +205,4 @@ function parseGlobalComponents (babel) {
133
205
function clearGlobalComponents ( ) {
134
206
globalComponents = { }
135
207
}
136
- module . exports = { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents }
208
+ module . exports = { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents, createMPParser }
0 commit comments