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 {
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";
+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";
let esbuildInitalized: boolean | Promise<void> = 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<string, Page>();
@@ -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);
+10 -5
View File
@@ -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";