-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'vitejs:main' into feat/ssr-import
- Loading branch information
Showing
37 changed files
with
463 additions
and
62 deletions.
There are no files selected for viewing
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
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
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
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import './minify.css' | ||
|
||
import css from './imported.css' | ||
text('.imported-css', 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,3 @@ | ||
.test-minify { | ||
color: rgba(255, 255, 0, 0.7); | ||
} |
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
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,36 @@ | ||
// @ts-check | ||
// this is automtically detected by scripts/jestPerTestSetup.ts and will replace | ||
// the default e2e test serve behavior | ||
|
||
const path = require('path') | ||
|
||
const port = (exports.port = 9530) | ||
|
||
/** | ||
* @param {string} root | ||
* @param {boolean} isProd | ||
*/ | ||
exports.serve = async function serve(root, isProd) { | ||
const { createServer } = require(path.resolve(root, 'server.js')) | ||
const { app, vite } = await createServer(root, isProd) | ||
|
||
return new Promise((resolve, reject) => { | ||
try { | ||
const server = app.listen(port, () => { | ||
resolve({ | ||
// for test teardown | ||
async close() { | ||
await new Promise((resolve) => { | ||
server.close(resolve) | ||
}) | ||
if (vite) { | ||
await vite.close() | ||
} | ||
} | ||
}) | ||
}) | ||
} catch (e) { | ||
reject(e) | ||
} | ||
}) | ||
} |
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,39 @@ | ||
import { port } from './serve' | ||
import fetch from 'node-fetch' | ||
|
||
const url = `http://localhost:${port}` | ||
|
||
describe('injected inline scripts', () => { | ||
test('no injected inline scripts are present', async () => { | ||
await page.goto(url) | ||
const inlineScripts = await page.$$eval('script', (nodes) => | ||
nodes.filter((n) => !n.getAttribute('src') && n.innerHTML) | ||
) | ||
expect(inlineScripts).toHaveLength(0) | ||
}) | ||
|
||
test('injected script proxied correctly', async () => { | ||
await page.goto(url) | ||
const proxiedScripts = await page.$$eval('script', (nodes) => | ||
nodes | ||
.filter((n) => { | ||
const src = n.getAttribute('src') | ||
if (!src) return false | ||
return src.includes('?html-proxy&index') | ||
}) | ||
.map((n) => n.getAttribute('src')) | ||
) | ||
|
||
// assert at least 1 proxied script exists | ||
expect(proxiedScripts).not.toHaveLength(0) | ||
|
||
const scriptContents = await Promise.all( | ||
proxiedScripts.map((src) => fetch(url + src).then((res) => res.text())) | ||
) | ||
|
||
// all proxied scripts return code | ||
for (const code of scriptContents) { | ||
expect(code).toBeTruthy() | ||
} | ||
}) | ||
}) |
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 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>SSR HTML</title> | ||
</head> | ||
<body> | ||
<h1>SSR Dynamic HTML</h1> | ||
</body> | ||
</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,15 @@ | ||
{ | ||
"name": "test-ssr-html", | ||
"private": true, | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "node server", | ||
"serve": "cross-env NODE_ENV=production node server", | ||
"debug": "node --inspect-brk server" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"cross-env": "^7.0.3", | ||
"express": "^4.17.1" | ||
} | ||
} |
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,75 @@ | ||
// @ts-check | ||
const fs = require('fs') | ||
const path = require('path') | ||
const express = require('express') | ||
|
||
const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD | ||
|
||
const DYNAMIC_SCRIPTS = ` | ||
<script type="module"> | ||
const p = document.createElement('p'); | ||
p.innerHTML = '✅ Dynamically injected inline script'; | ||
document.body.appendChild(p); | ||
</script> | ||
<script type="module" src="/src/app.js"></script> | ||
` | ||
|
||
async function createServer( | ||
root = process.cwd(), | ||
isProd = process.env.NODE_ENV === 'production' | ||
) { | ||
const resolve = (p) => path.resolve(__dirname, p) | ||
|
||
const app = express() | ||
|
||
/** | ||
* @type {import('vite').ViteDevServer} | ||
*/ | ||
let vite | ||
vite = await require('vite').createServer({ | ||
root, | ||
logLevel: isTest ? 'error' : 'info', | ||
server: { | ||
middlewareMode: 'ssr', | ||
watch: { | ||
// During tests we edit the files too fast and sometimes chokidar | ||
// misses change events, so enforce polling for consistency | ||
usePolling: true, | ||
interval: 100 | ||
} | ||
} | ||
}) | ||
// use vite's connect instance as middleware | ||
app.use(vite.middlewares) | ||
|
||
app.use('*', async (req, res) => { | ||
try { | ||
let [url] = req.originalUrl.split('?') | ||
if (url.endsWith('/')) url += 'index.html' | ||
|
||
const htmlLoc = resolve(`.${url}`) | ||
let html = fs.readFileSync(htmlLoc, 'utf8') | ||
html = html.replace('</body>', `${DYNAMIC_SCRIPTS}</body>`) | ||
html = await vite.transformIndexHtml(url, html) | ||
|
||
res.status(200).set({ 'Content-Type': 'text/html' }).end(html) | ||
} catch (e) { | ||
vite && vite.ssrFixStacktrace(e) | ||
console.log(e.stack) | ||
res.status(500).end(e.stack) | ||
} | ||
}) | ||
|
||
return { app, vite } | ||
} | ||
|
||
if (!isTest) { | ||
createServer().then(({ app }) => | ||
app.listen(3000, () => { | ||
console.log('http://localhost:3000') | ||
}) | ||
) | ||
} | ||
|
||
// for test use | ||
exports.createServer = createServer |
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,3 @@ | ||
const p = document.createElement('p') | ||
p.innerHTML = '✅ Dynamically injected script from file' | ||
document.body.appendChild(p) |
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
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
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
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
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
Oops, something went wrong.