perf: support only attaching slot scope ids when necessary
This is done by adding the `slotted: false` option to: - compiler-dom - compiler-ssr - compiler-sfc (forwarded to template compiler) At runtime, only slotted component will render slot fragments with slot scope Ids. For SSR, only slotted component will add slot scope Ids to rendered slot content. This should improve both runtime performance and reduce SSR rendered markup size. Note: requires SFC tooling (e.g. `vue-loader` and `vite`) to pass on the `slotted` option from the SFC descriptoer to the `compileTemplate` call.
This commit is contained in:
@@ -339,6 +339,15 @@ describe('compiler: transform <slot> outlets', () => {
|
||||
})
|
||||
})
|
||||
|
||||
test('slot with slotted: true', async () => {
|
||||
const ast = parseWithSlots(`<slot/>`, { slotted: true })
|
||||
expect((ast.children[0] as ElementNode).codegenNode).toMatchObject({
|
||||
type: NodeTypes.JS_CALL_EXPRESSION,
|
||||
callee: RENDER_SLOT,
|
||||
arguments: [`$slots`, `"default"`, `{}`, `undefined`, `true`]
|
||||
})
|
||||
})
|
||||
|
||||
test(`error on unexpected custom directive on <slot>`, () => {
|
||||
const onError = jest.fn()
|
||||
const source = `<slot v-foo />`
|
||||
|
||||
@@ -199,6 +199,12 @@ export interface TransformOptions extends SharedTransformCodegenOptions {
|
||||
* SFC scoped styles ID
|
||||
*/
|
||||
scopeId?: string | null
|
||||
/**
|
||||
* Indicates this SFC template has used :slotted in its styles
|
||||
* Defaults to `true` for backwards compatibility - SFC tooling should set it
|
||||
* to `false` if no `:slotted` usage is detected in `<style>`
|
||||
*/
|
||||
slotted?: boolean
|
||||
/**
|
||||
* SFC `<style vars>` injection string
|
||||
* Should already be an object expression, e.g. `{ 'xxxx-color': color }`
|
||||
|
||||
@@ -128,6 +128,7 @@ export function createTransformContext(
|
||||
isCustomElement = NOOP,
|
||||
expressionPlugins = [],
|
||||
scopeId = null,
|
||||
slotted = true,
|
||||
ssr = false,
|
||||
ssrCssVars = ``,
|
||||
bindingMetadata = EMPTY_OBJ,
|
||||
@@ -150,6 +151,7 @@ export function createTransformContext(
|
||||
isCustomElement,
|
||||
expressionPlugins,
|
||||
scopeId,
|
||||
slotted,
|
||||
ssr,
|
||||
ssrCssVars,
|
||||
bindingMetadata,
|
||||
|
||||
@@ -34,6 +34,16 @@ export const transformSlotOutlet: NodeTransform = (node, context) => {
|
||||
slotArgs.push(createFunctionExpression([], children, false, false, loc))
|
||||
}
|
||||
|
||||
if (context.slotted) {
|
||||
if (!slotProps) {
|
||||
slotArgs.push(`{}`)
|
||||
}
|
||||
if (!children.length) {
|
||||
slotArgs.push(`undefined`)
|
||||
}
|
||||
slotArgs.push(`true`)
|
||||
}
|
||||
|
||||
node.codegenNode = createCallExpression(
|
||||
context.helper(RENDER_SLOT),
|
||||
slotArgs,
|
||||
|
||||
Reference in New Issue
Block a user