From 5518728d9b518daaa51b3e9b67d39f410f974e54 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 23 Oct 2019 16:24:17 +0800 Subject: [PATCH] feat(v3): v-model --- packages/uni-app-plus/dist/index.v3.js | 42 +++----- .../compiler-app-plus.service.spec.js | 102 +++++++++--------- .../uni-template-compiler/__tests__/demo.js | 4 +- .../lib/app/parser/base-parser.js | 8 +- .../lib/app/pre-transform-node.js | 6 +- .../uni-template-compiler/lib/app/service.js | 27 ++--- .../uni-template-compiler/lib/app/util.js | 54 +++++++++- packages/uni-template-compiler/lib/util.js | 7 +- .../packages/vue-template-compiler/browser.js | 4 + .../packages/vue-template-compiler/build.js | 4 + .../lib/platforms/app-plus/index.js | 2 +- .../service/framework/plugins/data.js | 10 +- .../app-plus/service/publish-handler.js | 20 +--- 13 files changed, 160 insertions(+), 130 deletions(-) diff --git a/packages/uni-app-plus/dist/index.v3.js b/packages/uni-app-plus/dist/index.v3.js index 205414055..016548526 100644 --- a/packages/uni-app-plus/dist/index.v3.js +++ b/packages/uni-app-plus/dist/index.v3.js @@ -8598,19 +8598,8 @@ var serviceContext = (function () { } }); - function optimize (k, v) { - if ( - k === V_IF || - k === V_ELSE_IF || - k === V_SHOW - ) { - return v ? 1 : 0 - } - return v - } - function publishHandler (eventType, args, pageIds) { - args = JSON.stringify(args, optimize); + args = JSON.stringify(args); if (process.env.NODE_ENV !== 'production') { console.log(`UNIAPP[publishHandler]:[${+new Date()}]`, eventType, args, pageIds); } @@ -9158,17 +9147,17 @@ var serviceContext = (function () { return result } - function initData (Vue) { + function initData(Vue) { Vue.prototype._$s = setData; Vue.prototype._$i = setIfData; Vue.prototype._$f = setForData; Vue.prototype._$e = setElseIfData; - Vue.prototype._$setData = function setData (type, data) { + Vue.prototype._$setData = function setData(type, data) { this._$vd.push(type, this._$id, data); }; - Vue.prototype._$mounted = function mounted () { + Vue.prototype._$mounted = function mounted() { if (!this._$vd) { return } @@ -9181,7 +9170,7 @@ var serviceContext = (function () { } }; - Vue.prototype._$updated = function updated () { + Vue.prototype._$updated = function updated() { if (!this._$vd) { return } @@ -9199,13 +9188,13 @@ var serviceContext = (function () { }; Object.defineProperty(Vue.prototype, '_$vd', { - get () { + get() { return this.$root._$vdomSync } }); Vue.mixin({ - beforeCreate () { + beforeCreate() { if (this.$options.mpType) { this.mpType = this.$options.mpType; } @@ -9225,7 +9214,7 @@ var serviceContext = (function () { this._$newData = Object.create(null); } }, - beforeUpdate () { + beforeUpdate() { if (!this._$vd) { return } @@ -9234,7 +9223,7 @@ var serviceContext = (function () { console.log(`[${this._$id}] beforeUpdate ` + Date.now()); this._$newData = Object.create(null); }, - beforeDestroy () { + beforeDestroy() { if (!this._$vd) { return } @@ -9244,7 +9233,7 @@ var serviceContext = (function () { }); } - function setData (id, name, value) { + function setData(id, name, value) { switch (name) { case B_CLASS: value = this._$stringifyClass(value); @@ -9252,9 +9241,10 @@ var serviceContext = (function () { case B_STYLE: value = this._$normalizeStyleBinding(value); break - case V_IF: + case V_IF: + case V_SHOW: case V_ELSE_IF: - value = !!value; + value = value ? 1 : 0; break case V_FOR: return setForData.call(this, id, value) @@ -9263,7 +9253,7 @@ var serviceContext = (function () { return ((this._$newData[id] || (this._$newData[id] = {}))[name] = value) } - function setForData (id, value) { + function setForData(id, value) { const diffData = this._$newData[id] || (this._$newData[id] = {}); const vForData = diffData[V_FOR] || (diffData[V_FOR] = []); @@ -9284,11 +9274,11 @@ var serviceContext = (function () { return key } - function setIfData (id, value) { + function setIfData(id, value) { return ((this._$newData[id] || (this._$newData[id] = {}))[V_IF] = !!value) } - function setElseIfData (id, value) { + function setElseIfData(id, value) { return ((this._$newData[id] || (this._$newData[id] = {}))[V_ELSE_IF] = !!value) } diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus.service.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus.service.spec.js index 0dd170219..dbbdabc73 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus.service.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus.service.spec.js @@ -141,21 +141,21 @@ describe('codegen', () => { it('generate v-model directive', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},model:{value:(_$s(0,'v-model',test)),callback:function ($$v) {},expression:"_$s(0,'v-model',test)"}})}` + `with(this){return _c('input',{directives:[{name:"model",rawName:"v-model",value:(test),expression:"test"}],attrs:{"_i":0},domProps:{"value":_$s(0,'v-model',(test))},on:{"input":function($event){if($event.target.composing)return;test=$event.target.value}}})}` ) }) it('generate multiline v-model directive', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},model:{value:(_$s(0,'v-model',\n test \n)),callback:function ($$v) {},expression:"_$s(0,'v-model',\\n test \\n)"}})}` + `with(this){return _c('input',{directives:[{name:"model",rawName:"v-model",value:(\n test \n),expression:"\\n test \\n"}],attrs:{"_i":0},domProps:{"value":_$s(0,'v-model',(\n test \n))},on:{"input":function($event){if($event.target.composing)return;\n test \n=$event.target.value}}})}` ) }) it('generate multiline v-model directive on custom component', () => { assertCodegen( '', - `with(this){return _c('my-component',{attrs:{"_i":0},model:{value:(_$s(0,'v-model',\n test \n)),callback:function ($$v) {},expression:"_$s(0,'v-model',\\n test \\n)"}})}` + `with(this){return _c('my-component',{attrs:{"_i":0},model:{value:_$s(0,'v-model',(\n test \n)),callback:function ($$v) {\n test \n=$$v},expression:"\\n test \\n"}})}` ) }) @@ -272,7 +272,7 @@ describe('codegen', () => { // input + value assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"value":_$s(0,'a-value',msg),"_i":0}})}` + `with(this){return _c('input',{attrs:{"value":_$s(0,'a-value',msg),"_i":0}})}` ) // non input assertCodegen( @@ -284,59 +284,59 @@ describe('codegen', () => { it('generate attrs with v-bind directive', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"name":_$s(0,'a-name',field1),"_i":0}})}` + `with(this){return _c('input',{attrs:{"name":_$s(0,'a-name',field1),"_i":0}})}` ) }) it('generate static attrs', () => { assertCodegen( '', - `with(this){return _c('c-i',{})}` + `with(this){return _c('input',{})}` ) }) it('generate events with v-on directive', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":onInput}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":onInput}})}` ) }) it('generate events with method call', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){return onInput($event);}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){return onInput($event);}}})}` ) // empty arguments assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){return onInput();}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){return onInput();}}})}` ) // without semicolon assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){return onInput($event)}}})}` ) // multiple args assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){return onInput($event, 'abc', 5);}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){return onInput($event, 'abc', 5);}}})}` ) // expression in args assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){return onInput($event, 2+2);}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){return onInput($event, 2+2);}}})}` ) // tricky symbols in args assertCodegen( ``, - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){onInput(');[\\'());');}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){onInput(');[\\'());');}}})}` ) // function name including a `function` part (#9920) // 2.6.10 暂未修复此 bug // assertCodegen( // '', - // `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){return functionName()}}})}` + // `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){return functionName()}}})}` // ) }) @@ -344,64 +344,64 @@ describe('codegen', () => { // normal function assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){onInput1();onInput2()}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){onInput1();onInput2()}}})}` ) // function with multiple args assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){onInput1($event, 'text');onInput2('text2', $event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){onInput1($event, 'text');onInput2('text2', $event)}}})}` ) }) it('generate events with keycode', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return onInput($event)}}})}` ) // multiple keycodes (delete) assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}` ) // multiple keycodes (esc) assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"esc",27,$event.key,["Esc","Escape"]))return null;return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"esc",27,$event.key,["Esc","Escape"]))return null;return onInput($event)}}})}` ) // multiple keycodes (space) assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"space",32,$event.key,[" ","Spacebar"]))return null;return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"space",32,$event.key,[" ","Spacebar"]))return null;return onInput($event)}}})}` ) // multiple keycodes (chained) assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter")&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter")&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}` ) // number keycode assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&$event.keyCode!==13)return null;return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&$event.keyCode!==13)return null;return onInput($event)}}})}` ) // custom keycode assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"custom",undefined,$event.key,undefined))return null;return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"custom",undefined,$event.key,undefined))return null;return onInput($event)}}})}` ) }) it('generate events with generic modifiers', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){$event.stopPropagation();return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){$event.stopPropagation();return onInput($event)}}})}` ) assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){$event.preventDefault();return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){$event.preventDefault();return onInput($event)}}})}` ) assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if($event.target !== $event.currentTarget)return null;return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if($event.target !== $event.currentTarget)return null;return onInput($event)}}})}` ) }) @@ -409,81 +409,81 @@ describe('codegen', () => { it('generate events with generic modifiers and keycode correct order', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.preventDefault();return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.preventDefault();return onInput($event)}}})}` ) assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.stopPropagation();return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.stopPropagation();return onInput($event)}}})}` ) }) it('generate events with mouse event modifiers', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"click":function($event){if(!$event.ctrlKey)return null;return onClick($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"click":function($event){if(!$event.ctrlKey)return null;return onClick($event)}}})}` ) assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"click":function($event){if(!$event.shiftKey)return null;return onClick($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"click":function($event){if(!$event.shiftKey)return null;return onClick($event)}}})}` ) assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"click":function($event){if(!$event.altKey)return null;return onClick($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"click":function($event){if(!$event.altKey)return null;return onClick($event)}}})}` ) assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"click":function($event){if(!$event.metaKey)return null;return onClick($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"click":function($event){if(!$event.metaKey)return null;return onClick($event)}}})}` ) assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"click":function($event){if($event.ctrlKey||$event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"click":function($event){if($event.ctrlKey||$event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick($event)}}})}` ) assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"click":function($event){if(!$event.ctrlKey)return null;if($event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"click":function($event){if(!$event.ctrlKey)return null;if($event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick($event)}}})}` ) }) it('generate events with multiple modifiers', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){$event.stopPropagation();$event.preventDefault();if($event.target !== $event.currentTarget)return null;return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){$event.stopPropagation();$event.preventDefault();if($event.target !== $event.currentTarget)return null;return onInput($event)}}})}` ) }) it('generate events with capture modifier', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"!input":function($event){return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"!input":function($event){return onInput($event)}}})}` ) }) it('generate events with once modifier', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"~input":function($event){return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"~input":function($event){return onInput($event)}}})}` ) }) it('generate events with capture and once modifier', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"~!input":function($event){return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"~!input":function($event){return onInput($event)}}})}` ) }) it('generate events with once and capture modifier', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"~!input":function($event){return onInput($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"~!input":function($event){return onInput($event)}}})}` ) }) it('generate events with inline statement', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){current++}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){current++}}})}` ) }) @@ -491,42 +491,42 @@ describe('codegen', () => { // normal function assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function () { current++ }}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function () { current++ }}})}` ) // normal named function assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function fn () { current++ }}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function fn () { current++ }}})}` ) // arrow with no args assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":()=>current++}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":()=>current++}})}` ) // arrow with parens, single arg assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":(e) => current++}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":(e) => current++}})}` ) // arrow with parens, multi args assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":(a, b, c) => current++}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":(a, b, c) => current++}})}` ) // arrow with destructuring assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":({ a, b }) => current++}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":({ a, b }) => current++}})}` ) // arrow single arg no parens assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":e=>current++}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":e=>current++}})}` ) // with modifiers assertCodegen( ``, - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"keyup":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return (e=>current++)($event)}}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"keyup":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return (e=>current++)($event)}}})}` ) }) @@ -534,14 +534,14 @@ describe('codegen', () => { it('should not treat handler with unexpected whitespace as inline statement', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":onInput}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":onInput}})}` ) }) it('generate unhandled events', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){current++}}})}`, + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){current++}}})}`, ast => { ast.events.input = undefined } @@ -551,7 +551,7 @@ describe('codegen', () => { it('generate multiple event handlers', () => { assertCodegen( '', - `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":[function($event){current++},function($event){$event.stopPropagation();return onInput($event)}]}})}` + `with(this){return _c('input',{attrs:{"_i":0},on:{"input":[function($event){current++},function($event){$event.stopPropagation();return onInput($event)}]}})}` ) }) diff --git a/packages/uni-template-compiler/__tests__/demo.js b/packages/uni-template-compiler/__tests__/demo.js index 88c8eaff9..6d1cb2d7a 100644 --- a/packages/uni-template-compiler/__tests__/demo.js +++ b/packages/uni-template-compiler/__tests__/demo.js @@ -2,6 +2,8 @@ const compiler = require('../lib') const res = compiler.compile( ` + + `, { resourcePath: '/User/fxy/Documents/test.wxml', isReservedTag: function (tag) { @@ -19,4 +21,4 @@ const res = compiler.compile( console.log(require('util').inspect(res, { colors: true, depth: null -})) +})) diff --git a/packages/uni-template-compiler/lib/app/parser/base-parser.js b/packages/uni-template-compiler/lib/app/parser/base-parser.js index cd58a8983..426ac5313 100644 --- a/packages/uni-template-compiler/lib/app/parser/base-parser.js +++ b/packages/uni-template-compiler/lib/app/parser/base-parser.js @@ -32,10 +32,12 @@ function parseBinding (el, genVar) { el.styleBinding && (el.styleBinding = genVar('s', el.styleBinding)) } -function parseDirs (el, genVar) { +function parseDirs (el, genVar, ignoreDirs = []) { el.directives && el.directives.forEach(dir => { - dir.value && (dir.value = genVar('v-' + dir.name, dir.value)) - dir.isDynamicArg && (dir.arg = genVar('v-' + dir.name + '-arg', dir.arg)) + 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)) + } }) } diff --git a/packages/uni-template-compiler/lib/app/pre-transform-node.js b/packages/uni-template-compiler/lib/app/pre-transform-node.js index 6b348feb2..b90234f27 100644 --- a/packages/uni-template-compiler/lib/app/pre-transform-node.js +++ b/packages/uni-template-compiler/lib/app/pre-transform-node.js @@ -1,14 +1,14 @@ const { ID, hasOwn, - addAttr + addRawAttr } = require('./util') -module.exports = function preTransformNode (el, options) { +module.exports = function preTransformNode (el, options) { if (!hasOwn(options, 'nid')) { options.nid = 0 } - addAttr(el, ID, options.nid++) + addRawAttr(el, ID, options.nid++) if (el.attrsMap['v-for']) { el.forId = el.attrsMap[ID] } diff --git a/packages/uni-template-compiler/lib/app/service.js b/packages/uni-template-compiler/lib/app/service.js index 7027e1351..7bd9b502b 100644 --- a/packages/uni-template-compiler/lib/app/service.js +++ b/packages/uni-template-compiler/lib/app/service.js @@ -26,7 +26,7 @@ const { const parseEvent = require('./parser/event-parser') const parseBlock = require('./parser/block-parser') -const basePreTransformNode = require('./pre-transform-node') +const preTransformNode = require('./pre-transform-node') const optimize = require('./optimizer') @@ -87,7 +87,7 @@ function transformNode (el, parent, state) { parseIf(el, createGenVar) parseBinding(el, genVar) - parseDirs(el, genVar) + parseDirs(el, genVar, ['model']) if (!isComponent(el.tag)) { parseAttrs(el, genVar) @@ -106,27 +106,30 @@ function postTransformNode (el, options) { } } -function parseTag (el) { - if (el.tag === 'input' || el.tag === 'textarea') { - el.tag = `c-${el.tag.substr(0, 1)}` // 返回一个自定义组件标签,保证 v-model +function genVModel (el) { + if ( + (el.tag === 'input' || el.tag === 'textarea') && + el.directives && + el.directives.find(dir => dir.name === 'model') + ) { + const prop = el.props.find(prop => prop.name === 'value') + prop.value = createGenVar(el.attrsMap[ID])('v-model', prop.value) + } + if (el.model) { + el.model.value = createGenVar(el.attrsMap[ID])('v-model', el.model.value) } } function genData (el) { delete el.$parentIterator3 - if (el.model) { - el.model.callback = `function ($$v) {}` - } + genVModel(el) return '' } module.exports = { - preTransformNode: function (el, options) { - parseTag(el) - return basePreTransformNode(el, options) - }, + preTransformNode, postTransformNode, genData } diff --git a/packages/uni-template-compiler/lib/app/util.js b/packages/uni-template-compiler/lib/app/util.js index f17446e35..fc083a2c8 100644 --- a/packages/uni-template-compiler/lib/app/util.js +++ b/packages/uni-template-compiler/lib/app/util.js @@ -28,7 +28,7 @@ function isVar (str) { return true } -function addAttr (el, name, value) { +function addRawAttr (el, name, value) { el.attrsMap[name] = value el.attrsList.push({ name, @@ -42,7 +42,7 @@ function updateEleId (el, it, state) { } const id = el.attrsMap[ID] const newId = Number.isInteger(id) ? `("${id}-"+${it})` : `(${id}+${it})` - addAttr(el, ID, newId) + addRawAttr(el, ID, newId) const attr = el.attrs.find(attr => attr.name === ID) attr.value = newId el.children.forEach(child => { @@ -121,7 +121,7 @@ function processForKey (el) { const keyIndex = forEl.children.indexOf(el) el.key = `${forEl.forId}+'-${keyIndex}'+${it}` } else { // 当 template 下只有文本节点 - if (el.children && el.children.length && !el.children.find(child => child.key)) { + if (el.children && el.children.length && !el.children.find(child => child.key)) { el.children[0].parent = el el.children[0].key = `${forEl.forId}+'-0'+${it}` return true @@ -146,17 +146,63 @@ function traverseNode (el, parent, state) { el.scopedSlots && Object.values(el.scopedSlots).forEach(slot => traverseNode(slot, el, state)) } +function addAttr (el, name, value, dynamic) { + const attrs = dynamic + ? (el.dynamicAttrs || (el.dynamicAttrs = [])) + : (el.attrs || (el.attrs = [])) + attrs.push({ + name, + value, + dynamic + }) + el.plain = false +} + +function removeRawAttr (el, name) { + delete el.attrsMap[name] + const index = el.attrsList.findIndex(attr => attr.name === name) + index !== -1 && el.attrsList.splice(index, 1) +} + +function removeRawBindingAttr (el, name) { + removeRawAttr(el, ':' + name) + removeRawAttr(el, 'v-bind:' + name) + console.log(el) + throw new Error('123') +} + +function addHandler (el, name, value, important) { + const events = el.events || (el.events = {}) + const handlers = events[name] + const newHandler = { + value: value.trim(), + dynamic: undefined + } + if (Array.isArray(handlers)) { + important ? handlers.unshift(newHandler) : handlers.push(newHandler) + } else if (handlers) { + events[name] = important ? [newHandler, handlers] : [handlers, newHandler] + } else { + events[name] = newHandler + } + el.plain = false +} + module.exports = { V_FOR, V_IF, V_ELSE_IF, - ID, + ID, SET_DATA, GET_DATA, isVar, hasOwn, addAttr, + addRawAttr, + removeRawAttr, + removeRawBindingAttr, getForEl, + addHandler, processForKey, updateForEleId, getBindingAttr, diff --git a/packages/uni-template-compiler/lib/util.js b/packages/uni-template-compiler/lib/util.js index 267808c5b..563b4d9c3 100644 --- a/packages/uni-template-compiler/lib/util.js +++ b/packages/uni-template-compiler/lib/util.js @@ -183,12 +183,7 @@ const { } = require('./h5') function isComponent (tagName) { - if ( - tagName === 'block' || - tagName === 'template' || - tagName === 'c-i' || // v3 service input => c-i - tagName === 'c-t' // v3 service textarea => c-t - ) { + if (tagName === 'block' || tagName === 'template') { return false } return !hasOwn(tags, getTagName(tagName.replace('v-uni-', ''))) diff --git a/packages/vue-cli-plugin-uni/packages/vue-template-compiler/browser.js b/packages/vue-cli-plugin-uni/packages/vue-template-compiler/browser.js index 83dd5cd15..9dc3ced50 100644 --- a/packages/vue-cli-plugin-uni/packages/vue-template-compiler/browser.js +++ b/packages/vue-cli-plugin-uni/packages/vue-template-compiler/browser.js @@ -3696,6 +3696,10 @@ return } + if(process.env.UNI_PLATFORM !== 'h5'){ // fixed by xxxxxx 非 h5 平台 type 不会是 checkbox,radio + return + } + var typeBinding; if (map[':type'] || map['v-bind:type']) { typeBinding = getBindingAttr(el, 'type'); diff --git a/packages/vue-cli-plugin-uni/packages/vue-template-compiler/build.js b/packages/vue-cli-plugin-uni/packages/vue-template-compiler/build.js index 3e98e934c..a939b262a 100644 --- a/packages/vue-cli-plugin-uni/packages/vue-template-compiler/build.js +++ b/packages/vue-cli-plugin-uni/packages/vue-template-compiler/build.js @@ -3318,6 +3318,10 @@ function preTransformNode (el, options) { return } + if(process.env.UNI_PLATFORM !== 'h5'){ // fixed by xxxxxx 非 h5 平台 type 不会是 checkbox,radio + return + } + var typeBinding; if (map[':type'] || map['v-bind:type']) { typeBinding = getBindingAttr(el, 'type'); diff --git a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js index 6991efee1..40d8ee95c 100644 --- a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js +++ b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js @@ -213,7 +213,7 @@ module.exports = function (pagesJson, userManifestJson) { let flexDir = false - if (manifestJson.plus.nvueCompiler && manifestJson.plus.nvueCompiler === 'uni-app') { + if (process.env.UNI_USING_NVUE_COMPILER) { appJson.nvueCompiler = 'uni-app' flexDir = getFlexDirection(manifestJson.plus) } else { diff --git a/src/platforms/app-plus/service/framework/plugins/data.js b/src/platforms/app-plus/service/framework/plugins/data.js index e1396f59b..74cc6fd38 100644 --- a/src/platforms/app-plus/service/framework/plugins/data.js +++ b/src/platforms/app-plus/service/framework/plugins/data.js @@ -14,7 +14,8 @@ import { import { V_IF, - V_FOR, + V_FOR, + V_SHOW, V_ELSE_IF, B_CLASS, B_STYLE @@ -110,7 +111,7 @@ export function initData (Vue) { }) } -function setData (id, name, value) { +function setData (id, name, value) { switch (name) { case B_CLASS: value = this._$stringifyClass(value) @@ -118,9 +119,10 @@ function setData (id, name, value) { case B_STYLE: value = this._$normalizeStyleBinding(value) break - case V_IF: + case V_IF: + case V_SHOW: case V_ELSE_IF: - value = !!value + value = value ? 1 : 0 break case V_FOR: return setForData.call(this, id, value) diff --git a/src/platforms/app-plus/service/publish-handler.js b/src/platforms/app-plus/service/publish-handler.js index ed51a512b..715d46c02 100644 --- a/src/platforms/app-plus/service/publish-handler.js +++ b/src/platforms/app-plus/service/publish-handler.js @@ -1,23 +1,5 @@ -import { - V_IF, - V_ELSE_IF, - V_SHOW -} - from './constants' - -function optimize (k, v) { - if ( - k === V_IF || - k === V_ELSE_IF || - k === V_SHOW - ) { - return v ? 1 : 0 - } - return v -} - export function publishHandler (eventType, args, pageIds) { - args = JSON.stringify(args, optimize) + args = JSON.stringify(args) if (process.env.NODE_ENV !== 'production') { console.log(`UNIAPP[publishHandler]:[${+new Date()}]`, eventType, args, pageIds) } -- GitLab