Skip to content

Commit 09566d5

Browse files
committed
Merge branch 'develop' of github.com:Meituan-Dianping/mpvue into develop
2 parents 89045d7 + ddf6541 commit 09566d5

File tree

9 files changed

+353
-58
lines changed

9 files changed

+353
-58
lines changed

packages/mpvue-template-compiler/build.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,11 @@ var LIFECYCLE_HOOKS = [
11091109
'onReachBottom',
11101110
'onShareAppMessage',
11111111
'onPageScroll',
1112-
'onTabItemTap'
1112+
'onTabItemTap',
1113+
'attached',
1114+
'ready',
1115+
'moved',
1116+
'detached'
11131117
];
11141118

11151119
/* */
@@ -4820,7 +4824,7 @@ function generate$2 (obj, options) {
48204824
var attrs = Object.keys(attrsMap).map(function (k) { return convertAttr(k, attrsMap[k]); }).join(' ');
48214825

48224826
var tags = ['progress', 'checkbox', 'switch', 'input', 'radio', 'slider', 'textarea'];
4823-
if (tags.indexOf(tag) > -1) {
4827+
if (tags.indexOf(tag) > -1 && !(children && children.length)) {
48244828
return ("<" + tag + (attrs ? ' ' + attrs : '') + " />" + (ifConditionsArr.join('')))
48254829
}
48264830
return ("<" + tag + (attrs ? ' ' + attrs : '') + ">" + (child || '') + "</" + tag + ">" + (ifConditionsArr.join('')))

packages/mpvue-template-compiler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mpvue-template-compiler",
3-
"version": "1.0.12",
3+
"version": "1.0.13",
44
"description": "mpvue template compiler for Vue",
55
"main": "index.js",
66
"repository": {

packages/mpvue/index.js

Lines changed: 104 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,11 @@ var LIFECYCLE_HOOKS = [
322322
'onReachBottom',
323323
'onShareAppMessage',
324324
'onPageScroll',
325-
'onTabItemTap'
325+
'onTabItemTap',
326+
'attached',
327+
'ready',
328+
'moved',
329+
'detached'
326330
];
327331

328332
/* */
@@ -4144,7 +4148,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
41444148
});
41454149

41464150
Vue$3.version = '2.4.1';
4147-
Vue$3.mpvueVersion = '1.0.11';
4151+
Vue$3.mpvueVersion = '1.0.12';
41484152

41494153
/* globals renderer */
41504154

@@ -4974,30 +4978,106 @@ function getGlobalData (app, rootVueVM) {
49744978
}
49754979
}
49764980

