diff --git a/packages/mars-build/CHANGELOG.md b/packages/mars-build/CHANGELOG.md index 8b20857f..8f087e5a 100644 --- a/packages/mars-build/CHANGELOG.md +++ b/packages/mars-build/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.3.15](https://github.com/keyiran/Mars/compare/@marsjs/build@0.3.14...@marsjs/build@0.3.15) (2020-09-17) + + +### Features + +* **build & core:** support swan lifecycle onInit ([d83d1ea](https://github.com/keyiran/Mars/commit/d83d1ea)) + + + + + ## [0.3.14](https://github.com/max-team/Mars/compare/@marsjs/build@0.3.13...@marsjs/build@0.3.14) (2019-11-14) diff --git a/packages/mars-build/package.json b/packages/mars-build/package.json index d80f1922..44382f50 100644 --- a/packages/mars-build/package.json +++ b/packages/mars-build/package.json @@ -1,6 +1,6 @@ { "name": "@marsjs/build", - "version": "0.3.14", + "version": "0.3.15", "scripts": { "test": "jest --coverage" }, diff --git a/packages/mars-build/src/h5/transform/plugins/transformScriptPlugin.js b/packages/mars-build/src/h5/transform/plugins/transformScriptPlugin.js index 13c9a22d..7756e2ba 100644 --- a/packages/mars-build/src/h5/transform/plugins/transformScriptPlugin.js +++ b/packages/mars-build/src/h5/transform/plugins/transformScriptPlugin.js @@ -6,8 +6,11 @@ /* eslint-disable fecs-camelcase */ /* eslint-disable babel/new-cap */ +/* global Set */ + // 小程序page生命周期 const PAGE_LIFECYCLE_HOOKS = { + onInit: true, onLoad: true, onReady: true, onShow: true, @@ -23,7 +26,19 @@ const PAGE_LIFECYCLE_HOOKS = { }; +// 小程序和Vue App级生命周期映射关系 +const APP_LIFE_MAP_VUE = { + onLaunch: 'created', + onShow: 'mounted', + onHide: 'destroyed' + // onLoad: true, + // onReady: true, + // onUnload: true +}; + +// 小程序和Vue Page级生命周期映射关系(如果多个小程序生命周期映射到同一个vue生命周期,请严格保证书写顺序和生命周期调用顺序一致,例如onInit和onLoad) const PAGE_LIFE_MAP_VUE = { + onInit: 'created', onLoad: 'created', onReady: 'mounted', onUnload: 'destroyed', @@ -63,14 +78,6 @@ const COMP_LIFE_MAP_VUE = { type: 'pageLifetimes' } }; -const APP_PAOGE_LIFE_MAP_VUE = { - onLaunch: 'created', - onShow: 'mounted', - onHide: 'destroyed' - // onLoad: true, - // onReady: true, - // onUnload: true -}; // MAP: AOP 注册的生命周期方法的映射 const AOP_MAP = { @@ -219,10 +226,10 @@ function judgeComponent(properties) { * @return {Object} 处理的lifeItem */ function bindAOPEvents(lifeItem, t, key, isApp) { - const lifeTime = isApp ? APP_PAOGE_LIFE_MAP_VUE[key] : PAGE_LIFE_MAP_VUE[key]; - if (!lifeTime) { - // 非该页面生命周期则返回 - return; + const isAOPLifeTime = key in (isApp ? AOP_MAP.App : AOP_MAP.Page); + // 非当前页面AOP生命周期,不做处理返回 + if (!isAOPLifeTime) { + return lifeItem; } // 在生命周期内调用相应AOP事件 const AOPEventHookExpression = t.expressionStatement( @@ -412,39 +419,41 @@ module.exports = function getVisitor(options = {}) { // 如果有区别级别的 mpConfig 从 mpConfig 获取是否为组件 const isComponent = mpConfig ? mpConfig.component : judgeComponent(properties); - // 处理swan page 生命周期:onLoad 、onReady 、onUnload、onShow 、onHide - // 收集 app.vue 周期:onLaunch 、 onShow 、 onHide - ['onLoad', 'onReady', 'onUnload', 'onLaunch', 'onShow', 'onHide'].forEach(key => { - let lifeItem = properties.find(item => item.key.name === key); - let lifeMapKey = isApp ? APP_PAOGE_LIFE_MAP_VUE[key] : PAGE_LIFE_MAP_VUE[key]; - // 判断当前 vue sfc 是否是组件component - if (isComponent) { - return; - } - // 处理生命周期的AOP - if (useAOP) { - lifeItem = bindAOPEvents(lifeItem, t, key, isApp); - } - mapSwanLifeTime(properties, t, key, lifeItem, lifeMapKey, options); - }); - // 处理swan component 生命周期:created、 attached 、ready 、detached、show、hide - Object.keys(COMP_LIFE_MAP_VUE).forEach(key => { - const { - life: lifeMapKey, - type: lifeType - } = COMP_LIFE_MAP_VUE[key]; - const lifeTypeItem = properties.find(item => item.key.name === lifeType); - if (!lifeTypeItem) { - return; - } + if (isComponent) { + // 将小程序组件级生命周期映射到Vue + Object.keys(COMP_LIFE_MAP_VUE).forEach(key => { + const { + life: lifeMapKey, + type: lifeType + } = COMP_LIFE_MAP_VUE[key]; + const lifeTypeItem = properties.find(item => item.key.name === lifeType); + if (!lifeTypeItem) { + return; + } - const lifeItem = lifeTypeItem.value.properties.find(item => item.key.name === key); - if (!lifeItem) { - return; - } + const lifeItem = lifeTypeItem.value.properties.find(item => item.key.name === key); + if (!lifeItem) { + return; + } - mapSwanLifeTime(properties, t, key, lifeItem, lifeMapKey, options); - }); + mapSwanLifeTime(properties, t, key, lifeItem, lifeMapKey, options); + }); + } + else { + // 将小程序App和Page级生命周期映射到Vue + [ + ...new Set(Object.keys(PAGE_LIFE_MAP_VUE).concat(Object.keys(APP_LIFE_MAP_VUE))) + ].forEach(key => { + let lifeItem = properties.find(item => item.key.name === key); + let lifeMapKey = isApp ? APP_LIFE_MAP_VUE[key] : PAGE_LIFE_MAP_VUE[key]; + // 处理生命周期的AOP + if (useAOP) { + lifeItem = bindAOPEvents(lifeItem, t, key, isApp); + } + + mapSwanLifeTime(properties, t, key, lifeItem, lifeMapKey, options); + }); + } /* eslint-disable */ path.traverse(Property(t, options)); } diff --git a/packages/mars-core/CHANGELOG.md b/packages/mars-core/CHANGELOG.md index d57694d3..b7a80048 100644 --- a/packages/mars-core/CHANGELOG.md +++ b/packages/mars-core/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.3.9](https://github.com/keyiran/Mars/compare/@marsjs/core@0.3.8...@marsjs/core@0.3.9) (2020-09-17) + + +### Features + +* **build & core:** support swan lifecycle onInit ([d83d1ea](https://github.com/keyiran/Mars/commit/d83d1ea)) + + + + + ## [0.3.8](https://github.com/max-team/Mars/compare/@marsjs/core@0.3.7...@marsjs/core@0.3.8) (2020-03-12) diff --git a/packages/mars-core/package.json b/packages/mars-core/package.json index 84d8b0bf..29cb1558 100644 --- a/packages/mars-core/package.json +++ b/packages/mars-core/package.json @@ -1,6 +1,6 @@ { "name": "@marsjs/core", - "version": "0.3.8", + "version": "0.3.9", "scripts": { "test": "jest --coverage" }, diff --git a/packages/mars-core/src/base/createPage.js b/packages/mars-core/src/base/createPage.js index c48f941e..db1ed5a2 100644 --- a/packages/mars-core/src/base/createPage.js +++ b/packages/mars-core/src/base/createPage.js @@ -88,6 +88,24 @@ export function makeCreatePage(pageMixin, {handleProxy, handleModel}, setData, c data: initData, handleProxy, handleModel, + onInit(...args) { + let vm = this.$vue; + if (!vm) { + vm = createVue.call(this, options, args, {setData}); + } + else { + const query = args[0]; + vm.$mp.query = query; + vm.$mp.options = query; + } + + if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) { + console.log('[debug: mp pageHooks] onInit', this.__uid__); + } + // 先 callHook 保证数据可以初始化 + const ret = callHook.call(this, this.$vue, 'page', 'onInit', args); + return ret; + }, onLoad(...args) { let vm = this.$vue; if (!vm) {