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.
This commit is contained in:
Luca Casonato
2021-09-25 02:28:43 +02:00
committed by GitHub
parent a929b1022a
commit aeaddb1eaa
3 changed files with 28 additions and 19 deletions
+3 -3
View File
@@ -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 { export {
useCallback, useCallback,
useContext, useContext,
@@ -11,7 +11,7 @@ export {
useReducer, useReducer,
useRef, useRef,
useState, useState,
} from "https://x.lcas.dev/preact@10.5.11/hooks.js"; } from "https://esm.sh/preact@10.5.14/hooks";
export type { export type {
CreateHandle, CreateHandle,
EffectCallback, EffectCallback,
@@ -20,4 +20,4 @@ export type {
Reducer, Reducer,
Ref, Ref,
StateUpdater, StateUpdater,
} from "https://x.lcas.dev/preact@10.5.11/hooks.js"; } from "https://esm.sh/preact@10.5.14/hooks";
+15 -11
View File
@@ -1,13 +1,17 @@
import { denoPlugin, esbuild } from "./deps.ts"; import { denoPlugin, esbuild, esbuildTypes } from "./deps.ts";
import { Page } from "./types.ts"; import { Page } from "./types.ts";
let esbuildInitalized: boolean | Promise<void> = false; let esbuildInitalized: boolean | Promise<void> = false;
async function ensureEsbuildInialized() { async function ensureEsbuildInialized() {
if (esbuildInitalized === false) { if (esbuildInitalized === false) {
esbuildInitalized = esbuild.initialize({ if (Deno.run === undefined) {
wasmURL: "https://unpkg.com/esbuild-wasm@0.11.19/esbuild.wasm", esbuildInitalized = esbuild.initialize({
worker: false, wasmURL: "https://unpkg.com/esbuild-wasm@0.11.19/esbuild.wasm",
}); worker: false,
});
} else {
esbuild.initialize({});
}
await esbuildInitalized; await esbuildInitalized;
esbuildInitalized = true; esbuildInitalized = true;
} else if (esbuildInitalized instanceof Promise) { } else if (esbuildInitalized instanceof Promise) {
@@ -43,7 +47,7 @@ export class Bundler {
platform: "neutral", platform: "neutral",
plugins: [freshPlugin(this.#pages), denoPlugin()], plugins: [freshPlugin(this.#pages), denoPlugin()],
splitting: true, splitting: true,
target: ["chrome89", "firefox88", "safari13"], target: ["chrome90", "firefox88", "safari13"],
treeShaking: true, treeShaking: true,
write: false, 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 runtime = new URL("../../runtime.ts", import.meta.url);
const pageMap = new Map<string, Page>(); const pageMap = new Map<string, Page>();
@@ -100,8 +104,8 @@ function freshPlugin(pageList: Page[]): esbuild.Plugin {
name: "fresh", name: "fresh",
setup(build) { setup(build) {
build.onResolve({ filter: /fresh:\/\/.*/ }, function onResolve( build.onResolve({ filter: /fresh:\/\/.*/ }, function onResolve(
args: esbuild.OnResolveArgs, args: esbuildTypes.OnResolveArgs,
): esbuild.OnResolveResult | null | undefined { ): esbuildTypes.OnResolveResult | null | undefined {
return ({ return ({
path: args.path, path: args.path,
namespace: "fresh", namespace: "fresh",
@@ -111,8 +115,8 @@ function freshPlugin(pageList: Page[]): esbuild.Plugin {
build.onLoad( build.onLoad(
{ filter: /.*/, namespace: "fresh" }, { filter: /.*/, namespace: "fresh" },
function onLoad( function onLoad(
args: esbuild.OnLoadArgs, args: esbuildTypes.OnLoadArgs,
): esbuild.OnLoadResult | null | undefined { ): esbuildTypes.OnLoadResult | null | undefined {
const url = new URL(args.path); const url = new URL(args.path);
const path = url.pathname; const path = url.pathname;
const page = pageMap.get(path); const page = pageMap.get(path);
+10 -5
View File
@@ -1,17 +1,22 @@
// -- preact -- // -- 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 -- // -- 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 -- // -- router --
export * as router from "https://crux.land/router@0.0.4"; export * as router from "https://crux.land/router@0.0.4";
// -- media types -- // -- 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 -- // -- esbuild --
// @deno-types="https://unpkg.com/esbuild-wasm@0.11.19/esm/browser.d.ts" // @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"; import * as esbuildWasm from "https://gist.githubusercontent.com/lucacasonato/358c6b7e8198bfb2cf3d220e49fdcf5f/raw/3714cb0f59606eefc29ed0fea36d4cd93549938b/esbuild-wasm.js";
export { esbuild }; 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"; export { denoPlugin } from "https://deno.land/x/esbuild_deno_loader@0.3.0/mod.ts";