Skip to content

Commit 0d67f04

Browse files
authored
fix(browser): don't process the default css styles (#6861)
1 parent 08b264a commit 0d67f04

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

packages/browser/src/client/tester/tester.html

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@
55
<link rel="icon" href="{__VITEST_FAVICON__}" type="image/svg+xml">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>Vitest Browser Tester</title>
8-
<style>
9-
html {
10-
padding: 0;
11-
margin: 0;
12-
}
13-
body {
14-
padding: 0;
15-
margin: 0;
16-
min-height: 100vh;
17-
}
18-
</style>
198
</head>
209
<body>
2110
<script type="module" src="./tester.ts"></script>

packages/browser/src/node/plugin.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
425425
name: 'vitest:browser:transform-tester-html',
426426
enforce: 'pre',
427427
async transformIndexHtml(html, ctx) {
428-
if (!ctx.path.startsWith(browserServer.prefixTesterUrl)) {
428+
if (ctx.filename !== browserServer.testerFilepath) {
429429
return
430430
}
431431

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

442-
const testerScripts: HtmlTagDescriptor[] = []
443-
if (resolve(distRoot, 'client/tester/tester.html') !== browserServer.testerFilepath) {
442+
const testerTags: HtmlTagDescriptor[] = []
443+
const isDefaultTemplate = resolve(distRoot, 'client/tester/tester.html') === browserServer.testerFilepath
444+
if (!isDefaultTemplate) {
444445
const manifestContent = browserServer.manifest instanceof Promise
445446
? await browserServer.manifest
446447
: browserServer.manifest
447448
const testerEntry = manifestContent['tester/tester.html']
448449

449-
testerScripts.push({
450+
testerTags.push({
450451
tag: 'script',
451452
attrs: {
452453
type: 'module',
@@ -459,7 +460,7 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
459460
for (const importName of testerEntry.imports || []) {
460461
const entryManifest = manifestContent[importName]
461462
if (entryManifest) {
462-
testerScripts.push(
463+
testerTags.push(
463464
{
464465
tag: 'link',
465466
attrs: {
@@ -473,6 +474,24 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
473474
}
474475
}
475476
}
477+
else {
478+
// inject the reset style only in the default template,
479+
// allowing users to customize the style in their own template
480+
testerTags.push({
481+
tag: 'style',
482+
children: `
483+
html {
484+
padding: 0;
485+
margin: 0;
486+
}
487+
body {
488+
padding: 0;
489+
margin: 0;
490+
min-height: 100vh;
491+
}`,
492+
injectTo: 'head',
493+
})
494+
}
476495

477496
return [
478497
{
@@ -504,7 +523,7 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
504523
} as const
505524
: null,
506525
...browserServer.testerScripts,
507-
...testerScripts,
526+
...testerTags,
508527
{
509528
tag: 'script',
510529
attrs: {

packages/browser/src/node/serverTester.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Connect } from 'vite'
33
import type { BrowserServer } from './server'
44
import crypto from 'node:crypto'
55
import { stringify } from 'flatted'
6+
import { join } from 'pathe'
67
import { replacer } from './utils'
78

89
export async function resolveTester(
@@ -58,7 +59,8 @@ export async function resolveTester(
5859
: await server.testerHtml
5960

6061
try {
61-
const indexhtml = await server.vite.transformIndexHtml(url.pathname, testerHtml)
62+
const url = join('/@fs/', server.testerFilepath)
63+
const indexhtml = await server.vite.transformIndexHtml(url, testerHtml)
6264
return replacer(indexhtml, {
6365
__VITEST_FAVICON__: server.faviconUrl,
6466
__VITEST_INJECTOR__: injector,

0 commit comments

Comments
 (0)