4977-
/**
4978-
* 格式化 properties 属性,并给每个属性加上 observer 方法
4979-
*/
4981+
// 格式化 properties 属性,并给每个属性加上 observer 方法
4982+
4983+
// properties 的 一些类型 https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/component.html
4984+
// properties: {
4985+
// paramA: Number,
4986+
// myProperty: { // 属性名
4987+
// type: String, // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
4988+
// value: '', // 属性初始值(可选),如果未指定则会根据类型选择一个
4989+
// observer: function(newVal, oldVal, changedPath) {
4990+
// // 属性被改变时执行的函数(可选),也可以写成在methods段中定义的方法名字符串, 如:'_propertyChange'
4991+
// // 通常 newVal 就是新设置的数据, oldVal 是旧数据
4992+
// }
4993+
// },
4994+
// }
4995+
4996+
// props 的一些类型 https://cn.vuejs.org/v2/guide/components-props.html#ad
4997+
// props: {
4998+
// // 基础的类型检查 (`null` 匹配任何类型)
4999+
// propA: Number,
5000+
// // 多个可能的类型
5001+
// propB: [String, Number],
5002+
// // 必填的字符串
5003+
// propC: {
5004+
// type: String,
5005+
// required: true
5006+
// },
5007+
// // 带有默认值的数字
5008+
// propD: {
5009+
// type: Number,
5010+
// default: 100
5011+
// },
5012+
// // 带有默认值的对象
5013+
// propE: {
5014+
// type: Object,
5015+
// // 对象或数组且一定会从一个工厂函数返回默认值
5016+
// default: function () {
5017+
// return { message: 'hello' }
5018+
// }
5019+
// },
5020+
// // 自定义验证函数
5021+
// propF: {
5022+
// validator: function (value) {
5023+
// // 这个值必须匹配下列字符串中的一个
5024+
// return ['success', 'warning', 'danger'].indexOf(value) !== -1
5025+
// }
5026+
// }
5027+
// }
5028+
5029+
// core/util/options
5030+
function normalizeProps$1 (props, res, vm) {
5031+
if (!props) { return }
5032+
var i, val, name;
5033+
if (Array.isArray(props)) {
5034+
i = props.length;
5035+
while (i--) {
5036+
val = props[i];
5037+
if (typeof val === 'string') {
5038+
name = camelize(val);
5039+
res[name] = { type: null };
5040+
} else {}
5041+
}
5042+
} else if (isPlainObject(props)) {
5043+
for (var key in props) {
5044+
val = props[key];
5045+
name = camelize(key);
5046+
res[name] = isPlainObject(val)
5047+
? val
5048+
: { type: val };
5049+
}
5050+
}
5051+
5052+
// fix vueProps to properties
5053+
for (var key$1 in res) {
5054+
if (res.hasOwnProperty(key$1)) {
5055+
var item = res[key$1];
5056+
if (item.default) {
5057+
item.value = item.default;
5058+
}
5059+
var oldObserver = item.observer;
5060+
item.observer = function (newVal, oldVal) {
5061+
vm[name] = newVal;
5062+
// 先修改值再触发原始的 observer,跟 watch 行为保持一致
5063+
if (typeof oldObserver === 'function') {
5064+
oldObserver.call(vm, newVal, oldVal);
5065+
}
5066+
};
5067+
}
5068+
}
5069+
5070+
return res
5071+
}
5072+
49805073
function normalizeProperties (vm) {
4981-
var properties = vm.$options.properties || {};
5074+
var properties = vm.$options.properties;
5075+
var vueProps = vm.$options.props;
49825076
var res = {};
4983-
var val;
4984-
var loop = function ( key ) {
4985-
val = isPlainObject(properties[key])
4986-
? properties[key]
4987-
: { type: properties[key] };
4988-
res[key] = {
4989-
type: val.type,
4990-
value: val.value,
4991-
observer: function observer (newVal, oldVal) {
4992-
vm[key] = newVal; // 先修改值再触发原始的 observer,跟 watch 行为保持一致
4993-
if (typeof val.observer === 'function') {
4994-
val.observer.call(vm, newVal, oldVal);
4995-
}
4996-
}
4997-
};
4998-
};
49995077

5000-
for (var key in properties) loop( key );
5078+
normalizeProps$1(properties, res, vm);
5079+
normalizeProps$1(vueProps, res, vm);
5080+
50015081
return res
50025082
}
50035083

@@ -5112,7 +5192,7 @@ function initMP (mpType, next) {
51125192
ready: function ready () {
51135193
mp.status = 'ready';
51145194

5115-
callHook$1(rootVueVM, 'onReady');
5195+
callHook$1(rootVueVM, 'ready');
51165196
next();
51175197

51185198
// 只有页面需要 setData
@@ -5491,13 +5571,6 @@ function handleProxyWithVue (e) {
54915571
return result
54925572
}
54935573
handles.forEach(function (h) { return h(event); });
5494-
} else {
5495-
// TODO, 在初始化时进行判断或直接使用 vue 本身的错误提示
5496-
var ref$1 = rootVueVM.$mp.page;
5497-
var route = ref$1.route;
5498-
console.group(new Date() + ' 事件警告');
5499-
console.warn(("Do not have handler in current page: " + route + ". Please make sure that handler has been defined in " + route + ", or not use handler with 'v-if'"));
5500-
console.groupEnd();
55015574
}
55025575
}
55035576

