Skip to content

Commit

Permalink
test: Prevent regressions in static file serving of files outside the…
Browse files Browse the repository at this point in the history
… root (vitejs#2358)
  • Loading branch information
ElMassimo authored Mar 15, 2021
1 parent d3142cf commit ee7eb82
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const assetMatch = isBuild
? /\/foo\/assets\/asset\.\w{8}\.png/
: '/foo/nested/asset.png'

const outerAssetMatch = isBuild
? /\/foo\/assets\/asset\.\w{8}\.png/
: /\/foo\/@fs\/.+?\/css\/nested\/asset\.png/

const iconMatch = `/foo/icon.png`

test('should have no 404s', () => {
Expand Down Expand Up @@ -63,6 +67,12 @@ describe('asset imports from js', () => {
expect(await page.textContent('.asset-import-absolute')).toMatch(assetMatch)
})

test('outer', async () => {
expect(await page.textContent('.asset-import-outer')).toMatch(
outerAssetMatch
)
})

test('from /public', async () => {
expect(await page.textContent('.public-import')).toMatch(iconMatch)
})
Expand Down
14 changes: 14 additions & 0 deletions packages/playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ <h2>Raw References from publicDir</h2>
Raw CSS from publicDir should load (this should be red)
</li>
<li>
Filename with whitespace
<img src="/white space.png" />
</li>
<li>
Outer file using @fs
<img class="asset-outer-img" alt="img" />
</li>
</ul>

<h2>Asset Imports from JS</h2>
<ul>
<li>Relative: <code class="asset-import-relative"></code></li>
<li>Absolute: <code class="asset-import-absolute"></code></li>
<li>Outer (@fs): <code class="asset-import-outer"></code></li>
<li>From publicDir: <code class="public-import"></code></li>
</ul>

Expand Down Expand Up @@ -130,6 +136,10 @@ <h2>?url import</h2>
import absoluteUrl from '/nested/asset.png'
text('.asset-import-absolute', absoluteUrl)

import outerUrl from '@playground/css/nested/asset.png'
text('.asset-import-outer', outerUrl)
src('.asset-outer-img', outerUrl)

import publicUrl from '/icon.png'
text('.public-import', publicUrl)

Expand All @@ -146,4 +156,8 @@ <h2>?url import</h2>
function text(el, text) {
document.querySelector(el).textContent = text
}

function src(el, src) {
document.querySelector(el).src = src
}
</script>
1 change: 1 addition & 0 deletions packages/playground/assets/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
publicDir: 'static',
resolve: {
alias: {
'@playground': path.resolve(__dirname, '..'),
'@': path.resolve(__dirname, 'nested')
}
},
Expand Down
Binary file added packages/playground/css/nested/asset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ee7eb82

Please sign in to comment.