提交 9fa21db7 编写于 作者: fxy060608's avatar fxy060608

wip(mp): reduce template size

上级 2f0369be
......@@ -623,7 +623,7 @@ function parsePropType(key, type, defaultValue) {
function initDefaultProps(isBehavior = false) {
const properties = {};
if (!isBehavior) {
properties.vueId = {
properties.vI = {
type: String,
value: '',
};
......@@ -1357,7 +1357,7 @@ function initVm(mpInstance, createComponent) {
return;
}
const properties = mpInstance.props;
initVueIds(properties.vueId, mpInstance);
initVueIds(properties.vI, mpInstance);
const relationOptions = {
vuePid: mpInstance._$vuePid,
mpInstance,
......
......@@ -53,7 +53,7 @@ function initVm(
return
}
const properties = mpInstance.props
initVueIds(properties.vueId, mpInstance as any)
initVueIds(properties.vI, mpInstance as any)
const relationOptions: RelationOptions = {
vuePid: mpInstance._$vuePid,
mpInstance,
......
......@@ -175,8 +175,8 @@ function initRefs(instance, mpInstance) {
Object.defineProperty(instance, 'refs', {
get() {
const $refs = {};
selectAllComponents(mpInstance, '.vue-ref', $refs);
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
selectAllComponents(mpInstance, '.v-r', $refs);
const forComponents = mpInstance.selectAllComponents('.v-r-i-f');
forComponents.forEach((component) => {
const ref = component.dataset.ref;
if (!$refs[ref]) {
......@@ -662,7 +662,7 @@ function parsePropType(key, type, defaultValue) {
function initDefaultProps(isBehavior = false) {
const properties = {};
if (!isBehavior) {
properties.vueId = {
properties.vI = {
type: String,
value: '',
};
......@@ -1234,7 +1234,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) {
return {
attached() {
const properties = this.properties;
initVueIds(properties.vueId, this);
initVueIds(properties.vI, this);
const relationOptions = {
vuePid: this._$vuePid,
};
......
......@@ -5,7 +5,7 @@ describe('compiler: transform component', () => {
// test('basic', () => {
// assert(
// `<custom/>`,
// `<custom class="vue-ref" vue-id="2a9ec0b0-0" bind:__l="__l"/>`,
// `<custom class="v-r" v-i="2a9ec0b0-0" bind:__l="__l"/>`,
// `(_ctx, _cache) => {
// return {}
// }`,
......@@ -17,7 +17,7 @@ describe('compiler: transform component', () => {
test('component + component', () => {
assert(
`<custom><custom1/></custom>`,
`<custom vue-slots="{{['default']}}" class="vue-ref" vue-id="2a9ec0b0-0" bind:__l="__l"><custom1 class="vue-ref" vue-id="2a9ec0b0-1,2a9ec0b0-0" bind:__l="__l"/></custom>`,
`<custom vue-slots="{{['default']}}" class="v-r" v-i="2a9ec0b0-0" bind:__l="__l"><custom1 class="v-r" v-i="2a9ec0b0-1,2a9ec0b0-0" bind:__l="__l"/></custom>`,
`(_ctx, _cache) => {
return {}
}`,
......@@ -29,7 +29,7 @@ describe('compiler: transform component', () => {
test('component + component + component', () => {
assert(
`<custom><custom1><custom2/><custom2/></custom1></custom>`,
`<custom vue-slots="{{['default']}}" class="vue-ref" vue-id="2a9ec0b0-0" bind:__l="__l"><custom1 vue-slots="{{['default']}}" class="vue-ref" vue-id="2a9ec0b0-1,2a9ec0b0-0" bind:__l="__l"><custom2 class="vue-ref" vue-id="2a9ec0b0-2,2a9ec0b0-1" bind:__l="__l"/><custom2 class="vue-ref" vue-id="2a9ec0b0-3,2a9ec0b0-1" bind:__l="__l"/></custom1></custom>`,
`<custom vue-slots="{{['default']}}" class="v-r" v-i="2a9ec0b0-0" bind:__l="__l"><custom1 vue-slots="{{['default']}}" class="v-r" v-i="2a9ec0b0-1,2a9ec0b0-0" bind:__l="__l"><custom2 class="v-r" v-i="2a9ec0b0-2,2a9ec0b0-1" bind:__l="__l"/><custom2 class="v-r" v-i="2a9ec0b0-3,2a9ec0b0-1" bind:__l="__l"/></custom1></custom>`,
`(_ctx, _cache) => {
return {}
}`,
......@@ -41,7 +41,7 @@ describe('compiler: transform component', () => {
test('component with v-for', () => {
assert(
`<custom v-for="item in items"/>`,
`<custom wx:for="{{a}}" wx:for-item="item" class="vue-ref-in-for" vue-id="{{item.a}}" bind:__l="__l"/>`,
`<custom wx:for="{{a}}" wx:for-item="item" class="v-r-i-f" v-i="{{item.a}}" bind:__l="__l"/>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-0' + '-' + i0 }; }) }
}`,
......@@ -51,7 +51,7 @@ describe('compiler: transform component', () => {
)
assert(
`<custom v-for="(item,key,index) in items"/>`,
`<custom wx:for="{{a}}" wx:for-item="item" class="vue-ref-in-for" vue-id="{{item.a}}" bind:__l="__l"/>`,
`<custom wx:for="{{a}}" wx:for-item="item" class="v-r-i-f" v-i="{{item.a}}" bind:__l="__l"/>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.items, (item, key, index) => { return { a: '2a9ec0b0-0' + '-' + index }; }) }
}`,
......@@ -63,7 +63,7 @@ describe('compiler: transform component', () => {
test('component + component with v-for', () => {
assert(
`<custom><custom1 v-for="item in items"/></custom>`,
`<custom vue-slots="{{['default']}}" class="vue-ref" vue-id="2a9ec0b0-0" bind:__l="__l"><custom1 wx:for="{{a}}" wx:for-item="item" class="vue-ref-in-for" vue-id="{{item.a}}" bind:__l="__l"/></custom>`,
`<custom vue-slots="{{['default']}}" class="v-r" v-i="2a9ec0b0-0" bind:__l="__l"><custom1 wx:for="{{a}}" wx:for-item="item" class="v-r-i-f" v-i="{{item.a}}" bind:__l="__l"/></custom>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-1' + '-' + i0 + ',' + '2a9ec0b0-0' }; }) }
}`,
......@@ -75,7 +75,7 @@ describe('compiler: transform component', () => {
test('component with v-for + component', () => {
assert(
`<custom v-for="item in items"><custom1/></custom>`,
`<custom wx:for="{{a}}" wx:for-item="item" vue-slots="{{['default']}}" class="vue-ref-in-for" vue-id="{{item.b}}" bind:__l="__l"><custom1 class="vue-ref-in-for" vue-id="{{item.a}}" bind:__l="__l"/></custom>`,
`<custom wx:for="{{a}}" wx:for-item="item" vue-slots="{{['default']}}" class="v-r-i-f" v-i="{{item.b}}" bind:__l="__l"><custom1 class="v-r-i-f" v-i="{{item.a}}" bind:__l="__l"/></custom>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-1' + '-' + i0 + ',' + ('2a9ec0b0-0' + '-' + i0), b: '2a9ec0b0-0' + '-' + i0 }; }) }
}`,
......@@ -87,7 +87,7 @@ describe('compiler: transform component', () => {
test('component with v-for + component with v-for', () => {
assert(
`<custom v-for="item in items"><custom1 v-for="item1 in item.items"/></custom>`,
`<custom wx:for="{{a}}" wx:for-item="item" vue-slots="{{['default']}}" class="vue-ref-in-for" vue-id="{{item.b}}" bind:__l="__l"><custom1 wx:for="{{item.a}}" wx:for-item="item1" class="vue-ref-in-for" vue-id="{{item1.a}}" bind:__l="__l"/></custom>`,
`<custom wx:for="{{a}}" wx:for-item="item" vue-slots="{{['default']}}" class="v-r-i-f" v-i="{{item.b}}" bind:__l="__l"><custom1 wx:for="{{item.a}}" wx:for-item="item1" class="v-r-i-f" v-i="{{item1.a}}" bind:__l="__l"/></custom>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.items, (item, k0, i0) => { return { a: _vFor(item.items, (item1, k1, i1) => { return { a: '2a9ec0b0-1' + '-' + i0 + '-' + i1 + ',' + ('2a9ec0b0-0' + '-' + i0) }; }), b: '2a9ec0b0-0' + '-' + i0 }; }) }
}`,
......
import { assert } from './testUtils'
describe('compiler: transform ref', () => {
test('vue-ref', () => {
test('v-r', () => {
assert(
`<custom/>`,
`<custom class="vue-ref" vue-id="2a9ec0b0-0"/>`,
`<custom class="v-r" v-i="2a9ec0b0-0"/>`,
`(_ctx, _cache) => {
return {}
}`
)
assert(
`<custom/><custom/><custom1/>`,
`<custom class="vue-ref" vue-id="2a9ec0b0-0"/><custom class="vue-ref" vue-id="2a9ec0b0-1"/><custom1 class="vue-ref" vue-id="2a9ec0b0-2"/>`,
`<custom class="v-r" v-i="2a9ec0b0-0"/><custom class="v-r" v-i="2a9ec0b0-1"/><custom1 class="v-r" v-i="2a9ec0b0-2"/>`,
`(_ctx, _cache) => {
return {}
}`
)
})
test('vue-ref-in-for', () => {
test('v-r-i-f', () => {
assert(
`<custom v-for="item in items"/>`,
`<custom wx:for="{{a}}" wx:for-item="item" class="vue-ref-in-for" vue-id="{{item.a}}"/>`,
`<custom wx:for="{{a}}" wx:for-item="item" class="v-r-i-f" v-i="{{item.a}}"/>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-0' + '-' + i0 }; }) }
}`
......@@ -29,7 +29,7 @@ describe('compiler: transform ref', () => {
test('static ref', () => {
assert(
`<custom ref="custom"/>`,
`<custom class="vue-ref" data-ref="custom" vue-id="2a9ec0b0-0"/>`,
`<custom class="v-r" data-ref="custom" v-i="2a9ec0b0-0"/>`,
`(_ctx, _cache) => {
return {}
}`
......@@ -38,7 +38,7 @@ describe('compiler: transform ref', () => {
test('dynamic ref', () => {
assert(
`<custom :ref="custom"/>`,
`<custom class="vue-ref" data-ref="{{a}}" vue-id="2a9ec0b0-0"/>`,
`<custom class="v-r" data-ref="{{a}}" v-i="2a9ec0b0-0"/>`,
`(_ctx, _cache) => {
return { a: _ctx.custom }
}`
......
......@@ -4,7 +4,7 @@ describe('compiler: transform v-model', () => {
test(`component v-model`, () => {
assert(
`<Comp v-model="model" />`,
`<comp class="vue-ref" vue-id="2a9ec0b0-0" modelValue="{{a}}" bindupdateModelValue="{{b}}"/>`,
`<comp class="v-r" v-i="2a9ec0b0-0" modelValue="{{a}}" bindupdateModelValue="{{b}}"/>`,
`(_ctx, _cache) => {
return { a: _ctx.model, b: _vOn($event => _ctx.model = $event) }
}`
......@@ -13,7 +13,7 @@ describe('compiler: transform v-model', () => {
test(`component v-model with cache`, () => {
assert(
`<Comp v-model="model" />`,
`<comp class="vue-ref" vue-id="2a9ec0b0-0" modelValue="{{a}}" bindupdateModelValue="{{b}}"/>`,
`<comp class="v-r" v-i="2a9ec0b0-0" modelValue="{{a}}" bindupdateModelValue="{{b}}"/>`,
`(_ctx, _cache) => {
return { a: _ctx.model, b: _vOn($event => _ctx.model = $event) }
}`,
......
......@@ -4,14 +4,14 @@ describe('compiler: transform v-slot', () => {
test('default slot', () => {
assert(
`<custom><template v-slot/></custom>`,
`<custom vue-slots="{{['default']}}" class="vue-ref" vue-id="2a9ec0b0-0"><view /></custom>`,
`<custom vue-slots="{{['default']}}" class="v-r" v-i="2a9ec0b0-0"><view /></custom>`,
`(_ctx, _cache) => {
return {}
}`
)
assert(
`<custom>test</custom>`,
`<custom vue-slots="{{['default']}}" class="vue-ref" vue-id="2a9ec0b0-0">test</custom>`,
`<custom vue-slots="{{['default']}}" class="v-r" v-i="2a9ec0b0-0">test</custom>`,
`(_ctx, _cache) => {
return {}
}`
......@@ -20,7 +20,7 @@ describe('compiler: transform v-slot', () => {
test('named slots', () => {
assert(
`<custom><template v-slot:header/><template v-slot:default/><template v-slot:footer/></custom>`,
`<custom vue-slots="{{['header','default','footer']}}" class="vue-ref" vue-id="2a9ec0b0-0"><view slot="header"/><view slot="default"/><view slot="footer"/></custom>`,
`<custom vue-slots="{{['header','default','footer']}}" class="v-r" v-i="2a9ec0b0-0"><view slot="header"/><view slot="default"/><view slot="footer"/></custom>`,
`(_ctx, _cache) => {
return {}
}`
......@@ -30,7 +30,7 @@ describe('compiler: transform v-slot', () => {
test('scoped slots', () => {
assert(
`<custom><template v-slot:default="slotProps"><view>{{ slotProps.item }}</view></template></custom>`,
`<custom vue-slots="{{['default']}}" class="vue-ref" vue-id="2a9ec0b0-0"><view slot="default"><view>{{a}}</view></view></custom>`,
`<custom vue-slots="{{['default']}}" class="v-r" v-i="2a9ec0b0-0"><view slot="default"><view>{{a}}</view></view></custom>`,
`(_ctx, _cache) => {
return { a: _toDisplayString(_ctx.slotProps.item), b: slotProps }
}`
......
......@@ -48,7 +48,7 @@ export function baseCompile(template: string, options: CompilerOptions = {}) {
skipTransformIdentifier: options.skipTransformIdentifier === true,
})
options.vueId = genVueId(options)
options.hashId = genHashId(options)
if (options.filename) {
if (options.filters && options.miniProgram?.filter) {
......@@ -94,9 +94,9 @@ export function baseCompile(template: string, options: CompilerOptions = {}) {
return result
}
function genVueId(options: CompilerOptions) {
if (options.vueId) {
return options.vueId
function genHashId(options: CompilerOptions) {
if (options.hashId) {
return options.hashId
}
if (options.scopeId) {
return options.scopeId.replace('data-v-', '')
......
......@@ -46,7 +46,7 @@ interface SharedTransformCodegenOptions {
export interface TransformOptions
extends SharedTransformCodegenOptions,
ErrorHandlingOptions {
vueId?: string | null
hashId?: string | null
scopeId?: string | null
filters?: string[]
cacheHandlers?: boolean
......
......@@ -216,7 +216,7 @@ export function createTransformContext(
filename = '',
isTS = false,
inline = false,
vueId = null,
hashId = null,
scopeId = null,
filters = [],
bindingMetadata = EMPTY_OBJ,
......@@ -275,7 +275,7 @@ export function createTransformContext(
selfName: nameMatch && capitalize(camelize(nameMatch[1])),
isTS,
inline,
vueId,
hashId,
scopeId,
filters,
bindingMetadata,
......
......@@ -28,11 +28,11 @@ export const transformComponent: NodeTransform = (node, context) => {
}
function addVueId(node: ComponentNode, context: TransformContext) {
let { vueId, scopes, currentScope, currentVueId } = context
if (!vueId) {
let { hashId, scopes, currentScope, currentVueId } = context
if (!hashId) {
return
}
vueId = vueId + '-' + scopes.vueId++
let vueId = hashId + '-' + scopes.vueId++
const indexs: string[] = []
while (currentScope) {
if (isVForScope(currentScope)) {
......@@ -60,14 +60,16 @@ function addVueId(node: ComponentNode, context: TransformContext) {
}
}
if (value.includes('+')) {
return node.props.push(createBindDirectiveNode('vue-id', value))
return node.props.push(createBindDirectiveNode('v-i', value))
}
return node.props.push(createAttributeNode('vue-id', value))
return node.props.push(createAttributeNode('v-i', value))
}
function addVueRef(node: ComponentNode, context: TransformContext) {
return addStaticClass(
node,
context.scopes.vFor ? 'vue-ref-in-for' : 'vue-ref'
// vue-ref-in-for
// vue-ref
context.scopes.vFor ? 'v-r-i-f' : 'v-r'
)
}
......@@ -82,7 +82,8 @@ export const transformIdentifier: NodeTransform = (node, context) => {
}
}
// const builtInProps = ['vue-id']
// vue-id
// const builtInProps = ['v-i']
function isBuiltIn(_dir: DirectiveNode) {
return false
......
......@@ -37,7 +37,7 @@ function parsePropType(key: string, type: unknown, defaultValue: unknown) {
function initDefaultProps(isBehavior: boolean = false) {
const properties: Component.PropertyOption = {}
if (!isBehavior) {
properties.vueId = {
properties.vI = {
type: String,
value: '',
}
......
......@@ -88,8 +88,8 @@ export function initRefs(
Object.defineProperty(instance, 'refs', {
get() {
const $refs: Record<string, any> = {}
selectAllComponents(mpInstance, '.vue-ref', $refs)
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for')
selectAllComponents(mpInstance, '.v-r', $refs)
const forComponents = mpInstance.selectAllComponents('.v-r-i-f')
forComponents.forEach((component) => {
const ref = component.dataset.ref
if (!$refs[ref]) {
......
......@@ -175,8 +175,8 @@ function initRefs(instance, mpInstance) {
Object.defineProperty(instance, 'refs', {
get() {
const $refs = {};
selectAllComponents(mpInstance, '.vue-ref', $refs);
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
selectAllComponents(mpInstance, '.v-r', $refs);
const forComponents = mpInstance.selectAllComponents('.v-r-i-f');
forComponents.forEach((component) => {
const ref = component.dataset.ref;
if (!$refs[ref]) {
......@@ -651,7 +651,7 @@ function parsePropType(key, type, defaultValue) {
function initDefaultProps(isBehavior = false) {
const properties = {};
if (!isBehavior) {
properties.vueId = {
properties.vI = {
type: String,
value: '',
};
......@@ -1161,7 +1161,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) {
return {
attached() {
const properties = this.properties;
initVueIds(properties.vueId, this);
initVueIds(properties.vI, this);
const relationOptions = {
vuePid: this._$vuePid,
};
......
......@@ -175,8 +175,8 @@ function initRefs(instance, mpInstance) {
Object.defineProperty(instance, 'refs', {
get() {
const $refs = {};
selectAllComponents(mpInstance, '.vue-ref', $refs);
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
selectAllComponents(mpInstance, '.v-r', $refs);
const forComponents = mpInstance.selectAllComponents('.v-r-i-f');
forComponents.forEach((component) => {
const ref = component.dataset.ref;
if (!$refs[ref]) {
......@@ -651,7 +651,7 @@ function parsePropType(key, type, defaultValue) {
function initDefaultProps(isBehavior = false) {
const properties = {};
if (!isBehavior) {
properties.vueId = {
properties.vI = {
type: String,
value: '',
};
......@@ -1161,7 +1161,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) {
return {
attached() {
const properties = this.properties;
initVueIds(properties.vueId, this);
initVueIds(properties.vI, this);
const relationOptions = {
vuePid: this._$vuePid,
};
......
......@@ -175,8 +175,8 @@ function initRefs(instance, mpInstance) {
Object.defineProperty(instance, 'refs', {
get() {
const $refs = {};
selectAllComponents(mpInstance, '.vue-ref', $refs);
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
selectAllComponents(mpInstance, '.v-r', $refs);
const forComponents = mpInstance.selectAllComponents('.v-r-i-f');
forComponents.forEach((component) => {
const ref = component.dataset.ref;
if (!$refs[ref]) {
......@@ -654,7 +654,7 @@ function parsePropType(key, type, defaultValue) {
function initDefaultProps(isBehavior = false) {
const properties = {};
if (!isBehavior) {
properties.vueId = {
properties.vI = {
type: String,
value: '',
};
......@@ -1255,7 +1255,7 @@ function initLifetimes$1({ mocks, isPage, initRelation, vueOptions, }) {
return {
attached() {
const properties = this.properties;
initVueIds(properties.vueId, this);
initVueIds(properties.vI, this);
const relationOptions = {
vuePid: this._$vuePid,
};
......
......@@ -25,7 +25,7 @@ export function initLifetimes({
return {
attached(this: MPComponentInstance) {
const properties = this.properties
initVueIds(properties.vueId, this)
initVueIds(properties.vI, this)
const relationOptions: RelationOptions = {
vuePid: this._$vuePid,
}
......
......@@ -121,8 +121,8 @@ function initRefs(instance, mpInstance) {
Object.defineProperty(instance, 'refs', {
get() {
const $refs = {};
selectAllComponents(mpInstance, '.vue-ref', $refs);
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
selectAllComponents(mpInstance, '.v-r', $refs);
const forComponents = mpInstance.selectAllComponents('.v-r-i-f');
forComponents.forEach((component) => {
const ref = component.dataset.ref;
if (!$refs[ref]) {
......@@ -597,7 +597,7 @@ function parsePropType(key, type, defaultValue) {
function initDefaultProps(isBehavior = false) {
const properties = {};
if (!isBehavior) {
properties.vueId = {
properties.vI = {
type: String,
value: '',
};
......@@ -1107,7 +1107,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) {
return {
attached() {
const properties = this.properties;
initVueIds(properties.vueId, this);
initVueIds(properties.vI, this);
const relationOptions = {
vuePid: this._$vuePid,
};
......
......@@ -26,7 +26,7 @@ export function initLifetimes({
return {
attached(this: MPComponentInstance) {
const properties = this.properties
initVueIds(properties.vueId, this)
initVueIds(properties.vI, this)
const relationOptions: RelationOptions = {
vuePid: this._$vuePid,
}
......
......@@ -175,8 +175,8 @@ function initRefs(instance, mpInstance) {
Object.defineProperty(instance, 'refs', {
get() {
const $refs = {};
selectAllComponents(mpInstance, '.vue-ref', $refs);
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
selectAllComponents(mpInstance, '.v-r', $refs);
const forComponents = mpInstance.selectAllComponents('.v-r-i-f');
forComponents.forEach((component) => {
const ref = component.dataset.ref;
if (!$refs[ref]) {
......@@ -582,7 +582,7 @@ function parsePropType(key, type, defaultValue) {
function initDefaultProps(isBehavior = false) {
const properties = {};
if (!isBehavior) {
properties.vueId = {
properties.vI = {
type: String,
value: '',
};
......@@ -1177,7 +1177,7 @@ function initLifetimes$1({ mocks, isPage, initRelation, vueOptions, }) {
return {
attached() {
const properties = this.properties;
initVueIds(properties.vueId, this);
initVueIds(properties.vI, this);
const relationOptions = {
vuePid: this._$vuePid,
};
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册