Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/didi/mpx
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyuki committed Jun 10, 2019
2 parents 0638585 + 651451b commit 2189ac7
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 17 deletions.
19 changes: 18 additions & 1 deletion packages/core/src/convertor/wxToAli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@ import { mergeLifecycle } from './mergeLifecycle'

const NOTSUPPORTS = ['moved', 'pageLifetimes', 'definitionFilter']

function convertErrorDesc (key) {
console.error(`【MPX CONVERT ERROR】at ${global.currentResource || ''} : Don't support for convert the option【${key}】 of the wx-component into the ali-component`)
}

function notSupportTip (options) {
NOTSUPPORTS.forEach(key => {
if (options[key]) {
console.error(`【MPX CONVERT ERROR】at ${global.currentResource || ''}: Don't support for convert the option【${key}】 of the wx-component into the ali-component`)
convertErrorDesc(key)
delete options[key]
}
})
// relations部分支持
const relations = options['relations']
if (relations) {
Object.keys(relations).forEach(path => {
const item = relations[path]
if (item.target) {
convertErrorDesc('relations > target')
}
if (item.linkChanged) {
convertErrorDesc('relations > linkChanged')
}
})
}
}

export default {
Expand Down
21 changes: 17 additions & 4 deletions packages/core/src/core/mergeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ export default function mergeOptions (options = {}, type, needConvert = true) {
}

function extractMixins (mergeOptions, options, needConvert) {
aliasReplace(options, 'behaviors', 'mixins')
// 如果编译阶段behaviors都被当做mixins处理,那么进行别名替换
if (options.behaviors && type(options.behaviors[0]) === 'Object') {
aliasReplace(options, 'behaviors', 'mixins')
}
if (options.mixins) {
for (const mix of options.mixins) {
if (typeof mix === 'string') {
console.error(`Don't support for convert the string-formatted【behavior】into mixin`)
console.error(`【MPX CONVERT ERROR】at ${global.currentResource || ''} : Don't support for convert the string-formatted【behavior】into mixin`)
} else {
extractMixins(mergeOptions, mix, needConvert)
}
Expand Down Expand Up @@ -157,10 +160,12 @@ function mergeMixins (parent, child) {
mergeHooks(parent, child, key)
} else if (key === 'data') {
mergeDataFn(parent, child, key)
} else if (/computed|properties|props|methods|proto/.test(key)) {
} else if (/^(computed|properties|props|methods|proto)$/.test(key)) {
mergeSimpleProps(parent, child, key)
} else if (/watch|pageLifetimes|observers/.test(key)) {
} else if (/^(watch|pageLifetimes|observers)$/.test(key)) {
mergeToArray(parent, child, key)
} else if (/^behaviors$/.test(key)) {
mergeArray(parent, child, key)
} else if (key !== 'mixins') {
mergeDefault(parent, child, key)
}
Expand Down Expand Up @@ -218,6 +223,14 @@ function mergeData (parent, child, key) {
merge(parent[key], childVal)
}

function mergeArray (parent, child, key) {
const childVal = child[key]
if (!parent[key]) {
parent[key] = []
}
parent[key] = parent[key].concat(childVal)
}

function mergeToArray (parent, child, key) {
let parentVal = parent[key]
const childVal = child[key]
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/core/transferOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export default function transferOptions (options, type, builtInMixins = []) {
}
// 注入全局写入的mixins
options = mergeInjectedMixins(options, type)
// 注入内建的mixins
options.mixins = options.mixins ? builtInMixins.concat(options.mixins) : builtInMixins

if (currentInject && currentInject.injectComputed) {
// 编译计算属性注入
Expand All @@ -20,6 +18,8 @@ export default function transferOptions (options, type, builtInMixins = []) {
// 转换mode
options.mpxConvertMode = options.mpxConvertMode || getConvertMode(global.currentSrcMode, __mpx_mode__)
const rawOptions = mergeOptions(options, type)
// 注入内建的mixins, 内建mixin是按原始平台编写的,所以合并规则和rootMixins保持一致
rawOptions.mixins = builtInMixins
if (currentInject && currentInject.propKeys) {
const computedKeys = Object.keys(options.computed || {})
// 头条小程序受限父子组件生命周期顺序的问题,向子组件传递computed属性,子组件初始挂载时是拿不到对应数据的,在此做出提示
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/platform/builtInMixins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export default function getBuiltInMixins (options, type) {
injectHelperMixin(),
refsMixin(),
showMixin(type),
relationsMixin()
relationsMixin(type)
].filter(item => item)
}
4 changes: 2 additions & 2 deletions packages/core/src/platform/builtInMixins/relationsMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ function transferPath (relations, base) {
return newRelations
}

export default function relationsMixin () {
if (is('ali')) {
export default function relationsMixin (mixinType) {
if (is('ali') && mixinType === 'component') {
return {
methods: {
getRelationNodes (path) {
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/platform/patch/ali/getDefaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ function filterOptions (options, type) {

export function getDefaultOptions (type, { rawOptions = {}, currentInject }) {
const hookNames = type === 'component' ? ['onInit', 'didMount', 'didUnmount'] : ['onLoad', 'onReady', 'onUnload']
const options = filterOptions(rawOptions, type)
options.mixins = [{
const rootMixins = [{
[hookNames[0]] () {
// 提供代理对象需要的api
transformApiForProxy(this, currentInject)
Expand Down Expand Up @@ -126,5 +125,7 @@ export function getDefaultOptions (type, { rawOptions = {}, currentInject }) {
this.$mpxProxy && this.$mpxProxy.destroyed()
}
}]
return mergeOptions(options, type, false)
rawOptions.mixins = rawOptions.mixins ? rootMixins.concat(rawOptions.mixins) : rootMixins
rawOptions = mergeOptions(rawOptions, type, false)
return filterOptions(rawOptions, type)
}
21 changes: 17 additions & 4 deletions packages/core/src/platform/patch/wx/getDefaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,20 @@ function filterOptions (options) {
return newOptions
}

function getRootMixin (mixin) {
const supportBehavior = typeof Behavior !== 'undefined'
if (supportBehavior) {
return {
// eslint-disable-next-line no-undef
behaviors: [Behavior(mixin)]
}
} else {
return mixin
}
}

export function getDefaultOptions (type, { rawOptions = {}, currentInject }) {
const options = filterOptions(rawOptions)
options.mixins = [{
const rootMixins = [getRootMixin({
attached () {
// 提供代理对象需要的api
transformApiForProxy(this, currentInject)
Expand All @@ -121,6 +132,8 @@ export function getDefaultOptions (type, { rawOptions = {}, currentInject }) {
detached () {
this.$mpxProxy && this.$mpxProxy.destroyed()
}
}]
return mergeOptions(options, type, false)
})]
rawOptions.mixins = rawOptions.mixins ? rootMixins.concat(rawOptions.mixins) : rootMixins
rawOptions = mergeOptions(rawOptions, type, false)
return filterOptions(rawOptions)
}

0 comments on commit 2189ac7

Please sign in to comment.