提交 6fff804e 编写于 作者: fxy060608's avatar fxy060608

feat(v3): optimize service code

上级 95177e1d
......@@ -6482,8 +6482,9 @@ var serviceContext = (function () {
timestamp: Date.now()
}
}, [pageId]);
createPage(route, pageId, query, pageInstance).$mount();
try {
createPage(route, pageId, query, pageInstance).$mount();
} catch (e) {}
}
}
......@@ -8571,9 +8572,6 @@ var serviceContext = (function () {
});
function optimize (k, v) {
if (typeof v === 'undefined') {
return ''
}
if (
k === V_IF ||
k === V_ELSE_IF ||
......
......@@ -19,11 +19,11 @@ describe('codegen', () => {
)
assertCodegen(
'<div><block v-for="item in items"><div></div><div></div></block></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('div',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30}),attrs:{"_i":("2-"+$30)}}),_c('div',{key:_$f(1,{forIndex:$20,keyIndex:1,key:1+'-1'+$30}),attrs:{"_i":("3-"+$30)}})]})],2)}`
`with(this){return _c('div',[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('div',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30})}),_c('div',{key:_$f(1,{forIndex:$20,keyIndex:1,key:1+'-1'+$30})})]})],2)}`
)
assertCodegen(
'<div><block v-for="item in items" :key="item.id"><div></div><div></div></block></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('div',{key:_$f(1,{forIndex:$20,keyIndex:0,key:item.id+'_0'}),attrs:{"_i":("2-"+$30)}}),_c('div',{key:_$f(1,{forIndex:$20,keyIndex:1,key:item.id+'_1'}),attrs:{"_i":("3-"+$30)}})]})],2)}`
`with(this){return _c('div',[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('div',{key:_$f(1,{forIndex:$20,keyIndex:0,key:item.id+'_0'})}),_c('div',{key:_$f(1,{forIndex:$20,keyIndex:1,key:item.id+'_1'})})]})],2)}`
)
})
it('generate directive', () => {
......@@ -35,33 +35,33 @@ describe('codegen', () => {
it('generate v-for directive', () => {
assertCodegen(
'<div><template v-for="item in items"><div></div><div></div></template></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('div',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30}),attrs:{"_i":("2-"+$30)}}),_c('div',{key:_$f(1,{forIndex:$20,keyIndex:1,key:1+'-1'+$30}),attrs:{"_i":("3-"+$30)}})]})],2)}`
`with(this){return _c('div',[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('div',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30})}),_c('div',{key:_$f(1,{forIndex:$20,keyIndex:1,key:1+'-1'+$30})})]})],2)}`
)
assertCodegen(
'<div><template v-for="item in items">text</template></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('text',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30}),attrs:{"_i":("1-"+$30)}})]})],2)}`
`with(this){return _c('div',[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('text',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30}),attrs:{"_i":("1-"+$30)}})]})],2)}`
)
assertCodegen(
'<div><template v-for="item in items">{{text}}</template></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('text',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30}),extras:{t0:_s(text)},attrs:{"_i":("1-"+$30)}})]})],2)}`
`with(this){return _c('div',[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('text',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30}),extras:{t0:_s(text)},attrs:{"_i":("1-"+$30)}})]})],2)}`
)
assertCodegen(
'<div><template v-for="item in items"><span></span>{{text}}</template></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('span',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30}),attrs:{"_i":("2-"+$30)}}),_c('text',{extras:{t0:_s(text)},attrs:{"_i":("1-"+$30)}})]})],2)}`
`with(this){return _c('div',[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('span',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30})}),_c('text',{extras:{t0:_s(text)},attrs:{"_i":("1-"+$30)}})]})],2)}`
)
assertCodegen(
'<div><template v-for="item in items">a {{text1}} b {{text2}}</template></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('text',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30}),extras:{t0:_s(text1),t1:_s(text2)},attrs:{"_i":("1-"+$30)}})]})],2)}`
`with(this){return _c('div',[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [_c('text',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30}),extras:{t0:_s(text1),t1:_s(text2)},attrs:{"_i":("1-"+$30)}})]})],2)}`
)
assertCodegen(
'<div><template v-for="item in items"><span v-if="item.sub"></span></template></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [(_$i(("2-"+$30),item.sub))?_c('span',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30}),attrs:{"_i":("2-"+$30)}}):_e()]})],2)}`
`with(this){return _c('div',[_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return [(_$i(("2-"+$30),item.sub))?_c('span',{key:_$f(1,{forIndex:$20,keyIndex:0,key:1+'-0'+$30})}):_e()]})],2)}`
)
})
it('generate text with multiple statements', () => {
assertCodegen(
'<div>A{{ d | e | f }}B{{text}}C</div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_c('text',{extras:{t0:_s(_f("f")(_f("e")(d))),t1:_s(text)},attrs:{"_i":0}})])}`
`with(this){return _c('div',[_c('text',{extras:{t0:_s(_f("f")(_f("e")(d))),t1:_s(text)},attrs:{"_i":0}})])}`
)
})
})
......
......@@ -17,28 +17,28 @@ describe('codegen', () => {
'<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}})}`
)
})
it('generate v-for directive', () => {
assertCodegen(
'<div><template v-for="item in items"><div></div><div></div></template></div>',
`with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_l((_$g(1,'f')),function(item,$10,$20,$30){return [_c('v-uni-view',{key:item['k0'],attrs:{"_i":("2-"+$30)}}),_c('v-uni-view',{key:item['k1'],attrs:{"_i":("3-"+$30)}})]})],2)}`
)
assertCodegen(
'<div><template v-for="item in items"><span v-if="item.sub"></span></template></div>',
`with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_l((_$g(1,'f')),function(item,$10,$20,$30){return [(_$g(("2-"+$30),'i'))?_c('v-uni-label',{key:item['k0'],attrs:{"_i":("2-"+$30)}}):_e()]})],2)}`
)
})
it('generate events with multiple statements', () => {
assertCodegen(
'<div>A{{ d | e | f }}B{{text}}C</div>',
`with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("A"+(_$g(0,'t0'))+"B"+(_$g(0,'t1'))+"C")])}`
)
})
it('generate slot fallback content', () => {
assertCodegen(
'<div><slot><div>{{hi}}</div></slot></div>',
`with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_t("default",[_c('v-uni-view',{attrs:{"_i":2}},[_v((_$g(2,'t0',1)))])],{"_i":1})],2)}`
)
})
it('generate v-for directive', () => {
assertCodegen(
'<div><template v-for="item in items"><div></div><div></div></template></div>',
`with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_l((_$g(1,'f')),function(item,$10,$20,$30){return [_c('v-uni-view',{key:item['k0'],attrs:{"_i":("2-"+$30)}}),_c('v-uni-view',{key:item['k1'],attrs:{"_i":("3-"+$30)}})]})],2)}`
)
assertCodegen(
'<div><template v-for="item in items"><span v-if="item.sub"></span></template></div>',
`with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_l((_$g(1,'f')),function(item,$10,$20,$30){return [(_$g(("2-"+$30),'i'))?_c('v-uni-label',{key:item['k0'],attrs:{"_i":("2-"+$30)}}):_e()]})],2)}`
)
})
it('generate events with multiple statements', () => {
assertCodegen(
'<div>A{{ d | e | f }}B{{text}}C</div>',
`with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("A"+(_$g(0,'t0'))+"B"+(_$g(0,'t1'))+"C")])}`
)
})
it('generate slot fallback content', () => {
assertCodegen(
'<div><slot><div>{{hi}}</div></slot></div>',
`with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_t("default",[_c('v-uni-view',{attrs:{"_i":2}},[_v((_$g(2,'t0',1)))])],{"_i":1})],2)}`
)
})
})
/* eslint-enable quotes */
/* eslint-enable quotes */
......@@ -34,86 +34,86 @@ describe('codegen', () => {
it('generate filters with no arguments', () => {
assertCodegen(
'<div>{{ d | e() }}</div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_c('text',{extras:{t0:_s(_f("e")(d))},attrs:{"_i":0}})])}`
`with(this){return _c('div',[_c('text',{extras:{t0:_s(_f("e")(d))},attrs:{"_i":0}})])}`
)
})
it('generate v-for directive', () => {
assertCodegen(
'<div><li v-for="item in items" :key="item.uid"></li></div>',
`with(this){return _c('div',{attrs:{"_i":0}},_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return _c('li',{key:_$f(1,{forIndex:$20,key:item.uid}),attrs:{"_i":("1-"+$30)}})}),0)}`
`with(this){return _c('div',_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return _c('li',{key:_$f(1,{forIndex:$20,key:item.uid})})}),0)}`
)
// iterator syntax
assertCodegen(
'<div><li v-for="(item, i) in items"></li></div>',
`with(this){return _c('div',{attrs:{"_i":0}},_l((_$f(1,{forItems:items})),function(item,i,$20,$30){return _c('li',{key:_$f(1,{forIndex:$20,key:1+'-'+$30}),attrs:{"_i":("1-"+$30)}})}),0)}`
`with(this){return _c('div',_l((_$f(1,{forItems:items})),function(item,i,$20,$30){return _c('li',{key:_$f(1,{forIndex:$20,key:1+'-'+$30})})}),0)}`
)
assertCodegen(
'<div><li v-for="(item, key, index) in items"></li></div>',
`with(this){return _c('div',{attrs:{"_i":0}},_l((_$f(1,{forItems:items})),function(item,key,index,$30){return _c('li',{key:_$f(1,{forIndex:index,key:1+'-'+$30}),attrs:{"_i":("1-"+$30)}})}),0)}`
`with(this){return _c('div',_l((_$f(1,{forItems:items})),function(item,key,index,$30){return _c('li',{key:_$f(1,{forIndex:index,key:1+'-'+$30})})}),0)}`
)
// destructuring
assertCodegen(
'<div><li v-for="{ a, b } in items"></li></div>',
`with(this){return _c('div',{attrs:{"_i":0}},_l((_$f(1,{forItems:items})),function({ a, b },$10,$20,$30){return _c('li',{key:_$f(1,{forIndex:$20,key:1+'-'+$30}),attrs:{"_i":("1-"+$30)}})}),0)}`
`with(this){return _c('div',_l((_$f(1,{forItems:items})),function({ a, b },$10,$20,$30){return _c('li',{key:_$f(1,{forIndex:$20,key:1+'-'+$30})})}),0)}`
)
assertCodegen(
'<div><li v-for="({ a, b }, key, index) in items"></li></div>',
`with(this){return _c('div',{attrs:{"_i":0}},_l((_$f(1,{forItems:items})),function({ a, b },key,index,$30){return _c('li',{key:_$f(1,{forIndex:index,key:1+'-'+$30}),attrs:{"_i":("1-"+$30)}})}),0)}`
`with(this){return _c('div',_l((_$f(1,{forItems:items})),function({ a, b },key,index,$30){return _c('li',{key:_$f(1,{forIndex:index,key:1+'-'+$30})})}),0)}`
)
// v-for with extra element
assertCodegen(
'<div><p></p><li v-for="item in items"></li></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_c('p',{attrs:{"_i":1}}),_l((_$f(2,{forItems:items})),function(item,$10,$20,$30){return _c('li',{key:_$f(2,{forIndex:$20,key:2+'-'+$30}),attrs:{"_i":("2-"+$30)}})})],2)}`
`with(this){return _c('div',[_c('p'),_l((_$f(2,{forItems:items})),function(item,$10,$20,$30){return _c('li',{key:_$f(2,{forIndex:$20,key:2+'-'+$30})})})],2)}`
)
})
it('generate v-if directive', () => {
assertCodegen(
'<p v-if="show">hello</p>',
`with(this){return (_$i(0,show))?_c('p',{attrs:{"_i":0}}):_e()}`
`with(this){return (_$i(0,show))?_c('p'):_e()}`
)
})
it('generate v-else directive', () => {
assertCodegen(
'<div><p v-if="show">hello</p><p v-else>world</p></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[(_$i(1,show))?_c('p',{attrs:{"_i":1}}):_c('p',{attrs:{"_i":2}})])}`
`with(this){return _c('div',[(_$i(1,show))?_c('p'):_c('p')])}`
)
})
it('generate v-else-if directive', () => {
assertCodegen(
'<div><p v-if="show">hello</p><p v-else-if="hide">world</p></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[(_$i(1,show))?_c('p',{attrs:{"_i":1}}):(_$e(2,hide))?_c('p',{attrs:{"_i":2}}):_e()])}`
`with(this){return _c('div',[(_$i(1,show))?_c('p'):(_$e(2,hide))?_c('p'):_e()])}`
)
})
it('generate v-else-if with v-else directive', () => {
assertCodegen(
'<div><p v-if="show">hello</p><p v-else-if="hide">world</p><p v-else>bye</p></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[(_$i(1,show))?_c('p',{attrs:{"_i":1}}):(_$e(2,hide))?_c('p',{attrs:{"_i":2}}):_c('p',{attrs:{"_i":3}})])}`
`with(this){return _c('div',[(_$i(1,show))?_c('p'):(_$e(2,hide))?_c('p'):_c('p')])}`
)
})
it('generate multi v-else-if with v-else directive', () => {
assertCodegen(
'<div><p v-if="show">hello</p><p v-else-if="hide">world</p><p v-else-if="3">elseif</p><p v-else>bye</p></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[(_$i(1,show))?_c('p',{attrs:{"_i":1}}):(_$e(2,hide))?_c('p',{attrs:{"_i":2}}):(3)?_c('p',{attrs:{"_i":3}}):_c('p',{attrs:{"_i":4}})])}`
`with(this){return _c('div',[(_$i(1,show))?_c('p'):(_$e(2,hide))?_c('p'):(3)?_c('p'):_c('p')])}`
)
})
it('generate ref', () => {
assertCodegen(
'<p ref="component1"></p>',
`with(this){return _c('p',{ref:"component1",attrs:{"_i":0}})}`
`with(this){return _c('p',{ref:"component1"})}`
)
})
it('generate ref on v-for', () => {
assertCodegen(
'<ul><li v-for="item in items" ref="component1"></li></ul>',
`with(this){return _c('ul',{attrs:{"_i":0}},_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return _c('li',{key:_$f(1,{forIndex:$20,key:1+'-'+$30}),ref:"component1",refInFor:true,attrs:{"_i":("1-"+$30)}})}),0)}`
`with(this){return _c('ul',_l((_$f(1,{forItems:items})),function(item,$10,$20,$30){return _c('li',{key:_$f(1,{forIndex:$20,key:1+'-'+$30}),ref:"component1",refInFor:true})}),0)}`
)
})
......@@ -162,35 +162,35 @@ describe('codegen', () => {
it('generate template tag', () => {
assertCodegen(
'<div><template><p>{{hello}}</p></template></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[[_c('p',{attrs:{"_i":2}},[_c('text',{extras:{t0:_s(hello)},attrs:{"_i":2}})])]],2)}`
`with(this){return _c('div',[[_c('p',[_c('text',{extras:{t0:_s(hello)},attrs:{"_i":2}})])]],2)}`
)
})
it('generate single slot', () => {
assertCodegen(
'<div><slot></slot></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_t("default",null,{"_i":1})],2)}`
`with(this){return _c('div',[_t("default",null,{"_i":1})],2)}`
)
})
it('generate named slot', () => {
assertCodegen(
'<div><slot name="one"></slot></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_t("one",null,{"_i":1})],2)}`
`with(this){return _c('div',[_t("one",null,{"_i":1})],2)}`
)
})
it('generate slot fallback content', () => {
assertCodegen(
'<div><slot><div>hi</div></slot></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_t("default",[_c('div',{attrs:{"_i":2}})],{"_i":1})],2)}`
`with(this){return _c('div',[_t("default",[_c('div')],{"_i":1})],2)}`
)
})
it('generate slot target', () => {
assertCodegen(
'<p slot="one">hello world</p>',
`with(this){return _c('p',{attrs:{"_i":0},slot:"one"})}`
`with(this){return _c('p',{slot:"one"})}`
)
})
......@@ -245,7 +245,7 @@ describe('codegen', () => {
// static
assertCodegen(
'<p class="class1">hello world</p>',
`with(this){return _c('p',{attrs:{"_i":0}})}`
`with(this){return _c('p')}`
)
// dynamic
assertCodegen(
......@@ -291,7 +291,7 @@ describe('codegen', () => {
it('generate static attrs', () => {
assertCodegen(
'<input name="field1">',
`with(this){return _c('input',{attrs:{"_i":0}})}`
`with(this){return _c('input')}`
)
})
......@@ -558,7 +558,7 @@ describe('codegen', () => {
it('generate component', () => {
assertCodegen(
'<my-component name="mycomponent1" :msg="msg" @notify="onNotify"><div>hi</div></my-component>',
`with(this){return _c('my-component',{attrs:{"name":"mycomponent1","msg":msg,"_i":0},on:{"notify":onNotify}},[_c('div',{attrs:{"_i":1}})])}`
`with(this){return _c('my-component',{attrs:{"name":"mycomponent1","msg":msg,"_i":0},on:{"notify":onNotify}},[_c('div')])}`
)
})
......@@ -572,16 +572,16 @@ describe('codegen', () => {
it('generate is attribute', () => {
assertCodegen(
'<div is="component1"></div>',
`with(this){return _c("component1",{tag:"div",attrs:{"_i":0}})}`
`with(this){return _c("component1",{tag:"div"})}`
)
assertCodegen(
'<div :is="component1"></div>',
`with(this){return _c(component1,{tag:"div",attrs:{"_i":0}})}`
`with(this){return _c(component1,{tag:"div"})}`
)
// maybe a component and normalize type should be 1
assertCodegen(
'<div><div is="component1"></div></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_c("component1",{tag:"div",attrs:{"_i":1}})],1)}`
`with(this){return _c('div',[_c("component1",{tag:"div"})],1)}`
)
})
......@@ -589,12 +589,12 @@ 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',{attrs:{"_i":1}},[_c('span',{attrs:{"_i":2}})])}}]}})}`
`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')])}}]}})}`
)
// "have inline-template attrs, but not having exactly one child element
assertCodegen(
'<my-component inline-template><hr><hr></my-component>',
`with(this){return _c('my-component',{attrs:{"_i":0},inlineTemplate:{render:function(){with(this){return _c('hr',{attrs:{"_i":1}})}},staticRenderFns:[]}})}`
`with(this){return _c('my-component',{attrs:{"_i":0},inlineTemplate:{render:function(){with(this){return _c('hr')}},staticRenderFns:[]}})}`
)
assertCodegen(
'<my-component inline-template></my-component>',
......@@ -603,11 +603,11 @@ describe('codegen', () => {
// have "is" attribute
assertCodegen(
'<div is="myComponent" inline-template><div></div></div>',
`with(this){return _c("myComponent",{tag:"div",attrs:{"_i":0},inlineTemplate:{render:function(){with(this){return _c('div',{attrs:{"_i":1}})}},staticRenderFns:[]}})}`
`with(this){return _c("myComponent",{tag:"div",inlineTemplate:{render:function(){with(this){return _c('div')}},staticRenderFns:[]}})}`
)
assertCodegen(
'<div is="myComponent" inline-template></div>',
`with(this){return _c("myComponent",{tag:"div",attrs:{"_i":0}})}`
`with(this){return _c("myComponent",{tag:"div"})}`
)
// expect('Inline-template components must have exactly one child element.').toHaveBeenWarned()
// expect(console.error.calls.count()).toBe(3)
......@@ -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',{attrs:{"_i":0}},_l((10),function(i,$10,$20,$30){return _c('div',{attrs:{"_i":("1-"+$30)}},[_m(0,true)])}),0)}`
`with(this){return _c('div',_l((10),function(i,$10,$20,$30){return _c('div',[_m(0,true)])}),0)}`
// [`with(this){return _c('p',[_c('span')])}`]
)
})
......@@ -625,7 +625,7 @@ describe('codegen', () => {
// normalize type: 2
assertCodegen(
'<div><child></child><template v-for="item in list">{{ item }}</template></div>',
`with(this){return _c('div',{attrs:{"_i":0}},[_c('child',{attrs:{"_i":1}}),_l((_$f(2,{forItems:list})),function(item,$10,$20,$30){return [_c('text',{key:_$f(2,{forIndex:$20,keyIndex:0,key:2+'-0'+$30}),extras:{t0:_s(item)},attrs:{"_i":("2-"+$30)}})]})],2)}`
`with(this){return _c('div',[_c('child',{attrs:{"_i":1}}),_l((_$f(2,{forItems:list})),function(item,$10,$20,$30){return [_c('text',{key:_$f(2,{forIndex:$20,keyIndex:0,key:2+'-0'+$30}),extras:{t0:_s(item)},attrs:{"_i":("2-"+$30)}})]})],2)}`
)
})
......@@ -634,7 +634,7 @@ describe('codegen', () => {
// comments: true
// }, baseOptions)
// const template = '<div><!--comment--></div>'
// const generatedCode = `with(this){return _c('div',{attrs:{"_i":0}})}`
// const generatedCode = `with(this){return _c('div')}`
// const ast = parse(template, options)
// optimize(ast, options)
......@@ -648,7 +648,7 @@ describe('codegen', () => {
// comments: true
// }, baseOptions)
// const template = '<div><!--\n\'comment\'\n--></div>'
// const generatedCode = `with(this){return _c('div',{attrs:{"_i":0}})}`
// const generatedCode = `with(this){return _c('div')}`
// const ast = parse(template, options)
// optimize(ast, options)
......@@ -685,7 +685,7 @@ describe('codegen', () => {
it('not specified directives option', () => {
assertCodegen(
'<p v-if="show">hello world</p>',
`with(this){return (_$i(0,show))?_c('p',{attrs:{"_i":0}}):_e()}`, {
`with(this){return (_$i(0,show))?_c('p'):_e()}`, {
// isReservedTag
}
)
......@@ -695,7 +695,7 @@ describe('codegen', () => {
it('should compile single v-for component inside template', () => {
assertCodegen(
`<div><template v-if="ok"><foo v-for="i in 1" :key="i"></foo></template></div>`,
`with(this){return _c('div',{attrs:{"_i":0}},[(_$i(1,ok))?_l((1),function(i,$10,$20,$30){return _c('foo',{key:i,attrs:{"_i":("2-"+$30)}})}):_e()],2)}`
`with(this){return _c('div',[(_$i(1,ok))?_l((1),function(i,$10,$20,$30){return _c('foo',{key:i,attrs:{"_i":("2-"+$30)}})}):_e()],2)}`
)
})
})
......
......@@ -4,7 +4,7 @@ function assertCodegen (template, generatedCode, ...args) {
const compiled = compiler.compile(template, {
mp: {
platform: 'app-plus'
},
},
view: true
})
expect(compiled.render).toBe(generatedCode)
......@@ -16,11 +16,11 @@ describe('codegen', () => {
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}})}`
)
// 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}})}`
)
// 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}})}`
)
})
......@@ -699,4 +699,4 @@ describe('codegen', () => {
)
})
})
/* eslint-enable quotes */
/* eslint-enable quotes */
const compiler = require('../lib')
const res = compiler.compile(
`
<p v-show="shown">hello world</p>
<p slot="one">hello world</p>
`, {
resourcePath: '/User/fxy/Documents/test.wxml',
isReservedTag: function (tag) {
......@@ -13,10 +13,10 @@ const res = compiler.compile(
mp: {
platform: 'app-plus'
},
// service: true,
service: true,
view: true
})
console.log(require('util').inspect(res, {
colors: true,
depth: null
}))
}))
let isPlatformReservedTag
function no (a, b, c) {
return false
}
function isBuiltInTag (tag) {
if (tag === 'slot' || tag === 'component') {
return true
}
}
function isStatic (node) {
if (node.type === 2) {
return false
}
if (node.type === 3) {
return true
}
if (node.dynamicClass || node.dynamicStyle) {
return false
}
return !!(node.pre || (
!node.hasBindings && // no dynamic bindings
!isBuiltInTag(node.tag) && // not a built-in
isPlatformReservedTag(node.tag)
))
}
function markStatic (node) {
if (isStatic(node)) { // 静态节点且仅包含 ID 属性
if (
node.attrs.length === 1 &&
!node.key &&
!node.ref &&
!node.slotTarget
) {
node.plain = true
}
delete node.attrs
}
if (node.type === 1) {
for (let i = 0, l = node.children.length; i < l; i++) {
const child = node.children[i]
markStatic(child)
}
if (node.ifConditions) {
for (let i = 1, l = node.ifConditions.length; i < l; i++) {
const block = node.ifConditions[i].block
markStatic(block)
}
}
}
}
module.exports = function optimize (root, options) {
isPlatformReservedTag = options.isReservedTag || no
markStatic(root)
}
const {
ID,
ITERATOR,
isVar,
isVar,
getForEl,
processForKey,
updateForEleId,
......@@ -18,7 +18,9 @@ const parseBlock = require('./parser/block-parser')
const preTransformNode = require('./pre-transform-node')
function genData (el) {
const optimize = require('./optimizer')
function genData (el) {
delete el.$parentIterator3
const {
......@@ -222,7 +224,7 @@ function processDynamicText (el, state) {
}]
el.hasBindings = true
if (el.text) {
if (el.text) {
// fixed by xxxxxx 注意:保持平台一致性,trim 一下
const ret = parseText(el.text.trim(), false, state)
if (ret && ret.dynamicTexts.length) {
......@@ -296,16 +298,17 @@ function transformNode (el, parent, state) {
processDirs(el)
}
function postTransformNode (el) {
function postTransformNode (el, options) {
if (!el.parent) { // 从根节点开始递归处理
traverseNode(el, false, {
forIteratorId: 0,
transformNode
})
optimize(el, options)
}
}
module.exports = {
preTransformNode,
postTransformNode,
genData
}
}
......@@ -4,7 +4,7 @@ const NUMBER_RE = /^-?\d*(\.\d+)?$/
const ID = '_i'
const ITERATOR1 = '$1'
const ITERATOR2 = '$2'
const ITERATOR3 = '$3'
const ITERATOR3 = '$3'
const DATA_ROOT = '_$g'
const V_FOR = 'f'
......@@ -148,7 +148,7 @@ module.exports = {
V_FOR,
V_IF,
V_ELSE_IF,
ID,
ID,
DATA_ROOT,
isVar,
hasOwn,
......
......@@ -56,18 +56,18 @@ const v3 = {
'../../packages/webpack-uni-app-loader/view/script')
}]
})
}
const entry = {}
if (isAppService) {
entry['app-service'] = path.resolve(process.env.UNI_INPUT_DIR, getMainEntry())
} else if (isAppView) {
entry['app-view'] = path.resolve(process.env.UNI_INPUT_DIR, getMainEntry())
}
}
const entry = {}
if (isAppService) {
entry['app-service'] = path.resolve(process.env.UNI_INPUT_DIR, getMainEntry())
} else if (isAppView) {
entry['app-view'] = path.resolve(process.env.UNI_INPUT_DIR, getMainEntry())
}
return {
devtool,
mode: process.env.NODE_ENV,
mode: isAppView ? 'production' : process.env.NODE_ENV,
externals: {
vue: 'Vue'
},
......@@ -110,8 +110,8 @@ const v3 = {
]
}
},
chainWebpack (webpackConfig, vueOptions) {
webpackConfig.entryPoints.delete('app')
chainWebpack (webpackConfig, vueOptions) {
webpackConfig.entryPoints.delete('app')
const isAppService = !!vueOptions.pluginOptions['uni-app-plus']['service']
const isAppView = !!vueOptions.pluginOptions['uni-app-plus']['view']
......
......@@ -117,8 +117,9 @@ export function registerPage ({
timestamp: Date.now()
}
}, [pageId])
createPage(route, pageId, query, pageInstance).$mount()
try {
createPage(route, pageId, query, pageInstance).$mount()
} catch (e) {}
}
}
......
......@@ -6,9 +6,6 @@ import {
from './constants'
function optimize (k, v) {
if (typeof v === 'undefined') {
return ''
}
if (
k === V_IF ||
k === V_ELSE_IF ||
......
import Vue from 'vue'
import {
hasOwn
}
from 'uni-shared'
import {
PAGE_CREATE,
MOUNTED_DATA,
......@@ -74,16 +69,10 @@ function vdSync ({
}
function getData (id, name, isFallbackContent = false) {
const root = this.$r
if (hasOwn(root, id)) {
const vNodeData = root[id]
if (hasOwn(vNodeData, name)) {
return vNodeData[name]
} else {
!isFallbackContent && console.error(this.$options.__file + `:[${this._$id}]$r[${id}][${name}] is undefined`)
}
} else {
!isFallbackContent && console.error(this.$options.__file + `:[${this._$id}]$r[${id}] is undefined`)
try {
return this.$r[id][name]
} catch (e) {
!isFallbackContent && console.error(this.$options.__file + `:[${this._$id}]$r[${id}][${name}] is undefined`)
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册