Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: virtual html sourcemap warning #7440

Merged
merged 3 commits into from Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/playground/css-sourcemap/__tests__/serve.spec.ts
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
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
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
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
@@ -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