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:
Evan You
2021-03-05 12:12:49 -05:00
parent f74b16ccfe
commit 02cbbb718c
11 changed files with 110 additions and 31 deletions

View File

@@ -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 }`

View File

@@ -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,

View File

@@ -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,