Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/dynamic query and dynamic render #8

Merged
merged 5 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/core/src/core/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,15 +571,22 @@ export default class MpxProxy {
const _r = this.target._r.bind(this.target)
const _sc = this.target._sc.bind(this.target)
const _g = this.target._g.bind(this.target)
const _dr = this.target.dynamicRender?.bind(this.target)
const _dr = this.target.dynamicRender?.bind(this.target)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dynamicRender 这个感觉叫 getAst 比较直观点?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加个下划线前缀吧,约定是个比较私有的api

const moduleId = this.target.__moduleId
const dynamicTarget = this.target.__dynamic

const effect = this.effect = new ReactiveEffect(() => {
// pre render for props update
if (this.propsUpdatedFlag) {
this.updatePreRender()
}
if (dynamicTarget || _dr) {
const ast = (_dr && typeof _dr === 'function') ? _dr() : dynamic.getAst(moduleId)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

函数的判断用 isFunction 就行

return _r(false, _g(ast, moduleId))
}
if (this.target.__injectedRender) {
try {
return this.target.__injectedRender(_i, _c, _r, _sc, _g, _dr, dynamic)
return this.target.__injectedRender(_i, _c, _r, _sc, _g)
} catch (e) {
warn('Failed to execute render function, degrade to full-set-data mode.', this.options.mpxFileResource, e)
this.render()
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/platform/builtInMixins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import pageScrollMixin from './pageScrollMixin'
import componentGenericsMixin from './componentGenericsMixin'
import getTabBarMixin from './getTabBarMixin'
import pageRouteMixin from './pageRouteMixin'
// import runtimeModulesMixin from './runtimeModulesMixin'

export default function getBuiltInMixins (options, type) {
let bulitInMixins = []
Expand Down Expand Up @@ -40,8 +39,7 @@ export default function getBuiltInMixins (options, type) {
bulitInMixins = bulitInMixins.concat([
renderHelperMixin(),
showMixin(type),
i18nMixin()
// runtimeModulesMixin()
i18nMixin(),
])
}
}
Expand Down
33 changes: 0 additions & 33 deletions packages/core/src/platform/builtInMixins/runtimeModulesMixin.js

This file was deleted.

16 changes: 13 additions & 3 deletions packages/core/src/platform/patch/ali/getDefaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,21 @@ function transformApiForProxy (context, currentInject) {
}
})
}
if (currentInject.getRuntimeModules) {
if (currentInject.moduleId) {
Object.defineProperties(context, {
__getRuntimeModules: {
__moduleId: {
get () {
return currentInject.getRuntimeModules
return currentInject.moduleId
},
configurable: false
}
})
}
if (currentInject.dynamic) {
Object.defineProperties(context, {
__dynamic: {
get () {
return currentInject.dynamic
},
configurable: false
}
Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/platform/patch/wx/getDefaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,21 @@ function transformApiForProxy (context, currentInject) {
}
})
}
if (currentInject.getRuntimeModules) {
if (currentInject.moduleId) {
Object.defineProperties(context, {
__getRuntimeModules: {
__moduleId: {
get () {
return currentInject.getRuntimeModules
return currentInject.moduleId
},
configurable: false
}
})
}
if (currentInject.dynamic) {
Object.defineProperties(context, {
__dynamic: {
get () {
return currentInject.dynamic
},
configurable: false
}
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/vnode/staticMap.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ createComponent({
r: { // vdom 数据
type: Object,
value: {
nodeType: 'block'
nt: 'block'
}
}
},
Expand Down
63 changes: 29 additions & 34 deletions packages/webpack-plugin/lib/template-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,51 +123,46 @@ global.currentInject = {
moduleId: ${moduleIdString}
};\n`

if (runtimeCompile) {
resultSource += `
global.currentInject.render = function(_i, _c, _r, _sc, _g, _dr, _d) {
const ast = (_dr && typeof _dr === 'function') ? _dr(${moduleIdString}) : _d.getAst(${moduleIdString})
_r(false, _g(ast, ${moduleIdString}))
}
`
} else {
const rawCode = compiler.genNode(ast)
if (rawCode) {
try {
const ignoreMap = Object.assign({
_i: true,
_c: true,
_sc: true,
_r: true
}, meta.wxsModuleMap)
const bindResult = optimizeRenderLevel === 2
? bindThis.transformSimple(rawCode, {
ignoreMap
})
: bindThis.transform(rawCode, {
needCollect: true,
renderReduce: optimizeRenderLevel === 1,
ignoreMap
})
resultSource += `
if(runtimeCompile){
resultSource += `global.currentInject.dynamic = true`
}

const rawCode = runtimeCompile ? '' : compiler.genNode(ast)
if (rawCode) {
try {
const ignoreMap = Object.assign({
_i: true,
_c: true,
_sc: true,
_r: true
}, meta.wxsModuleMap)
const bindResult = optimizeRenderLevel === 2
? bindThis.transformSimple(rawCode, {
ignoreMap
})
: bindThis.transform(rawCode, {
needCollect: true,
renderReduce: optimizeRenderLevel === 1,
ignoreMap
})
resultSource += `
global.currentInject.render = function (_i, _c, _r, _sc) {
${bindResult.code}
_r(${optimizeRenderLevel === 2 ? 'true' : ''});
};\n`
if ((mode === 'tt' || mode === 'swan') && bindResult.propKeys) {
resultSource += `global.currentInject.propKeys = ${JSON.stringify(bindResult.propKeys)};\n`
}
} catch (e) {
error(`
if ((mode === 'tt' || mode === 'swan') && bindResult.propKeys) {
resultSource += `global.currentInject.propKeys = ${JSON.stringify(bindResult.propKeys)};\n`
}
} catch (e) {
error(`
Invalid render function generated by the template, please check!\n
Template result:
${result}\n
Error code:
${rawCode}
Error Detail:
${e.stack}`)
return result
}
return result
}
}

Expand Down
Loading