render-default.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import fs from 'node:fs/promises';
  2. import { loremIpsumHtml, loremIpsumMd } from './_util.js';
  3. // Map of files to be generated and tested for rendering.
  4. // Ideally each content should be similar for comparison.
  5. const renderFiles = {
  6. 'components/ListItem.astro': `\
  7. ---
  8. const { className, item, attrs } = Astro.props;
  9. const nested = item !== 0;
  10. ---
  11. <li class={className}>
  12. <a
  13. href={item}
  14. aria-current={item === 0}
  15. class:list={[{ large: !nested }, className]}
  16. {...attrs}
  17. >
  18. <span>{item}</span>
  19. </a>
  20. </li>
  21. `,
  22. 'components/Sublist.astro': `\
  23. ---
  24. import ListItem from '../components/ListItem.astro';
  25. const { items } = Astro.props;
  26. const className = "text-red-500";
  27. const style = { color: "red" };
  28. ---
  29. <ul style={style}>
  30. {items.map((item) => (
  31. <ListItem className={className} item={item} attrs={{}} />
  32. ))}
  33. </ul>
  34. `,
  35. 'pages/astro.astro': `\
  36. ---
  37. const className = "text-red-500";
  38. const style = { color: "red" };
  39. const items = Array.from({ length: 10000 }, (_, i) => ({i}));
  40. ---
  41. <html>
  42. <head>
  43. <title>My Site</title>
  44. </head>
  45. <body>
  46. <h1 class={className + ' text-lg'}>List</h1>
  47. <ul style={style}>
  48. {items.map((item) => (
  49. <li class={className}>
  50. <a
  51. href={item.i}
  52. aria-current={item.i === 0}
  53. class:list={[{ large: item.i === 0 }, className]}
  54. {...({})}
  55. >
  56. <span>{item.i}</span>
  57. </a>
  58. </li>
  59. ))}
  60. </ul>
  61. ${Array.from({ length: 1000 })
  62. .map(() => `<p>${loremIpsumHtml}</p>`)
  63. .join('\n')}
  64. </body>
  65. </html>`,
  66. 'pages/md.md': `\
  67. # List
  68. ${Array.from({ length: 1000 }, (_, i) => i)
  69. .map((v) => `- ${v}`)
  70. .join('\n')}
  71. ${Array.from({ length: 1000 })
  72. .map(() => loremIpsumMd)
  73. .join('\n\n')}
  74. `,
  75. 'pages/mdx.mdx': `\
  76. export const className = "text-red-500";
  77. export const style = { color: "red" };
  78. export const items = Array.from({ length: 1000 }, (_, i) => i);
  79. # List
  80. <ul style={style}>
  81. {items.map((item) => (
  82. <li class={className}>{item}</li>
  83. ))}
  84. </ul>
  85. ${Array.from({ length: 1000 })
  86. .map(() => loremIpsumMd)
  87. .join('\n\n')}
  88. `,
  89. };
  90. export const renderPages = [];
  91. for (const file of Object.keys(renderFiles)) {
  92. if (file.startsWith('pages/')) {
  93. renderPages.push(file.replace('pages/', ''));
  94. }
  95. }
  96. /**
  97. * @param {URL} projectDir
  98. */
  99. export async function run(projectDir) {
  100. await fs.rm(projectDir, { recursive: true, force: true });
  101. await fs.mkdir(new URL('./src/pages', projectDir), { recursive: true });
  102. await fs.mkdir(new URL('./src/components', projectDir), { recursive: true });
  103. await Promise.all(
  104. Object.entries(renderFiles).map(([name, content]) => {
  105. return fs.writeFile(new URL(`./src/${name}`, projectDir), content, 'utf-8');
  106. })
  107. );
  108. await fs.writeFile(
  109. new URL('./astro.config.js', projectDir),
  110. `\
  111. import { defineConfig } from 'astro/config';
  112. import timer from '@benchmark/timer';
  113. import mdx from '@astrojs/mdx';
  114. export default defineConfig({
  115. integrations: [mdx()],
  116. output: 'server',
  117. adapter: timer(),
  118. });`,
  119. 'utf-8'
  120. );
  121. }