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,87 @@ function parseConfig (babel) {
68
68
return { visitor : configVisitor }
69
69
}
70
70
71
+ function createMPParser ( ) {
72
+ return {
73
+ visitor : {
74
+ // 将import Vue from 'vue' 替换为 import {Vue, createMP} from 'vue'
75
+ ImportDeclaration : function ( path ) {
76
+ if ( path . node . specifiers && path . node . source && path . node . source . value === 'vue' ) {
77
+ const specifiersValue = path . node . specifiers [ 0 ] . local . name
78
+ path . node . specifiers = [
79
+ t . importSpecifier ( t . identifier ( specifiersValue ) , t . identifier ( specifiersValue ) ) ,
80
+ t . importSpecifier ( t . identifier ( 'createMP' ) , t . identifier ( 'createMP' ) )
81
+ ]
82
+ }
83
+ } ,
84
+ // 处理const app = new Vue(App)
85
+ // 构造为 createMp({
86
+ // mpType: App.mpType,
87
+ // init(){
88
+ // return new Vue(App)
89
+ // }
90
+ // })
91
+ VariableDeclaration : function ( path ) {
92
+ if ( path . node . declarations &&
93
+ t . isVariableDeclarator ( path . node . declarations [ 0 ] ) &&
94
+ t . isNewExpression ( path . node . declarations [ 0 ] . init ) &&
95
+ t . isIdentifier ( path . node . declarations [ 0 ] . init . callee , { name : 'Vue' } )
96
+ ) {
97
+ const fnExpression = t . functionExpression (
98
+ null ,
99
+ [ ] ,
100
+ t . blockStatement (
101
+ [
102
+ t . returnStatement (
103
+ t . newExpression (
104
+ path . node . declarations [ 0 ] . init . callee ,
105
+ path . node . declarations [ 0 ] . init . arguments
106
+ )
107
+ )
108
+ ]
109
+ )
110
+ )
111
+ // 构造createMP的options
112
+ const objExpression = t . objectExpression (
113
+ [
114
+ t . objectProperty (
115
+ t . identifier ( 'mpType' ) ,
116
+ t . MemberExpression (
117
+ t . identifier ( path . node . declarations [ 0 ] . init . arguments [ 0 ] . name ) ,
118
+ t . identifier ( 'mpType' )
119
+ )
120
+ ) ,
121
+ t . objectProperty (
122
+ t . identifier ( 'init' ) ,
123
+ fnExpression
124
+ )
125
+ ]
126
+ )
127
+ path . replaceWith ( t . expressionStatement (
128
+ t . callExpression (
129
+ t . identifier ( 'createMP' ) ,
130
+ [
131
+ objExpression
132
+ ]
133
+ )
134
+ ) )
135
+ }
136
+ } ,
137
+ // main.js中app.$mount()移除掉
138
+ ExpressionStatement : function ( path ) {
139
+ if (
140
+ path . parentPath . node . type === 'Program' &&
141
+ path . node . expression . callee &&
142
+ path . node . expression . callee . property &&
143
+ path . node . expression . callee . property . name === '$mount'
144
+ ) {
145
+ path . remove ( )
146
+ }
147
+ }
148
+ }
149
+ }
150
+ }
151
+
71
152
// 解析 components
72
153
const traverseComponentsVisitor = {
73
154
Property : function ( path ) {
@@ -133,4 +214,4 @@ function parseGlobalComponents (babel) {
133
214
function clearGlobalComponents ( ) {
134
215
globalComponents = { }
135
216
}
136
- module . exports = { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents }
217
+ module . exports = { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents, createMPParser }
0 commit comments