Skip to content

Commit cc0cd60

Browse files
authored
perf(engine-core): render separate style tags (#2870)
* perf(engine-core): render separate style tags * fix: use ArrayUnshift instead of ArraySplice
1 parent a016322 commit cc0cd60

File tree

14 files changed

+70
-13
lines changed

14 files changed

+70
-13
lines changed

packages/@lwc/engine-core/src/framework/stylesheet.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: MIT
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
7-
import { ArrayJoin, ArrayPush, isArray, isNull, isUndefined, KEY__SCOPED_CSS } from '@lwc/shared';
7+
import { ArrayMap, ArrayPush, isArray, isNull, isUndefined, KEY__SCOPED_CSS } from '@lwc/shared';
88

99
import api from './api';
1010
import { RenderMode, ShadowMode, VM } from './vm';
@@ -199,7 +199,7 @@ function getNearestNativeShadowComponent(vm: VM): VM | null {
199199
return owner;
200200
}
201201

202-
export function createStylesheet(vm: VM, stylesheets: string[]): VNode | null {
202+
export function createStylesheet(vm: VM, stylesheets: string[]): VNode[] | null {
203203
const {
204204
renderMode,
205205
shadowMode,
@@ -215,8 +215,7 @@ export function createStylesheet(vm: VM, stylesheets: string[]): VNode | null {
215215
// the first time the VM renders.
216216

217217
// native shadow or light DOM, SSR
218-
const combinedStylesheetContent = ArrayJoin.call(stylesheets, '\n');
219-
return createInlineStyleVNode(combinedStylesheetContent);
218+
return ArrayMap.call(stylesheets, createInlineStyleVNode) as VNode[];
220219
} else {
221220
// native shadow or light DOM, DOM renderer
222221
const root = getNearestNativeShadowComponent(vm);

packages/@lwc/engine-core/src/framework/template.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function evaluateTemplate(vm: VM, html: Template): VNodes {
180180
// Evaluate, create stylesheet and cache the produced VNode for future
181181
// re-rendering.
182182
const stylesheetsContent = getStylesheetsContent(vm, html);
183-
context.styleVNode =
183+
context.styleVNodes =
184184
stylesheetsContent.length === 0
185185
? null
186186
: createStylesheet(vm, stylesheetsContent);
@@ -200,9 +200,9 @@ export function evaluateTemplate(vm: VM, html: Template): VNodes {
200200
isUpdatingTemplate = true;
201201

202202
vnodes = html.call(undefined, api, component, cmpSlots, context.tplCache);
203-
const { styleVNode } = context;
204-
if (!isNull(styleVNode)) {
205-
ArrayUnshift.call(vnodes, styleVNode);
203+
const { styleVNodes } = context;
204+
if (!isNull(styleVNodes)) {
205+
ArrayUnshift.apply(vnodes, styleVNodes);
206206
}
207207
});
208208
},

packages/@lwc/engine-core/src/framework/vm.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ export interface Context {
8787
hasTokenInAttribute: boolean | undefined;
8888
/** Whether or not light DOM scoped styles are present in the stylesheets. */
8989
hasScopedStyles: boolean | undefined;
90-
/** The VNode injected in all the shadow trees to apply the associated component stylesheets. */
91-
styleVNode: VNode | null;
90+
/** The VNodes injected in all the shadow trees to apply the associated component stylesheets. */
91+
styleVNodes: VNode[] | null;
9292
/** Object used by the template function to store information that can be reused between
9393
* different render cycle of the same template. */
9494
tplCache: TemplateCache;
@@ -304,7 +304,7 @@ export function createVM<HostNode, HostElement>(
304304
hasTokenInClass: undefined,
305305
hasTokenInAttribute: undefined,
306306
hasScopedStyles: undefined,
307-
styleVNode: null,
307+
styleVNodes: null,
308308
tplCache: EmptyObject,
309309
wiredConnecting: EmptyArray,
310310
wiredDisconnecting: EmptyArray,

packages/@lwc/engine-server/src/__tests__/fixtures/light-dom-multiple/expected.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<x-parent>
22
<style type="text/css">
33
p {background-color: blue;}
4-
p {color: red;}
4+
</style>
5+
<style type="text/css">
6+
p {color: red;}
57
</style>
68
<p>
79
Hello

packages/@lwc/engine-server/src/__tests__/fixtures/light-dom-scoped-styles/expected.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<x-basic class="x-basic_basic-host">
22
<style class="x-basic_basic" type="text/css">
33
p {color: red;}
4-
p.x-basic_basic {background-color: blue;}.x-basic_basic-host {display: block;border: 1px solid black;}
4+
</style>
5+
<style class="x-basic_basic" type="text/css">
6+
p.x-basic_basic {background-color: blue;}.x-basic_basic-host {display: block;border: 1px solid black;}
57
</style>
68
<p class="x-basic_basic">
79
Hello
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<x-parent>
2+
<template shadowroot="open">
3+
<style type="text/css">
4+
div {border: 1px solid turquoise;}
5+
</style>
6+
<style type="text/css">
7+
div {background: blue;}
8+
</style>
9+
<x-child>
10+
<template shadowroot="open">
11+
<style type="text/css">
12+
div {border: 1px solid turquoise;}
13+
</style>
14+
<style type="text/css">
15+
div {color: red;}
16+
</style>
17+
<div>
18+
Hello child
19+
</div>
20+
</template>
21+
</x-child>
22+
<div>
23+
Hello parent
24+
</div>
25+
</template>
26+
</x-parent>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const tagName = 'x-parent';
2+
export { default } from 'x/parent';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import '../shared.css';
2+
3+
div {
4+
color: red;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>Hello child</div>
3+
</template>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { LightningElement } from 'lwc';
2+
3+
export default class extends LightningElement {}

0 commit comments

Comments
 (0)