Skip to content

Commit

Permalink
Svelte 4 & auto deno imports (#46)
Browse files Browse the repository at this point in the history
* chore(IDE): VSCode default formatter

* feat(svelte): move to svelte@4

use the deno esbuild plugins to handle
npm specifiers and remove the need for
a custom svelte/internal plugin

* fix: use patched version for GHA

renaming non-empty folder throws os error 39

* feat: ignore cache folder

* chore(prettier): move to v3

doctype is lowercase, now

* feat(svelte): support extra modules

it’s now easy to animate!
  • Loading branch information
mxdvl authored Aug 28, 2023
1 parent 3e4d5c5 commit 504dba2
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ build

components/Island.svelte
.DS_Store

.cache
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"[jsonc]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
"[javascript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
"cSpell.words": [
"hydrator",
"mononykus"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This is a Work-in-progress. If you want a production framework, look at
- [x] Figure out how to build `Island.svelte` cleanly
- [x] Figure out how to serve `islands.js` cleanly
- [x] Handle nested islands
- [ ] Allow imports of `svelte/store`, `svelte/motion`, `svelte/transition`,
- [x] Allow imports of `svelte/store`, `svelte/motion`, `svelte/transition`,
`svelte/animate` and `svelte/easing`, but not `svelte/register`.

## About the name Mononykus
Expand Down
212 changes: 205 additions & 7 deletions deno.lock

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/_site/components/Bee.island.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script>
import { onMount } from "svelte/internal";
import { spring } from "svelte/motion";
const coords = spring({ x: 50, y: 50 });
onMount(() => {
coords.set({ x: 100, y: 200 }, { hard: true });
setInterval(() => {
coords.set(
{ x: Math.random() * 600, y: Math.random() * 600 },
{ soft: 400 }
);
}, 600);
});
</script>

<aside style={`transform: translate3D(${$coords.x}px,${$coords.y}px,0);`}>
🐝
</aside>

<style>
aside {
position: absolute;
top: 0;
left: 0;
}
</style>
3 changes: 3 additions & 0 deletions src/_site/routes/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import HelloFriend from "../components/HelloFriend.island.svelte";
import Counter from "../components/Counter.island.svelte";
import Body from "../components/Body.svelte";
import Bee from "../components/Bee.island.svelte";
</script>

<svelte:head>
Expand Down Expand Up @@ -48,6 +49,8 @@
<div class="island">
<Counter />
</div>

<Bee />
</main>
</Body>

Expand Down
2 changes: 1 addition & 1 deletion src/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Deno.test({
await process.output();

assert(html.startsWith(
"<!DOCTYPE html>",
"<!doctype html>",
));
assert(html.includes(
"<title>Mononykus – Deno + Svelte</title>",
Expand Down
30 changes: 24 additions & 6 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as esbuild from "https://deno.land/x/[email protected]/mod.js";
import { svelte_components } from "./esbuild_plugins/svelte_components.ts";
import { svelte_internal } from "./esbuild_plugins/svelte_internal.ts";
import * as esbuild from "https://deno.land/x/[email protected]/mod.js";
import {
svelte_components,
VERSION,
} from "./esbuild_plugins/svelte_components.ts";
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";
Expand All @@ -10,6 +12,7 @@ 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://raw.githubusercontent.com/mxdvl/esbuild_deno_loader/patch-1/mod.ts";

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

Expand Down Expand Up @@ -39,6 +42,21 @@ const options: Options = {
minify: !flags.watch || flags.minify,
};

const importMap = {
imports: {
"svelte": `npm:svelte@${VERSION}`,
"svelte/internal": `npm:svelte@${VERSION}/internal`,
"svelte/internal/disclose-version":
`npm:svelte@${VERSION}/internal/disclose-version`,
"svelte/easing": `npm:svelte@${VERSION}/easing`,
"svelte/motion": `npm:svelte@${VERSION}/motion`,
"svelte/register": `npm:svelte@${VERSION}/register`,
"svelte/store": `npm:svelte@${VERSION}/store`,
"svelte/transition": `npm:svelte@${VERSION}/transition`,
},
};
const importMapURL = `data:application/json,${JSON.stringify(importMap)}`;

// clean out old builds, if they exist
const clean = async (out_dir: Options["out_dir"]) => {
try {
Expand Down Expand Up @@ -86,7 +104,7 @@ const rebuild = async ({
const baseESBuildConfig = {
logLevel: "info",
format: "esm",
minify: minify,
minify,
bundle: true,
} as const satisfies Partial<esbuild.BuildOptions>;

Expand All @@ -95,7 +113,7 @@ const rebuild = async ({
write: false,
plugins: [
svelte_components(site_dir, base),
svelte_internal,
...denoPlugins({ importMapURL }),
build_routes,
],
outdir: out_dir,
Expand All @@ -107,7 +125,7 @@ const rebuild = async ({
write: true,
plugins: [
svelte_components(site_dir, base),
svelte_internal,
...denoPlugins({ importMapURL }),
],
outdir: out_dir + "components/",
splitting: true,
Expand Down
4 changes: 2 additions & 2 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 "https://deno.land/x/[email protected].16/mod.js";
import type { Plugin } from "https://deno.land/x/[email protected].19/mod.js";
import { dirname } from "https://deno.land/[email protected]/path/mod.ts";
import { ensureDir } from "https://deno.land/[email protected]/fs/ensure_dir.ts";
import { get_route_html } from "./get_route_html.ts";
Expand Down Expand Up @@ -48,7 +48,7 @@ export const build_routes: Plugin = {

await Deno.writeTextFile(
dist_path,
get_route_html({ html, css, head: deduped_head }),
await get_route_html({ html, css, head: deduped_head }),
);
}));

Expand Down
7 changes: 3 additions & 4 deletions src/esbuild_plugins/get_route_html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-expect-error -- the package is untyped
import { format } from "npm:prettier";
import { format } from "npm:[email protected]";

interface TemplateOptions {
css: string;
Expand Down Expand Up @@ -27,7 +26,7 @@ export const get_route_html = ({ html, css, head }: {
html: string;
css: string;
head: string;
}) => {
}): Promise<string> => {
const page = template({
css,
head,
Expand All @@ -46,6 +45,6 @@ export const get_route_html = ({ html, css, head }: {
);
} catch (_) {
console.warn("Could not format the html");
return page;
return Promise.resolve(page);
}
};
20 changes: 15 additions & 5 deletions src/esbuild_plugins/svelte_components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {
dirname,
resolve,
} from "https://deno.land/[email protected]/path/mod.ts";
import type { Plugin } from "https://deno.land/x/[email protected].16/mod.js";
import type { Plugin } from "https://deno.land/x/[email protected].19/mod.js";
import { normalize } from "https://deno.land/[email protected]/path/mod.ts";
import { compile } from "npm:svelte@3.58/compiler";
import type { ComponentType } from "npm:svelte@3.58";
import { compile, VERSION } from "npm:svelte@4.2.0/compiler";
import type { ComponentType } from "npm:svelte@4.2.0";

const filter = /\.svelte$/;
const name = "mononykus/svelte";
Expand Down Expand Up @@ -44,7 +44,7 @@ export const svelte_components = (
setup(build) {
const generate = build.initialOptions.write ? "dom" : "ssr";

build.onResolve({ filter }, ({ path, kind, importer }) => {
build.onResolve({ filter }, ({ path, kind, importer, resolveDir }) => {
if (generate === "dom") {
if (
kind === "import-statement" &&
Expand All @@ -56,6 +56,10 @@ export const svelte_components = (
path: path.replace(/\.svelte$/, ".js"),
external: true,
};
} else {
return {
path: resolve(resolveDir, path),
};
}
} else {
if (
Expand All @@ -67,6 +71,10 @@ export const svelte_components = (
path: resolve(dirname(importer), path),
suffix: ssr_island,
};
} else {
return {
path: resolve(resolveDir, path),
};
}
}
});
Expand All @@ -88,7 +96,7 @@ export const svelte_components = (

const { js: { code } } = compile(source, {
generate,
css: "injected",
css: "external",
cssHash: ({ hash, css }) => `◖${hash(css)}◗`,
hydratable: generate === "dom",
enableSourcemap: false,
Expand Down Expand Up @@ -133,3 +141,5 @@ export const svelte_components = (
});
},
});

export { VERSION };
24 changes: 0 additions & 24 deletions src/esbuild_plugins/svelte_internal.ts

This file was deleted.

0 comments on commit 504dba2

Please sign in to comment.