Skip to content

Commit

Permalink
Improve testing suite
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Sep 24, 2021
1 parent 353c754 commit 347d5fd
Show file tree
Hide file tree
Showing 96 changed files with 1,155 additions and 1,720 deletions.
2 changes: 1 addition & 1 deletion examples/starter/src/components/Tour.astro
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { Markdown } from 'astro/components';
<h2>👀 Want to learn more?</h2>
<p>Feel free to check <a href="https://github.com/snowpackjs/astro">our documentation</a> or jump into our <a href="https://astro.build/chat">Discord server</a>.</p>
</section>

</article>

<style>
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"dependencies": {},
"devDependencies": {
"@changesets/cli": "^2.16.0",
"@types/jest": "^27.0.1",
"@octokit/action": "^3.15.4",
"@types/jest": "^27.0.2",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.18.0",
"autoprefixer": "^10.2.6",
Expand All @@ -56,7 +56,7 @@
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"execa": "^5.0.0",
"jest": "^27.1.0",
"jest": "^27.2.1",
"lerna": "^4.0.0",
"prettier": "^2.3.2",
"tiny-glob": "^0.2.8",
Expand Down
1 change: 0 additions & 1 deletion packages/astro/.gitignore

This file was deleted.

10 changes: 5 additions & 5 deletions packages/astro/components/Code.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import shiki from 'shiki';
export interface Props {
/** The code to highlight. Required. */
code: string;
/**
* The language of your code. Defaults to "plaintext".
* Supports all languages listed here: https://github.com/shikijs/shiki/blob/main/docs/themes.md
/**
* The language of your code. Defaults to "plaintext".
* Supports all languages listed here: https://github.com/shikijs/shiki/blob/main/docs/themes.md
*/
lang?: string;
/**
/**
* The styling theme. Defaults to "github-dark".
* Supports all themes listed here: https://github.com/shikijs/shiki/blob/main/docs/themes.md
* Supports all themes listed here: https://github.com/shikijs/shiki/blob/main/docs/themes.md
* Instructions for loading a custom theme: https://github.com/shikijs/shiki/blob/main/docs/themes.md#loading-theme
*/
theme?: string;
Expand Down
6 changes: 0 additions & 6 deletions packages/astro/jest.config.js

This file was deleted.

10 changes: 0 additions & 10 deletions packages/astro/jest.setup.js

This file was deleted.

8 changes: 0 additions & 8 deletions packages/astro/jest.teardown.js

This file was deleted.

5 changes: 1 addition & 4 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"estree-util-value-to-estree": "^1.2.0",
"fast-xml-parser": "^3.19.0",
"fdir": "^5.1.0",
"get-port": "^5.1.1",
"html-entities": "^2.3.2",
"kleur": "^4.1.4",
"mime": "^2.5.2",
Expand Down Expand Up @@ -82,9 +81,7 @@
"@types/mime": "^2.0.3",
"@types/node-fetch": "^2.5.12",
"@types/send": "^0.17.1",
"@types/yargs-parser": "^20.2.1",
"sqlite": "^4.0.23",
"sqlite3": "^5.0.2"
"@types/yargs-parser": "^20.2.1"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0",
Expand Down
66 changes: 37 additions & 29 deletions packages/astro/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { performance } from 'perf_hooks';
import vite, { ViteDevServer } from 'vite';
import { fileURLToPath } from 'url';
import { pad } from '../dev/util.js';
import { defaultLogOptions, warn } from '../logger.js';
import { defaultLogOptions, levels, warn } from '../logger.js';
import { generatePaginateFunction } from '../runtime/paginate.js';
import { createRouteManifest, validateGetStaticPathsModule, validateGetStaticPathsResult } from '../runtime/routing.js';
import { generateRssFunction } from '../runtime/rss.js';
Expand All @@ -18,6 +18,7 @@ import { kb, profileHTML, profileJS } from './stats.js';
import { generateSitemap } from '../runtime/sitemap.js';

export interface BuildOptions {
mode?: string;
logging: LogOptions;
}

Expand All @@ -30,6 +31,7 @@ export default async function build(config: AstroConfig, options: BuildOptions =
class AstroBuilder {
private config: AstroConfig;
private logging: LogOptions;
private mode = 'production';
private origin: string;
private routeCache: RouteCache = {};
private manifest: ManifestData;
Expand All @@ -39,6 +41,7 @@ class AstroBuilder {
warn(options.logging, 'config', `Set "buildOptions.site" to generate correct canonical URLs and sitemap`);
}

if (options.mode) this.mode = options.mode;
this.config = config;
const port = config.devOptions.port; // no need to save this (don’t rely on port in builder)
this.logging = options.logging;
Expand All @@ -51,10 +54,10 @@ class AstroBuilder {
const start = performance.now();

// 1. initialize fresh Vite instance
const { config, logging, origin } = this;
const { logging, origin } = this;
const viteConfig = await loadViteConfig(
{
mode: 'production',
mode: this.mode,
server: {
hmr: { overlay: false },
middlewareMode: 'ssr',
Expand All @@ -65,7 +68,6 @@ class AstroBuilder {
const viteServer = await vite.createServer(viteConfig);

// 2. get all routes
const outDir = new URL('./dist/', this.config.projectRoot);
const allPages: Promise<{ html: string; name: string }>[] = [];
const assets: Record<string, string> = {}; // additional assets to be written
await Promise.all(
Expand All @@ -86,7 +88,7 @@ class AstroBuilder {
const staticPaths = await this.getStaticPathsForRoute(route, viteServer);
// handle RSS (TODO: improve this?)
if (staticPaths.rss && staticPaths.rss.xml) {
const rssFile = new URL(staticPaths.rss.url.replace(/^\/?/, './'), outDir);
const rssFile = new URL(staticPaths.rss.url.replace(/^\/?/, './'), this.config.dist);
if (assets[fileURLToPath(rssFile)]) {
throw new Error(
`[getStaticPaths] RSS feed ${staticPaths.rss.url} already exists.\nUse \`rss(data, {url: '...'})\` to choose a unique, custom URL. (${route.component})`
Expand All @@ -97,10 +99,12 @@ class AstroBuilder {
// TODO: throw error if conflict
staticPaths.paths.forEach((staticPath) => {
allPages.push(
ssr({ astroConfig: this.config, filePath, logging, mode: 'production', origin, route, routeCache: this.routeCache, pathname: staticPath, viteServer }).then((html) => ({
html,
name: staticPath.replace(/\/?$/, '/index.html').replace(/^\//, ''),
}))
ssr({ astroConfig: this.config, filePath, logging, mode: 'production', origin, route, routeCache: this.routeCache, pathname: staticPath, viteServer }).then(
(html) => ({
html,
name: staticPath.replace(/\/?$/, '/index.html').replace(/^\//, ''),
})
)
);
});
}
Expand All @@ -115,53 +119,57 @@ class AstroBuilder {
build: {
emptyOutDir: true,
minify: 'esbuild', // significantly faster than "terser" but may produce slightly-bigger bundles
outDir: fileURLToPath(outDir),
outDir: fileURLToPath(this.config.dist),
rollupOptions: {
input: [],
output: { format: 'esm' },
},
target: 'es2020', // must match an esbuild target
},
root: fileURLToPath(config.projectRoot),
server: viteConfig.server,
plugins: [
rollupPluginHTML({
input,
extractAssets: false,
}),
...(viteConfig.plugins || []),
],
publicDir: viteConfig.publicDir,
root: viteConfig.root,
server: viteConfig.server,
});

// 4. write assets to disk
await Promise.all(
Object.keys(assets).map(async (k) => {
if (!assets[k]) return;
const filePath = new URL(`file://${k}`);
await fs.promises.mkdir(new URL('./', filePath), { recursive: true });
await fs.promises.writeFile(filePath, assets[k], 'utf8');
delete assets[k]; // free up memory
})
);
Object.keys(assets).map((k) => {
if (!assets[k]) return;
const filePath = new URL(`file://${k}`);
fs.mkdirSync(new URL('./', filePath), { recursive: true });
fs.writeFileSync(filePath, assets[k], 'utf8');
delete assets[k]; // free up memory
});

// 5. build sitemap
let sitemapTime = 0;
if (this.config.buildOptions.sitemap && this.config.buildOptions.site) {
const sitemapStart = performance.now();
const sitemap = generateSitemap(input.map(({ name }) => new URL(`/${name}`, this.config.buildOptions.site).href));
const sitemapPath = new URL('sitemap.xml', outDir);
const sitemapPath = new URL('sitemap.xml', this.config.dist);
await fs.promises.mkdir(new URL('./', sitemapPath), { recursive: true });
await fs.promises.writeFile(sitemapPath, sitemap, 'utf8');
sitemapTime = performance.now() - sitemapStart;
}

// 6. log output
await this.printStats({
cwd: outDir,
pageCount: input.length,
pageTime: Math.round(performance.now() - start),
sitemapTime,
});
// 6. clean up
await viteServer.close();

// 7. log output
if (logging.level && levels[logging.level] <= levels['info']) {
await this.printStats({
cwd: this.config.dist,
pageCount: input.length,
pageTime: Math.round(performance.now() - start),
sitemapTime,
});
}
}

/** Extract all static paths from a dynamic route */
Expand Down
6 changes: 1 addition & 5 deletions packages/astro/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { AstroConfig, AstroUserConfig } from './@types/astro';

import { existsSync } from 'fs';
import getPort from 'get-port';
import * as colors from 'kleur/colors';
import path from 'path';
import { pathToFileURL } from 'url';
Expand Down Expand Up @@ -58,10 +57,7 @@ export const AstroConfigSchema = z.object({
devOptions: z
.object({
hostname: z.string().optional().default('localhost'),
port: z
.number()
.optional()
.transform((val) => val || getPort({ port: getPort.makeRange(3000, 3050) })),
port: z.number().optional().default(3000),
tailwindConfig: z.string().optional(),
trailingSlash: z
.union([z.literal('always'), z.literal('never'), z.literal('ignore')])
Expand Down
Loading

0 comments on commit 347d5fd

Please sign in to comment.