From b0229d7f26404d8c687f1eacb7ff12c2e54339c9 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 23 Oct 2019 20:26:20 +0800 Subject: [PATCH] feat(v3): v-slot --- .../compiler-app-plus-extra.service.spec.js | 7 +++++++ .../compiler-app-plus-extra.view.spec.js | 6 ++++++ .../__tests__/compiler-app-plus.view.spec.js | 2 +- .../uni-template-compiler/__tests__/demo.js | 8 +++----- .../lib/app/parser/block-parser.js | 19 ++++++++++++++++--- .../lib/app/parser/component-parser.js | 2 +- .../uni-template-compiler/lib/app/service.js | 4 ++-- .../uni-template-compiler/lib/app/view.js | 2 +- packages/uni-template-compiler/lib/index.js | 3 ++- packages/uni-template-compiler/lib/util.js | 2 +- 10 files changed, 40 insertions(+), 15 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js index 1ad94dee8f..c25fdb3e88 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js @@ -63,6 +63,13 @@ describe('codegen', () => { '
A{{ d | e | f }}B{{text}}C
', `with(this){return _c('div',[_v((_$s(0,'t0',_s(_f("f")(_f("e")(d)))))+(_$s(0,'t1',_s(text))))])}` ) + }) + + it('generate v-slot', () => { + assertCodegen( + '{{ user.firstName }}', + `with(this){return _c('current-user',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function({ user }){return [_v((_$s(0,'t0',_s(user.firstName))))]}}])})}` + ) }) }) /* eslint-enable quotes */ diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js index 70d1b4cebd..f4ed62645b 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js @@ -39,6 +39,12 @@ describe('codegen', () => { '
{{hi}}
', `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_t("default",[_c('v-uni-view',{attrs:{"_i":2}},[_v((_$g(2,'t0')))])],{"_i":1})],2)}` ) + }) + it('generate v-slot', () => { + assertCodegen( + '{{ user.firstName }}', + `with(this){return _c('current-user',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function({ user }){return [_v((_$g(0,'t0')))]}}])})}` + ) }) }) /* eslint-enable quotes */ diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus.view.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus.view.spec.js index 630099c5bf..2aef257bef 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus.view.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus.view.spec.js @@ -565,7 +565,7 @@ describe('codegen', () => { it('generate svg component with children', () => { assertCodegen( '', - `with(this){return _c('svg',{attrs:{"_i":0}},[_c('my-comp',{attrs:{"_i":1}},[_c('circle',{attrs:{"_i":2}})])],1)}` + `with(this){return _c('svg',{attrs:{"_i":0}},[_c('my-comp',{attrs:{"_i":1}},[_c('circle',{attrs:{"_i":2}})],1)],1)}` ) }) diff --git a/packages/uni-template-compiler/__tests__/demo.js b/packages/uni-template-compiler/__tests__/demo.js index 96eb055756..5ad3df2f93 100644 --- a/packages/uni-template-compiler/__tests__/demo.js +++ b/packages/uni-template-compiler/__tests__/demo.js @@ -1,10 +1,8 @@ const compiler = require('../lib') const res = compiler.compile( ` - - - - `, { +
+`, { resourcePath: '/User/fxy/Documents/test.wxml', isReservedTag: function (tag) { return true @@ -15,7 +13,7 @@ const res = compiler.compile( mp: { platform: 'app-plus' }, - // service: true, + service: true, view: true }) console.log(require('util').inspect(res, { diff --git a/packages/uni-template-compiler/lib/app/parser/block-parser.js b/packages/uni-template-compiler/lib/app/parser/block-parser.js index 993f997fc4..2feaefe9b8 100644 --- a/packages/uni-template-compiler/lib/app/parser/block-parser.js +++ b/packages/uni-template-compiler/lib/app/parser/block-parser.js @@ -1,7 +1,20 @@ -module.exports = function parseBlock (el) { - if (el.tag === 'block') { +const { + ID, + hasOwn, + addRawAttr +} = require('../util') + +module.exports = function parseBlock (el, parent) { + if (el.tag === 'template' && !hasOwn(el.attrsMap, ID)) { + /** + * + * {{ user.firstName }} + * + */ + addRawAttr(el, ID, parent.attrsMap[ID]) + } else if (el.tag === 'block') { el.tag = 'template' - const vForKey = el.key + const vForKey = el.key if (vForKey) { delete el.key el.children.forEach((childEl, index) => { diff --git a/packages/uni-template-compiler/lib/app/parser/component-parser.js b/packages/uni-template-compiler/lib/app/parser/component-parser.js index 97f07cbde8..c558bd6bf5 100644 --- a/packages/uni-template-compiler/lib/app/parser/component-parser.js +++ b/packages/uni-template-compiler/lib/app/parser/component-parser.js @@ -10,6 +10,6 @@ module.exports = function parseComponent (el) { // 需要把自定义组件的 attrs, props 全干掉 if (el.tag && !hasOwn(tags, el.tag.replace('v-uni-', ''))) { // 仅保留 ID - el.attrs = el.attrs.filter(attr => attr.name === ID) + el.attrs && (el.attrs = el.attrs.filter(attr => attr.name === ID)) } } diff --git a/packages/uni-template-compiler/lib/app/service.js b/packages/uni-template-compiler/lib/app/service.js index 7bd9b502bc..28adee2ece 100644 --- a/packages/uni-template-compiler/lib/app/service.js +++ b/packages/uni-template-compiler/lib/app/service.js @@ -66,7 +66,7 @@ function transformNode (el, parent, state) { if (el.type === 3) { return } - parseBlock(el) + parseBlock(el, parent) parseEvent(el) updateForEleId(el, state) @@ -115,7 +115,7 @@ function genVModel (el) { const prop = el.props.find(prop => prop.name === 'value') prop.value = createGenVar(el.attrsMap[ID])('v-model', prop.value) } - if (el.model) { + if (el.model) { el.model.value = createGenVar(el.attrsMap[ID])('v-model', el.model.value) } } diff --git a/packages/uni-template-compiler/lib/app/view.js b/packages/uni-template-compiler/lib/app/view.js index 5fa7e70ffe..e22130f37e 100644 --- a/packages/uni-template-compiler/lib/app/view.js +++ b/packages/uni-template-compiler/lib/app/view.js @@ -57,7 +57,7 @@ function transformNode (el, parent, state) { if (el.type === 3) { return } - parseBlock(el) + parseBlock(el, parent) parseComponent(el) parseEvent(el) // 更新 id diff --git a/packages/uni-template-compiler/lib/index.js b/packages/uni-template-compiler/lib/index.js index 20e2714d67..4286c2baa4 100644 --- a/packages/uni-template-compiler/lib/index.js +++ b/packages/uni-template-compiler/lib/index.js @@ -43,7 +43,8 @@ module.exports = { return compiled } else if (options.view) { (options.modules || (options.modules = [])).push(require('./app/view')) - options.optimize = false // 暂不启用 staticRenderFns + options.optimize = false // 暂不启用 staticRenderFns + options.isReservedTag = (tagName) => false // 均为组件 return compile(source, options) } diff --git a/packages/uni-template-compiler/lib/util.js b/packages/uni-template-compiler/lib/util.js index 563b4d9c33..5a4fd8d77d 100644 --- a/packages/uni-template-compiler/lib/util.js +++ b/packages/uni-template-compiler/lib/util.js @@ -185,7 +185,7 @@ const { function isComponent (tagName) { if (tagName === 'block' || tagName === 'template') { return false - } + } return !hasOwn(tags, getTagName(tagName.replace('v-uni-', ''))) } -- GitLab