diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index e8bc904d73bea5b90344c29c066c39eed5fe629c..b720fe7789c7512fbf28d141ca0fb6a5a5bc8d31 100644 --- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js @@ -553,22 +553,22 @@ describe('mp:compiler-extra', () => { assertCodegen( '{{handle(item)}}', '{{item.m0}}', - 'with(this){var l0=__map(list,function(item,index){var m0=handle(item);return{$orig:__get_orig(item),m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' + 'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=handle(item);return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' ) assertCodegen( '{{handle(item)}}{{item.title}}', '{{item.m0+item.$orig.title}}', - 'with(this){var l0=__map(list,function(item,index){var m0=handle(item);return{$orig:__get_orig(item),m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' + 'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=handle(item);return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' ) assertCodegen( '{{handle(item1)}}{{item1.title}}', '{{item1.m0+item1.$orig.title}}', - 'with(this){var l1=__map(list,function(item,index){var l0=__map(list1,function(item1,index1){var m0=handle(item1);return{$orig:__get_orig(item1),m0:m0}});return{$orig:__get_orig(item),l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' + 'with(this){var l1=__map(list,function(item,index){var $orig=__get_orig(item);var l0=__map(list1,function(item1,index1){var $orig=__get_orig(item1);var m0=handle(item1);return{$orig:$orig,m0:m0}});return{$orig:$orig,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' ) assertCodegen( 'title: {{ section.title|prefix }}{{ sub_titles|prefix }}', '{{"title: "+section.f0}}{{sub_titles.f1}}', - 'with(this){var l1=__map(sections,function(section,index){var f0=_f("prefix")(section.title);var l0=__map(section.sub_titles,function(sub_titles,_index){var f1=_f("prefix")(sub_titles);return{$orig:__get_orig(sub_titles),f1:f1}});return{$orig:__get_orig(section),f0:f0,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' + 'with(this){var l1=__map(sections,function(section,index){var $orig=__get_orig(section);var f0=_f("prefix")(section.title);var l0=__map(section.sub_titles,function(sub_titles,_index){var $orig=__get_orig(sub_titles);var f1=_f("prefix")(sub_titles);return{$orig:$orig,f1:f1}});return{$orig:$orig,f0:f0,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' ) assertCodegen( @@ -579,12 +579,12 @@ describe('mp:compiler-extra', () => { assertCodegen( '{{item.item.id | test | test1}}', '{{item.f0}}', - 'with(this){var l0=__map(list,function(item,index){var f0=_f("test1")(_f("test")(item.item.id));return{$orig:__get_orig(item),f0:f0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' + 'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var f0=_f("test1")(_f("test")(item.item.id));return{$orig:$orig,f0:f0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' ) assertCodegen( '{{ item.split(\'\').join(\' \') }}', '{{item.g0}}', - 'with(this){var l0=__map(list,function(item,i){var g0=item.split("").join(" ");return{$orig:__get_orig(item),g0:g0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' + 'with(this){var l0=__map(list,function(item,i){var $orig=__get_orig(item);var g0=item.split("").join(" ");return{$orig:$orig,g0:g0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' ) assertCodegen( ` @@ -595,7 +595,7 @@ describe('mp:compiler-extra', () => { `, '{{\'\'+item2.m0+\'\'}}', - 'with(this){var l1=__map(tabList,function(item,index){var l0=__map(list[item.key],function(item2,index2){var m0=formatIt(item2.id);return{$orig:__get_orig(item2),m0:m0}});return{$orig:__get_orig(item),l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' + 'with(this){var l1=__map(tabList,function(item,index){var $orig=__get_orig(item);var l0=__map(list[item.key],function(item2,index2){var $orig=__get_orig(item2);var m0=formatIt(item2.id);return{$orig:$orig,m0:m0}});return{$orig:$orig,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' ) }) @@ -676,4 +676,21 @@ describe('mp:compiler-extra', () => { '' ) }) + it('generate v-if', () => { + assertCodegen( + '{{getValue(key)}}', + '{{$root.m0}}', + 'with(this){var m0=show?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0}})}' + ) + assertCodegen( + '{{getValue(key)}}', + '{{$root.m1}}', + 'with(this){var m0=getValue(key);var m1=m0?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}' + ) + assertCodegen( + '{{getValue(item)}}', + '{{item.m0}}', + 'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=item?getValue(item):null;return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' + ) + }) }) diff --git a/packages/uni-template-compiler/lib/script/traverse/member-expr.js b/packages/uni-template-compiler/lib/script/traverse/member-expr.js index 9678461d8e57cef35bc66c65e7ce2949dda44300..06e16d73a345833b9f8f3ee335cbac447fd997cc 100644 --- a/packages/uni-template-compiler/lib/script/traverse/member-expr.js +++ b/packages/uni-template-compiler/lib/script/traverse/member-expr.js @@ -1,4 +1,9 @@ const t = require('@babel/types') +const traverse = require('@babel/traverse').default + +const { + VAR_ROOT +} = require('../../constants') function isMatch (name, forItem, forIndex) { return name === forItem || name === forIndex @@ -33,8 +38,36 @@ function findScoped (path, state) { return scoped || state } +function findTest (path, state) { + let tests + while (path.parentPath && path.key !== 'body') { + if (path.key === 'consequent' || path.key === 'alternate') { + let test = t.arrayExpression([t.clone(path.container.test)]) + traverse(test, { + noScope: true, + MemberExpression (path) { + const names = state.scoped.map(scoped => scoped.forItem) + const node = path.node + const objectName = node.object.name + if (objectName === VAR_ROOT || names.includes(objectName)) { + path.replaceWith(node.property) + } + } + }) + test = test.elements[0] + if (path.key === 'alternate') { + test = t.unaryExpression('!', test) + } + tests = tests ? t.logicalExpression('&&', test, tests) : test + } + path = path.parentPath + } + return tests +} + module.exports = function getMemberExpr (path, name, init, state, variableDeclaration = true) { const scoped = findScoped(path, state) + const test = findTest(path, state) if (!variableDeclaration) { scoped.declarationArray.push(t.expressionStatement(init)) @@ -45,7 +78,7 @@ module.exports = function getMemberExpr (path, name, init, state, variableDeclar scoped.propertyArray.push(t.objectProperty(identifier, identifier)) scoped.declarationArray.push( - t.variableDeclaration('var', [t.variableDeclarator(identifier, init)]) + t.variableDeclaration('var', [t.variableDeclarator(identifier, test ? t.conditionalExpression(test, init, t.nullLiteral()) : init)]) ) state.identifierArray.push(identifier) diff --git a/packages/uni-template-compiler/lib/script/traverse/statements.js b/packages/uni-template-compiler/lib/script/traverse/statements.js index ea53c462eb86b56a18a3826cbc1785f433a190c7..8d2725c29a061c31f7cad4c7eaface48c4e8a443 100644 --- a/packages/uni-template-compiler/lib/script/traverse/statements.js +++ b/packages/uni-template-compiler/lib/script/traverse/statements.js @@ -43,20 +43,24 @@ function getMapCallExpression ( forIndex ) { const blockStatement = [] + // var $orgi = __get_orig(forItem) + blockStatement.push(t.variableDeclaration('var', [ + t.variableDeclarator(t.identifier(VAR_ORIGINAL), t.callExpression(t.identifier(INTERNAL_GET_ORIG), [ + t.identifier(forItem) + ])) + ])) if (declarationArray.length) { declarationArray.forEach(declaration => { blockStatement.push(declaration) }) blockStatement.push(t.returnStatement( - // return {$orgi:__get_orig(forItem)} + // return {$orgi:$orgi} t.objectExpression( [ t.objectProperty( t.identifier(VAR_ORIGINAL), - t.callExpression(t.identifier(INTERNAL_GET_ORIG), [ - t.identifier(forItem) - ]) + t.identifier(VAR_ORIGINAL) ) ].concat(objectPropertyArray) ) @@ -116,4 +120,4 @@ module.exports = { getMapCallExpression, getDataExpressionStatement, getEventExpressionStatement -} +} diff --git a/src/platforms/h5/view/components/ad/index.vue b/src/platforms/h5/view/components/ad/index.vue index f3c9514a4321a0552e28be0354cf8737227fb48a..7c8e909967c9e4232b0de417359ee4186daeeba2 100644 --- a/src/platforms/h5/view/components/ad/index.vue +++ b/src/platforms/h5/view/components/ad/index.vue @@ -24,6 +24,9 @@ class AdConfig { return this._instance } + static IC = 0 + static IS = 0 + constructor () { this._instance = null this._adConfig = null @@ -55,6 +58,7 @@ class AdConfig { } get (adpid, success, fail) { + AdConfig.IC++ if (this._adConfig != null) { this._doCallback(adpid, success, fail) if (this.isExpired) { @@ -73,6 +77,7 @@ class AdConfig { } _doCallback (adpid, success, fail) { + AdConfig.IS++ var data = this._adConfig if (data[adpid]) { success(data[adpid]) @@ -267,7 +272,9 @@ export default { AdReport.instance.get({ h: __uniConfig.compilerVersion, a: this.adpid, - at: 30 + at: 30, + ic: AdConfig.IC, + is: AdConfig.IS }) }, beforeDestroy () { @@ -338,11 +345,16 @@ export default { this._startCheckTimer() }, _renderKY (data) { + var randomId = this._randomId() var ad = document.createElement('script') - ad.src = data.src || data.url + ad.src = (data.src || data.url) + '&_ct=' + randomId + + var adView = document.createElement('div') + adView.setAttribute('id', randomId) + adView.appendChild(ad) this.$refs.container.innerHTML = '' - this.$refs.container.append(ad) + this.$refs.container.append(adView) this._startCheckTimer() }, @@ -390,6 +402,13 @@ export default { t: taskId, at: type }) + }, + _randomId () { + var result = '' + for (let i = 0; i < 2; i++) { + result += (65536 * (1 + Math.random()) | 0).toString(16).substring(1) + } + return result } } }