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 038465efea630864edf52e64fc55be0040bde040..5db21b7a19d93aff1c6977a89a6706c47d33ed8d 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 @@ -28,7 +28,7 @@ describe('codegen', () => { ) assertCodegen( '
{{item}}
', - `with(this){return _c('div',[_l((_$s(1,'f',{forItems:list})),function(item,index,$20,$30){return [[_c('text',{key:_$s(("3-"+$30),'a-key',index+'_0'+'_0')},[_v((_$s(("3-"+$30),'t0',_s(item))))])]]})],2)}` + `with(this){return _c('div',[_l((_$s(1,'f',{forItems:list,fill:true})),function(item,index,$20,$30){return [[_c('text',{key:_$s(("3-"+$30),'a-key',index+'_0'+'_0')},[_v((_$s(("3-"+$30),'t0',_s(item))))])]]})],2)}` ) }) it('generate directive', () => { diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index 176a3a263d85d7b1ac69971a065a144153e9a496..814fe3769bb3126508afb025590bd051889fad36 100644 --- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js @@ -194,15 +194,15 @@ describe('mp:compiler-extra', () => { it('generate default slot', () => { assertCodegen( 'text', - `text` + `text` ) assertCodegen( 'text123213', - `text123213` + `text123213` ) assertCodegen( 'text', - `text` + `text` ) }) @@ -221,21 +221,21 @@ describe('mp:compiler-extra', () => { assertCodegen( '默认', - `fc默认` + `fc默认` ) assertCodegen( 'text', - `text` + `text` ) assertCodegen( 'text123213', - `text123213` + `text123213` ) assertCodegen( '', - `` + `` ) assertCodegen( ` @@ -247,7 +247,7 @@ describe('mp:compiler-extra', () => {

Here's some contact info

`, - `Here might be a page titleHere's some contact infoA paragraph for the main content.` + `Here might be a page titleHere's some contact infoA paragraph for the main content.` ) }) @@ -429,41 +429,41 @@ describe('mp:compiler-extra', () => { it('generate events with v-on directive on custom component', () => { assertCodegen( '', - `` + `` ) assertCodegen( '', - `` + `` ) }) it('generate v-model directive on custom component', () => { assertCodegen( '1', - `1` + `1` ) assertCodegen( '2', - `2` + `2` ) assertCodegen( '3', - `3` + `3` ) assertCodegen( '4', - `4` + `4` ) assertCodegen( '4', - `4` + `4` ) }) it('generate object property on custom component', () => { assertCodegen( '', - `` + `` ) }) it('generate v-text directive', () => { @@ -490,23 +490,23 @@ describe('mp:compiler-extra', () => { it('generate v-bind directive with sync modifier', () => { assertCodegen( '', - `` + `` ) assertCodegen( '', - `` + `` ) assertCodegen( '', - `` + `` ) assertCodegen( '', - `` + `` ) assertCodegen( '', - `` + `` ) }) diff --git a/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js b/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js index feb5ad4010b9210016a5a2deabb9b9577994e21d..26d12f14e8288a9a7ac59a75bc29e833f1ea3a3f 100644 --- a/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js @@ -31,33 +31,33 @@ describe('mp:compiler-mp-alipay', () => { it('generate ref', () => { assertCodegen( 'text', - `text` + `text` ) assertCodegen( 'text123213', - `text123213` + `text123213` ) assertCodegen( '', - `` + `` ) assertCodegen( '', - `` + `` ) }) it('generate default slot', () => { assertCodegen( 'text', - `text` + `text` ) assertCodegen( 'text123213', - `text123213` + `text123213` ) assertCodegen( 'text', - `text` + `text` ) }) it('generate class binding', () => { @@ -114,7 +114,7 @@ describe('mp:compiler-mp-alipay', () => { it('generate events with v-on directive', () => { assertCodegen( ``, - ``, + ``, `with(this){var a0={color:"#4cd964",size:"22",type:"spinner"};$mp.data=Object.assign({},{$root:{a0:a0}})}` ) diff --git a/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js b/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js index d3e06222cc413bba4070b0c4b3887d0e29c15350..5540e69bebc82e65dc657160d06c01e4b7b7c9de 100644 --- a/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js @@ -24,33 +24,33 @@ describe('mp:compiler-mp-baidu', () => { it('generate scoped slot', () => { assertCodegen( '', - `{{foo}}` + `{{foo}}` ) assertCodegen( '{{ bar.foo }}', - `{{foo}}` + `{{foo}}` ) }) it('generate named scoped slot', () => { assertCodegen( '', - `{{foo}}` + `{{foo}}` ) assertCodegen( '{{ bar.foo }}', - `{{foo}}` + `{{foo}}` ) }) it('generate scoped slot with multiline v-if', () => { assertCodegen( '', - `{{foo}}` + `{{foo}}` ) assertCodegen( '{{ bar.foo }}', - `{{foo}}` + `{{foo}}` ) }) @@ -68,23 +68,23 @@ describe('mp:compiler-mp-baidu', () => { it('generate vue id', () => { assertCodegen( '', - `` + `` ) assertCodegen( '', - `` + `` ) assertCodegen( '', - `` + `` ) assertCodegen( '', - `` + `` ) assertCodegen( '', - `` + `` ) }) diff --git a/packages/uni-template-compiler/__tests__/compiler-mp-toutiao.spec.js b/packages/uni-template-compiler/__tests__/compiler-mp-toutiao.spec.js index 0a22563b446cb24dc5191f05dea61535b47d0dfb..0fdd0ea9d732febb4680ec8cadc6a018a2d3bb90 100644 --- a/packages/uni-template-compiler/__tests__/compiler-mp-toutiao.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-mp-toutiao.spec.js @@ -25,7 +25,7 @@ describe('mp:compiler-mp-toutiao', () => { it('generate ref', () => { assertCodegen( '', - `` + `` ) }) diff --git a/packages/uni-template-compiler/__tests__/compiler-mp-weixin.spec.js b/packages/uni-template-compiler/__tests__/compiler-mp-weixin.spec.js index 8e88ae670414426b7018ecad50d62f3b14da4755..135de1667a4eddd4fd0e7022061e55d5d89e405f 100644 --- a/packages/uni-template-compiler/__tests__/compiler-mp-weixin.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-mp-weixin.spec.js @@ -23,14 +23,14 @@ describe('mp:compiler-mp-weixin', () => { it('generate scoped slot', () => { assertCodegen( '', - ``, + ``, function (res) { expect(res.generic[0]).toBe('test-foo-default') } ) assertCodegen( '{{ bar.foo }}', - ``, + ``, function (res) { expect(res.generic[0]).toBe('test-foo-default') } @@ -40,14 +40,14 @@ describe('mp:compiler-mp-weixin', () => { it('generate named scoped slot', () => { assertCodegen( '', - ``, + ``, function (res) { expect(res.generic[0]).toBe('test-foo-foo') } ) assertCodegen( '{{ bar.foo }}', - ``, + ``, function (res) { expect(res.generic[0]).toBe('test-foo-foo') } @@ -57,14 +57,14 @@ describe('mp:compiler-mp-weixin', () => { it('generate scoped slot with multiline v-if', () => { assertCodegen( '', - ``, + ``, function (res) { expect(res.generic[0]).toBe('test-foo-default') } ) assertCodegen( '{{ bar.foo }}', - ``, + ``, function (res) { expect(res.generic[0]).toBe('test-foo-foo') } @@ -90,7 +90,7 @@ describe('mp:compiler-mp-weixin', () => { it('generate page-meta', () => { assertCodegen(// TODO vue-id '', - `` + `` ) }) }) diff --git a/packages/uni-template-compiler/__tests__/compiler.spec.js b/packages/uni-template-compiler/__tests__/compiler.spec.js index 2c93123110058ada03fb549166edf616d652469d..418a276707cf732e2cc6383321b6056c0ffad71d 100644 --- a/packages/uni-template-compiler/__tests__/compiler.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler.spec.js @@ -159,7 +159,7 @@ describe('mp:compiler', () => { it('generate multiline v-model directive on custom component', () => { assertCodegen( '', - `` + `` ) }) @@ -558,7 +558,7 @@ describe('mp:compiler', () => { it('generate component', () => { assertCodegen( '
hi
', - `hi` + `hi` // `with(this){if(!$mp.events){$mp.events=__get_event({"e0":{on:{"notify":onNotify},component:true}})}}` ) }) @@ -573,7 +573,7 @@ describe('mp:compiler', () => { it('generate is attribute', () => { assertCodegen( '
', - '' + '' ) // assertCodegen( // '
', @@ -582,7 +582,7 @@ describe('mp:compiler', () => { // maybe a component and normalize type should be 1 assertCodegen( '
', - '' + '' ) }) @@ -619,7 +619,7 @@ describe('mp:compiler', () => { // normalize type: 2 assertCodegen( '
', - `{{item}}` + `{{item}}` ) }) @@ -662,7 +662,7 @@ describe('mp:compiler', () => { it('should compile single v-for component inside template', () => { assertCodegen( `
`, - `` + `` ) }) }) diff --git a/packages/uni-template-compiler/__tests__/demo.js b/packages/uni-template-compiler/__tests__/demo.js index d4febd8180062e3e459203fcead60d5a40702292..88d2fae464a58d3e56098a173f576939f9fdc62d 100644 --- a/packages/uni-template-compiler/__tests__/demo.js +++ b/packages/uni-template-compiler/__tests__/demo.js @@ -19,7 +19,12 @@ const scopedPath = path.resolve(__dirname, '../../') const compiler = require('../lib') const res = compiler.compile( ` -{{item}} + + + + + + `, { miniprogram: true, resourcePath: '/User/fxy/Documents/test.wxml', @@ -32,9 +37,9 @@ const res = compiler.compile( mp: { platform: 'mp-weixin' }, - filterModules: ['swipe'], - service: true, - view: true + filterModules: ['swipe'] + // service: true, + // view: true }) console.log(require('util').inspect(res, { diff --git a/packages/uni-template-compiler/lib/index.js b/packages/uni-template-compiler/lib/index.js index 904536958326df78af65fd78bd9bd5c483637be8..ff13c3db73f05cee8ba2fff9694fd4dc86b3959d 100644 --- a/packages/uni-template-compiler/lib/index.js +++ b/packages/uni-template-compiler/lib/index.js @@ -1,5 +1,5 @@ const path = require('path') - +const hash = require('hash-sum') const parser = require('@babel/parser') const { @@ -95,6 +95,11 @@ module.exports = { options.mp.scopeId = options.scopeId options.mp.resourcePath = options.resourcePath + if (options.resourcePath) { + options.mp.hashId = hash(options.resourcePath) + } else { + options.mp.hashId = '' + } options.mp.globalUsingComponents = options.globalUsingComponents || Object.create(null) diff --git a/packages/uni-template-compiler/lib/script/traverse/visitor.js b/packages/uni-template-compiler/lib/script/traverse/visitor.js index 0dd5124452fb2beada2a8a691ece782c4d244477..f7bbb7c12beb23a36be7bec33055d85591cb14a2 100644 --- a/packages/uni-template-compiler/lib/script/traverse/visitor.js +++ b/packages/uni-template-compiler/lib/script/traverse/visitor.js @@ -58,7 +58,8 @@ function addVueId (path, state) { if (!state.options.hasOwnProperty('$vueId')) { state.options.$vueId = 1 } - const vueId = String(state.options.$vueId++) + const hashId = state.options.hashId + const vueId = String((hashId ? (hashId + '-') : '') + (state.options.$vueId++)) let value