Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display route errors #54

Merged
merged 3 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mxdvl/mononykus",
"version": "0.7.7",
"version": "0.7.8",
"license": "MIT",
"exports": "./src/build.ts",
"tasks": {
Expand Down
91 changes: 57 additions & 34 deletions src/esbuild_plugins/build_routes.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -9,6 +9,48 @@ interface SSROutput {
css?: { code: string };
}

const FAILURE_FLAG = "<!--mononykus:failed-->";

/** 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(
/<script[\s\S]*?<\/script>/g,
(module) => {
if (modules.has(module)) {
return "";
}
modules.add(module);
return module;
},
);

return { html, css, head };
} catch (error) {
return {
html: `${FAILURE_FLAG}<h1>ERROR</h1><pre>${String(error)}</pre>`,
head: "",
css: "",
};
}
}

export const build_routes: Plugin = {
name: "mononykus/build-routes",
setup(build) {
Expand All @@ -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(
/<script[\s\S]*?<\/script>/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"),
);
}
});
},
};
Loading