summaryrefslogtreecommitdiff
path: root/src/generate.ts
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@matthewphillips.info> 2021-03-25 14:06:08 -0400
committerGravatar GitHub <noreply@github.com> 2021-03-25 14:06:08 -0400
commit3db595937719b89956c594e4a76ee68ae8de098a (patch)
treee463889925f71539f28730f957b195b1806b3cb0 /src/generate.ts
parent18e7cc5af903543ac6f46780bfea67c13c6517df (diff)
downloadastro-3db595937719b89956c594e4a76ee68ae8de098a.tar.gz
astro-3db595937719b89956c594e4a76ee68ae8de098a.tar.zst
astro-3db595937719b89956c594e4a76ee68ae8de098a.zip
First pass at the build (#27)
This updates `astro build` to do a production build. It works! No optimizations yet.
Diffstat (limited to 'src/generate.ts')
-rw-r--r--src/generate.ts61
1 files changed, 0 insertions, 61 deletions
diff --git a/src/generate.ts b/src/generate.ts
deleted file mode 100644
index 4a31cc291..000000000
--- a/src/generate.ts
+++ /dev/null
@@ -1,61 +0,0 @@
-import type { AstroConfig } from './@types/astro';
-import { loadConfiguration, startServer as startSnowpackServer } from 'snowpack';
-import { promises as fsPromises } from 'fs';
-import { relative as pathRelative } from 'path';
-
-const { mkdir, readdir, stat, writeFile } = fsPromises;
-
-async function* allPages(root: URL): AsyncGenerator<URL, void, unknown> {
- for (const filename of await readdir(root)) {
- const fullpath = new URL(filename, root);
- const info = await stat(fullpath);
-
- if (info.isDirectory()) {
- yield* allPages(new URL(fullpath + '/'));
- } else {
- yield fullpath;
- }
- }
-}
-
-export default async function (astroConfig: AstroConfig) {
- const { projectRoot, astroRoot } = astroConfig;
- const pageRoot = new URL('./pages/', astroRoot);
- const dist = new URL(astroConfig.dist + '/', projectRoot);
-
- const configPath = new URL('./snowpack.config.js', projectRoot).pathname;
- const config = await loadConfiguration(
- {
- root: projectRoot.pathname,
- devOptions: { open: 'none', output: 'stream' },
- },
- configPath
- );
- const snowpack = await startSnowpackServer({
- config,
- lockfile: null, // TODO should this be required?
- });
-
- const runtime = snowpack.getServerRuntime();
-
- for await (const filepath of allPages(pageRoot)) {
- const rel = pathRelative(astroRoot.pathname, filepath.pathname); // pages/index.astro
- const pagePath = `/_astro/${rel.replace(/\.(astro|md)/, '.js')}`;
-
- try {
- const outPath = new URL('./' + rel.replace(/\.(astro|md)/, '.html'), dist);
- const outFolder = new URL('./', outPath);
- const mod = await runtime.importModule(pagePath);
- const html = await mod.exports.default({});
-
- await mkdir(outFolder, { recursive: true });
- await writeFile(outPath, html, 'utf-8');
- } catch (err) {
- console.error('Unable to generate page', rel);
- console.error(err);
- }
- }
-
- await snowpack.shutdown();
- process.exit(0);
-}