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 all 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 _getAst = this.target._getAst?.bind(this.target)
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 || _getAst) {
const ast = (_getAst && isFunction(_getAst)) ? _getAst() : dynamic.getAst(moduleId)
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
2 changes: 0 additions & 2 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 @@ -41,7 +40,6 @@ export default function getBuiltInMixins (options, type) {
renderHelperMixin(),
showMixin(type),
i18nMixin()
// runtimeModulesMixin()
])
}
}
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
61 changes: 28 additions & 33 deletions packages/webpack-plugin/lib/template-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,50 +124,45 @@ global.currentInject = {
};\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 += `
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