From 685cf7d51d2ce6d541008fef2c055c659667c523 Mon Sep 17 00:00:00 2001 From: Max Duval Date: Wed, 26 Apr 2023 10:22:27 +0100 Subject: [PATCH 1/4] fix: all paths are absolute default build output is now current working directory --- .github/workflows/deploy.yml | 4 ++-- src/build.ts | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 94f93ce..bd8d4dd 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -47,7 +47,7 @@ jobs: with: project: mononykus entrypoint: https://deno.land/std@0.183.0/http/file_server.ts - root: src/_site/build + root: build gh: if: github.ref_name == 'main' @@ -80,7 +80,7 @@ jobs: - name: Upload artifact uses: actions/upload-pages-artifact@v1 with: - path: src/_site/build + path: build - name: Deploy to GitHub Pages id: deployment diff --git a/src/build.ts b/src/build.ts index 65d1369..b3cee22 100644 --- a/src/build.ts +++ b/src/build.ts @@ -9,6 +9,8 @@ import { walk } from "https://deno.land/std@0.177.0/fs/walk.ts"; import { create_handler } from "./server.ts"; import { globToRegExp } from "https://deno.land/std@0.182.0/path/glob.ts"; import { copy } from "https://deno.land/std@0.179.0/fs/copy.ts"; +import { resolve } from "https://deno.land/std@0.177.0/path/mod.ts"; +import { normalize as normalise } from "https://deno.land/std@0.177.0/path/posix.ts"; // the web is posix const flags = parse(Deno.args, { string: ["site", "build", "base"], @@ -16,9 +18,9 @@ const flags = parse(Deno.args, { default: { site: "_site/", dev: false, base: "/" }, }); -const site_dir = flags.site.replace(/\/?$/, "/"); -const build_dir = (flags.build ?? `${site_dir}build/`).replace(/\/?$/, "/"); -const base_path = flags.base.replace(/\/?$/, "/"); +const site_dir = resolve(flags.site); +const build_dir = resolve(flags.build ?? "build"); +const base_path = normalise(flags.base + "/"); // clean out old builds, if they exist try { @@ -35,10 +37,10 @@ export const get_svelte_files = async ({ const glob = (glob: string) => globToRegExp(glob, { globstar: true }); const files: string[] = []; for await ( - const { path } of walk(site_dir + dir, { + const { path } of walk(resolve(site_dir, dir), { match: [ - glob(site_dir + "/routes/**/*.svelte"), - glob(site_dir + "/components/**/*.island.svelte"), + glob(resolve(site_dir, "routes") + "/**/*.svelte"), + glob(resolve(site_dir, "components") + "/**/*.island.svelte"), ], includeDirs: false, }) @@ -76,13 +78,19 @@ const islandsESBuildConfig: esbuild.BuildOptions = { svelte_components, svelte_internal, ], - outdir: build_dir + "components/", + outdir: resolve(build_dir, "components/"), splitting: true, ...baseESBuildConfig, }; const copy_assets = async () => - await copy(site_dir + "assets", build_dir + "assets", { overwrite: true }); + await copy( + resolve(site_dir, "assets"), + resolve(build_dir, "assets"), + { + overwrite: true, + }, + ); const rebuild = async () => { await Promise.all([ From 526a8752db78a2ccc08475660e2851629cebd6df Mon Sep 17 00:00:00 2001 From: Max Duval Date: Wed, 26 Apr 2023 10:22:43 +0100 Subject: [PATCH 2/4] docs: more informative logs --- src/build.ts | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/build.ts b/src/build.ts index b3cee22..6878aae 100644 --- a/src/build.ts +++ b/src/build.ts @@ -11,6 +11,7 @@ import { globToRegExp } from "https://deno.land/std@0.182.0/path/glob.ts"; import { copy } from "https://deno.land/std@0.179.0/fs/copy.ts"; import { resolve } from "https://deno.land/std@0.177.0/path/mod.ts"; import { normalize as normalise } from "https://deno.land/std@0.177.0/path/posix.ts"; // the web is posix +import { bold, underline } from "https://deno.land/std@0.177.0/fmt/colors.ts"; const flags = parse(Deno.args, { string: ["site", "build", "base"], @@ -22,12 +23,7 @@ const site_dir = resolve(flags.site); const build_dir = resolve(flags.build ?? "build"); const base_path = normalise(flags.base + "/"); -// clean out old builds, if they exist -try { - await Deno.remove(build_dir, { recursive: true }); -} catch (_error) { - // do nothing -} +console.info("ðŸŠķ", " – ", bold("Mononykus")); export const get_svelte_files = async ({ dir, @@ -50,10 +46,8 @@ export const get_svelte_files = async ({ return files; }; -await ensureDir(build_dir); - const baseESBuildConfig = { - logLevel: "info", + logLevel: "silent", format: "esm", minify: !flags.dev, bundle: true, @@ -83,6 +77,16 @@ const islandsESBuildConfig: esbuild.BuildOptions = { ...baseESBuildConfig, }; +const prepare = async () => { + try { + await Deno.remove(build_dir, { recursive: true }); + } catch (_error) { + // do nothing + } + + await ensureDir(build_dir); +}; + const copy_assets = async () => await copy( resolve(site_dir, "assets"), @@ -92,7 +96,7 @@ const copy_assets = async () => }, ); -const rebuild = async () => { +const build = async () => { await Promise.all([ esbuild.build(routesESBuildConfig), esbuild.build(islandsESBuildConfig), @@ -100,7 +104,8 @@ const rebuild = async () => { ]); }; -await rebuild(); +await prepare(); +await build(); if (flags.dev) { const watcher = Deno.watchFs(site_dir); @@ -110,9 +115,14 @@ if (flags.dev) { if (path && (kind === "modify" || kind === "create")) { if (path.includes(build_dir)) continue; clearTimeout(timeout); - timeout = setTimeout(rebuild, 6); + timeout = setTimeout(build, 6); } } -} else { - Deno.exit(0); } + +console.info("\nServe the following directory:"); +console.info(underline(build_dir)); + +// Ensure that we exit promptly, +// without waiting for any async code in SSR islands +Deno.exit(0); From 38eab1fde5fe25857fc8a67b059e8eb6121472ce Mon Sep 17 00:00:00 2001 From: Max Duval Date: Wed, 26 Apr 2023 10:25:20 +0100 Subject: [PATCH 3/4] fix: correct favicon path --- src/_site/routes/index.svelte | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/_site/routes/index.svelte b/src/_site/routes/index.svelte index bb3153e..f64759f 100644 --- a/src/_site/routes/index.svelte +++ b/src/_site/routes/index.svelte @@ -5,7 +5,10 @@ import Body from "../components/Body.svelte"; -Mononykus – Deno + Svelte + + Mononykus – Deno + Svelte + +
From c226cece14c236f3e9d9b36e3b1268a4475f3a27 Mon Sep 17 00:00:00 2001 From: Max Duval Date: Wed, 26 Apr 2023 12:11:36 +0100 Subject: [PATCH 4/4] feat: pass base path to routes --- src/_site/routes/index.svelte | 8 +++++++- src/build.ts | 2 +- src/esbuild_plugins/routes.ts | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/_site/routes/index.svelte b/src/_site/routes/index.svelte index f64759f..d5c757f 100644 --- a/src/_site/routes/index.svelte +++ b/src/_site/routes/index.svelte @@ -3,11 +3,17 @@ import HelloFriend from "../components/HelloFriend.island.svelte"; import Counter from "../components/Counter.island.svelte"; import Body from "../components/Body.svelte"; + + export let base_path = "/"; Mononykus – Deno + Svelte - + diff --git a/src/build.ts b/src/build.ts index 6878aae..5418468 100644 --- a/src/build.ts +++ b/src/build.ts @@ -21,7 +21,7 @@ const flags = parse(Deno.args, { const site_dir = resolve(flags.site); const build_dir = resolve(flags.build ?? "build"); -const base_path = normalise(flags.base + "/"); +const base_path = normalise("/" + flags.base + "/"); console.info("ðŸŠķ", " – ", bold("Mononykus")); diff --git a/src/esbuild_plugins/routes.ts b/src/esbuild_plugins/routes.ts index b3ff278..3b35c14 100644 --- a/src/esbuild_plugins/routes.ts +++ b/src/esbuild_plugins/routes.ts @@ -24,11 +24,11 @@ export const build_routes = ( "data:application/javascript," + encodeURIComponent(route.text) ) as { default: { - render(): SSROutput; + render(props: { base_path: string }): SSROutput; }; }; - const { html, css: _css, head } = module.default.render(); + const { html, css: _css, head } = module.default.render({ base_path }); const css = _css?.code ?? ""; const dist_path = route.path.replace(".js", ".html");