Skip to content

Commit

Permalink
fix(browser): don't process the default css styles (#6861)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Nov 5, 2024
1 parent 08b264a commit 0d67f04
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
11 changes: 0 additions & 11 deletions packages/browser/src/client/tester/tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
<link rel="icon" href="{__VITEST_FAVICON__}" type="image/svg+xml">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vitest Browser Tester</title>
<style>
html {
padding: 0;
margin: 0;
}
body {
padding: 0;
margin: 0;
min-height: 100vh;
}
</style>
</head>
<body>
<script type="module" src="./tester.ts"></script>
Expand Down
31 changes: 25 additions & 6 deletions packages/browser/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
name: 'vitest:browser:transform-tester-html',
enforce: 'pre',
async transformIndexHtml(html, ctx) {
if (!ctx.path.startsWith(browserServer.prefixTesterUrl)) {
if (ctx.filename !== browserServer.testerFilepath) {
return
}

Expand All @@ -439,14 +439,15 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
? browserServer.stateJs
: await browserServer.stateJs

const testerScripts: HtmlTagDescriptor[] = []
if (resolve(distRoot, 'client/tester/tester.html') !== browserServer.testerFilepath) {
const testerTags: HtmlTagDescriptor[] = []
const isDefaultTemplate = resolve(distRoot, 'client/tester/tester.html') === browserServer.testerFilepath
if (!isDefaultTemplate) {
const manifestContent = browserServer.manifest instanceof Promise
? await browserServer.manifest
: browserServer.manifest
const testerEntry = manifestContent['tester/tester.html']

testerScripts.push({
testerTags.push({
tag: 'script',
attrs: {
type: 'module',
Expand All @@ -459,7 +460,7 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
for (const importName of testerEntry.imports || []) {
const entryManifest = manifestContent[importName]
if (entryManifest) {
testerScripts.push(
testerTags.push(
{
tag: 'link',
attrs: {
Expand All @@ -473,6 +474,24 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
}
}
}
else {
// inject the reset style only in the default template,
// allowing users to customize the style in their own template
testerTags.push({
tag: 'style',
children: `
html {
padding: 0;
margin: 0;
}
body {
padding: 0;
margin: 0;
min-height: 100vh;
}`,
injectTo: 'head',
})
}

return [
{
Expand Down Expand Up @@ -504,7 +523,7 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
} as const
: null,
...browserServer.testerScripts,
...testerScripts,
...testerTags,
{
tag: 'script',
attrs: {
Expand Down
4 changes: 3 additions & 1 deletion packages/browser/src/node/serverTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Connect } from 'vite'
import type { BrowserServer } from './server'
import crypto from 'node:crypto'
import { stringify } from 'flatted'
import { join } from 'pathe'
import { replacer } from './utils'

export async function resolveTester(
Expand Down Expand Up @@ -58,7 +59,8 @@ export async function resolveTester(
: await server.testerHtml

try {
const indexhtml = await server.vite.transformIndexHtml(url.pathname, testerHtml)
const url = join('/@fs/', server.testerFilepath)
const indexhtml = await server.vite.transformIndexHtml(url, testerHtml)
return replacer(indexhtml, {
__VITEST_FAVICON__: server.faviconUrl,
__VITEST_INJECTOR__: injector,
Expand Down

0 comments on commit 0d67f04

Please sign in to comment.