-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ssr): avoid transforming json file in ssrTransform (#6597)
- Loading branch information
Showing
9 changed files
with
166 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<div class="fetch-json-module"> | ||
json-module: | ||
<pre></pre> | ||
<code></code> | ||
</div> | ||
<div class="fetch-json-fs"> | ||
json-fs: | ||
<pre></pre> | ||
<code></code> | ||
</div> | ||
|
||
<script type="module"> | ||
const startModule = Date.now() | ||
text('.fetch-json-module pre', await (await fetch('/json-module')).text()) | ||
text('.fetch-json-module code', Date.now() - startModule) | ||
|
||
const startFs = Date.now() | ||
text('.fetch-json-fs pre', await (await fetch('/json-fs')).text()) | ||
text('.fetch-json-fs code', Date.now() - startFs) | ||
function text(sel, text) { | ||
document.querySelector(sel).textContent = text | ||
} | ||
</script> |
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,88 @@ | ||
// @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 | ||
|
||
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 | ||
} | ||
}, | ||
json: { | ||
stringify: true | ||
} | ||
}) | ||
// 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.ssr.html' | ||
|
||
if (url === '/json-module') { | ||
console.time('load module') | ||
const json = JSON.stringify(await vite.ssrLoadModule('/test.json')) | ||
console.timeEnd('load module') | ||
res.status(200).end('' + json.length) | ||
return | ||
} | ||
|
||
if (url === '/json-fs') { | ||
console.time('transform module') | ||
const source = fs.readFileSync('./test.json', { encoding: 'utf-8' }) | ||
const json = await vite.ssrTransform( | ||
`export default ${source}`, | ||
null, | ||
'./output.json' | ||
) | ||
console.timeEnd('transform module') | ||
res.status(200).end(String(json.code.length)) | ||
return | ||
} | ||
|
||
const htmlLoc = resolve(`.${url}`) | ||
let html = fs.readFileSync(htmlLoc, 'utf8') | ||
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.