-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(css): add support for injecting css into custom elements
- Loading branch information
Sven Tschui
committed
Feb 26, 2023
1 parent
2aad552
commit 94b41d9
Showing
9 changed files
with
140 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { expect, test } from 'vitest' | ||
import { editFile, getColor, page, untilUpdated } from '~utils' | ||
|
||
test('css inject', async () => { | ||
const linkedOutside = await page.$('.linked.outside') | ||
const linkedInside = await page.$('.linked.inside') | ||
const importedOutside = await page.$('.imported.outside') | ||
const importedInside = await page.$('.imported.inside') | ||
|
||
expect(await getColor(linkedOutside)).toBe('red') | ||
expect(await getColor(linkedInside)).toBe('black') | ||
expect(await getColor(importedOutside)).toBe('black') | ||
expect(await getColor(importedInside)).toBe('red') | ||
|
||
editFile('linked.css', (code) => code.replace('color: red', 'color: blue')) | ||
|
||
await untilUpdated(() => getColor(linkedOutside), 'blue') | ||
expect(await getColor(linkedInside)).toBe('black') | ||
|
||
editFile('imported.css', (code) => code.replace('color: red', 'color: blue')) | ||
|
||
await untilUpdated(() => getColor(importedInside), 'blue') | ||
expect(await getColor(importedOutside)).toBe('black') | ||
}) |
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 @@ | ||
.imported { | ||
color: red; | ||
} |
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,37 @@ | ||
<script></script> | ||
<link rel="stylesheet" href="./linked.css" /> | ||
|
||
<div class="wrapper"> | ||
<h1>CSS Inject</h1> | ||
|
||
<p class="linked outside"><linked></p> | ||
|
||
<p class="imported outside"><imported></p> | ||
|
||
<custom-element></custom-element> | ||
</div> | ||
|
||
<script type="module"> | ||
customElements.define( | ||
'custom-element', | ||
class CustomElement extends HTMLElement { | ||
constructor() { | ||
super() | ||
console.log('ctor') | ||
this.attachShadow({ mode: 'open' }) | ||
} | ||
|
||
connectedCallback() { | ||
console.log('connected') | ||
|
||
this.shadowRoot.innerHTML = ` | ||
<p class="linked inside"><linked></p> | ||
<p class="imported inside"><imported></p> | ||
` | ||
|
||
import('./imported.css') | ||
} | ||
}, | ||
) | ||
</script> |
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 @@ | ||
.linked { | ||
color: red; | ||
} |
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,11 @@ | ||
{ | ||
"name": "@vitejs/test-css-sourcemap", | ||
"private": true, | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"debug": "node --inspect-brk ../../packages/vite/bin/vite", | ||
"preview": "vite preview" | ||
} | ||
} |
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,21 @@ | ||
const MagicString = require('magic-string') | ||
|
||
/** | ||
* @type {import('vite').UserConfig} | ||
*/ | ||
module.exports = { | ||
resolve: { | ||
alias: { | ||
'@': __dirname, | ||
}, | ||
}, | ||
css: { | ||
devSourcemap: true, | ||
inject: (node) => { | ||
document.body.querySelector('custom-element').shadowRoot.appendChild(node) | ||
}, | ||
}, | ||
build: { | ||
sourcemap: true, | ||
}, | ||
} |