Skip to content

Commit

Permalink
fix(html): rewrite assets url in <template> (#17988)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnylost committed Sep 2, 2024
1 parent dc3c14f commit 413c86a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
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 @@ -588,3 +588,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 @@ -413,6 +413,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

0 comments on commit 413c86a

Please sign in to comment.