Skip to content

Commit

Permalink
fix: virtual html sourcemap warning (#7440)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Mar 24, 2022
1 parent 47668b5 commit 476786b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/playground/css-sourcemap/__tests__/serve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ if (!isBuild) {
import './imported.styl'
</script>
<iframe src=\\"virtual.html\\"></iframe>
",
],
"version": 3,
Expand Down
2 changes: 2 additions & 0 deletions packages/playground/css-sourcemap/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ <h1>CSS Sourcemap</h1>

import './imported.styl'
</script>

<iframe src="virtual.html"></iframe>
22 changes: 21 additions & 1 deletion packages/playground/css-sourcemap/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,25 @@ module.exports = {
},
build: {
sourcemap: true
}
},
plugins: [
{
name: 'virtual-html',
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url === '/virtual.html') {
const t = await server.transformIndexHtml(
'/virtual.html',
'<style> .foo { color: red; } </style> <p class="foo">virtual html</p>'
)
res.setHeader('Content-Type', 'text/html')
res.statusCode = 200
res.end(t)
return
}
next()
})
}
}
]
}
4 changes: 4 additions & 0 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,8 @@ export function resolveHtmlTransforms(
return [preHooks, postHooks]
}

export const maybeVirtualHtmlSet = new Set<string>()

export async function applyHtmlTransforms(
html: string,
hooks: IndexHtmlTransformHook[],
Expand All @@ -715,6 +717,8 @@ export async function applyHtmlTransforms(
const bodyTags: HtmlTagDescriptor[] = []
const bodyPrependTags: HtmlTagDescriptor[] = []

maybeVirtualHtmlSet.add(ctx.filename)

for (const hook of hooks) {
const res = await hook(html, ctx)
if (!res) {
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/server/sourcemap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from 'path'
import { promises as fs } from 'fs'
import type { Logger } from '../logger'
import { createDebugger } from '../utils'
import { createDebugger, normalizePath } from '../utils'
import type { SourceMap } from 'rollup'
import { maybeVirtualHtmlSet } from '../plugins/html'

const isDebug = !!process.env.DEBUG
const debug = createDebugger('vite:sourcemap', {
Expand Down Expand Up @@ -42,6 +43,7 @@ export async function injectSourcesContent(
sourcePath = path.resolve(sourceRoot, sourcePath)
}
return fs.readFile(sourcePath, 'utf-8').catch(() => {
if (maybeVirtualHtmlSet.has(normalizePath(sourcePath))) return null
missingSources.push(sourcePath)
return null
})
Expand Down

0 comments on commit 476786b

Please sign in to comment.