-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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: __VITE_PUBLIC_ASSET__hash__ in HTML #9247
Conversation
publicDir
42a2fbb
to
2c3d85a
Compare
// TODO: To investigate why `await getBg('.inline-style-public') === "url("http://localhost:5173/icon.png")"` | ||
// It supposes to be `url("http://localhost:5173/foo/icon.png")` | ||
// (I built the playground to verify) | ||
expect(await getBg('.inline-style-public')).toContain(iconMatch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is failing because during dev await getBg('.inline-style-public')
is "url("http://localhost:5173/icon.png")"
.
Vite's test runs with dev and then runs with build+preview.
I think there is a different bug here. await getBg('.inline-style-public')
should return url("http://localhost:5173/foo/icon.png")
and not url("http://localhost:5173/icon.png")"
during dev too. (because src="/raw.js"
(at assets/index.html
line 18) is rewrote to src="/foo/raw.js"
during dev.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I guess there is a bug in Vitest configuration here but I'm not familiar so I haven't catched it yet. I will investigate more. But if you spot the bug, please let me know, or just push the change directly to my branch. Much appreciated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The weird thing is that if it's class style, it works fine. The test fail with inline style only. So I am nor sure it's a bug in Vitest config or the code implementation 😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess there is a bug in Vitest configuration here but I'm not familiar so I haven't catched it yet.
No, I meant there is a bug in Vite.
The weird thing is that if it's class style, it works fine.
Good finding 👍 I think this is a different bug.
I haven't checked closely yet but I suppose style
attributes needs to be rewrote here during dev.
vite/packages/vite/src/node/server/middlewares/indexHtml.ts
Lines 185 to 253 in 2c3d85a
await traverseHtml(html, htmlPath, (node) => { | |
if (node.type !== NodeTypes.ELEMENT) { | |
return | |
} | |
// script tags | |
if (node.tag === 'script') { | |
const { src, isModule } = getScriptInfo(node) | |
if (src) { | |
processNodeUrl(src, s, config, htmlPath, originalUrl, moduleGraph) | |
} else if (isModule && node.children.length) { | |
addInlineModule(node, 'js') | |
} | |
} | |
if (node.tag === 'style' && node.children.length) { | |
const children = node.children[0] as TextNode | |
styleUrl.push({ | |
start: children.loc.start.offset, | |
end: children.loc.end.offset, | |
code: children.content | |
}) | |
} | |
// elements with [href/src] attrs | |
const assetAttrs = assetAttrsConfig[node.tag] | |
if (assetAttrs) { | |
for (const p of node.props) { | |
if ( | |
p.type === NodeTypes.ATTRIBUTE && | |
p.value && | |
assetAttrs.includes(p.name) | |
) { | |
processNodeUrl(p, s, config, htmlPath, originalUrl) | |
} | |
} | |
} | |
}) | |
await Promise.all( | |
styleUrl.map(async ({ start, end, code }, index) => { | |
const url = `${proxyModulePath}?html-proxy&direct&index=${index}.css` | |
// ensure module in graph after successful load | |
const mod = await moduleGraph.ensureEntryFromUrl(url, false) | |
ensureWatchedFile(watcher, mod.file, config.root) | |
const result = await server!.pluginContainer.transform(code, mod.id!) | |
s.overwrite(start, end, result?.code || '') | |
}) | |
) | |
html = s.toString() | |
return { | |
html, | |
tags: [ | |
{ | |
tag: 'script', | |
attrs: { | |
type: 'module', | |
src: path.posix.join(base, CLIENT_PUBLIC_PATH) | |
}, | |
injectTo: 'head-prepend' | |
} | |
] | |
} | |
} |
If you prefer, I think we could go with commenting out this line for now since it's a different bug. (I'll create a issue or PR later)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank for your help. I will separate it to a different test case then skip it for now. I will push it soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just re-ran the code again in DEV mode and BUILD mode. I confirm that the url()
in inline style is missing base
url.
This is what I get when I visit http://127.0.0.1:5173/icon.png
directly.
But I don't understand why it works fine as a background image in a CSS rule
So I guess we just discovered another bug.
I also pushed the commit to skip the failed test for DEV mode.
…tyle in DEV mode
publicDir
Description
index.html
, if we refer to a file inpublicDir
insideurl()
(e.g:style="background: url(/icon.png)"
), it becomes__VITE_PUBLIC_ASSET__hash__
after building (e.g:style="background: url(__VITE_PUBLIC_ASSET__018698dc__)"
).public/fonts/font.woff
inindex.html
contains__VITE_PUBLIC_ASSET__
#9174playground/assets
to reproduce this bug__VITE_PUBLIC_ASSET__hash__
with the actual file inpublicDir
. (packages/vite/src/node/plugins/html.ts
)playground/assets/__tests__/assets.spec.ts
to cover changes. (not passed all yet)Related PR
Additional context
publicDir
does not load Upgrade to vite 3 bestofjs/bestofjs#148 (comment)You can see the preview at https://bestofjs-68pbasy3a-bestofjs.vercel.app/
There is no file such as
https://bestofjs-68pbasy3a-bestofjs.vercel.app/__VITE_PUBLIC_ASSET__03d6a1cf__
Questions
https://github.com/vitejs/vite/pull/9247/files#diff-be0c593011deabf61343720611c7586df51ddc269f187678cdbf85ff72a7973bR370-R373
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).