Skip to content

Commit d547c07

Browse files
authored
Merge pull request #57 from mpvue/xt/dev
mpvue-loader 3.0
2 parents ab988c4 + 8a51db4 commit d547c07

File tree

3 files changed

+92
-6
lines changed

3 files changed

+92
-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: 83 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,87 @@ function parseConfig (babel) {
6868
return { visitor: configVisitor }
6969
}
7070

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+
71152
// 解析 components
72153
const traverseComponentsVisitor = {
73154
Property: function (path) {
@@ -133,4 +214,4 @@ function parseGlobalComponents (babel) {
133214
function clearGlobalComponents () {
134215
globalComponents = {}
135216
}
136-
module.exports = { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents }
217+
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)