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: allow getting URL of JS files in publicDir #17915

Merged
merged 1 commit into from
Sep 1, 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
5 changes: 4 additions & 1 deletion packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isSameFileUri,
normalizePath,
removeLeadingSlash,
urlRE,
} from '../../utils'
import {
cleanUrl,
Expand Down Expand Up @@ -86,7 +87,9 @@ export function servePublicMiddleware(
if (
(publicFiles && !publicFiles.has(toFilePath(req.url!))) ||
isImportRequest(req.url!) ||
isInternalRequest(req.url!)
isInternalRequest(req.url!) ||
// for `/public-file.js?url` to be transformed
urlRE.test(req.url!)
) {
return next()
}
Expand Down
12 changes: 12 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ describe('asset imports from js', () => {
"
`)
})

test('from /public (js)', async () => {
expect(await page.textContent('.public-js-import')).toMatch(
'/foo/bar/raw.js',
)
expect(await page.textContent('.public-js-import-content'))
.toMatchInlineSnapshot(`
"document.querySelector('.raw-js').textContent =
'[success] Raw js from /public loaded'
"
`)
})
})

describe('css url() references', () => {
Expand Down
11 changes: 11 additions & 0 deletions playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ <h2>Asset Imports from JS</h2>
From publicDir (json): <code class="public-json-import"></code> Content:
<code class="public-json-import-content"></code>
</li>
<li>
From publicDir (js): <code class="public-js-import"></code> Content:
<code class="public-js-import-content"></code>
</li>
</ul>

<h2>CSS url references</h2>
Expand Down Expand Up @@ -441,6 +445,13 @@ <h3>assets in noscript</h3>
text('.public-json-import-content', await res.text())
})()

import publicJsUrl from '/raw.js?url'
text('.public-js-import', publicJsUrl)
;(async () => {
const res = await fetch(publicJsUrl)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import(publicJsUrl) doesn't work due to #14850

text('.public-js-import-content', await res.text())
})()

import svgFrag from './nested/fragment.svg'
text('.svg-frag-import-path', svgFrag)
document.querySelector('.svg-frag-import').src = svgFrag + '#icon-heart-view'
Expand Down