Skip to content

Commit

Permalink
fix: fallback to <style> insertion when css contains @import
Browse files Browse the repository at this point in the history
fix #448
  • Loading branch information
yyx990803 committed Jun 24, 2020
1 parent 50537c4 commit 422b4aa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions playground/css/TestPostCss.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default {}
</script>

<style>
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
body {
& .postcss-from-sfc {
color: green;
Expand Down
18 changes: 15 additions & 3 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ const sheetsMap = new Map()

export function updateStyle(id: string, content: string) {
let style = sheetsMap.get(id)
if (supportsConstructedSheet) {
if (supportsConstructedSheet && !content.includes('@import')) {
if (style && !(style instanceof CSSStyleSheet)) {
removeStyle(id)
style = undefined
}

if (!style) {
style = new CSSStyleSheet()
style.replaceSync(content)
Expand All @@ -172,6 +177,11 @@ export function updateStyle(id: string, content: string) {
style.replaceSync(content)
}
} else {
if (style && !(style instanceof HTMLStyleElement)) {
removeStyle(id)
style = undefined
}

if (!style) {
style = document.createElement('style')
style.setAttribute('type', 'text/css')
Expand All @@ -187,11 +197,13 @@ export function updateStyle(id: string, content: string) {
function removeStyle(id: string) {
let style = sheetsMap.get(id)
if (style) {
if (supportsConstructedSheet) {
if (style instanceof CSSStyleSheet) {

This comment has been minimized.

Copy link
@SegaraRai

SegaraRai Jun 24, 2020

Contributor

Thank you for the fix.
I think this should be supportsConstructedSheet && style instanceof CSSStyleSheet or !(style instanceof HTMLStyleElement) to avoid ReferenceError or TypeError.
Sorry, I was just mistaken.

// @ts-ignore
const index = document.adoptedStyleSheets.indexOf(style)
// @ts-ignore
document.adoptedStyleSheets.splice(index, 1)
document.adoptedStyleSheets = document.adoptedStyleSheets.filter(
(s: CSSStyleSheet) => s !== style
)
} else {
document.head.removeChild(style)
}
Expand Down

0 comments on commit 422b4aa

Please sign in to comment.