From ec4beaf6c3ced3484a3230aa3b81cc98c9eb077c Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sat, 3 Oct 2020 02:13:28 +0200 Subject: [PATCH 1/5] refactor: remove SSG exports from bundles --- src/bundle.ts | 6 ++++-- src/plugins/dext.ts | 14 +++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/bundle.ts b/src/bundle.ts index 2cfde09..3c51886 100644 --- a/src/bundle.ts +++ b/src/bundle.ts @@ -134,9 +134,11 @@ export async function bundle( ) as OutputChunk[]; for (const out of chunks) { - const page = pages.pages.find((p) => p.path === out.facadeModuleId!); const filename = `/${out.fileName}`; - if (page) { + if (out.facadeModuleId && out.facadeModuleId.startsWith("dext-page://")) { + const page = pages.pages.find((p) => + p.path === out.facadeModuleId!.substring("dext-page://".length) + )!; const imports = [ flattenImports(chunks, out.fileName), ...out.implicitlyLoadedBefore, diff --git a/src/plugins/dext.ts b/src/plugins/dext.ts index 44ae5ec..1ccc8d1 100644 --- a/src/plugins/dext.ts +++ b/src/plugins/dext.ts @@ -28,8 +28,8 @@ export function dextPlugin( for (const component in pageMap) { implicitlyLoadedAfterOneOf.push(component); this.emitFile({ - id: component, name: pageMap[component].name.replace("/", "-"), + id: "dext-page://" + component, type: "chunk", }); } @@ -41,9 +41,17 @@ export function dextPlugin( }, resolveId(source, referrer) { if (referrer === "dext:///main.js") return source; + if (referrer?.startsWith("dext-page://")) { + return this.resolve(source, referrer.substring("dext-page://".length)); + } return null; }, load(id) { + if (id.startsWith("dext-page://")) { + return `export { default } from "${ + id.substring("dext-page://".length) + }";`; + } if (id == "dext:///main.js") { const bundle = `import { h, hydrate, Router, Route, AsyncRoute, Error404, loadComponent } from "${runtimeURL}"; @@ -57,7 +65,7 @@ function Dext() { ${ Object.entries(pageMap).map(([id, page]) => { - return ` loadComponent(import("${id}"), ${ + return ` loadComponent(import("dext-page://${id}"), ${ page.hasGetStaticData ? "true" : "false" }, path)} />`; }).join("\n ") @@ -77,7 +85,7 @@ hydrate(, document.getElementById("__dext")!);`; for (const name in bundle) { const file = bundle[name]; if (file.type === "chunk" && file.isEntry) { - const component = file.facadeModuleId!; + const component = file.facadeModuleId!.substring("dext-page://".length); const page = pageMap[component]; const imports = [ From a22c37bf87fdf7884930c5a81dd8453e67741b0f Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sat, 3 Oct 2020 02:24:59 +0200 Subject: [PATCH 2/5] fix `dext serve` and update readme --- README.md | 2 +- cli.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cbdb18b..d40044f 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ The Preact Framework for Deno. Dext.ts is heavily inspired by Next.js. - Zero config - Pre-render pages at build time (SSG) -- Tiny (example is only 6.2KB of JS) +- Tiny (example is only 5.9KB of JS) - Client hydration - Built-in routing - Zero config TypeScript support diff --git a/cli.ts b/cli.ts index bd62229..9e421ec 100644 --- a/cli.ts +++ b/cli.ts @@ -80,6 +80,7 @@ async function build(_options: unknown, root?: string) { JSON.stringify(pages.pages.map((page) => ({ name: page.name, route: page.route, + hasGetStaticPaths: page.hasGetStaticPaths, }))), ); From 5a4c91afc83746a8fc7445708c0d4da90cd1209e Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sat, 3 Oct 2020 02:27:57 +0200 Subject: [PATCH 3/5] fmt --- src/plugins/dext.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/plugins/dext.ts b/src/plugins/dext.ts index 1ccc8d1..a815937 100644 --- a/src/plugins/dext.ts +++ b/src/plugins/dext.ts @@ -85,7 +85,9 @@ hydrate(, document.getElementById("__dext")!);`; for (const name in bundle) { const file = bundle[name]; if (file.type === "chunk" && file.isEntry) { - const component = file.facadeModuleId!.substring("dext-page://".length); + const component = file.facadeModuleId!.substring( + "dext-page://".length, + ); const page = pageMap[component]; const imports = [ From 4677de04d850d061636fc1a1e7b5975d468ccdc8 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sat, 3 Oct 2020 02:30:43 +0200 Subject: [PATCH 4/5] fix tests --- tests/integration_test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/integration_test.ts b/tests/integration_test.ts index 29e33e8..9d604a3 100644 --- a/tests/integration_test.ts +++ b/tests/integration_test.ts @@ -18,7 +18,7 @@ integrationTest({ JSON.parse( await Deno.readTextFile(join(ctx.dir, ".dext", "pagemap.json")), ), - [{ name: "index", route: "/" }], + [{ name: "index", route: "/", hasGetStaticPaths: false }], ); const staticdir = join(ctx.dir, ".dext", "static"); @@ -46,8 +46,8 @@ integrationTest({ await Deno.readTextFile(join(ctx.dir, ".dext", "pagemap.json")), ), [ - { name: "index", route: "/" }, - { name: "uppercase/[str]", route: "/uppercase/:str" }, + { name: "index", route: "/", hasGetStaticPaths: false }, + { name: "uppercase/[str]", route: "/uppercase/:str", hasGetStaticPaths: true }, ], ); From 9244b69f8d9c81155a03e0b9835bd0362084a912 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sat, 3 Oct 2020 02:33:29 +0200 Subject: [PATCH 5/5] fmt --- tests/integration_test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/integration_test.ts b/tests/integration_test.ts index 9d604a3..dd78d36 100644 --- a/tests/integration_test.ts +++ b/tests/integration_test.ts @@ -47,7 +47,11 @@ integrationTest({ ), [ { name: "index", route: "/", hasGetStaticPaths: false }, - { name: "uppercase/[str]", route: "/uppercase/:str", hasGetStaticPaths: true }, + { + name: "uppercase/[str]", + route: "/uppercase/:str", + hasGetStaticPaths: true, + }, ], );