Skip to content

Commit

Permalink
fix: Replace __VITE_PUBLIC_ASSET__ by files in public folder
Browse files Browse the repository at this point in the history
  • Loading branch information
nvh95 committed Jul 20, 2022
1 parent 4cd9625 commit 42a2fbb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/vite/src/node/plugins/asset.ts
Expand Up @@ -239,6 +239,13 @@ export function getAssetFilename(
return assetHashToFilenameMap.get(config)?.get(hash)
}

export function getPublicAssetFilename(
hash: string,
config: ResolvedConfig
): string | undefined {
return publicAssetUrlCache.get(config)?.get(hash)
}

export function resolveAssetFileNames(
config: ResolvedConfig
): string | ((chunkInfo: PreRenderedAsset) => string) {
Expand Down
18 changes: 16 additions & 2 deletions packages/vite/src/node/plugins/html.ts
Expand Up @@ -34,6 +34,8 @@ import {
assetUrlRE,
checkPublicFile,
getAssetFilename,
getPublicAssetFilename,
publicAssetUrlRE,
urlToBuiltUrl
} from './asset'
import { isCSSRequest } from './css'
Expand Down Expand Up @@ -606,13 +608,16 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
for (const [id, html] of processedHtml) {
const relativeUrlPath = path.posix.relative(config.root, id)
const assetsBase = getBaseInHTML(relativeUrlPath, config)
const toOutputAssetFilePath = (filename: string) => {
const toOutputAssetFilePath = (
filename: string,
type: 'asset' | 'public' = 'asset'
) => {
if (isExternalUrl(filename)) {
return filename
} else {
return toOutputFilePathInHtml(
filename,
'asset',
type,
relativeUrlPath,
'html',
config,
Expand Down Expand Up @@ -709,6 +714,15 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
)
})

result = result.replace(publicAssetUrlRE, (_, fileHash) => {
return normalizePath(
toOutputAssetFilePath(
getPublicAssetFilename(fileHash, config)!,
'public'
)
)
})

if (chunk && canInlineEntry) {
// all imports from entry have been inlined to html, prevent rollup from outputting it
delete bundle[chunk.fileName]
Expand Down
8 changes: 8 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Expand Up @@ -364,3 +364,11 @@ test('relative path in html asset', async () => {
expect(await page.textContent('.relative-js')).toMatch('hello')
expect(await getColor('.relative-css')).toMatch('red')
})

test('url to public folder', async () => {
expect(await getBg('.style-public-assets')).toContain(iconMatch)
// TODO: To investigate why `await getBg('.inline-style-public') === "url("http://localhost:5173/icon.png")"`
// It supposes to be `url("http://localhost:5173/static/icon.png")`
// (I built the playground to verify)
expect(await getBg('.inline-style-public')).toContain(iconMatch)
})
4 changes: 3 additions & 1 deletion playground/assets/index.html
Expand Up @@ -272,7 +272,9 @@ <h3>from public folder</h3>
background: url('/icon.png');
}
</style>
<p style="background: url(/icon.png)">inline style</p>
<p class="inline-style-public" style="background: url(/icon.png)">
inline style
</p>
<p class="style-public-assets">use style class</p>

<h3 class="import-css">@import</h3>
Expand Down

0 comments on commit 42a2fbb

Please sign in to comment.