Skip to content

Commit

Permalink
refactor: move to JSR
Browse files Browse the repository at this point in the history
  • Loading branch information
mxdvl committed Jun 18, 2024
1 parent 07c3d8b commit 2d58b6b
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
"hydrator",
"mononykus"
]
}
}
3 changes: 3 additions & 0 deletions deno.jsonc → deno.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"name": "@mxdvl/mononykus",
"version": "0.7.3",
"exports": "./src/build.ts",
"tasks": {
"build": "deno run -A src/build.ts --site_dir src/_site",
"dev": "deno run -A src/build.ts --site_dir src/_site --watch --base=mononykus"
Expand Down
377 changes: 377 additions & 0 deletions deno.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/build.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
import { assert } from "jsr:@std/testing/asserts";
import { build } from "./build.ts";

const base = "mononykus/";
Expand Down
42 changes: 21 additions & 21 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import * as esbuild from "https://deno.land/x/[email protected]/mod.js";
import { svelte_components } from "./esbuild_plugins/svelte_components.ts";
import { denoPlugins } from "jsr:@luca/esbuild-deno-loader@^0.10.3";
import { parseArgs } from "jsr:@std/[email protected]/parse-args";
import { copy, ensureDir, walk } from "jsr:@std/[email protected]";
import { globToRegExp, normalize } from "jsr:@std/[email protected]";
import * as esbuild from "npm:[email protected]";
import { build_routes } from "./esbuild_plugins/build_routes.ts";
import { ensureDir } from "https://deno.land/[email protected]/fs/ensure_dir.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { walk } from "https://deno.land/[email protected]/fs/walk.ts";
import { svelte_components } from "./esbuild_plugins/svelte_components.ts";
import { create_handler } from "./server.ts";
import { globToRegExp } from "https://deno.land/[email protected]/path/glob.ts";
import { copy } from "https://deno.land/[email protected]/fs/copy.ts";
import { normalize } from "https://deno.land/[email protected]/path/mod.ts";
import { denoPlugins } from "https://deno.land/x/[email protected]/mod.ts";

const slashify = (path: string) => normalize(path + "/");

Expand All @@ -20,7 +16,7 @@ type Options = {
minify: boolean;
};

const flags = parse(Deno.args, {
const flags = parseArgs(Deno.args, {
string: ["site_dir", "out_dir", "base"],
boolean: ["minify", "watch"],
default: {
Expand Down Expand Up @@ -56,7 +52,7 @@ export const get_svelte_files = async ({
}: {
site_dir: Options["site_dir"];
dir: "routes/" | "components/";
}) => {
}): Promise<string[]> => {
const glob = (glob: string) => globToRegExp(glob, { globstar: true });
const files: string[] = [];
for await (
Expand All @@ -82,7 +78,7 @@ export const rebuild = async ({
out_dir,
site_dir,
minify,
}: Options) => {
}: Options): Promise<void> => {
const baseESBuildConfig = {
logLevel: "info",
format: "esm",
Expand Down Expand Up @@ -114,7 +110,7 @@ export const rebuild = async ({
...baseESBuildConfig,
};

return Promise.all([
await Promise.all([
esbuild.build(routesESBuildConfig),
esbuild.build(islandsESBuildConfig),
copy_assets({ site_dir, out_dir }),
Expand All @@ -128,7 +124,7 @@ export const build = async (
site_dir: _site_dir = options.site_dir,
minify = options.minify,
}: Partial<Options> = {},
) => {
): Promise<void> => {
const base = slashify(_base);
const out_dir = slashify(_out_dir);
const site_dir = slashify(_site_dir);
Expand All @@ -147,7 +143,8 @@ export const watch = async (
site_dir: _site_dir = options.site_dir,
minify = options.minify,
}: Partial<Options> = {},
) => {
signal: AbortSignal,
): Promise<void> => {
const base = slashify(_base);
const out_dir = slashify(_out_dir);
const site_dir = slashify(_site_dir);
Expand All @@ -158,7 +155,7 @@ export const watch = async (

await _rebuild();

serve(create_handler({ base, out_dir }), { port: 4507 });
Deno.serve({ port: 4507, signal }, create_handler({ base, out_dir }));

const watcher = Deno.watchFs(site_dir);
let timeout;
Expand All @@ -173,12 +170,15 @@ export const watch = async (

if (import.meta.main) {
if (flags.watch) {
Deno.addSignalListener("SIGINT", async () => {
console.log("\nShutting down gracefully…");
const controller = new AbortController();
const shutdown = async () => {
console.log("\nShutting down gracefully, light as a feather…");
controller.abort();
await esbuild.stop();
});
};

await watch(options);
Deno.addSignalListener("SIGINT", shutdown);
await watch(options, controller.signal).catch(shutdown);
} else {
await build(options);
}
Expand Down
6 changes: 3 additions & 3 deletions src/esbuild_plugins/build_routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Plugin } from "https://deno.land/x/esbuild@v0.20.1/mod.js";
import { dirname } from "https://deno.land/std@0.182.0/path/mod.ts";
import { ensureDir } from "https://deno.land/std@0.177.0/fs/ensure_dir.ts";
import type { Plugin } from "npm:esbuild@0.20.2";
import { dirname } from "jsr:@std/path@0.224/dirname";
import { ensureDir } from "jsr:@std/fs@0.224/ensure-dir";
import { get_route_html } from "./get_route_html.ts";

interface SSROutput {
Expand Down
8 changes: 4 additions & 4 deletions src/esbuild_plugins/svelte_components.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
basename,
dirname,
normalize as normalise,
resolve,
} from "https://deno.land/[email protected]/path/mod.ts";
import type { Plugin } from "https://deno.land/x/[email protected]/mod.js";
import { normalize } from "https://deno.land/[email protected]/path/mod.ts";
} from "jsr:@std/[email protected]";
import type { Plugin } from "npm:[email protected]";
import { compile, VERSION } from "npm:[email protected]/compiler";
import type { ComponentType } from "npm:[email protected]";

Expand Down Expand Up @@ -89,7 +89,7 @@ export const svelte_components = (
.replace(/(\.island)?\.svelte$/, "")
.replaceAll(/(\.|\W)/g, "_");

const module_src = normalize("/" + base_path + path.split(site_dir)[1])
const module_src = normalise("/" + base_path + path.split(site_dir)[1])
.replace(
/svelte$/,
"js",
Expand Down
7 changes: 3 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Handler } from "https://deno.land/[email protected]/http/server.ts";
import { serveDir } from "https://deno.land/[email protected]/http/file_server.ts";
import { normalize as normalise } from "https://deno.land/[email protected]/path/posix.ts";
import { serveDir } from "jsr:@std/[email protected]/file-server";
import { normalize as normalise } from "jsr:@std/[email protected]";

interface ServerOptions {
base: string;
Expand All @@ -9,7 +8,7 @@ interface ServerOptions {

export const create_handler = (
{ base, out_dir }: ServerOptions,
): Handler => ((req) => {
): Deno.ServeHandler => ((req) => {
const url = new URL(req.url);

const normalised_base = normalise("/" + base);
Expand Down

0 comments on commit 2d58b6b

Please sign in to comment.