From b2dc3473540bbd40bb8b3a26d4dab9dfebd5552a Mon Sep 17 00:00:00 2001 From: Max Duval Date: Sat, 24 Aug 2024 20:12:47 +0100 Subject: [PATCH] Display route errors (#54) * feat: actually helpful route message * chore: format * chore: release version --- LICENSE.md | 23 ++++---- deno.json | 2 +- src/esbuild_plugins/build_routes.ts | 91 ++++++++++++++++++----------- 3 files changed, 69 insertions(+), 47 deletions(-) diff --git a/LICENSE.md b/LICENSE.md index 26d7a8a..8abb7de 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -2,20 +2,19 @@ MIT License Copyright © 2024 Max Duval -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/deno.json b/deno.json index 1c09bb3..a683534 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@mxdvl/mononykus", - "version": "0.7.7", + "version": "0.7.8", "license": "MIT", "exports": "./src/build.ts", "tasks": { diff --git a/src/esbuild_plugins/build_routes.ts b/src/esbuild_plugins/build_routes.ts index 901f9fb..854f24e 100644 --- a/src/esbuild_plugins/build_routes.ts +++ b/src/esbuild_plugins/build_routes.ts @@ -1,4 +1,4 @@ -import type { Plugin } from "esbuild"; +import type { OutputFile, Plugin } from "esbuild"; import { dirname } from "@std/path/dirname"; import { ensureDir } from "@std/fs/ensure-dir"; import { get_route_html } from "./get_route_html.ts"; @@ -9,6 +9,48 @@ interface SSROutput { css?: { code: string }; } +const FAILURE_FLAG = ""; + +/** safely render Svelte components */ +async function render( + { text }: OutputFile, +): Promise<{ html: string; css: string; head: string }> { + try { + const module = await import( + "data:application/javascript," + encodeURIComponent(text) + ) as { + default: { + render(): SSROutput; + }; + }; + + const { html, css: raw_css, head: raw_head } = module.default.render(); + + const css = raw_css?.code ?? ""; + + // remove any duplicate module imports (in cases where a page uses an island more than once) + const modules = new Set(); + const head = raw_head.replace( + //g, + (module) => { + if (modules.has(module)) { + return ""; + } + modules.add(module); + return module; + }, + ); + + return { html, css, head }; + } catch (error) { + return { + html: `${FAILURE_FLAG}

ERROR

${String(error)}
`, + head: "", + css: "", + }; + } +} + export const build_routes: Plugin = { name: "mononykus/build-routes", setup(build) { @@ -17,51 +59,32 @@ export const build_routes: Plugin = { const routes = result.outputFiles ?? []; - await Promise.allSettled(routes.map(async (route) => { - const module = await import( - "data:application/javascript," + encodeURIComponent(route.text) - ) as { - default: { - render(): SSROutput; - }; - }; - - const { html, css: _css, head } = module.default.render(); - - // remove any duplicate module imports (in cases where a page uses an island more than once) - const modules = new Set(); - const deduped_head = head.replace( - //g, - (module) => { - if (modules.has(module)) { - return ""; - } - modules.add(module); - return module; - }, - ); - - const css = _css?.code ?? ""; - + const results = await Promise.all(routes.map(async (route) => { const dist_path = route.path.replace(".js", ".html"); await ensureDir(dirname(dist_path)); + const template = await render(route); + await Deno.writeTextFile( dist_path, - await get_route_html({ html, css, head: deduped_head }), + await get_route_html(template), ); - })).then((results) => { - const { length } = results.filter(({ status }) => - status === "rejected" - ); - if (length > 0) console.log(`Failed to build ${length} routes.`); - }); + + return template.html.startsWith(FAILURE_FLAG) ? dist_path : undefined; + })); console.log( `Built ${routes.length} routes in ${ Math.ceil(performance.now() - start) }ms`, ); + + const failures = results.filter((result) => !!result); + if (failures.length > 0) { + console.warn( + ["–––", "Failed to build some routes:", ...failures].join("\n"), + ); + } }); }, };