From: Evan You Date: Fri, 5 Mar 2021 17:12:49 +0000 (-0500) Subject: perf: support only attaching slot scope ids when necessary X-Git-Tag: v3.0.8~64 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=02cbbb718ca226b087c42e6f132120931307c2a6;p=thirdparty%2Fvuejs%2Fcore.git 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. --- diff --git a/packages/compiler-core/__tests__/transforms/transformSlotOutlet.spec.ts b/packages/compiler-core/__tests__/transforms/transformSlotOutlet.spec.ts index 82a5c8e50e..1a4ea3da8b 100644 --- a/packages/compiler-core/__tests__/transforms/transformSlotOutlet.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformSlotOutlet.spec.ts @@ -339,6 +339,15 @@ describe('compiler: transform outlets', () => { }) }) + test('slot with slotted: true', async () => { + const ast = parseWithSlots(``, { 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 `, () => { const onError = jest.fn() const source = `` diff --git a/packages/compiler-core/src/options.ts b/packages/compiler-core/src/options.ts index 2850da196b..d8d2573a6e 100644 --- a/packages/compiler-core/src/options.ts +++ b/packages/compiler-core/src/options.ts @@ -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 ``).descriptor + .slotted + ).toBe(false) + expect( + parse(``) + .descriptor.slotted + ).toBe(true) + expect( + parse(``) + .descriptor.slotted + ).toBe(true) + }) + test('error tolerance', () => { const { errors } = parse(`