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(html): rewrite assets url in <template> #17988

Merged
merged 1 commit into from
Sep 2, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ function traverseNodes(
node: DefaultTreeAdapterMap['node'],
visitor: (node: DefaultTreeAdapterMap['node']) => void,
) {
if (node.nodeName === 'template') {
node = (node as DefaultTreeAdapterMap['template']).content
}
visitor(node)
if (
nodeIsElement(node) ||
Expand Down
7 changes: 7 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,10 @@ test.runIf(isBuild)('assets inside <noscript> is rewrote', async () => {
/<img class="noscript" src="\/foo\/bar\/assets\/asset-[-\w]+\.png" \/>/,
)
})

test.runIf(isBuild)('assets inside <template> is rewrote', async () => {
const indexHtml = readFile('./dist/foo/index.html')
expect(indexHtml).toMatch(
/<img class="template" src="\/foo\/bar\/assets\/asset-[-\w]+\.png" \/>/,
)
})
5 changes: 5 additions & 0 deletions playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,11 @@ <h3>assets in noscript</h3>
<img class="noscript" src="./nested/asset.png" />
</noscript>

<h3>assets in template</h3>
<template>
<img class="template" src="./nested/asset.png" />
</template>

<link rel="stylesheet" href="asset/style.css" />
<div class="relative-css">link style</div>
<div class="relative-js"></div>
Expand Down