-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(css): add support for injecting css into custom elements #12206
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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') | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.imported { | ||
color: red; | ||
} |
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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about this feature to "alternate" the Wondering why you want to do it this implicit way, instead of appending the CSS manually with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?inline can't be applied (as far as I'm aware) to styles within Vue SFCs. Which is my use case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @antfu does ?inline works for css imported by 3rd party lib? |
||
} | ||
}, | ||
) | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.linked { | ||
color: red; | ||
} |
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" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @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, | ||
}, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of the tests are failing on this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They fail for production mode which is not in our scope. One would need to find another way for production mode support. As we worked around this issue I'll close this PR. Anyone is welcome to re-use my work to continue on this.