-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into sarah11918-patch-1
- Loading branch information
Showing
70 changed files
with
736 additions
and
125 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Add getStaticPaths type helpers to infer params and props |
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Add `--help` to various commands: `check`, `sync`, `dev`, `preview`, and `build` |
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,122 @@ | ||
import fs from 'fs/promises'; | ||
import http from 'http'; | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import { execaCommand } from 'execa'; | ||
import { waitUntilBusy } from 'port-authority'; | ||
import { markdownTable } from 'markdown-table'; | ||
import { renderFiles } from '../make-project/render-default.js'; | ||
import { astroBin } from './_util.js'; | ||
|
||
const port = 4322; | ||
|
||
export const defaultProject = 'render-default'; | ||
|
||
/** @typedef {{ avg: number, stdev: number, max: number }} Stat */ | ||
|
||
/** | ||
* @param {URL} projectDir | ||
* @param {URL} outputFile | ||
*/ | ||
export async function run(projectDir, outputFile) { | ||
const root = fileURLToPath(projectDir); | ||
|
||
console.log('Building...'); | ||
await execaCommand(`${astroBin} build`, { | ||
cwd: root, | ||
stdio: 'inherit', | ||
}); | ||
|
||
console.log('Previewing...'); | ||
const previewProcess = execaCommand(`${astroBin} preview --port ${port}`, { | ||
cwd: root, | ||
stdio: 'inherit', | ||
}); | ||
|
||
console.log('Waiting for server ready...'); | ||
await waitUntilBusy(port, { timeout: 5000 }); | ||
|
||
console.log('Running benchmark...'); | ||
const result = await benchmarkRenderTime(); | ||
|
||
console.log('Killing server...'); | ||
if (!previewProcess.kill('SIGTERM')) { | ||
console.warn('Failed to kill server process id:', previewProcess.pid); | ||
} | ||
|
||
console.log('Writing results to', fileURLToPath(outputFile)); | ||
await fs.writeFile(outputFile, JSON.stringify(result, null, 2)); | ||
|
||
console.log('Result preview:'); | ||
console.log('='.repeat(10)); | ||
console.log(`#### Render\n\n`); | ||
console.log(printResult(result)); | ||
console.log('='.repeat(10)); | ||
|
||
console.log('Done!'); | ||
} | ||
|
||
async function benchmarkRenderTime() { | ||
/** @type {Record<string, number[]>} */ | ||
const result = {}; | ||
for (const fileName of Object.keys(renderFiles)) { | ||
// Render each file 100 times and push to an array | ||
for (let i = 0; i < 100; i++) { | ||
const pathname = '/' + fileName.slice(0, -path.extname(fileName).length); | ||
const renderTime = await fetchRenderTime(`http://localhost:${port}${pathname}`); | ||
if (!result[pathname]) result[pathname] = []; | ||
result[pathname].push(renderTime); | ||
} | ||
} | ||
/** @type {Record<string, Stat>} */ | ||
const processedResult = {}; | ||
for (const [pathname, times] of Object.entries(result)) { | ||
// From the 100 results, calculate average, standard deviation, and max value | ||
const avg = times.reduce((a, b) => a + b, 0) / times.length; | ||
const stdev = Math.sqrt( | ||
times.map((x) => Math.pow(x - avg, 2)).reduce((a, b) => a + b, 0) / times.length | ||
); | ||
const max = Math.max(...times); | ||
processedResult[pathname] = { avg, stdev, max }; | ||
} | ||
return processedResult; | ||
} | ||
|
||
/** | ||
* @param {Record<string, Stat>} result | ||
*/ | ||
function printResult(result) { | ||
return markdownTable( | ||
[ | ||
['Page', 'Avg (ms)', 'Stdev (ms)', 'Max (ms)'], | ||
...Object.entries(result).map(([pathname, { avg, stdev, max }]) => [ | ||
pathname, | ||
avg.toFixed(2), | ||
stdev.toFixed(2), | ||
max.toFixed(2), | ||
]), | ||
], | ||
{ | ||
align: ['l', 'r', 'r', 'r'], | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Simple fetch utility to get the render time sent by `@astrojs/timer` in plain text | ||
* @param {string} url | ||
* @returns {Promise<number>} | ||
*/ | ||
function fetchRenderTime(url) { | ||
return new Promise((resolve, reject) => { | ||
const req = http.request(url, (res) => { | ||
res.setEncoding('utf8'); | ||
let data = ''; | ||
res.on('data', (chunk) => (data += chunk)); | ||
res.on('error', (e) => reject(e)); | ||
res.on('end', () => resolve(+data)); | ||
}); | ||
req.on('error', (e) => reject(e)); | ||
req.end(); | ||
}); | ||
} |
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.
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,87 @@ | ||
import fs from 'fs/promises'; | ||
import { loremIpsumHtml, loremIpsumMd } from './_util.js'; | ||
|
||
// Map of files to be generated and tested for rendering. | ||
// Ideally each content should be similar for comparison. | ||
export const renderFiles = { | ||
'astro.astro': `\ | ||
--- | ||
const className = "text-red-500"; | ||
const style = { color: "red" }; | ||
const items = Array.from({ length: 1000 }, (_, i) => i); | ||
--- | ||
<html> | ||
<head> | ||
<title>My Site</title> | ||
</head> | ||
<body> | ||
<h1 class={className + ' text-lg'}>List</h1> | ||
<ul style={style}> | ||
{items.map((item) => ( | ||
<li class={className}>{item}</li> | ||
))} | ||
</ul> | ||
${Array.from({ length: 1000 }) | ||
.map(() => `<p>${loremIpsumHtml}</p>`) | ||
.join('\n')} | ||
</body> | ||
</html>`, | ||
'md.md': `\ | ||
# List | ||
${Array.from({ length: 1000 }, (_, i) => i) | ||
.map((v) => `- ${v}`) | ||
.join('\n')} | ||
${Array.from({ length: 1000 }) | ||
.map(() => loremIpsumMd) | ||
.join('\n\n')} | ||
`, | ||
'mdx.mdx': `\ | ||
export const className = "text-red-500"; | ||
export const style = { color: "red" }; | ||
export const items = Array.from({ length: 1000 }, (_, i) => i); | ||
# List | ||
<ul style={style}> | ||
{items.map((item) => ( | ||
<li class={className}>{item}</li> | ||
))} | ||
</ul> | ||
${Array.from({ length: 1000 }) | ||
.map(() => loremIpsumMd) | ||
.join('\n\n')} | ||
`, | ||
}; | ||
|
||
/** | ||
* @param {URL} projectDir | ||
*/ | ||
export async function run(projectDir) { | ||
await fs.rm(projectDir, { recursive: true, force: true }); | ||
await fs.mkdir(new URL('./src/pages', projectDir), { recursive: true }); | ||
|
||
await Promise.all( | ||
Object.entries(renderFiles).map(([name, content]) => { | ||
return fs.writeFile(new URL(`./src/pages/${name}`, projectDir), content, 'utf-8'); | ||
}) | ||
); | ||
|
||
await fs.writeFile( | ||
new URL('./astro.config.js', projectDir), | ||
`\ | ||
import { defineConfig } from 'astro/config'; | ||
import timer from '@astrojs/timer'; | ||
import mdx from '@astrojs/mdx'; | ||
export default defineConfig({ | ||
integrations: [mdx()], | ||
output: 'server', | ||
adapter: timer(), | ||
});`, | ||
'utf-8' | ||
); | ||
} |
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 |
---|---|---|
|
@@ -11,6 +11,6 @@ | |
"astro": "astro" | ||
}, | ||
"dependencies": { | ||
"astro": "^2.0.17" | ||
"astro": "^2.0.18" | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,6 @@ | |
"dependencies": { | ||
"svelte": "^3.48.0", | ||
"@astrojs/svelte": "^2.0.2", | ||
"astro": "^2.0.17" | ||
"astro": "^2.0.18" | ||
} | ||
} |
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.