summaryrefslogtreecommitdiff
path: root/src/dev.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/dev.ts')
-rw-r--r--src/dev.ts107
1 files changed, 28 insertions, 79 deletions
diff --git a/src/dev.ts b/src/dev.ts
index 0872ffe74..93a890057 100644
--- a/src/dev.ts
+++ b/src/dev.ts
@@ -1,12 +1,11 @@
import type { AstroConfig } from './@types/astro';
import type { LogOptions } from './logger.js';
-import { loadConfiguration, startServer as startSnowpackServer, logger as snowpackLogger } from 'snowpack';
-import { existsSync, promises as fsPromises } from 'fs';
+
+import { logger as snowpackLogger } from 'snowpack';
import http from 'http';
import { relative as pathRelative } from 'path';
-import { defaultLogDestination, info, error, parseError } from './logger.js';
-
-const { readFile } = fsPromises;
+import { defaultLogDestination, error, parseError } from './logger.js';
+import { createRuntime } from './runtime.js';
const hostname = '127.0.0.1';
const port = 3000;
@@ -20,95 +19,45 @@ const logging: LogOptions = {
};
export default async function (astroConfig: AstroConfig) {
- const { projectRoot, hmxRoot } = astroConfig;
-
- const internalPath = new URL('./frontend/', import.meta.url);
- const snowpackConfigPath = new URL('./snowpack.config.js', projectRoot);
+ const { projectRoot } = astroConfig;
- // Workaround for SKY-251
- const hmxPlugOptions: { resolve?: (s: string) => string } = {};
- if (existsSync(new URL('./package-lock.json', projectRoot))) {
- const pkgLockStr = await readFile(new URL('./package-lock.json', projectRoot), 'utf-8');
- const pkgLock = JSON.parse(pkgLockStr);
- hmxPlugOptions.resolve = (pkgName: string) => {
- const ver = pkgLock.dependencies[pkgName].version;
- return `/_snowpack/pkg/${pkgName}.v${ver}.js`;
- };
- }
-
- const snowpackConfig = await loadConfiguration(
- {
- root: projectRoot.pathname,
- mount: {
- [hmxRoot.pathname]: '/_hmx',
- [internalPath.pathname]: '/__hmx_internal__',
- },
- plugins: [['astro/snowpack-plugin', hmxPlugOptions]],
- devOptions: {
- open: 'none',
- output: 'stream',
- port: 0,
- },
- packageOptions: {
- knownEntrypoints: ['preact-render-to-string'],
- external: ['@vue/server-renderer'],
- },
- },
- snowpackConfigPath.pathname
- );
- const snowpack = await startSnowpackServer({
- config: snowpackConfig,
- lockfile: null,
- });
- const runtime = snowpack.getServerRuntime();
+ const runtime = await createRuntime(astroConfig, logging);
const server = http.createServer(async (req, res) => {
- const fullurl = new URL(req.url || '/', 'https://example.org/');
- const reqPath = decodeURI(fullurl.pathname);
- const selectedPage = reqPath.substr(1) || 'index';
- info(logging, 'access', reqPath);
+ const result = await runtime.load(req.url);
- const selectedPageLoc = new URL(`./pages/${selectedPage}.hmx`, hmxRoot);
- const selectedPageMdLoc = new URL(`./pages/${selectedPage}.md`, hmxRoot);
- const selectedPageUrl = `/_hmx/pages/${selectedPage}.js`;
-
- // Non-hmx pages
- if (!existsSync(selectedPageLoc) && !existsSync(selectedPageMdLoc)) {
- try {
- const result = await snowpack.loadUrl(reqPath);
+ switch (result.statusCode) {
+ case 200: {
if (result.contentType) {
res.setHeader('Content-Type', result.contentType);
}
res.write(result.contents);
res.end();
- } catch (err) {
+ break;
+ }
+ case 404: {
+ const fullurl = new URL(req.url || '/', 'https://example.org/');
+ const reqPath = decodeURI(fullurl.pathname);
error(logging, 'static', 'Not found', reqPath);
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('Not Found');
+ break;
}
- return;
- }
-
- try {
- const mod = await runtime.importModule(selectedPageUrl);
- const html = await mod.exports.default();
- res.statusCode = 200;
- res.setHeader('Content-Type', 'text/html; charset=utf-8');
- res.end(html);
- } catch (err) {
- switch (err.code) {
- case 'parse-error': {
- err.filename = pathRelative(projectRoot.pathname, err.filename);
- debugger;
- parseError(logging, err);
- break;
- }
- default: {
- console.error(err.code, err);
- error(logging, 'running hmx', err);
- break;
+ case 500: {
+ switch (result.type) {
+ case 'parse-error': {
+ const err = result.error;
+ err.filename = pathRelative(projectRoot.pathname, err.filename);
+ parseError(logging, err);
+ break;
+ }
+ default: {
+ error(logging, 'executing hmx', result.error);
+ break;
+ }
}
+ break;
}
}
});