forked from vitejs/vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: coverage to settings related with backend integrations (vitejs#…
- Loading branch information
1 parent
fb46a33
commit c2b7a64
Showing
8 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
packages/playground/backend-integration/__tests__/backend-integration.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { isBuild, readManifest } from '../../testUtils' | ||
|
||
const outerAssetMatch = isBuild | ||
? /\/dev\/assets\/logo\.\w{8}\.png/ | ||
: /\/dev\/@fs\/.+?\/images\/logo\.png/ | ||
|
||
test('should have no 404s', () => { | ||
browserLogs.forEach((msg) => { | ||
expect(msg).not.toMatch('404') | ||
}) | ||
}) | ||
|
||
describe('asset imports from js', () => { | ||
test('file outside root', async () => { | ||
expect( | ||
await page.textContent('.asset-reference.outside-root .asset-url') | ||
).toMatch(outerAssetMatch) | ||
}) | ||
}) | ||
|
||
if (isBuild) { | ||
test('manifest', async () => { | ||
const manifest = readManifest('dev') | ||
const htmlEntry = manifest['index.html'] | ||
expect(htmlEntry.css.length).toEqual(1) | ||
expect(htmlEntry.assets.length).toEqual(1) | ||
}) | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/playground/backend-integration/frontend/entrypoints/global.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@import '~/styles/background.css'; | ||
@import '../../references.css'; | ||
|
||
html, body { | ||
font-family: sans-serif; | ||
line-height: 2.4rem; | ||
} | ||
|
||
body { | ||
margin: 4vh auto; | ||
max-width: 800px; | ||
padding: 0 4vw; | ||
} | ||
|
||
ul { | ||
padding: 0 .4em; | ||
margin: 0; | ||
} | ||
|
||
li { | ||
display: flex; | ||
align-items: center; | ||
} | ||
|
||
img { | ||
height: 32px; | ||
width: 32px; | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/playground/backend-integration/frontend/entrypoints/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<!DOCTYPE html> | ||
|
||
<link rel="stylesheet" href="/global.css" /> | ||
|
||
<h1>Backend Integration</h1> | ||
|
||
<p> | ||
This test configures the <code>root</code> to simulate a Laravel/Rails setup. | ||
</p> | ||
|
||
<h2>JS Asset References</h2> | ||
|
||
<ul> | ||
<li class="asset-reference outside-root">Asset Outside Root</li> | ||
</ul> | ||
|
||
<h2>CSS Asset References</h2> | ||
|
||
<ul> | ||
<li> | ||
Background URL with Alias: | ||
<div class="background-asset outside-root--aliased"></div> | ||
</li> | ||
<li> | ||
Background URL with Relative Path: | ||
<div class="background-asset outside-root--relative"></div> | ||
</li> | ||
</ul> | ||
|
||
<script type="module"> | ||
import './global.css' | ||
|
||
// Importing a file outside the `root` should provide an @fs path. | ||
import outsideRootUrl from '~/images/logo.png' | ||
setAssetReference('.outside-root', outsideRootUrl) | ||
|
||
// Helper: Allows to test the URL content as well as the request being served. | ||
function setAssetReference(elSelector, url) { | ||
const text = document.createElement('code') | ||
text.classList.add('asset-url') | ||
text.textContent = url | ||
|
||
const img = document.createElement('img') | ||
img.classList.add('asset-preview') | ||
img.src = url | ||
|
||
const el = document.querySelector(`.asset-reference${elSelector}`) | ||
el.appendChild(img) | ||
el.appendChild(text) | ||
} | ||
</script> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions
15
packages/playground/backend-integration/frontend/styles/background.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.background-asset { | ||
background-repeat: no-repeat; | ||
background-size: 100%; | ||
display: inline-block; | ||
height: 32px; | ||
width: 32px; | ||
} | ||
|
||
.outside-root--aliased { | ||
background-image: url('~/images/logo.png'); | ||
} | ||
|
||
.outside-root--relative { | ||
background-image: url('../images/logo.png'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "test-backend-integration", | ||
"private": true, | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"debug": "node --inspect-brk ../../vite/bin/vite", | ||
"serve": "vite preview" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.asset-reference { | ||
display: grid; | ||
grid-template-areas: | ||
"summary preview ." | ||
"url url url"; | ||
} | ||
|
||
.asset-url { | ||
grid-area: url; | ||
white-space: nowrap; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const path = require('path') | ||
const glob = require('fast-glob') | ||
const normalizePath = require('vite').normalizePath | ||
|
||
/** | ||
* @returns {import('vite').Plugin} | ||
*/ | ||
function BackendIntegrationExample() { | ||
return { | ||
name: 'backend-integration', | ||
config() { | ||
const projectRoot = __dirname | ||
const sourceCodeDir = path.join(projectRoot, 'frontend') | ||
const root = path.join(sourceCodeDir, 'entrypoints') | ||
const outDir = path.relative(root, path.join(projectRoot, 'dist/dev')) | ||
|
||
const entrypoints = glob | ||
.sync(`${normalizePath(root)}/**/*`, { onlyFiles: true }) | ||
.map((filename) => [path.relative(root, filename), filename]) | ||
|
||
return { | ||
build: { | ||
manifest: true, | ||
outDir, | ||
rollupOptions: { | ||
input: Object.fromEntries(entrypoints) | ||
} | ||
}, | ||
root, | ||
resolve: { | ||
alias: { | ||
'~': sourceCodeDir | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @returns {import('vite').UserConfig} | ||
*/ | ||
module.exports = { | ||
base: '/dev/', | ||
plugins: [BackendIntegrationExample()] | ||
} |