From aeaddb1eaa4a5919586cb88abe2ff56d7339372b Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sat, 25 Sep 2021 02:28:43 +0200 Subject: [PATCH] chore: use esm.sh + optionally use native esbuild (#52) `preact` and `preact-render-to-string` are now imported from esm.sh. Additionally, the native binary build of esbuild will now be used in deno cli to speed up bundle times. --- src/runtime/deps.ts | 6 +++--- src/server/bundle.ts | 26 +++++++++++++++----------- src/server/deps.ts | 15 ++++++++++----- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/runtime/deps.ts b/src/runtime/deps.ts index 078044eb..f4a1658c 100644 --- a/src/runtime/deps.ts +++ b/src/runtime/deps.ts @@ -1,4 +1,4 @@ -export * from "https://x.lcas.dev/preact@10.5.11/mod.js"; +export * from "https://esm.sh/preact@10.5.14"; export { useCallback, useContext, @@ -11,7 +11,7 @@ export { useReducer, useRef, useState, -} from "https://x.lcas.dev/preact@10.5.11/hooks.js"; +} from "https://esm.sh/preact@10.5.14/hooks"; export type { CreateHandle, EffectCallback, @@ -20,4 +20,4 @@ export type { Reducer, Ref, StateUpdater, -} from "https://x.lcas.dev/preact@10.5.11/hooks.js"; +} from "https://esm.sh/preact@10.5.14/hooks"; diff --git a/src/server/bundle.ts b/src/server/bundle.ts index ee767fc0..e567d563 100644 --- a/src/server/bundle.ts +++ b/src/server/bundle.ts @@ -1,13 +1,17 @@ -import { denoPlugin, esbuild } from "./deps.ts"; +import { denoPlugin, esbuild, esbuildTypes } from "./deps.ts"; import { Page } from "./types.ts"; let esbuildInitalized: boolean | Promise = false; async function ensureEsbuildInialized() { if (esbuildInitalized === false) { - esbuildInitalized = esbuild.initialize({ - wasmURL: "https://unpkg.com/esbuild-wasm@0.11.19/esbuild.wasm", - worker: false, - }); + if (Deno.run === undefined) { + esbuildInitalized = esbuild.initialize({ + wasmURL: "https://unpkg.com/esbuild-wasm@0.11.19/esbuild.wasm", + worker: false, + }); + } else { + esbuild.initialize({}); + } await esbuildInitalized; esbuildInitalized = true; } else if (esbuildInitalized instanceof Promise) { @@ -43,7 +47,7 @@ export class Bundler { platform: "neutral", plugins: [freshPlugin(this.#pages), denoPlugin()], splitting: true, - target: ["chrome89", "firefox88", "safari13"], + target: ["chrome90", "firefox88", "safari13"], treeShaking: true, write: false, }); @@ -87,7 +91,7 @@ export class Bundler { } } -function freshPlugin(pageList: Page[]): esbuild.Plugin { +function freshPlugin(pageList: Page[]): esbuildTypes.Plugin { const runtime = new URL("../../runtime.ts", import.meta.url); const pageMap = new Map(); @@ -100,8 +104,8 @@ function freshPlugin(pageList: Page[]): esbuild.Plugin { name: "fresh", setup(build) { build.onResolve({ filter: /fresh:\/\/.*/ }, function onResolve( - args: esbuild.OnResolveArgs, - ): esbuild.OnResolveResult | null | undefined { + args: esbuildTypes.OnResolveArgs, + ): esbuildTypes.OnResolveResult | null | undefined { return ({ path: args.path, namespace: "fresh", @@ -111,8 +115,8 @@ function freshPlugin(pageList: Page[]): esbuild.Plugin { build.onLoad( { filter: /.*/, namespace: "fresh" }, function onLoad( - args: esbuild.OnLoadArgs, - ): esbuild.OnLoadResult | null | undefined { + args: esbuildTypes.OnLoadArgs, + ): esbuildTypes.OnLoadResult | null | undefined { const url = new URL(args.path); const path = url.pathname; const page = pageMap.get(path); diff --git a/src/server/deps.ts b/src/server/deps.ts index 460626d4..5ec2ad83 100644 --- a/src/server/deps.ts +++ b/src/server/deps.ts @@ -1,17 +1,22 @@ // -- preact -- -export { renderToString } from "https://x.lcas.dev/preact@10.5.11/ssr.js"; +export { renderToString } from "https://esm.sh/preact-render-to-string@5.1.19?deps=preact@10.5.14"; // -- std -- -export { extname } from "https://deno.land/std@0.102.0/path/mod.ts"; +export { extname } from "https://deno.land/std@0.108.0/path/mod.ts"; // -- router -- export * as router from "https://crux.land/router@0.0.4"; // -- media types -- -export { lookup as mediaTypeLookup } from "https://deno.land/x/media_types@v2.9.3/mod.ts"; +export { lookup as mediaTypeLookup } from "https://deno.land/x/media_types@v2.10.2/mod.ts"; // -- esbuild -- // @deno-types="https://unpkg.com/esbuild-wasm@0.11.19/esm/browser.d.ts" -import * as esbuild from "https://gist.githubusercontent.com/lucacasonato/358c6b7e8198bfb2cf3d220e49fdcf5f/raw/3714cb0f59606eefc29ed0fea36d4cd93549938b/esbuild-wasm.js"; -export { esbuild }; +import * as esbuildWasm from "https://gist.githubusercontent.com/lucacasonato/358c6b7e8198bfb2cf3d220e49fdcf5f/raw/3714cb0f59606eefc29ed0fea36d4cd93549938b/esbuild-wasm.js"; +import * as esbuildNative from "https://deno.land/x/esbuild@v0.13.2/mod.js"; +// @ts-ignore trust me +const esbuild: typeof esbuildWasm = Deno.run === undefined + ? esbuildWasm + : esbuildNative; +export { esbuild, esbuildWasm as esbuildTypes }; export { denoPlugin } from "https://deno.land/x/esbuild_deno_loader@0.3.0/mod.ts";