Skip to content

Commit f0f5512

Browse files
authored
fix: multiple style tags (vuejs#116)
1 parent ab4b7cd commit f0f5512

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/output/Preview.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,18 @@ async function updatePreview() {
216216
)
217217
218218
const codeToEval = [
219-
`window.__modules__ = {};window.__css__ = '';` +
219+
`window.__modules__ = {};window.__css__ = [];` +
220220
`if (window.__app__) window.__app__.unmount();` +
221221
(isSSR
222222
? ``
223223
: `document.body.innerHTML = '<div id="app"></div>' + \`${
224224
previewOptions?.bodyHTML || ''
225225
}\``),
226226
...modules,
227-
`setTimeout(()=> document.getElementById('__sfc-styles').innerHTML = window.__css__,1)`,
227+
`setTimeout(()=> {
228+
document.querySelectorAll('style[css]').forEach(el => el.remove())
229+
document.head.insertAdjacentHTML('beforeend', window.__css__.map(s => \`<style css>\${s}</style>\`).join('\\n'))
230+
}, 1)`,
228231
]
229232
230233
// if main file is a vue file, mount it.
@@ -256,6 +259,7 @@ async function updatePreview() {
256259
// eval code in sandbox
257260
await proxy.eval(codeToEval)
258261
} catch (e: any) {
262+
console.error(e)
259263
runtimeError.value = (e as Error).message
260264
}
261265
}

src/output/moduleCompiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function compileModulesForPreview(store: Store, isSSR = false) {
2828
const file = store.state.files[filename]
2929
if (!seen.has(file)) {
3030
processed.push(
31-
`\nwindow.__css__ += ${JSON.stringify(file.compiled.css)}`
31+
`\nwindow.__css__.push(${JSON.stringify(file.compiled.css)})`
3232
)
3333
}
3434
}
@@ -79,7 +79,7 @@ function processFile(
7979
)
8080
// append css
8181
if (file.compiled.css && !isSSR) {
82-
js += `\nwindow.__css__ += ${JSON.stringify(file.compiled.css)}`
82+
js += `\nwindow.__css__.push(${JSON.stringify(file.compiled.css)})`
8383
}
8484

8585
// push self

src/output/srcdoc.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
}
99
</style>
1010
<!-- PREVIEW-OPTIONS-HEAD-HTML -->
11-
<style id="__sfc-styles"></style>
1211
<script>
1312
;(() => {
1413
let scriptEls = []

0 commit comments

Comments
 (0)