-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: use constructable stylesheets (#2460)
* perf: use constructable stylesheets * fix: update packages/perf-benchmarks-components/scripts/generate-styled-components.js Co-authored-by: Pierre-Marie Dartus <p.dartus@salesforce.com> * fix: respond to PR comments * fix: fix formatting Co-authored-by: Pierre-Marie Dartus <p.dartus@salesforce.com>
- Loading branch information
1 parent
6277957
commit 1898df2
Showing
19 changed files
with
315 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/integration-karma/test/shadow-dom/multiple-templates/index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { createElement } from 'lwc'; | ||
import Multi from 'x/multi'; | ||
|
||
if (process.env.NATIVE_SHADOW) { | ||
describe('Shadow DOM styling - multiple shadow DOM components', () => { | ||
it('Does not duplicate styles if template is re-rendered', () => { | ||
const element = createElement('x-multi', { is: Multi }); | ||
|
||
const getNumStyleSheets = () => { | ||
if (element.shadowRoot.adoptedStyleSheets) { | ||
return element.shadowRoot.adoptedStyleSheets.length; | ||
} else { | ||
return element.shadowRoot.querySelectorAll('style').length; | ||
} | ||
}; | ||
|
||
document.body.appendChild(element); | ||
return Promise.resolve() | ||
.then(() => { | ||
expect(getComputedStyle(element.shadowRoot.querySelector('div')).color).toEqual( | ||
'rgb(0, 0, 255)' | ||
); | ||
expect(getNumStyleSheets()).toEqual(1); | ||
element.next(); | ||
}) | ||
.then(() => { | ||
expect(getComputedStyle(element.shadowRoot.querySelector('div')).color).toEqual( | ||
'rgb(255, 0, 0)' | ||
); | ||
expect(getNumStyleSheets()).toEqual(2); | ||
element.next(); | ||
}) | ||
.then(() => { | ||
expect(getComputedStyle(element.shadowRoot.querySelector('div')).color).toEqual( | ||
'rgb(0, 0, 255)' | ||
); | ||
expect(getNumStyleSheets()).toEqual(2); | ||
}); | ||
}); | ||
}); | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/shadow-dom/multiple-templates/x/multi/a.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.blue { | ||
color: blue; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/shadow-dom/multiple-templates/x/multi/a.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<div class="blue">a</div> | ||
</template> |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/shadow-dom/multiple-templates/x/multi/b.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.red { | ||
color: red; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/shadow-dom/multiple-templates/x/multi/b.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<div class="red">b</div> | ||
</template> |
16 changes: 16 additions & 0 deletions
16
packages/integration-karma/test/shadow-dom/multiple-templates/x/multi/multi.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { LightningElement, api } from 'lwc'; | ||
import A from './a.html'; | ||
import B from './b.html'; | ||
|
||
export default class Multi extends LightningElement { | ||
current = A; | ||
|
||
@api | ||
next() { | ||
this.current = this.current === A ? B : A; | ||
} | ||
|
||
render() { | ||
return this.current; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/perf-benchmarks-components/scripts/generate-styled-components.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
import path from 'path'; | ||
import fs from 'fs'; | ||
import os from 'os'; | ||
|
||
const NUM_COMPONENTS = 1000; | ||
|
||
// Generates some components with individual CSS for each one. | ||
// We could use @rollup/plugin-virtual for this, but @lwc/rollup-plugin deliberately | ||
// filters virtual modules, so it's simpler to just write to a temp dir. | ||
export function generateStyledComponents() { | ||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'lwc-')); | ||
|
||
const components = Array(NUM_COMPONENTS) | ||
.fill() | ||
.map((_, i) => | ||
path.join(tmpDir, `src/benchmark/styledComponent${i}/styledComponent${i}.js`) | ||
); | ||
|
||
components.forEach((jsFilename, i) => { | ||
const cssFilename = jsFilename.replace('.js', '.css'); | ||
const htmlFilename = jsFilename.replace('.js', '.html'); | ||
|
||
const js = | ||
'import { LightningElement } from "lwc"; export default class extends LightningElement {}'; | ||
const css = `div { color: ${i.toString(16).padStart(6, '0')}}`; | ||
const html = '<template><div></div></template>'; | ||
|
||
fs.mkdirSync(path.dirname(jsFilename), { recursive: true }); | ||
fs.writeFileSync(jsFilename, js, 'utf-8'); | ||
fs.writeFileSync(cssFilename, css, 'utf-8'); | ||
fs.writeFileSync(htmlFilename, html, 'utf-8'); | ||
}); | ||
|
||
const oneComponentFilename = path.join(tmpDir, 'src/benchmark/styledComponent.js'); | ||
const oneComponent = `export { default } from ${JSON.stringify(components[0])};`; | ||
fs.writeFileSync(oneComponentFilename, oneComponent, 'utf-8'); | ||
|
||
const allComponentsFilename = path.join(tmpDir, 'src/benchmark/styledComponents.js'); | ||
const allComponents = ` | ||
${components.map((mod, i) => `import cmp${i} from ${JSON.stringify(mod)}`).join(';')}; | ||
export default [${components.map((_, i) => `cmp${i}`).join(',')}];`; | ||
fs.writeFileSync(allComponentsFilename, allComponents, 'utf-8'); | ||
|
||
return { | ||
styledComponents: [oneComponentFilename, allComponentsFilename], | ||
tmpDir, | ||
}; | ||
} |
12 changes: 12 additions & 0 deletions
12
...ngine-dom/benchmark-styled-component/ss-styled-component-create-1k-different.benchmark.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
import components from 'perf-benchmarks-components/dist/dom/benchmark/styledComponents.js'; | ||
import { styledComponentBenchmark } from '../../../utils/styledComponentBenchmark'; | ||
|
||
// Create 1k components with different CSS in each component | ||
styledComponentBenchmark(`ss-benchmark-styled-component/create/1k/different`, components); |
12 changes: 12 additions & 0 deletions
12
...s__/engine-dom/benchmark-styled-component/ss-styled-component-create-1k-same.benchmark.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
import StyledComponent from 'perf-benchmarks-components/dist/dom/benchmark/styledComponent.js'; | ||
import { styledComponentBenchmark } from '../../../utils/styledComponentBenchmark'; | ||
|
||
// Create 1k components with the same CSS in each component | ||
styledComponentBenchmark(`ss-benchmark-styled-component/create/1k/same`, StyledComponent); |
12 changes: 12 additions & 0 deletions
12
..._/engine-dom/benchmark-styled-component/styled-component-create-1k-different.benchmark.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
import components from 'perf-benchmarks-components/dist/dom/benchmark/styledComponents.js'; | ||
import { styledComponentBenchmark } from '../../../utils/styledComponentBenchmark'; | ||
|
||
// Create 1k components with different CSS in each component | ||
styledComponentBenchmark(`benchmark-styled-component/create/1k/different`, components); |
Oops, something went wrong.