Skip to content

Commit 6e14208

Browse files
author
xutao15
committed
replace mount in mainJS with createMP
1 parent ab988c4 commit 6e14208

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

lib/mp-compiler/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const fs = require('fs')
44
const deepEqual = require('deep-equal')
55
const compiler = require('mpvue-template-compiler')
66

7-
const { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents } = require('./parse')
7+
const { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents, createMPParser } = require('./parse')
88
const { parseComponentsDeps: parseComponentsDepsTs } = require('./parse-ts')
99
const { genPageML } = require('./templates')
1010

@@ -151,11 +151,15 @@ function compileMP (content, mpOptioins) {
151151

152152
const babelrc = getBabelrc(mpOptioins.globalBabelrc)
153153
// app入口进行全局component解析
154-
const { metadata } = babel.transform(content, { extends: babelrc, plugins: isApp ? [parseConfig, parseGlobalComponents] : [parseConfig] })
155-
154+
const { metadata, code } = babel.transform(content, { extends: babelrc, plugins: isApp ? [parseConfig, createMPParser, parseGlobalComponents] : [parseConfig, createMPParser] })
155+
if (code) {
156+
content = code
157+
}
158+
if (!metadata.globalComponents) {
159+
metadata.globalComponents = {}
160+
}
156161
// metadata: config
157162
const { rootComponent, globalComponents: globalComps } = metadata
158-
159163
if (isApp) {
160164
// 保存旧数据,用于对比
161165
const oldGlobalComponents = globalComponents

lib/mp-compiler/parse.js

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const generate = require('babel-generator').default
44
const babelon = require('babelon')
5+
const t = require('babel-types')
56

67
function getImportsMap (metadata) {
78
let { importsMap } = metadata
@@ -59,7 +60,6 @@ const configVisitor = {
5960
if (!arg) {
6061
return
6162
}
62-
6363
const v = arg.type === 'Identifier' ? importsMap[arg.name] : importsMap['App']
6464
metadata.rootComponent = v || importsMap['index'] || importsMap['main']
6565
}
@@ -68,6 +68,78 @@ function parseConfig (babel) {
6868
return { visitor: configVisitor }
6969
}
7070

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+
71143
// 解析 components
72144
const traverseComponentsVisitor = {
73145
Property: function (path) {
@@ -133,4 +205,4 @@ function parseGlobalComponents (babel) {
133205
function clearGlobalComponents () {
134206
globalComponents = {}
135207
}
136-
module.exports = { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents }
208+
module.exports = { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents, createMPParser }

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"babel-core": "^6.25.0",
7777
"babel-loader": "^7.0.0",
7878
"babel-preset-env": "^1.6.0",
79+
"babel-types": "^6.26.0",
7980
"chai": "^4.1.0",
8081
"coffee-loader": "^0.7.2",
8182
"coffee-script": "^1.12.6",

0 commit comments

Comments
 (0)