From 40374dc47613b7084e3d2aba8fc0e4428d34ff11 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 2 Oct 2020 17:36:25 -0700 Subject: [PATCH] refactor: exclude SSG exports from bundles (#17) --- README.md | 2 +- cli.ts | 1 + src/bundle.ts | 6 ++++-- src/plugins/dext.ts | 16 +++++++++++++--- tests/integration_test.ts | 10 +++++++--- 5 files changed, 26 insertions(+), 9 deletions(-) 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, }))), ); 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..a815937 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,9 @@ 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 = [ diff --git a/tests/integration_test.ts b/tests/integration_test.ts index 29e33e8..dd78d36 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,12 @@ 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, + }, ], );