From 7e0278bbb31053e372cef228798748c7a6f3272a Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Tue, 23 Aug 2022 15:29:12 +0200 Subject: [PATCH] feat: add support for preact devtools in dev mode (#639) Co-authored-by: Luca Casonato --- src/runtime/main_dev.ts | 2 ++ src/server/bundle.ts | 21 ++++++++++++++++++--- src/server/context.ts | 7 ++++++- 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/runtime/main_dev.ts diff --git a/src/runtime/main_dev.ts b/src/runtime/main_dev.ts new file mode 100644 index 00000000..2551dd5e --- /dev/null +++ b/src/runtime/main_dev.ts @@ -0,0 +1,2 @@ +import "preact/debug"; +export { revive } from "./main.ts"; diff --git a/src/server/bundle.ts b/src/server/bundle.ts index e63d7fb0..a2991dc9 100644 --- a/src/server/bundle.ts +++ b/src/server/bundle.ts @@ -1,3 +1,4 @@ +import { BuildOptions } from "https://deno.land/x/esbuild@v0.14.51/mod.js"; import { BUILD_ID } from "./constants.ts"; import { denoPlugin, esbuild, toFileUrl } from "./deps.ts"; import { Island, Plugin } from "./types.ts"; @@ -25,16 +26,25 @@ export class Bundler { #islands: Island[]; #plugins: Plugin[]; #cache: Map | Promise | undefined = undefined; + #dev: boolean; - constructor(islands: Island[], plugins: Plugin[], importMapURL: URL) { + constructor( + islands: Island[], + plugins: Plugin[], + importMapURL: URL, + dev: boolean, + ) { this.#islands = islands; this.#plugins = plugins; this.#importMapURL = importMapURL; + this.#dev = dev; } async bundle() { const entryPoints: Record = { - "main": new URL("../../src/runtime/main.ts", import.meta.url).href, + main: this.#dev + ? new URL("../../src/runtime/main_dev.ts", import.meta.url).href + : new URL("../../src/runtime/main.ts", import.meta.url).href, }; for (const island of this.#islands) { @@ -49,13 +59,18 @@ export class Bundler { const absWorkingDir = Deno.cwd(); await ensureEsbuildInitialized(); + // In dev-mode we skip identifier minification to be able to show proper + // component names in Preact DevTools instead of single characters. + const minifyOptions: Partial = this.#dev + ? { minifyIdentifiers: false, minifySyntax: true, minifyWhitespace: true } + : { minify: true }; const bundle = await esbuild.build({ bundle: true, define: { __FRSH_BUILD_ID: `"${BUILD_ID}"` }, entryPoints, format: "esm", metafile: true, - minify: true, + ...minifyOptions, outdir: ".", // This is requried to ensure the format of the outputFiles path is the same // between windows and linux diff --git a/src/server/context.ts b/src/server/context.ts index f7f06893..54812ccb 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -85,8 +85,13 @@ export class ServerContext { this.#notFound = notFound; this.#error = error; this.#plugins = plugins; - this.#bundler = new Bundler(this.#islands, this.#plugins, importMapURL); this.#dev = typeof Deno.env.get("DENO_DEPLOYMENT_ID") !== "string"; // Env var is only set in prod (on Deploy). + this.#bundler = new Bundler( + this.#islands, + this.#plugins, + importMapURL, + this.#dev, + ); } /**