提交 761437f6 编写于 作者: 朱立行

fix(uni-mp-xhs): 修复小红书小程序Page和自定义组件问题

上级 27ead72a
...@@ -11,9 +11,9 @@ describe('mp-xhs: transform component', () => { ...@@ -11,9 +11,9 @@ describe('mp-xhs: transform component', () => {
) )
assert( assert(
`<textarea @input="input"></textarea>`, `<textarea @input="input"></textarea>`,
`<block xhs:if="{{r0}}"><textarea bindinput="__e" data-e-o="{{a}}"></textarea></block>`, `<block xhs:if="{{r0}}"><textarea bindinput="{{a}}"></textarea></block>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return { a: { 'input': _o(_ctx.input) } } return { a: _o(_ctx.input) }
}` }`
) )
assert( assert(
...@@ -25,9 +25,9 @@ describe('mp-xhs: transform component', () => { ...@@ -25,9 +25,9 @@ describe('mp-xhs: transform component', () => {
) )
assert( assert(
`<textarea v-if="ok1" @input="input"/><textarea v-else-if="ok2"/><textarea v-else @input="input"/>`, `<textarea v-if="ok1" @input="input"/><textarea v-else-if="ok2"/><textarea v-else @input="input"/>`,
`<textarea xhs:if="{{a}}" bindinput="__e" data-e-o="{{b}}"/><textarea xhs:elif="{{c}}"/><block xhs:else><textarea xhs:if="{{r0}}" bindinput="__e" data-e-o="{{d}}"/></block>`, `<textarea xhs:if="{{a}}" bindinput="{{b}}"/><textarea xhs:elif="{{c}}"/><block xhs:else><textarea xhs:if="{{r0}}" bindinput="{{d}}"/></block>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return _e({ a: _ctx.ok1 }, _ctx.ok1 ? { b: { 'input': _o(_ctx.input) } } : _ctx.ok2 ? {} : { d: { 'input': _o(_ctx.input) } }, { c: _ctx.ok2 }) return _e({ a: _ctx.ok1 }, _ctx.ok1 ? { b: _o(_ctx.input) } : _ctx.ok2 ? {} : { d: _o(_ctx.input) }, { c: _ctx.ok2 })
}` }`
) )
}) })
...@@ -41,23 +41,23 @@ describe('mp-xhs: transform component', () => { ...@@ -41,23 +41,23 @@ describe('mp-xhs: transform component', () => {
) )
assert( assert(
`<editor @ready="ready"/>`, `<editor @ready="ready"/>`,
`<block xhs:if="{{r0}}"><editor bindready="__e" data-e-o="{{a}}"/></block>`, `<block xhs:if="{{r0}}"><editor bindready="{{a}}"/></block>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return { a: { 'ready': _o(_ctx.ready) } } return { a: _o(_ctx.ready) }
}` }`
) )
assert( assert(
`<editor v-if="ok1" @ready="ready"/><editor v-else-if="ok2"/><editor v-else/>`, `<editor v-if="ok1" @ready="ready"/><editor v-else-if="ok2"/><editor v-else/>`,
`<editor xhs:if="{{a}}" bindready="__e" data-e-o="{{b}}"/><editor xhs:elif="{{c}}"/><editor xhs:else/>`, `<editor xhs:if="{{a}}" bindready="{{b}}"/><editor xhs:elif="{{c}}"/><editor xhs:else/>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return _e({ a: _ctx.ok1 }, _ctx.ok1 ? { b: { 'ready': _o(_ctx.ready) } } : _ctx.ok2 ? {} : {}, { c: _ctx.ok2 }) return _e({ a: _ctx.ok1 }, _ctx.ok1 ? { b: _o(_ctx.ready) } : _ctx.ok2 ? {} : {}, { c: _ctx.ok2 })
}` }`
) )
assert( assert(
`<editor v-if="ok1" @ready="ready"/><editor v-else-if="ok2"/><editor v-else @ready="ready"/>`, `<editor v-if="ok1" @ready="ready"/><editor v-else-if="ok2"/><editor v-else @ready="ready"/>`,
`<editor xhs:if="{{a}}" bindready="__e" data-e-o="{{b}}"/><editor xhs:elif="{{c}}"/><block xhs:else><editor xhs:if="{{r0}}" bindready="__e" data-e-o="{{d}}"/></block>`, `<editor xhs:if="{{a}}" bindready="{{b}}"/><editor xhs:elif="{{c}}"/><block xhs:else><editor xhs:if="{{r0}}" bindready="{{d}}"/></block>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return _e({ a: _ctx.ok1 }, _ctx.ok1 ? { b: { 'ready': _o(_ctx.ready) } } : _ctx.ok2 ? {} : { d: { 'ready': _o(_ctx.ready) } }, { c: _ctx.ok2 }) return _e({ a: _ctx.ok1 }, _ctx.ok1 ? { b: _o(_ctx.ready) } : _ctx.ok2 ? {} : { d: _o(_ctx.ready) }, { c: _ctx.ok2 })
}` }`
) )
}) })
......
import { assert } from './testUtils'
describe('mp-xhs: transform v-model', () => {
test(`component v-model`, () => {
assert(
`<Comp v-model="model" />`,
`<comp xhs:if="{{b}}" u-i="2a9ec0b0-0" eO="{{a}}" bindupdateModelValue="__e" u-p="{{b}}"/>`,
`(_ctx, _cache) => {
return { a: _j({ 'updateModelValue': _o($event => _ctx.model = $event) }), b: _p({ modelValue: _ctx.model }) }
}`
)
})
test(`component v-model with cache`, () => {
assert(
`<Comp v-model="model" />`,
`<comp xhs:if="{{b}}" u-i="2a9ec0b0-0" eO="{{a}}" bindupdateModelValue="__e" u-p="{{b}}"/>`,
`(_ctx, _cache) => {
return { a: _j({ 'updateModelValue': _o($event => _ctx.model = $event) }), b: _p({ modelValue: _ctx.model }) }
}`,
{
cacheHandlers: true,
}
)
})
test(`input,textarea v-model`, () => {
assert(
`<input v-model="model" />`,
`<input value="{{a}}" bindinput="{{b}}"/>`,
`(_ctx, _cache) => {
return { a: _ctx.model, b: _o($event => _ctx.model = $event.detail.value) }
}`
)
assert(
`<textarea v-model="model" />`,
`<block xhs:if="{{r0}}"><textarea value="{{a}}" bindinput="{{b}}"/></block>`,
`(_ctx, _cache) => {
return { a: _ctx.model, b: _o($event => _ctx.model = $event.detail.value) }
}`
)
})
test(`input v-model + v-on`, () => {
assert(
`<input @input="input" v-model="model" />`,
`<input bindinput="__e" data-e-o="{{a}}" value="{{b}}"/>`,
`(_ctx, _cache) => {
return { a: { 'input': _o([$event => _ctx.model = $event.detail.value, _ctx.input]) }, b: _ctx.model }
}`
)
})
})
import { assert } from './testUtils'
describe('mp-xhs: transform v-on', () => {
describe('component', () => {
test(`built-in event`, () => {
assert(
`<custom @tap="tap"/>`,
`<custom bindtap="__e" u-i="2a9ec0b0-0" eO="{{a}}"/>`,
`(_ctx, _cache) => {
return { a: _j({ 'tap': _o(_ctx.tap) }) }
}`
)
})
test(`custom event`, () => {
assert(
`<custom @click="click"/>`,
`<custom bindclick="__e" u-i="2a9ec0b0-0" eO="{{a}}"/>`,
`(_ctx, _cache) => {
return { a: _j({ 'click': _o(_ctx.click) }) }
}`
)
}),
test(`multi custom event`, () => {
assert(
`<custom @unmount="unmount" @update:modelValue="changeHandle" @custom-mount="mount();created();"/>`,
`<custom bindunmount="__e" bindupdateModelValue="__e" bindcustomMount="__e" u-i="2a9ec0b0-0" eO="{{a}}"/>`,
`(_ctx, _cache) => {
return { a: _j({ 'unmount': _o(_ctx.unmount), 'updateModelValue': _o(_ctx.changeHandle), 'customMount': _o($event => { _ctx.mount(); _ctx.created(); }) }) }
}`
)
})
})
})
...@@ -4,14 +4,14 @@ describe('compiler: transform v-slot', () => { ...@@ -4,14 +4,14 @@ describe('compiler: transform v-slot', () => {
test('default slot', () => { test('default slot', () => {
assert( assert(
`<custom><template v-slot/></custom>`, `<custom><template v-slot/></custom>`,
`<custom u-s="{{['d']}}" u-i="2a9ec0b0-0"><view/></custom>`, `<custom u-s="{{['d']}}" u-i="2a9ec0b0-0" bind:__l="__l"><view/></custom>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return {} return {}
}` }`
) )
assert( assert(
`<custom>test</custom>`, `<custom>test</custom>`,
`<custom u-s="{{['d']}}" u-i="2a9ec0b0-0">test</custom>`, `<custom u-s="{{['d']}}" u-i="2a9ec0b0-0" bind:__l="__l">test</custom>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return {} return {}
}` }`
...@@ -20,14 +20,14 @@ describe('compiler: transform v-slot', () => { ...@@ -20,14 +20,14 @@ describe('compiler: transform v-slot', () => {
test('named slots', () => { test('named slots', () => {
assert( assert(
`<custom><template v-slot:header/><template v-slot:default/><template v-slot:footer/></custom>`, `<custom><template v-slot:header/><template v-slot:default/><template v-slot:footer/></custom>`,
`<custom u-s="{{['header','d','footer']}}" u-i="2a9ec0b0-0"><view slot="header"/><view/><view slot="footer"/></custom>`, `<custom u-s="{{['header','d','footer']}}" u-i="2a9ec0b0-0" bind:__l="__l"><view slot="header"/><view/><view slot="footer"/></custom>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return {} return {}
}` }`
) )
assert( assert(
`<unicloud-db v-slot:default="{data, loading, error, options}"><view v-if="error">{{error.message}}</view><view v-else></view></unicloud-db>`, `<unicloud-db v-slot:default="{data, loading, error, options}"><view v-if="error">{{error.message}}</view><view v-else></view></unicloud-db>`,
`<unicloud-db u-s="{{['d']}}" u-i="2a9ec0b0-0"><view><block xhs:for="{{a}}" xhs:for-item="v0" xhs:key="c"><view xhs:if="{{v0.a}}">{{v0.b}}</view><view xhs:else></view></block></view></unicloud-db>`, `<unicloud-db u-s="{{['d']}}" u-i="2a9ec0b0-0" bind:__l="__l"><view><block xhs:for="{{a}}" xhs:for-item="v0" xhs:key="c"><view xhs:if="{{v0.a}}">{{v0.b}}</view><view xhs:else></view></block></view></unicloud-db>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return { a: _w(({ data, loading, error, options }, s0, i0) => { return _e({ a: error }, error ? { b: _t(error.message) } : {}, { c: i0 }); }, { name: 'd', path: 'a', vueId: '2a9ec0b0-0' }) } return { a: _w(({ data, loading, error, options }, s0, i0) => { return _e({ a: error }, error ? { b: _t(error.message) } : {}, { c: i0 }); }, { name: 'd', path: 'a', vueId: '2a9ec0b0-0' }) }
}` }`
...@@ -37,7 +37,7 @@ describe('compiler: transform v-slot', () => { ...@@ -37,7 +37,7 @@ describe('compiler: transform v-slot', () => {
test('scoped slots', () => { test('scoped slots', () => {
assert( assert(
`<custom><template v-slot:default="slotProps"><view>{{ slotProps.item }}</view></template></custom>`, `<custom><template v-slot:default="slotProps"><view>{{ slotProps.item }}</view></template></custom>`,
`<custom u-s="{{['d']}}" u-i="2a9ec0b0-0"><view><block xhs:for="{{a}}" xhs:for-item="slotProps" xhs:key="b"><view>{{slotProps.a}}</view></block></view></custom>`, `<custom u-s="{{['d']}}" u-i="2a9ec0b0-0" bind:__l="__l"><view><block xhs:for="{{a}}" xhs:for-item="slotProps" xhs:key="b"><view>{{slotProps.a}}</view></block></view></custom>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return { a: _w((slotProps, s0, i0) => { return { a: _t(slotProps.item), b: i0 }; }, { name: 'd', path: 'a', vueId: '2a9ec0b0-0' }) } return { a: _w((slotProps, s0, i0) => { return { a: _t(slotProps.item), b: i0 }; }, { name: 'd', path: 'a', vueId: '2a9ec0b0-0' }) }
}` }`
...@@ -47,7 +47,7 @@ describe('compiler: transform v-slot', () => { ...@@ -47,7 +47,7 @@ describe('compiler: transform v-slot', () => {
test('scoped slots + scoped slots', () => { test('scoped slots + scoped slots', () => {
assert( assert(
`<custom><template v-slot:default="slotProps"><custom1><template v-slot:default="slotProps1">{{ slotProps.item }}{{ slotProps1.item }}</template></custom1></template></custom>`, `<custom><template v-slot:default="slotProps"><custom1><template v-slot:default="slotProps1">{{ slotProps.item }}{{ slotProps1.item }}</template></custom1></template></custom>`,
`<custom u-s="{{['d']}}" u-i="2a9ec0b0-0"><view><block xhs:for="{{a}}" xhs:for-item="slotProps" xhs:key="d"><custom1 u-s="{{['d']}}" u-i="{{slotProps.c}}"><view><block xhs:for="{{slotProps.a}}" xhs:for-item="slotProps1" xhs:key="b">{{slotProps.b}}{{slotProps1.a}}</block></view></custom1></block></view></custom>`, `<custom u-s="{{['d']}}" u-i="2a9ec0b0-0" bind:__l="__l"><view><block xhs:for="{{a}}" xhs:for-item="slotProps" xhs:key="d"><custom1 u-s="{{['d']}}" u-i="{{slotProps.c}}" bind:__l="__l"><view><block xhs:for="{{slotProps.a}}" xhs:for-item="slotProps1" xhs:key="b">{{slotProps.b}}{{slotProps1.a}}</block></view></custom1></block></view></custom>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return { a: _w((slotProps, s0, i0) => { return { a: _w((slotProps1, s1, i1) => { return { a: _t(slotProps1.item), b: i1 }; }, { name: 'd', path: 'a[' + i0 + '].' + 'a', vueId: '2a9ec0b0-1' + '-' + i0 + ',' + '2a9ec0b0-0' }), b: _t(slotProps.item), c: '2a9ec0b0-1' + '-' + i0 + ',' + '2a9ec0b0-0', d: i0 }; }, { name: 'd', path: 'a', vueId: '2a9ec0b0-0' }) } return { a: _w((slotProps, s0, i0) => { return { a: _w((slotProps1, s1, i1) => { return { a: _t(slotProps1.item), b: i1 }; }, { name: 'd', path: 'a[' + i0 + '].' + 'a', vueId: '2a9ec0b0-1' + '-' + i0 + ',' + '2a9ec0b0-0' }), b: _t(slotProps.item), c: '2a9ec0b0-1' + '-' + i0 + ',' + '2a9ec0b0-0', d: i0 }; }, { name: 'd', path: 'a', vueId: '2a9ec0b0-0' }) }
}` }`
...@@ -57,7 +57,7 @@ describe('compiler: transform v-slot', () => { ...@@ -57,7 +57,7 @@ describe('compiler: transform v-slot', () => {
test('v-if + scoped slots', () => { test('v-if + scoped slots', () => {
assert( assert(
`<custom><template v-if="ok" v-slot:default="slotProps"><view>{{ slotProps.item }}</view></template></custom>`, `<custom><template v-if="ok" v-slot:default="slotProps"><view>{{ slotProps.item }}</view></template></custom>`,
`<custom u-s="{{['d']}}" u-i="2a9ec0b0-0"><view xhs:if="{{a}}"><block xhs:for="{{b}}" xhs:for-item="slotProps" xhs:key="b"><view>{{slotProps.a}}</view></block></view></custom>`, `<custom u-s="{{['d']}}" u-i="2a9ec0b0-0" bind:__l="__l"><view xhs:if="{{a}}"><block xhs:for="{{b}}" xhs:for-item="slotProps" xhs:key="b"><view>{{slotProps.a}}</view></block></view></custom>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return _e({ a: _ctx.ok }, _ctx.ok ? { b: _w((slotProps, s0, i0) => { return { a: _t(slotProps.item), b: i0 }; }, { name: 'd', path: 'b', vueId: '2a9ec0b0-0' }) } : {}) return _e({ a: _ctx.ok }, _ctx.ok ? { b: _w((slotProps, s0, i0) => { return { a: _t(slotProps.item), b: i0 }; }, { name: 'd', path: 'b', vueId: '2a9ec0b0-0' }) } : {})
}` }`
...@@ -67,7 +67,7 @@ describe('compiler: transform v-slot', () => { ...@@ -67,7 +67,7 @@ describe('compiler: transform v-slot', () => {
test('v-for + scoped slots', () => { test('v-for + scoped slots', () => {
assert( assert(
`<custom v-for="item in items"><template v-slot:default="slotProps"><view>{{ slotProps.item }}</view></template></custom>`, `<custom v-for="item in items"><template v-slot:default="slotProps"><view>{{ slotProps.item }}</view></template></custom>`,
`<custom xhs:for="{{a}}" xhs:for-item="item" u-s="{{['d']}}" u-i="{{item.b}}"><view><block xhs:for="{{item.a}}" xhs:for-item="slotProps" xhs:key="b"><view>{{slotProps.a}}</view></block></view></custom>`, `<custom xhs:for="{{a}}" xhs:for-item="item" u-s="{{['d']}}" u-i="{{item.b}}" bind:__l="__l"><view><block xhs:for="{{item.a}}" xhs:for-item="slotProps" xhs:key="b"><view>{{slotProps.a}}</view></block></view></custom>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _w((slotProps, s1, i1) => { return { a: _t(slotProps.item), b: i1 }; }, { name: 'd', path: 'a[' + i0 + '].' + 'a', vueId: '2a9ec0b0-0' + '-' + i0 }), b: '2a9ec0b0-0' + '-' + i0 }; }) } return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _w((slotProps, s1, i1) => { return { a: _t(slotProps.item), b: i1 }; }, { name: 'd', path: 'a[' + i0 + '].' + 'a', vueId: '2a9ec0b0-0' + '-' + i0 }), b: '2a9ec0b0-0' + '-' + i0 }; }) }
}` }`
...@@ -77,7 +77,7 @@ describe('compiler: transform v-slot', () => { ...@@ -77,7 +77,7 @@ describe('compiler: transform v-slot', () => {
test('v-for + v-for + scoped slots', () => { test('v-for + v-for + scoped slots', () => {
assert( assert(
`<view v-for="item in items"><custom v-for="item1 in item.list" :item="item1"><template v-slot:default="slotProps"><view>{{ slotProps.item }}</view></template></custom></view>`, `<view v-for="item in items"><custom v-for="item1 in item.list" :item="item1"><template v-slot:default="slotProps"><view>{{ slotProps.item }}</view></template></custom></view>`,
`<view xhs:for="{{a}}" xhs:for-item="item"><custom xhs:for="{{item.a}}" xhs:for-item="item1" u-s="{{['d']}}" u-i="{{item1.b}}" u-p="{{item1.c}}"><view><block xhs:for="{{item1.a}}" xhs:for-item="slotProps" xhs:key="b"><view>{{slotProps.a}}</view></block></view></custom></view>`, `<view xhs:for="{{a}}" xhs:for-item="item"><custom xhs:for="{{item.a}}" xhs:for-item="item1" u-s="{{['d']}}" u-i="{{item1.b}}" bind:__l="__l" u-p="{{item1.c}}"><view><block xhs:for="{{item1.a}}" xhs:for-item="slotProps" xhs:key="b"><view>{{slotProps.a}}</view></block></view></custom></view>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _f(item.list, (item1, k1, i1) => { return { a: _w((slotProps, s2, i2) => { return { a: _t(slotProps.item), b: i2 }; }, { name: 'd', path: 'a[' + i0 + '].' + ('a[' + i1 + '].') + 'a', vueId: '2a9ec0b0-0' + '-' + i0 + '-' + i1 }), b: '2a9ec0b0-0' + '-' + i0 + '-' + i1, c: _p({ item: item1 }) }; }) }; }) } return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _f(item.list, (item1, k1, i1) => { return { a: _w((slotProps, s2, i2) => { return { a: _t(slotProps.item), b: i2 }; }, { name: 'd', path: 'a[' + i0 + '].' + ('a[' + i1 + '].') + 'a', vueId: '2a9ec0b0-0' + '-' + i0 + '-' + i1 }), b: '2a9ec0b0-0' + '-' + i0 + '-' + i1, c: _p({ item: item1 }) }; }) }; }) }
}` }`
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
var uniCliShared = require('@dcloudio/uni-cli-shared'); var uniCliShared = require('@dcloudio/uni-cli-shared');
var initMiniProgramPlugin = require('@dcloudio/uni-mp-vite'); var initMiniProgramPlugin = require('@dcloudio/uni-mp-vite');
var path = require('path'); var path = require('path');
var uniMpCompiler = require('@dcloudio/uni-mp-compiler');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
...@@ -60,30 +59,9 @@ var source = { ...@@ -60,30 +59,9 @@ var source = {
condition: condition condition: condition
}; };
/** const directiveTransforms = {};
* 小红书小程序的Page和自定义组件,不支持动态事件绑定,需要通过__e转发
*/
const transformOn = uniCliShared.createTransformOn(uniMpCompiler.transformOn, {
match: (name, node, context) => {
return true;
},
});
/**
* 小红书小程序的自定义组件,不支持动态事件绑定,故 v-model 也需要调整
*/
const transformModel = uniCliShared.createTransformModel(uniMpCompiler.transformModel);
const directiveTransforms = {
on: transformOn,
model: transformModel,
};
const compilerOptions = { const compilerOptions = {
nodeTransforms: [ nodeTransforms: [uniCliShared.transformRef, uniCliShared.transformComponentLink, uniCliShared.transformMatchMedia],
uniCliShared.transformRef,
// transformComponentLink,
uniCliShared.transformMatchMedia,
],
directiveTransforms, directiveTransforms,
}; };
const COMPONENTS_DIR = 'xhscomponents'; const COMPONENTS_DIR = 'xhscomponents';
...@@ -166,18 +144,11 @@ const options = { ...@@ -166,18 +144,11 @@ const options = {
config: ['project.config.json'], config: ['project.config.json'],
source, source,
}, },
template: Object.assign(Object.assign({}, miniProgram), { filter: template: Object.assign(Object.assign({}, miniProgram), { filter: {
// undefined,
{
extname: '.sjs', extname: '.sjs',
lang: 'sjs', lang: 'sjs',
generate(filter, filename) { generate(filter, filename) {
if (filename) { return `<sjs src="${filename}.sjs" module="${filter.name}"/>`;
return `<import-sjs src="${filename}.sjs" module="${filter.name}"/>`;
}
return `<import-sjs module="${filter.name}">
${filter.code}
</import-sjs>`;
}, },
}, extname: '.xhsml', compilerOptions }), }, extname: '.xhsml', compilerOptions }),
style: { style: {
......
import { SLOT_DEFAULT_NAME, EventChannel, invokeArrayFns, MINI_PROGRAM_PAGE_RUNTIME_HOOKS, ON_LOAD, ON_SHOW, ON_HIDE, ON_UNLOAD, ON_RESIZE, ON_TAB_ITEM_TAP, ON_REACH_BOTTOM, ON_PULL_DOWN_REFRESH, ON_ADD_TO_FAVORITES, isUniLifecycleHook, ON_READY, ON_LAUNCH, ON_ERROR, ON_THEME_CHANGE, ON_PAGE_NOT_FOUND, ON_UNHANDLE_REJECTION, customizeEvent, addLeadingSlash, stringifyQuery } from '@dcloudio/uni-shared'; import { SLOT_DEFAULT_NAME, EventChannel, invokeArrayFns, MINI_PROGRAM_PAGE_RUNTIME_HOOKS, ON_LOAD, ON_SHOW, ON_HIDE, ON_UNLOAD, ON_RESIZE, ON_TAB_ITEM_TAP, ON_REACH_BOTTOM, ON_PULL_DOWN_REFRESH, ON_ADD_TO_FAVORITES, isUniLifecycleHook, ON_READY, ON_LAUNCH, ON_ERROR, ON_THEME_CHANGE, ON_PAGE_NOT_FOUND, ON_UNHANDLE_REJECTION, customizeEvent, addLeadingSlash, stringifyQuery } from '@dcloudio/uni-shared';
import { isArray, isFunction, hasOwn, extend, hyphenate, isPlainObject, isObject, isString } from '@vue/shared'; import { isArray, isFunction, hasOwn, extend, hyphenate, isPlainObject, isObject } from '@vue/shared';
import { ref, nextTick, findComponentPropsData, toRaw, updateProps, hasQueueJob, invalidateJob, devtoolsComponentAdded, getExposeProxy, pruneComponentPropsCache, EMPTY_OBJ, isRef, setTemplateRef } from 'vue'; import { ref, nextTick, findComponentPropsData, toRaw, updateProps, hasQueueJob, invalidateJob, devtoolsComponentAdded, getExposeProxy, pruneComponentPropsCache } from 'vue';
import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n'; import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n';
const MP_METHODS = [ const MP_METHODS = [
...@@ -830,53 +830,6 @@ function initSpecialMethods(mpInstance) { ...@@ -830,53 +830,6 @@ function initSpecialMethods(mpInstance) {
}); });
} }
} }
function handleRef(ref) {
if (!ref) {
return;
}
const refName = ref.props.uR; // data-ref
const refInForName = ref.props.uRIF; // data-ref-in-for
if (!refName && !refInForName) {
return;
}
const instance = this.$vm.$;
const refs = instance.refs === EMPTY_OBJ ? (instance.refs = {}) : instance.refs;
const { setupState } = instance;
const refValue = ref.$vm;
if (refName) {
if (isString(refName)) {
refs[refName] = refValue;
if (hasOwn(setupState, refName)) {
setupState[refName] = refValue;
}
}
else {
setRef(refName, refValue, refs, setupState);
}
}
else if (refInForName) {
if (isString(refInForName)) {
(refs[refInForName] || (refs[refInForName] = [])).push(refValue);
}
else {
setRef(refInForName, refValue, refs, setupState);
}
}
}
function isTemplateRef(opts) {
return !!(opts && opts.r);
}
function setRef(ref, refValue, refs, setupState) {
if (isRef(ref)) {
ref.value = refValue;
}
else if (isFunction(ref)) {
const templateRef = ref(refValue, refs);
if (isTemplateRef(templateRef)) {
setTemplateRef(templateRef, refValue, setupState);
}
}
}
function createVueComponent(mpType, mpInstance, vueOptions, parent) { function createVueComponent(mpType, mpInstance, vueOptions, parent) {
return $createComponent({ return $createComponent({
type: vueOptions, type: vueOptions,
...@@ -907,21 +860,21 @@ function initCreatePage() { ...@@ -907,21 +860,21 @@ function initCreatePage() {
this.props = query; this.props = query;
}, },
onShow() { onShow() {
if (this.$vm) {
this.$vm.$callHook(ON_SHOW);
}
if (__VUE_PROD_DEVTOOLS__) { if (__VUE_PROD_DEVTOOLS__) {
devtoolsComponentAdded(this.$vm.$); devtoolsComponentAdded(this.$vm.$);
} }
}, this.$vm = createVueComponent('page', this, vueOptions);
onReady() { this.$vm.$callHook('mounted');
this.$vm = createVueComponent("page", this, vueOptions);
this.$vm.$callHook("mounted");
this.$vm.$callHook(ON_LOAD, this.options); this.$vm.$callHook(ON_LOAD, this.options);
initSpecialMethods(this); initSpecialMethods(this);
if (this.$vm) {
this.$vm.$callHook(ON_SHOW);
}
},
onReady() {
setTimeout(() => { setTimeout(() => {
this.$vm.$callHook(ON_READY); this.$vm.$callHook(ON_READY);
}); }, 50);
}, },
onUnload() { onUnload() {
if (this.$vm) { if (this.$vm) {
...@@ -929,16 +882,8 @@ function initCreatePage() { ...@@ -929,16 +882,8 @@ function initCreatePage() {
$destroyComponent(this.$vm); $destroyComponent(this.$vm);
} }
}, },
__r: handleRef,
__l: handleLink, __l: handleLink,
__e: function (event) { __e: handleEvent,
var _a, _b;
const { currentTarget: { dataset }, } = event;
dataset['eO'] = Object.assign(Object.assign({}, dataset['eO']), { tap: ((_a = dataset['eO']) === null || _a === void 0 ? void 0 : _a.tap) || ((_b = dataset['eO']) === null || _b === void 0 ? void 0 : _b.click) });
// console.log('触发了page事件', event)
// @ts-ignore
return handleEvent.call(this, event);
},
}; };
if (isPlainObject(vueOptions.events)) { if (isPlainObject(vueOptions.events)) {
extend(pageOptions.events, vueOptions.events); extend(pageOptions.events, vueOptions.events);
...@@ -959,23 +904,12 @@ function isPage(mpInstance) { ...@@ -959,23 +904,12 @@ function isPage(mpInstance) {
return !!mpInstance.route; return !!mpInstance.route;
} }
function initRelation(mpInstance, detail) { function initRelation(mpInstance, detail) {
// 小红书自定义组件从customEventMap取事件名
mpInstance.customEventMap = {
// @ts-ignore
__l: '__l',
};
mpInstance.triggerEvent('__l', detail); mpInstance.triggerEvent('__l', detail);
} }
function parse(componentOptions) { function parse(componentOptions) {
const methods = componentOptions.methods; const methods = componentOptions.methods;
methods.__e = function (event) { methods.__e = handleEvent;
var _a, _b; methods.__l = handleLink;
const { currentTarget: { dataset }, } = event;
dataset['eO'] = Object.assign(Object.assign({}, dataset['eO']), { tap: ((_a = dataset['eO']) === null || _a === void 0 ? void 0 : _a.tap) || ((_b = dataset['eO']) === null || _b === void 0 ? void 0 : _b.click) });
// console.log('触发了component事件', event)
// @ts-ignore
return handleEvent.call(this, event);
};
} }
var parseComponentOptions = /*#__PURE__*/Object.freeze({ var parseComponentOptions = /*#__PURE__*/Object.freeze({
......
import path from 'path' import path from 'path'
import type { CompilerOptions } from '@vue/compiler-core' import type { CompilerOptions } from '@vue/compiler-core'
import { import {
// COMPONENT_CUSTOM_HIDDEN,
// copyMiniProgramPluginJson,
// copyMiniProgramThemeJson,
MiniProgramCompilerOptions, MiniProgramCompilerOptions,
// transformComponentLink, transformComponentLink,
transformMatchMedia, transformMatchMedia,
transformRef, transformRef,
} from '@dcloudio/uni-cli-shared' } from '@dcloudio/uni-cli-shared'
import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite' import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite'
import source from './project.config.json' import source from './project.config.json'
import { transformOn } from './transforms/vOn'
import { transformModel } from './transforms/vModel'
const directiveTransforms = { const directiveTransforms = {}
on: transformOn,
model: transformModel,
}
export const compilerOptions: CompilerOptions = { export const compilerOptions: CompilerOptions = {
nodeTransforms: [ nodeTransforms: [transformRef, transformComponentLink, transformMatchMedia],
transformRef,
// transformComponentLink,
transformMatchMedia,
],
directiveTransforms, directiveTransforms,
} }
...@@ -114,20 +102,13 @@ export const options: UniMiniProgramPluginOptions = { ...@@ -114,20 +102,13 @@ export const options: UniMiniProgramPluginOptions = {
template: { template: {
/* eslint-disable no-restricted-syntax */ /* eslint-disable no-restricted-syntax */
...miniProgram, ...miniProgram,
filter: filter: {
// undefined, extname: '.sjs',
{ lang: 'sjs',
extname: '.sjs', generate(filter, filename) {
lang: 'sjs', return `<sjs src="${filename}.sjs" module="${filter.name}"/>`
generate(filter, filename) {
if (filename) {
return `<import-sjs src="${filename}.sjs" module="${filter.name}"/>`
}
return `<import-sjs module="${filter.name}">
${filter.code}
</import-sjs>`
},
}, },
},
extname: '.xhsml', extname: '.xhsml',
compilerOptions, compilerOptions,
}, },
......
import { createTransformModel } from '@dcloudio/uni-cli-shared'
import { transformModel as baseTransformModel } from '@dcloudio/uni-mp-compiler'
/**
* 小红书小程序的自定义组件,不支持动态事件绑定,故 v-model 也需要调整
*/
export const transformModel = createTransformModel(baseTransformModel)
import { createTransformOn } from '@dcloudio/uni-cli-shared'
import { transformOn as baseTransformOn } from '@dcloudio/uni-mp-compiler'
/**
* 小红书小程序的Page和自定义组件,不支持动态事件绑定,需要通过__e转发
*/
export const transformOn = createTransformOn(baseTransformOn, {
match: (name, node, context) => {
return true
},
})
...@@ -24,12 +24,7 @@ import { ...@@ -24,12 +24,7 @@ import {
stringifyQuery, stringifyQuery,
} from '@dcloudio/uni-shared' } from '@dcloudio/uni-shared'
import { import { handleLink, initSpecialMethods, createVueComponent } from './util'
handleRef,
handleLink,
initSpecialMethods,
createVueComponent,
} from './util'
import { extend, isPlainObject } from '@vue/shared' import { extend, isPlainObject } from '@vue/shared'
...@@ -48,21 +43,21 @@ export function initCreatePage() { ...@@ -48,21 +43,21 @@ export function initCreatePage() {
this.props = query this.props = query
}, },
onShow() { onShow() {
if (this.$vm) {
this.$vm.$callHook(ON_SHOW)
}
if (__VUE_PROD_DEVTOOLS__) { if (__VUE_PROD_DEVTOOLS__) {
devtoolsComponentAdded(this.$vm.$) devtoolsComponentAdded(this.$vm.$)
} }
},
onReady() {
this.$vm = createVueComponent('page', this, vueOptions) this.$vm = createVueComponent('page', this, vueOptions)
this.$vm.$callHook('mounted') this.$vm.$callHook('mounted')
this.$vm.$callHook(ON_LOAD, this.options) this.$vm.$callHook(ON_LOAD, this.options)
initSpecialMethods(this) initSpecialMethods(this)
if (this.$vm) {
this.$vm.$callHook(ON_SHOW)
}
},
onReady() {
setTimeout(() => { setTimeout(() => {
this.$vm.$callHook(ON_READY) this.$vm.$callHook(ON_READY)
}) }, 50)
}, },
onUnload() { onUnload() {
if (this.$vm) { if (this.$vm) {
...@@ -70,21 +65,8 @@ export function initCreatePage() { ...@@ -70,21 +65,8 @@ export function initCreatePage() {
$destroyComponent(this.$vm) $destroyComponent(this.$vm)
} }
}, },
__r: handleRef,
__l: handleLink, __l: handleLink,
__e: function (event: any) { __e: handleEvent,
const {
currentTarget: { dataset },
} = event
dataset['eO'] = {
// eslint-disable-next-line no-restricted-syntax
...dataset['eO'],
tap: dataset['eO']?.tap || dataset['eO']?.click,
}
// console.log('触发了page事件', event)
// @ts-ignore
return handleEvent.call(this, event)
},
} }
if (isPlainObject(vueOptions.events)) { if (isPlainObject(vueOptions.events)) {
extend(pageOptions.events!, vueOptions.events) extend(pageOptions.events!, vueOptions.events)
......
...@@ -3,8 +3,7 @@ import { ...@@ -3,8 +3,7 @@ import {
MPComponentOptions, MPComponentOptions,
handleEvent, handleEvent,
} from '@dcloudio/uni-mp-core' } from '@dcloudio/uni-mp-core'
import { handleLink } from '@dcloudio/uni-mp-weixin'
// import { handleLink } from '@dcloudio/uni-mp-weixin'
export { handleLink, initLifetimes } from '@dcloudio/uni-mp-weixin' export { handleLink, initLifetimes } from '@dcloudio/uni-mp-weixin'
...@@ -18,11 +17,6 @@ export function initRelation( ...@@ -18,11 +17,6 @@ export function initRelation(
mpInstance: MPComponentInstance, mpInstance: MPComponentInstance,
detail: Record<string, unknown> detail: Record<string, unknown>
) { ) {
// 小红书自定义组件从customEventMap取事件名
mpInstance.customEventMap = {
// @ts-ignore
__l: '__l',
}
mpInstance.triggerEvent('__l', detail) mpInstance.triggerEvent('__l', detail)
} }
export function parse(componentOptions: MPComponentOptions) { export function parse(componentOptions: MPComponentOptions) {
...@@ -30,17 +24,6 @@ export function parse(componentOptions: MPComponentOptions) { ...@@ -30,17 +24,6 @@ export function parse(componentOptions: MPComponentOptions) {
string, string,
(...args: any[]) => any (...args: any[]) => any
> >
methods.__e = function (event: any) { methods.__e = handleEvent
const { methods.__l = handleLink
currentTarget: { dataset },
} = event
dataset['eO'] = {
// eslint-disable-next-line no-restricted-syntax
...dataset['eO'],
tap: dataset['eO']?.tap || dataset['eO']?.click,
}
// console.log('触发了component事件', event)
// @ts-ignore
return handleEvent.call(this, event)
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册