提交 0c21bacb 编写于 作者: fxy060608's avatar fxy060608

feat(v3): custom directives

上级 9be8526b
......@@ -6801,10 +6801,6 @@ var plugin = {
Vue.prototype._$queue = queue;
Vue.prototype._m = function renderStatic() {
return this._e()
};
Vue.prototype.__call_hook = callHook$2;
// 运行时需要格式化 class,style
Vue.prototype._$stringifyClass = stringifyClass;
......
......@@ -15,7 +15,7 @@ describe('codegen', () => {
it('generate directive', () => {
assertCodegen(
'<p v-custom1:[arg1].modifier="value1" v-custom2></p>',
`with(this){return _c('v-uni-view',{directives:[{name:"custom1",rawName:"v-custom1:[arg1].modifier",value:(_$g(0,'v-custom1')),expression:"_$g(0,'v-custom1')",arg:_$g(0,'v-custom1-arg'),modifiers:{"modifier":true}},{name:"custom2",rawName:"v-custom2"}],attrs:{"_i":0}})}`
`with(this){return _c('v-uni-view',{attrs:{"_i":0}})}`
)
})
it('generate v-for directive', () => {
......
......@@ -589,7 +589,7 @@ describe('codegen', () => {
// have "inline-template'"
assertCodegen(
'<my-component inline-template><p><span>hello world</span></p></my-component>',
`with(this){return _c('my-component',{attrs:{"_i":0},inlineTemplate:{render:function(){with(this){return _m(0)}},staticRenderFns:[function(){with(this){return _c('p',[_c('span')])}}]}})}`
`with(this){return _c('my-component',{attrs:{"_i":0},inlineTemplate:{render:function(){with(this){return _c('p',[_c('span')])}},staticRenderFns:[]}})}`
)
// "have inline-template attrs, but not having exactly one child element
assertCodegen(
......@@ -616,7 +616,7 @@ describe('codegen', () => {
it('generate static trees inside v-for', () => {
assertCodegen(
`<div><div v-for="i in 10"><p><span></span></p></div></div>`,
`with(this){return _c('div',_l((10),function(i,$10,$20,$30){return _c('div',[_m(0,true)])}),0)}`
`with(this){return _c('div',_l((10),function(i,$10,$20,$30){return _c('div',[_c('p',[_c('span')])])}),0)}`
// [`with(this){return _c('p',[_c('span')])}`]
)
})
......@@ -660,7 +660,7 @@ describe('codegen', () => {
it('does not squash templates inside v-pre', () => {
assertCodegen(
'<div v-pre><template><p>{{msg}}</p></template></div>',
`with(this){return _m(0)}`
`with(this){return _c('div',[[_c('p')]],2)}`
)
// const template = '<div v-pre><template><p>{{msg}}</p></template></div>'
// const generatedCode = `with(this){return _m(0)}`
......
......@@ -15,12 +15,12 @@ describe('codegen', () => {
it('generate directive', () => {
assertCodegen(
'<p v-custom1:arg1.modifier="value1" v-custom2></p>',
`with(this){return _c('v-uni-view',{directives:[{name:"custom1",rawName:"v-custom1:arg1.modifier",value:(_$g(0,'v-custom1')),expression:"_$g(0,'v-custom1')",arg:"arg1",modifiers:{"modifier":true}},{name:"custom2",rawName:"v-custom2"}],attrs:{"_i":0}})}`
`with(this){return _c('v-uni-view',{attrs:{"_i":0}})}`
)
// extra
assertCodegen(
'<p v-custom1:[arg1].modifier="value1" v-custom2></p>',
`with(this){return _c('v-uni-view',{directives:[{name:"custom1",rawName:"v-custom1:[arg1].modifier",value:(_$g(0,'v-custom1')),expression:"_$g(0,'v-custom1')",arg:_$g(0,'v-custom1-arg'),modifiers:{"modifier":true}},{name:"custom2",rawName:"v-custom2"}],attrs:{"_i":0}})}`
`with(this){return _c('v-uni-view',{attrs:{"_i":0}})}`
)
})
......
const compiler = require('../lib')
const res = compiler.compile(
`
<keep-alive exclude="componentWithStatus1"><component is="componentWithStatus"/></keep-alive>
<p v-text="test"></p>
`, {
resourcePath: '/User/fxy/Documents/test.wxml',
isReservedTag: function (tag) {
......@@ -13,7 +13,7 @@ const res = compiler.compile(
mp: {
platform: 'app-plus'
},
service: true,
// service: true,
view: true
})
console.log(require('util').inspect(res, {
......
......@@ -13,7 +13,6 @@ const {
parseIf,
parseFor,
parseText,
parseDirs,
parseAttrs,
parseProps,
parseBinding
......@@ -54,6 +53,41 @@ function parseKey (el) {
}
}
function parseDirs (el, genVar, ignoreDirs, includeDirs = []) {
if (!el.directives) {
return
}
el.directives = el.directives.filter(dir => {
if (includeDirs.indexOf(dir.name) !== -1) {
if (ignoreDirs.indexOf(dir.name) === -1) {
dir.value && (dir.value = genVar('v-' + dir.name, dir.value))
dir.isDynamicArg && (dir.arg = genVar('v-' + dir.name + '-arg', dir.arg))
}
return true
}
})
}
const includeDirs = [
'text',
'html',
'bind',
'model',
'show',
'if',
'else',
'else-if',
'for',
'on',
'bind',
'slot',
'pre',
'cloak',
'once'
]
const ignoreDirs = ['model']
function transformNode (el, parent, state) {
if (el.type === 3) {
return
......@@ -86,7 +120,7 @@ function transformNode (el, parent, state) {
parseIf(el, createGenVar)
parseBinding(el, genVar)
parseDirs(el, genVar, ['model'])
parseDirs(el, genVar, ignoreDirs, includeDirs)
parseAttrs(el, genVar)
parseProps(el, genVar)
}
......
......@@ -30,7 +30,7 @@ module.exports = {
compile (source, options = {}) {
if (options.service) {
(options.modules || (options.modules = [])).push(require('./app/service'))
options.optimize = true // 启用 staticRenderFns
options.optimize = false // 启用 staticRenderFns
// domProps => attrs
options.mustUseProp = () => false
options.isReservedTag = (tagName) => !isComponent(tagName) // 非组件均为内置
......@@ -38,7 +38,6 @@ module.exports = {
// clear staticRenderFns
const compiled = compile(source, options)
compiled.staticRenderFns.length = 0
return compiled
} else if (options.view) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册