packages/mpvue/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mpvue",
3-
"version": "1.0.12",
3+
"version": "1.0.13",
44
"description": "Vue Runtime for mini program",
55
"main": "index.js",
66
"repository": {

src/platforms/mp/join-code-in-build.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ exports.mpLifecycleHooks = `'onLaunch',
2323
'onReachBottom',
2424
'onShareAppMessage',
2525
'onPageScroll',
26-
'onTabItemTap'`
26+
'onTabItemTap',
27+
'attached',
28+
'ready',
29+
'moved',
30+
'detached'`

src/platforms/mp/runtime/lifecycle.js

Lines changed: 105 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { isPlainObject } from 'shared/util'
21
import { handleError } from 'core/util/index'
32
import { observe } from 'core/observer/index'
43
import { proxy } from 'core/instance/state'
54

5+
import {
6+
camelize,
7+
isPlainObject
8+
} from 'shared/util'
9+
import { warn } from 'core/util/debug'
10+
611
export function callHook (vm, hook, params) {
712
let handlers = vm.$options[hook]
813
if (hook === 'onError' && handlers) {
@@ -40,28 +45,108 @@ function getGlobalData (app, rootVueVM) {
4045
}
4146
}
4247

43-
/**
44-
* 格式化 properties 属性,并给每个属性加上 observer 方法
45-
*/
46-
function normalizeProperties (vm) {
47-
const properties = vm.$options.properties || {}
48-
const res = {}
49-
let val
50-
for (const key in properties) {
51-
val = isPlainObject(properties[key])
52-
? properties[key]
53-
: { type: properties[key] }
54-
res[key] = {
55-
type: val.type,
56-
value: val.value,
57-
observer (newVal, oldVal) {
58-
vm[key] = newVal // 先修改值再触发原始的 observer,跟 watch 行为保持一致
59-
if (typeof val.observer === 'function') {
60-
val.observer.call(vm, newVal, oldVal)
48+
// 格式化 properties 属性,并给每个属性加上 observer 方法
49+
50+
// properties 的 一些类型 https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/component.html
51+
// properties: {
52+
// paramA: Number,
53+
// myProperty: { // 属性名
54+
// type: String, // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
55+
// value: '', // 属性初始值(可选),如果未指定则会根据类型选择一个
56+
// observer: function(newVal, oldVal, changedPath) {
57+
// // 属性被改变时执行的函数(可选),也可以写成在methods段中定义的方法名字符串, 如:'_propertyChange'
58+
// // 通常 newVal 就是新设置的数据, oldVal 是旧数据
59+
// }
60+
// },
61+
// }
62+
63+
// props 的一些类型 https://cn.vuejs.org/v2/guide/components-props.html#ad
64+
// props: {
65+
// // 基础的类型检查 (`null` 匹配任何类型)
66+
// propA: Number,
67+
// // 多个可能的类型
68+
// propB: [String, Number],
69+
// // 必填的字符串
70+
// propC: {
71+
// type: String,
72+
// required: true
73+
// },
74+
// // 带有默认值的数字
75+
// propD: {
76+
// type: Number,
77+
// default: 100
78+
// },
79+
// // 带有默认值的对象
80+
// propE: {
81+
// type: Object,
82+
// // 对象或数组且一定会从一个工厂函数返回默认值
83+
// default: function () {
84+
// return { message: 'hello' }
85+
// }
86+
// },
87+
// // 自定义验证函数
88+
// propF: {
89+
// validator: function (value) {
90+
// // 这个值必须匹配下列字符串中的一个
91+
// return ['success', 'warning', 'danger'].indexOf(value) !== -1
92+
// }
93+
// }
94+
// }
95+
96+
// core/util/options
97+
function normalizeProps (props, res, vm) {
98+
if (!props) return
99+
let i, val, name
100+
if (Array.isArray(props)) {
101+
i = props.length
102+
while (i--) {
103+
val = props[i]
104+
if (typeof val === 'string') {
105+
name = camelize(val)
106+
res[name] = { type: null }
107+
} else if (process.env.NODE_ENV !== 'production') {
108+
warn('props must be strings when using array syntax.')
109+
}
110+
}
111+
} else if (isPlainObject(props)) {
112+
for (const key in props) {
113+
val = props[key]
114+
name = camelize(key)
115+
res[name] = isPlainObject(val)
116+
? val
117+
: { type: val }
118+
}
119+
}
120+
121+
// fix vueProps to properties
122+
for (const key in res) {
123+
if (res.hasOwnProperty(key)) {
124+
const item = res[key]
125+
if (item.default) {
126+
item.value = item.default
127+
}
128+
const oldObserver = item.observer
129+
item.observer = function (newVal, oldVal) {
130+
vm[name] = newVal
131+
// 先修改值再触发原始的 observer,跟 watch 行为保持一致
132+
if (typeof oldObserver === 'function') {
133+
oldObserver.call(vm, newVal, oldVal)
61134
}
62135
}
63136
}
64137
}
138+
139+
return res
140+
}
141+
142+
function normalizeProperties (vm) {
143+
const properties = vm.$options.properties
144+
const vueProps = vm.$options.props
145+
const res = {}
146+
147+
normalizeProps(properties, res, vm)
148+
normalizeProps(vueProps, res, vm)
149+
65150
return res
66151
}
67152

@@ -172,7 +257,7 @@ export function initMP (mpType, next) {
172257
ready () {
173258
mp.status = 'ready'
174259

175-
callHook(rootVueVM, 'onReady')
260+
callHook(rootVueVM, 'ready')
176261
next()
177262

178263
// 只有页面需要 setData

test/mp/helpers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ function strToRegExp (str) {
55

66
// runtime
77
// fix mp env
8-
const { App, Page, getApp } = require('./mp.runtime')
8+
const { App, Page, getApp, Component } = require('./mp.runtime')
99
global.App = App
1010
global.Page = Page
1111
global.getApp = getApp
12+
global.Component = Component
1213

1314
const Vue = require('../../../packages/mpvue')
1415

0 commit comments

Comments
 (0)