Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler-vapor): remove unused empty DOM from slots when whitespace is preserve #13009

Open
wants to merge 2 commits into
base: vapor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,68 @@ export function render(_ctx) {
return n1
}"
`;

exports[`compiler: transform slot > with whitespace: 'preserve' > implicit default slot 1`] = `
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
const t0 = _template(" Header ")
const t1 = _template(" ")
const t2 = _template("<p></p>")

export function render(_ctx) {
const _component_Comp = _resolveComponent("Comp")
const n4 = _createComponentWithFallback(_component_Comp, null, {
"header": () => {
const n0 = t0()
return n0
},
"default": () => {
const n2 = t1()
const n3 = t2()
return [n2, n3]
}
}, true)
return n4
}"
`;

exports[`compiler: transform slot > with whitespace: 'preserve' > named default slot + implicit whitespace content 1`] = `
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
const t0 = _template(" Header ")
const t1 = _template(" Default ")

export function render(_ctx) {
const _component_Comp = _resolveComponent("Comp")
const n4 = _createComponentWithFallback(_component_Comp, null, {
"header": () => {
const n0 = t0()
return n0
},
"default": () => {
const n2 = t1()
return n2
}
}, true)
return n4
}"
`;

exports[`compiler: transform slot > with whitespace: 'preserve' > should not generate whitespace only default slot 1`] = `
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
const t0 = _template(" Header ")
const t1 = _template(" Footer ")

export function render(_ctx) {
const _component_Comp = _resolveComponent("Comp")
const n4 = _createComponentWithFallback(_component_Comp, null, {
"header": () => {
const n0 = t0()
return n0
},
"footer": () => {
const n2 = t1()
return n2
}
}, true)
return n4
}"
`;
55 changes: 55 additions & 0 deletions packages/compiler-vapor/__tests__/transforms/vSlot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,59 @@ describe('compiler: transform slot', () => {
})
})
})

describe(`with whitespace: 'preserve'`, () => {
test('named default slot + implicit whitespace content', () => {
const source = `
<Comp>
<template #header> Header </template>
<template #default> Default </template>
</Comp>
`
const { code } = compileWithSlots(source, {
whitespace: 'preserve',
})

expect(
`Extraneous children found when component already has explicitly named default slot.`,
).not.toHaveBeenWarned()
expect(code).toMatchSnapshot()
})

test('implicit default slot', () => {
const source = `
<Comp>
<template #header> Header </template>
<p/>
</Comp>
`
const { code } = compileWithSlots(source, {
whitespace: 'preserve',
})

expect(
`Extraneous children found when component already has explicitly named default slot.`,
).not.toHaveBeenWarned()
expect(code).toMatchSnapshot()
})

test('should not generate whitespace only default slot', () => {
const source = `
<Comp>
<template #header> Header </template>
<template #footer> Footer </template>
</Comp>
`
const { code, ir } = compileWithSlots(source, {
whitespace: 'preserve',
})

const slots = (ir.block.operation[0] as any).slots[0].slots
// should be: header, footer (no default)
expect(Object.keys(slots).length).toBe(2)
expect(!!slots['default']).toBe(false)

expect(code).toMatchSnapshot()
})
})
})
18 changes: 13 additions & 5 deletions packages/compiler-vapor/src/transforms/vSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,19 @@ function transformComponentSlot(
) {
const { children } = node
const arg = dir && dir.arg
const nonSlotTemplateChildren = children.filter(
n =>
isNonWhitespaceContent(node) &&
!(n.type === NodeTypes.ELEMENT && n.props.some(isVSlot)),
)

// whitespace: 'preserve'
let indexes: number[] = []
const nonSlotTemplateChildren = children.filter((n, i) => {
if (isNonWhitespaceContent(n)) {
return !(n.type === NodeTypes.ELEMENT && n.props.some(isVSlot))
} else {
indexes.push(i)
}
})
if (!nonSlotTemplateChildren.length) {
indexes.forEach(i => children.splice(i, 1))
}

const [block, onExit] = createSlotBlock(node, dir, context)

Expand Down