Skip to content

Commit

Permalink
fix: multiple style tags (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jun 26, 2023
1 parent ab4b7cd commit f0f5512
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/output/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,18 @@ async function updatePreview() {
)
const codeToEval = [
`window.__modules__ = {};window.__css__ = '';` +
`window.__modules__ = {};window.__css__ = [];` +
`if (window.__app__) window.__app__.unmount();` +
(isSSR
? ``
: `document.body.innerHTML = '<div id="app"></div>' + \`${
previewOptions?.bodyHTML || ''
}\``),
...modules,
`setTimeout(()=> document.getElementById('__sfc-styles').innerHTML = window.__css__,1)`,
`setTimeout(()=> {
document.querySelectorAll('style[css]').forEach(el => el.remove())
document.head.insertAdjacentHTML('beforeend', window.__css__.map(s => \`<style css>\${s}</style>\`).join('\\n'))
}, 1)`,
]
// if main file is a vue file, mount it.
Expand Down Expand Up @@ -256,6 +259,7 @@ async function updatePreview() {
// eval code in sandbox
await proxy.eval(codeToEval)
} catch (e: any) {
console.error(e)
runtimeError.value = (e as Error).message
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/output/moduleCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function compileModulesForPreview(store: Store, isSSR = false) {
const file = store.state.files[filename]
if (!seen.has(file)) {
processed.push(
`\nwindow.__css__ += ${JSON.stringify(file.compiled.css)}`
`\nwindow.__css__.push(${JSON.stringify(file.compiled.css)})`
)
}
}
Expand Down Expand Up @@ -79,7 +79,7 @@ function processFile(
)
// append css
if (file.compiled.css && !isSSR) {
js += `\nwindow.__css__ += ${JSON.stringify(file.compiled.css)}`
js += `\nwindow.__css__.push(${JSON.stringify(file.compiled.css)})`
}

// push self
Expand Down
1 change: 0 additions & 1 deletion src/output/srcdoc.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
}
</style>
<!-- PREVIEW-OPTIONS-HEAD-HTML -->
<style id="__sfc-styles"></style>
<script>
;(() => {
let scriptEls = []
Expand Down

0 comments on commit f0f5512

Please sign in to comment.