From 48bfa365172bf883660368e55b6e905b8e739996 Mon Sep 17 00:00:00 2001 From: X Date: Sat, 9 Dec 2023 15:45:07 +0800 Subject: [PATCH] Add esm-cli package --- packages/esm-cli/README.md | 57 ++++ packages/{esm-hot => esm-cli}/bin/cli.mjs | 28 +- packages/esm-cli/package.json | 35 ++ packages/{esm-hot => esm-cli}/src/fs.mjs | 0 packages/{esm-hot => esm-cli}/src/index.mjs | 52 +-- packages/{esm-hot => esm-cli}/src/mime.mjs | 0 .../templates/react/App.jsx} | 0 .../templates/react}/index.html | 5 +- packages/esm-cli/templates/react/sw.js | 3 + packages/esm-cli/templates/svelte/App.svelte | 11 + packages/esm-cli/templates/svelte/index.html | 25 ++ packages/esm-cli/templates/svelte/sw.js | 3 + packages/esm-cli/templates/vue/App.vue | 16 + packages/esm-cli/templates/vue/index.html | 25 ++ packages/esm-cli/templates/vue/sw.js | 3 + packages/esm-cli/types/index.d.ts | 22 ++ packages/esm-cli/vendor/hono-server@1.3.1.mjs | 309 ++++++++++++++++++ packages/esm-hot/README.md | 1 - packages/esm-hot/package.json | 28 -- packages/esm-hot/types/index.d.ts | 20 -- packages/esm.sh/README.md | 7 - packages/esm.sh/build.mjs | 23 -- packages/esm.sh/package.json | 33 -- packages/esm.sh/pnpm-lock.yaml | 249 -------------- packages/esm.sh/src/index.ts | 1 - packages/esm.sh/src/worker.ts | 1 - packages/esm.sh/tsconfig.json | 9 - packages/esm.sh/types/index.d.ts | 1 - packages/esm.sh/types/worker.d.ts | 1 - server/embed/CLI.deno.ts | 22 +- 30 files changed, 552 insertions(+), 438 deletions(-) create mode 100644 packages/esm-cli/README.md rename packages/{esm-hot => esm-cli}/bin/cli.mjs (61%) create mode 100644 packages/esm-cli/package.json rename packages/{esm-hot => esm-cli}/src/fs.mjs (100%) rename packages/{esm-hot => esm-cli}/src/index.mjs (69%) rename packages/{esm-hot => esm-cli}/src/mime.mjs (100%) rename packages/{esm-hot/test/App.tsx => esm-cli/templates/react/App.jsx} (100%) rename packages/{esm-hot/test => esm-cli/templates/react}/index.html (72%) create mode 100644 packages/esm-cli/templates/react/sw.js create mode 100644 packages/esm-cli/templates/svelte/App.svelte create mode 100644 packages/esm-cli/templates/svelte/index.html create mode 100644 packages/esm-cli/templates/svelte/sw.js create mode 100644 packages/esm-cli/templates/vue/App.vue create mode 100644 packages/esm-cli/templates/vue/index.html create mode 100644 packages/esm-cli/templates/vue/sw.js create mode 100644 packages/esm-cli/types/index.d.ts create mode 100644 packages/esm-cli/vendor/hono-server@1.3.1.mjs delete mode 100644 packages/esm-hot/README.md delete mode 100644 packages/esm-hot/package.json delete mode 100644 packages/esm-hot/types/index.d.ts delete mode 100644 packages/esm.sh/README.md delete mode 100644 packages/esm.sh/build.mjs delete mode 100644 packages/esm.sh/package.json delete mode 100644 packages/esm.sh/pnpm-lock.yaml delete mode 100644 packages/esm.sh/src/index.ts delete mode 100644 packages/esm.sh/src/worker.ts delete mode 100644 packages/esm.sh/tsconfig.json delete mode 100644 packages/esm.sh/types/index.d.ts delete mode 100644 packages/esm.sh/types/worker.d.ts diff --git a/packages/esm-cli/README.md b/packages/esm-cli/README.md new file mode 100644 index 000000000..3207e7479 --- /dev/null +++ b/packages/esm-cli/README.md @@ -0,0 +1,57 @@ +# esm.sh + +The [esm.sh](https://esm.sh) CLI/API for serving hot applications. + +## Using the CLI tool + +The CLI tool is used to run a hot application in current directory. + +```bash +npx esm.sh -w +``` + +> The `-w` option is for watching the file changes to enable HMR. + +## Using the API + +The esm.sh API uses standard web APIs to serve hot applications. + +```ts +export interface ServeOptions { + /** The root path, default to current working directory. */ + root?: string; + /** The fallback route, default is `index.html`. */ + fallback?: string; + /** Wtaching file changes for HMR, default is `false` */ + watch?: boolean; +} + +export function serveHost( + options?: ServeOptions, +): (req: Request) => Promise; +``` + +For Node.js runtime, you need `@hono/server` to listen to the requests. + +```js +import { serve } from "@hono/server"; +import { serveHot } from "esm.sh"; + +serve({ port: 3000, fetch: serveHot() }); +``` + +For Deno runtime, you can use `serveHot` directly. + +```js +import { serveHot } from "https://esm.sh"; + +Deno.server(serveHot()); +``` + +For Bun runtime: + +```js +import { serveHot } from "esm.sh"; + +Bun.serve({ port: 3000, fetch: serveHot() }); +``` diff --git a/packages/esm-hot/bin/cli.mjs b/packages/esm-cli/bin/cli.mjs similarity index 61% rename from packages/esm-hot/bin/cli.mjs rename to packages/esm-cli/bin/cli.mjs index 3b367ca1f..908c33a18 100755 --- a/packages/esm-hot/bin/cli.mjs +++ b/packages/esm-cli/bin/cli.mjs @@ -1,20 +1,18 @@ #!/usr/bin/env node -import { serve } from "@hono/node-server"; +import { existsSync } from "node:fs"; +import { serve } from "../vendor/hono-server@1.3.1.mjs"; import { serveHot } from "../src/index.mjs"; if (process.argv.includes("--help") || process.argv.includes("-h")) { console.log(` -Usage: npx @esm.sh/hot [options] +Usage: npx esm.sh [options] [root] Options: - --cwd Current working directory (default: ".") - --help, -h Show help + --help, -h Show help message --host Host to listen on (default: "localhost") - --plugins Plugins for service worker (default: []) --port, -p Port number to listen on (default: 3000) - --spa Enable SPA mode - --watch Watch file changes for HMR + --watch, -w Watch file changes for HMR `); process.exit(0); } @@ -24,6 +22,12 @@ const args = { }; process.argv.slice(2).forEach((arg) => { + if (!arg.startsWith("-")) { + if (existsSync(arg)) { + args.root = arg; + } + return; + } const [key, value] = arg.split("="); if ((key === "--port" || key === "-p") && value) { args.port = parseInt(value); @@ -32,18 +36,8 @@ process.argv.slice(2).forEach((arg) => { } } else if (key === "--host" && value) { args.host = value; - } else if (key === "--spa") { - if (value) { - args.spa = { index: value }; - } else { - args.spa = true; - } } else if (key === "--watch" || key === "-w") { args.watch = true; - } else if (key === "--cwd" && value) { - args.cwd = value; - } else if (key === "--plugins" && value) { - args.plugins = value.split(","); } }); diff --git a/packages/esm-cli/package.json b/packages/esm-cli/package.json new file mode 100644 index 000000000..b29cceea4 --- /dev/null +++ b/packages/esm-cli/package.json @@ -0,0 +1,35 @@ +{ + "name": "esm.sh", + "version": "0.135.0", + "description": "The CLI tool for esm.sh hot applications.", + "type": "module", + "main": "src/index.mjs", + "module": "src/index.mjs", + "types": "types/index.d.ts", + "bin": "./bin/cli.mjs", + "exports": { + ".": { + "import": "./src/index.mjs", + "types": "./types/index.d.ts" + }, + "./package.json": "./package.json" + }, + "scripts": { + "test": "./bin/cli.mjs -w test" + }, + "files": [ + "bin/", + "src/", + "templates/", + "types/", + "vendor/" + ], + "engines": { + "node": ">=18.14.1" + }, + "repository": { + "type": "git", + "url": "https//github.com/esm-dev/esm.sh" + }, + "license": "MIT" +} diff --git a/packages/esm-hot/src/fs.mjs b/packages/esm-cli/src/fs.mjs similarity index 100% rename from packages/esm-hot/src/fs.mjs rename to packages/esm-cli/src/fs.mjs diff --git a/packages/esm-hot/src/index.mjs b/packages/esm-cli/src/index.mjs similarity index 69% rename from packages/esm-hot/src/index.mjs rename to packages/esm-cli/src/index.mjs index 2252ef88e..ca70b9227 100644 --- a/packages/esm-hot/src/index.mjs +++ b/packages/esm-cli/src/index.mjs @@ -1,38 +1,31 @@ import { openFile } from "./fs.mjs"; const enc = new TextEncoder(); -const regexpPluginNaming = /^[a-zA-Z0-9][\w\.\-]*(@\d+\.\d+\.\d+)?$/; /** - * serves a hot app. + * Creates a fetch handler for serving hot applications. * @param {import("../types").ServeOptions} options * @returns {(req: Request) => Promise} */ export const serveHot = (options) => { - if (options.plugins) { - options.plugins = options.plugins.filter((name) => - regexpPluginNaming.test(name) - ); - } - const { spa, watch, plugins, cwd = "." } = options; + const { root = ".", fallback = "index.html", watch } = options; const fsWatchHandlers = new Set(); if (watch) { import("node:fs").then(({ watch }) => { watch( - cwd, + root, { recursive: true }, (event, filename) => { - fsWatchHandlers.forEach((handler) => - handler(event === "change" ? "modify" : event, "/" + filename) - ); + if (!/(^|\/)(\.|node_modules\/)/.test(filename) && !filename.endsWith(".log")) { + fsWatchHandlers.forEach((handler) => + handler(event === "change" ? "modify" : event, "/" + filename) + ); + } }, ); console.log(`Watching files changed...`); }); } - if (plugins?.length) { - console.log(`Using plugins: ${plugins.join(", ")}`); - } return async (req) => { const url = new URL(req.url); const pathname = decodeURIComponent(url.pathname); @@ -62,12 +55,9 @@ export const serveHot = (options) => { }, ); } - let file = pathname.includes(".") ? await openFile(cwd + pathname) : null; + let file = pathname.includes(".") ? await openFile(root + pathname) : null; if (!file && pathname === "/sw.js") { const hotUrl = new URL("https://esm.sh/v135/hot"); - if (plugins?.length) { - hotUrl.searchParams.set("plugins", plugins); - } return new Response(`import hot from "${hotUrl.href}";hot.listen();`, { headers: { "content-type": "application/javascript; charset=utf-8", @@ -76,19 +66,15 @@ export const serveHot = (options) => { }); } if (!file) { - if (spa) { - const index = "index.html"; - if (typeof spa === "string" && spa.endsWith(".html")) { - index = spa; - } else if (spa.index && spa.index.endsWith(".html")) { - index = spa.index; - } - file = await openFile(cwd + "/" + index); - } else { - file = await openFile(cwd + pathname + ".html"); - if (!file) { - file = await openFile(cwd + pathname + "/index.html"); - } + const list = [ + pathname + ".html", + pathname + "/index.html", + "/404.html", + "/" + fallback, + ]; + for (const filename of list) { + file = await openFile(root + filename); + if (file) break; } } if (file) { @@ -123,5 +109,3 @@ export const serveHot = (options) => { return new Response("Not Found", { status: 404 }); }; }; - -export default serveHot; diff --git a/packages/esm-hot/src/mime.mjs b/packages/esm-cli/src/mime.mjs similarity index 100% rename from packages/esm-hot/src/mime.mjs rename to packages/esm-cli/src/mime.mjs diff --git a/packages/esm-hot/test/App.tsx b/packages/esm-cli/templates/react/App.jsx similarity index 100% rename from packages/esm-hot/test/App.tsx rename to packages/esm-cli/templates/react/App.jsx diff --git a/packages/esm-hot/test/index.html b/packages/esm-cli/templates/react/index.html similarity index 72% rename from packages/esm-hot/test/index.html rename to packages/esm-cli/templates/react/index.html index 78cebb83f..9a201a783 100644 --- a/packages/esm-hot/test/index.html +++ b/packages/esm-cli/templates/react/index.html @@ -8,14 +8,15 @@ - + + +

Hello {name}!

+ + diff --git a/packages/esm-cli/templates/svelte/index.html b/packages/esm-cli/templates/svelte/index.html new file mode 100644 index 000000000..23c179c64 --- /dev/null +++ b/packages/esm-cli/templates/svelte/index.html @@ -0,0 +1,25 @@ + + + + + + + 🔥 + + + + + + + + + diff --git a/packages/esm-cli/templates/svelte/sw.js b/packages/esm-cli/templates/svelte/sw.js new file mode 100644 index 000000000..897ce29c1 --- /dev/null +++ b/packages/esm-cli/templates/svelte/sw.js @@ -0,0 +1,3 @@ +import hot from "https://esm.sh/v135/hot?plugins=svelte" + +hot.listen() diff --git a/packages/esm-cli/templates/vue/App.vue b/packages/esm-cli/templates/vue/App.vue new file mode 100644 index 000000000..c0fa8f6b1 --- /dev/null +++ b/packages/esm-cli/templates/vue/App.vue @@ -0,0 +1,16 @@ + + + + + diff --git a/packages/esm-cli/templates/vue/index.html b/packages/esm-cli/templates/vue/index.html new file mode 100644 index 000000000..7c03e5951 --- /dev/null +++ b/packages/esm-cli/templates/vue/index.html @@ -0,0 +1,25 @@ + + + + + + + 🔥 + + + + + + + + + diff --git a/packages/esm-cli/templates/vue/sw.js b/packages/esm-cli/templates/vue/sw.js new file mode 100644 index 000000000..56ec6a4b3 --- /dev/null +++ b/packages/esm-cli/templates/vue/sw.js @@ -0,0 +1,3 @@ +import hot from "https://esm.sh/v135/hot?plugins=vue" + +hot.listen() diff --git a/packages/esm-cli/types/index.d.ts b/packages/esm-cli/types/index.d.ts new file mode 100644 index 000000000..2b10ab294 --- /dev/null +++ b/packages/esm-cli/types/index.d.ts @@ -0,0 +1,22 @@ +export interface FsFile { + size: number; + lastModified: number | null; + contentType: string; + body: ReadableStream; + close: () => Promise; +} + +/** The options for `serveHost` */ +export interface ServeOptions { + /** The root path, default to current working directory. */ + root?: string; + /** The fallback route, default is `index.html`. */ + fallback?: `${string}.html`; + /** Wtaching file changes for HMR, default is `false` */ + watch?: boolean; +} + +/** Creates a fetch handler for serving hot applications. */ +export function serveHost( + options?: ServeOptions, +): (req: Request) => Promise; diff --git a/packages/esm-cli/vendor/hono-server@1.3.1.mjs b/packages/esm-cli/vendor/hono-server@1.3.1.mjs new file mode 100644 index 000000000..0905a225f --- /dev/null +++ b/packages/esm-cli/vendor/hono-server@1.3.1.mjs @@ -0,0 +1,309 @@ +/** + * @hono/node-server v1.3.1 + * @author Yusuke Wada + * @license MIT + * + */ + +// src/server.ts +import { createServer as createServerHTTP } from "http"; + +// src/globals.ts +import crypto from "crypto"; + +// src/utils.ts +function writeFromReadableStream(stream, writable) { + if (stream.locked) { + throw new TypeError("ReadableStream is locked."); + } else if (writable.destroyed) { + stream.cancel(); + return; + } + const reader = stream.getReader(); + writable.on("close", cancel); + writable.on("error", cancel); + reader.read().then(flow, cancel); + return reader.closed.finally(() => { + writable.off("close", cancel); + writable.off("error", cancel); + }); + function cancel(error) { + reader.cancel(error).catch(() => { + }); + if (error) + writable.destroy(error); + } + function onDrain() { + reader.read().then(flow, cancel); + } + function flow({ done, value }) { + try { + if (done) { + writable.end(); + } else if (!writable.write(value)) { + writable.once("drain", onDrain); + } else { + return reader.read().then(flow, cancel); + } + } catch (e) { + cancel(e); + } + } +} +var buildOutgoingHttpHeaders = (headers) => { + const res = {}; + const cookies = []; + for (const [k, v] of headers) { + if (k === "set-cookie") { + cookies.push(v); + } else { + res[k] = v; + } + } + if (cookies.length > 0) { + res["set-cookie"] = cookies; + } + res["content-type"] ??= "text/plain;charset=UTF-8"; + return res; +}; + +// src/response.ts +var responseCache = Symbol("responseCache"); +var cacheKey = Symbol("cache"); +var globalResponse = global.Response; +var Response2 = class { + #body; + #init; + // @ts-ignore + get cache() { + delete this[cacheKey]; + return this[responseCache] ||= new globalResponse(this.#body, this.#init); + } + constructor(body, init) { + this.#body = body; + this.#init = init; + if (typeof body === "string" || body instanceof ReadableStream) { + let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" }; + if (headers instanceof Headers) { + headers = buildOutgoingHttpHeaders(headers); + } + this[cacheKey] = [init?.status || 200, body, headers]; + } + } +}; +[ + "body", + "bodyUsed", + "headers", + "ok", + "redirected", + "status", + "statusText", + "trailers", + "type", + "url" +].forEach((k) => { + Object.defineProperty(Response2.prototype, k, { + get() { + return this.cache[k]; + } + }); +}); +["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => { + Object.defineProperty(Response2.prototype, k, { + value: function() { + return this.cache[k](); + } + }); +}); +Object.setPrototypeOf(Response2, globalResponse); +Object.setPrototypeOf(Response2.prototype, globalResponse.prototype); +Object.defineProperty(global, "Response", { + value: Response2 +}); + +// src/globals.ts +Object.defineProperty(global, "Response", { + value: Response2 +}); +var webFetch = global.fetch; +if (typeof global.crypto === "undefined") { + global.crypto = crypto; +} +global.fetch = (info, init) => { + init = { + // Disable compression handling so people can return the result of a fetch + // directly in the loader without messing with the Content-Encoding header. + compress: false, + ...init + }; + return webFetch(info, init); +}; + +// src/request.ts +import { Readable } from "stream"; +var newRequestFromIncoming = (method, url, incoming) => { + const headerRecord = []; + const len = incoming.rawHeaders.length; + for (let i = 0; i < len; i += 2) { + headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]); + } + const init = { + method, + headers: headerRecord + }; + if (!(method === "GET" || method === "HEAD")) { + init.body = Readable.toWeb(incoming); + init.duplex = "half"; + } + return new Request(url, init); +}; +var getRequestCache = Symbol("getRequestCache"); +var requestCache = Symbol("requestCache"); +var incomingKey = Symbol("incomingKey"); +var requestPrototype = { + get method() { + return this[incomingKey].method || "GET"; + }, + get url() { + return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`; + }, + [getRequestCache]() { + return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]); + } +}; +[ + "body", + "bodyUsed", + "cache", + "credentials", + "destination", + "headers", + "integrity", + "mode", + "redirect", + "referrer", + "referrerPolicy", + "signal" +].forEach((k) => { + Object.defineProperty(requestPrototype, k, { + get() { + return this[getRequestCache]()[k]; + } + }); +}); +["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => { + Object.defineProperty(requestPrototype, k, { + value: function() { + return this[getRequestCache]()[k](); + } + }); +}); +Object.setPrototypeOf(requestPrototype, global.Request.prototype); +var newRequest = (incoming) => { + const req = Object.create(requestPrototype); + req[incomingKey] = incoming; + return req; +}; + +// src/listener.ts +var regBuffer = /^no$/i; +var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i; +var handleFetchError = (e) => new Response(null, { + status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500 +}); +var handleResponseError = (e, outgoing) => { + const err = e instanceof Error ? e : new Error("unknown error", { cause: e }); + if (err.code === "ERR_STREAM_PREMATURE_CLOSE") { + console.info("The user aborted a request."); + } else { + console.error(e); + outgoing.destroy(err); + } +}; +var responseViaCache = (res, outgoing) => { + const [status, body, header] = res[cacheKey]; + if (typeof body === "string") { + header["Content-Length"] = Buffer.byteLength(body); + outgoing.writeHead(status, header); + outgoing.end(body); + } else { + outgoing.writeHead(status, header); + return writeFromReadableStream(body, outgoing)?.catch( + (e) => handleResponseError(e, outgoing) + ); + } +}; +var responseViaResponseObject = async (res, outgoing) => { + if (res instanceof Promise) { + res = await res.catch(handleFetchError); + } + if (cacheKey in res) { + try { + return responseViaCache(res, outgoing); + } catch (e) { + return handleResponseError(e, outgoing); + } + } + const resHeaderRecord = buildOutgoingHttpHeaders(res.headers); + if (res.body) { + try { + if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant + resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) { + outgoing.writeHead(res.status, resHeaderRecord); + await writeFromReadableStream(res.body, outgoing); + } else { + const buffer = await res.arrayBuffer(); + resHeaderRecord["content-length"] = buffer.byteLength; + outgoing.writeHead(res.status, resHeaderRecord); + outgoing.end(new Uint8Array(buffer)); + } + } catch (e) { + handleResponseError(e, outgoing); + } + } else { + outgoing.writeHead(res.status, resHeaderRecord); + outgoing.end(); + } +}; +var getRequestListener = (fetchCallback) => { + return (incoming, outgoing) => { + let res; + const req = newRequest(incoming); + try { + res = fetchCallback(req); + if (cacheKey in res) { + return responseViaCache(res, outgoing); + } + } catch (e) { + if (!res) { + res = handleFetchError(e); + } else { + return handleResponseError(e, outgoing); + } + } + return responseViaResponseObject(res, outgoing); + }; +}; + +// src/server.ts +var createAdaptorServer = (options) => { + const fetchCallback = options.fetch; + const requestListener = getRequestListener(fetchCallback); + const createServer = options.createServer || createServerHTTP; + const server = createServer(options.serverOptions || {}, requestListener); + return server; +}; +var serve = (options, listeningListener) => { + const server = createAdaptorServer(options); + server.listen(options?.port ?? 3e3, options.hostname ?? "0.0.0.0", () => { + const serverInfo = server.address(); + listeningListener && listeningListener(serverInfo); + }); + return server; +}; +export { + createAdaptorServer, + getRequestListener, + serve +}; diff --git a/packages/esm-hot/README.md b/packages/esm-hot/README.md deleted file mode 100644 index 5f8db78f0..000000000 --- a/packages/esm-hot/README.md +++ /dev/null @@ -1 +0,0 @@ -# 🔥 esm.sh/hot diff --git a/packages/esm-hot/package.json b/packages/esm-hot/package.json deleted file mode 100644 index 2e74833a3..000000000 --- a/packages/esm-hot/package.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "name": "@esm.sh/hot", - "version": "0.135.0", - "description": "🔥 esm.sh/hot", - "type": "module", - "main": "src/index.mjs", - "module": "src/index.mjs", - "types": "types/index.d.ts", - "bin": { - "hot": "./bin/cli.mjs" - }, - "scripts": { - "test": "./bin/cli.mjs -w --spa --plugins=tsx --cwd=test" - }, - "dependencies": { - "@hono/node-server": "^1.3.1" - }, - "files": [ - "bin/", - "src/", - "types/" - ], - "repository": { - "type": "git", - "url": "https//github.com/esm-dev/esm.sh" - }, - "license": "MIT" -} diff --git a/packages/esm-hot/types/index.d.ts b/packages/esm-hot/types/index.d.ts deleted file mode 100644 index 8659eb639..000000000 --- a/packages/esm-hot/types/index.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -export interface FsFile { - size: number; - lastModified: number | null; - contentType: string; - body: ReadableStream; - close: () => Promise; -} - -export interface ServeOptions { - cwd?: string; - spa?: boolean | string | { index: string }; - plugins?: string[]; - watch?: boolean; -} - -export function serveHost( - options?: ServeOptions, -): (req: Request) => Promise; - -export default serveHost; diff --git a/packages/esm.sh/README.md b/packages/esm.sh/README.md deleted file mode 100644 index e9d05f75d..000000000 --- a/packages/esm.sh/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# esm.sh - -A Npm package for esm.sh service. - -- Loader for Node.js to load ES6 modules from esm.sh (WIP) -- Run esm.sh server locally (WIP) -- Reexport [esm-worker](../esm-worker/README.md) to `esm.sh/worker` diff --git a/packages/esm.sh/build.mjs b/packages/esm.sh/build.mjs deleted file mode 100644 index 806cdacf6..000000000 --- a/packages/esm.sh/build.mjs +++ /dev/null @@ -1,23 +0,0 @@ -import { build as esbuild } from "esbuild"; - -const build = (options) => { - return esbuild({ - target: "esnext", - format: "esm", - platform: "browser", - outdir: "dist", - bundle: true, - external: ["esm-worker"], - minify: true, - logLevel: "info", - outExtension: { ".js": ".mjs" }, - ...options, - }); -}; - -await build({ - entryPoints: [ - "src/index.ts", - "src/worker.ts", - ], -}); diff --git a/packages/esm.sh/package.json b/packages/esm.sh/package.json deleted file mode 100644 index 59fd775db..000000000 --- a/packages/esm.sh/package.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "name": "esm.sh", - "version": "0.0.6", - "description": "A fast, smart, & global content delivery network (CDN) for modern(es2015+) web development.", - "repository": "https://github.com/esm-dev/esm.sh", - "license": "MIT", - "type": "module", - "module": "dist/index.mjs", - "types": "types/index.d.ts", - "scripts": { - "prepublishOnly": "node build.mjs" - }, - "files": [ - "dist/", - "types/" - ], - "exports": { - ".": { - "types": "./types/index.d.ts", - "import": "./dist/index.mjs" - }, - "./worker": { - "types": "./types/worker.d.ts", - "import": "./dist/worker.mjs" - } - }, - "dependencies": { - "esm-worker": "0.135.0" - }, - "devDependencies": { - "esbuild": "0.19.7" - } -} diff --git a/packages/esm.sh/pnpm-lock.yaml b/packages/esm.sh/pnpm-lock.yaml deleted file mode 100644 index 0b7452589..000000000 --- a/packages/esm.sh/pnpm-lock.yaml +++ /dev/null @@ -1,249 +0,0 @@ -lockfileVersion: '6.0' - -settings: - autoInstallPeers: true - excludeLinksFromLockfile: false - -dependencies: - esm-worker: - specifier: 0.135.0 - version: 0.135.0 - -devDependencies: - esbuild: - specifier: 0.19.7 - version: 0.19.7 - -packages: - - /@esbuild/android-arm64@0.19.7: - resolution: {integrity: sha512-YEDcw5IT7hW3sFKZBkCAQaOCJQLONVcD4bOyTXMZz5fr66pTHnAet46XAtbXAkJRfIn2YVhdC6R9g4xa27jQ1w==} - engines: {node: '>=12'} - cpu: [arm64] - os: [android] - requiresBuild: true - dev: true - optional: true - - /@esbuild/android-arm@0.19.7: - resolution: {integrity: sha512-YGSPnndkcLo4PmVl2tKatEn+0mlVMr3yEpOOT0BeMria87PhvoJb5dg5f5Ft9fbCVgtAz4pWMzZVgSEGpDAlww==} - engines: {node: '>=12'} - cpu: [arm] - os: [android] - requiresBuild: true - dev: true - optional: true - - /@esbuild/android-x64@0.19.7: - resolution: {integrity: sha512-jhINx8DEjz68cChFvM72YzrqfwJuFbfvSxZAk4bebpngGfNNRm+zRl4rtT9oAX6N9b6gBcFaJHFew5Blf6CvUw==} - engines: {node: '>=12'} - cpu: [x64] - os: [android] - requiresBuild: true - dev: true - optional: true - - /@esbuild/darwin-arm64@0.19.7: - resolution: {integrity: sha512-dr81gbmWN//3ZnBIm6YNCl4p3pjnabg1/ZVOgz2fJoUO1a3mq9WQ/1iuEluMs7mCL+Zwv7AY5e3g1hjXqQZ9Iw==} - engines: {node: '>=12'} - cpu: [arm64] - os: [darwin] - requiresBuild: true - dev: true - optional: true - - /@esbuild/darwin-x64@0.19.7: - resolution: {integrity: sha512-Lc0q5HouGlzQEwLkgEKnWcSazqr9l9OdV2HhVasWJzLKeOt0PLhHaUHuzb8s/UIya38DJDoUm74GToZ6Wc7NGQ==} - engines: {node: '>=12'} - cpu: [x64] - os: [darwin] - requiresBuild: true - dev: true - optional: true - - /@esbuild/freebsd-arm64@0.19.7: - resolution: {integrity: sha512-+y2YsUr0CxDFF7GWiegWjGtTUF6gac2zFasfFkRJPkMAuMy9O7+2EH550VlqVdpEEchWMynkdhC9ZjtnMiHImQ==} - engines: {node: '>=12'} - cpu: [arm64] - os: [freebsd] - requiresBuild: true - dev: true - optional: true - - /@esbuild/freebsd-x64@0.19.7: - resolution: {integrity: sha512-CdXOxIbIzPJmJhrpmJTLx+o35NoiKBIgOvmvT+jeSadYiWJn0vFKsl+0bSG/5lwjNHoIDEyMYc/GAPR9jxusTA==} - engines: {node: '>=12'} - cpu: [x64] - os: [freebsd] - requiresBuild: true - dev: true - optional: true - - /@esbuild/linux-arm64@0.19.7: - resolution: {integrity: sha512-inHqdOVCkUhHNvuQPT1oCB7cWz9qQ/Cz46xmVe0b7UXcuIJU3166aqSunsqkgSGMtUCWOZw3+KMwI6otINuC9g==} - engines: {node: '>=12'} - cpu: [arm64] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /@esbuild/linux-arm@0.19.7: - resolution: {integrity: sha512-Y+SCmWxsJOdQtjcBxoacn/pGW9HDZpwsoof0ttL+2vGcHokFlfqV666JpfLCSP2xLxFpF1lj7T3Ox3sr95YXww==} - engines: {node: '>=12'} - cpu: [arm] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /@esbuild/linux-ia32@0.19.7: - resolution: {integrity: sha512-2BbiL7nLS5ZO96bxTQkdO0euGZIUQEUXMTrqLxKUmk/Y5pmrWU84f+CMJpM8+EHaBPfFSPnomEaQiG/+Gmh61g==} - engines: {node: '>=12'} - cpu: [ia32] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /@esbuild/linux-loong64@0.19.7: - resolution: {integrity: sha512-BVFQla72KXv3yyTFCQXF7MORvpTo4uTA8FVFgmwVrqbB/4DsBFWilUm1i2Oq6zN36DOZKSVUTb16jbjedhfSHw==} - engines: {node: '>=12'} - cpu: [loong64] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /@esbuild/linux-mips64el@0.19.7: - resolution: {integrity: sha512-DzAYckIaK+pS31Q/rGpvUKu7M+5/t+jI+cdleDgUwbU7KdG2eC3SUbZHlo6Q4P1CfVKZ1lUERRFP8+q0ob9i2w==} - engines: {node: '>=12'} - cpu: [mips64el] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /@esbuild/linux-ppc64@0.19.7: - resolution: {integrity: sha512-JQ1p0SmUteNdUaaiRtyS59GkkfTW0Edo+e0O2sihnY4FoZLz5glpWUQEKMSzMhA430ctkylkS7+vn8ziuhUugQ==} - engines: {node: '>=12'} - cpu: [ppc64] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /@esbuild/linux-riscv64@0.19.7: - resolution: {integrity: sha512-xGwVJ7eGhkprY/nB7L7MXysHduqjpzUl40+XoYDGC4UPLbnG+gsyS1wQPJ9lFPcxYAaDXbdRXd1ACs9AE9lxuw==} - engines: {node: '>=12'} - cpu: [riscv64] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /@esbuild/linux-s390x@0.19.7: - resolution: {integrity: sha512-U8Rhki5PVU0L0nvk+E8FjkV8r4Lh4hVEb9duR6Zl21eIEYEwXz8RScj4LZWA2i3V70V4UHVgiqMpszXvG0Yqhg==} - engines: {node: '>=12'} - cpu: [s390x] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /@esbuild/linux-x64@0.19.7: - resolution: {integrity: sha512-ZYZopyLhm4mcoZXjFt25itRlocKlcazDVkB4AhioiL9hOWhDldU9n38g62fhOI4Pth6vp+Mrd5rFKxD0/S+7aQ==} - engines: {node: '>=12'} - cpu: [x64] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /@esbuild/netbsd-x64@0.19.7: - resolution: {integrity: sha512-/yfjlsYmT1O3cum3J6cmGG16Fd5tqKMcg5D+sBYLaOQExheAJhqr8xOAEIuLo8JYkevmjM5zFD9rVs3VBcsjtQ==} - engines: {node: '>=12'} - cpu: [x64] - os: [netbsd] - requiresBuild: true - dev: true - optional: true - - /@esbuild/openbsd-x64@0.19.7: - resolution: {integrity: sha512-MYDFyV0EW1cTP46IgUJ38OnEY5TaXxjoDmwiTXPjezahQgZd+j3T55Ht8/Q9YXBM0+T9HJygrSRGV5QNF/YVDQ==} - engines: {node: '>=12'} - cpu: [x64] - os: [openbsd] - requiresBuild: true - dev: true - optional: true - - /@esbuild/sunos-x64@0.19.7: - resolution: {integrity: sha512-JcPvgzf2NN/y6X3UUSqP6jSS06V0DZAV/8q0PjsZyGSXsIGcG110XsdmuWiHM+pno7/mJF6fjH5/vhUz/vA9fw==} - engines: {node: '>=12'} - cpu: [x64] - os: [sunos] - requiresBuild: true - dev: true - optional: true - - /@esbuild/win32-arm64@0.19.7: - resolution: {integrity: sha512-ZA0KSYti5w5toax5FpmfcAgu3ZNJxYSRm0AW/Dao5up0YV1hDVof1NvwLomjEN+3/GMtaWDI+CIyJOMTRSTdMw==} - engines: {node: '>=12'} - cpu: [arm64] - os: [win32] - requiresBuild: true - dev: true - optional: true - - /@esbuild/win32-ia32@0.19.7: - resolution: {integrity: sha512-CTOnijBKc5Jpk6/W9hQMMvJnsSYRYgveN6O75DTACCY18RA2nqka8dTZR+x/JqXCRiKk84+5+bRKXUSbbwsS0A==} - engines: {node: '>=12'} - cpu: [ia32] - os: [win32] - requiresBuild: true - dev: true - optional: true - - /@esbuild/win32-x64@0.19.7: - resolution: {integrity: sha512-gRaP2sk6hc98N734luX4VpF318l3w+ofrtTu9j5L8EQXF+FzQKV6alCOHMVoJJHvVK/mGbwBXfOL1HETQu9IGQ==} - engines: {node: '>=12'} - cpu: [x64] - os: [win32] - requiresBuild: true - dev: true - optional: true - - /esbuild@0.19.7: - resolution: {integrity: sha512-6brbTZVqxhqgbpqBR5MzErImcpA0SQdoKOkcWK/U30HtQxnokIpG3TX2r0IJqbFUzqLjhU/zC1S5ndgakObVCQ==} - engines: {node: '>=12'} - hasBin: true - requiresBuild: true - optionalDependencies: - '@esbuild/android-arm': 0.19.7 - '@esbuild/android-arm64': 0.19.7 - '@esbuild/android-x64': 0.19.7 - '@esbuild/darwin-arm64': 0.19.7 - '@esbuild/darwin-x64': 0.19.7 - '@esbuild/freebsd-arm64': 0.19.7 - '@esbuild/freebsd-x64': 0.19.7 - '@esbuild/linux-arm': 0.19.7 - '@esbuild/linux-arm64': 0.19.7 - '@esbuild/linux-ia32': 0.19.7 - '@esbuild/linux-loong64': 0.19.7 - '@esbuild/linux-mips64el': 0.19.7 - '@esbuild/linux-ppc64': 0.19.7 - '@esbuild/linux-riscv64': 0.19.7 - '@esbuild/linux-s390x': 0.19.7 - '@esbuild/linux-x64': 0.19.7 - '@esbuild/netbsd-x64': 0.19.7 - '@esbuild/openbsd-x64': 0.19.7 - '@esbuild/sunos-x64': 0.19.7 - '@esbuild/win32-arm64': 0.19.7 - '@esbuild/win32-ia32': 0.19.7 - '@esbuild/win32-x64': 0.19.7 - dev: true - - /esm-worker@0.135.0: - resolution: {integrity: sha512-V5IkDLPo/A7Rebb7L9z7Vp6ygCaFMLSNOIFZiS0XvRQjVD7OuSJpUWNAO3n2PM7v98UNopqHGpQtg4SNc5M+mg==} - dev: false diff --git a/packages/esm.sh/src/index.ts b/packages/esm.sh/src/index.ts deleted file mode 100644 index ab622ddec..000000000 --- a/packages/esm.sh/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { version } from "../package.json"; diff --git a/packages/esm.sh/src/worker.ts b/packages/esm.sh/src/worker.ts deleted file mode 100644 index 9f40f14d1..000000000 --- a/packages/esm.sh/src/worker.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "esm-worker" diff --git a/packages/esm.sh/tsconfig.json b/packages/esm.sh/tsconfig.json deleted file mode 100644 index 29be05a2b..000000000 --- a/packages/esm.sh/tsconfig.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "compilerOptions": { - "target": "ESNext", - "module": "ESNext", - "moduleResolution": "bundler", - "allowImportingTsExtensions": true - }, - "include": [ "src/**/*.ts" ] -} diff --git a/packages/esm.sh/types/index.d.ts b/packages/esm.sh/types/index.d.ts deleted file mode 100644 index 9a468d947..000000000 --- a/packages/esm.sh/types/index.d.ts +++ /dev/null @@ -1 +0,0 @@ -export const version: string; diff --git a/packages/esm.sh/types/worker.d.ts b/packages/esm.sh/types/worker.d.ts deleted file mode 100644 index 4477467f4..000000000 --- a/packages/esm.sh/types/worker.d.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "esm-worker"; diff --git a/server/embed/CLI.deno.ts b/server/embed/CLI.deno.ts index 873a83a14..24ddc4dcf 100644 --- a/server/embed/CLI.deno.ts +++ b/server/embed/CLI.deno.ts @@ -182,15 +182,14 @@ async function init(_args: string[], _options: Record) { if (imFilename === "deno.json") { await saveImportMap({ ...config, - ...importMap + ...importMap, }); - } - else { + } else { await Deno.writeTextFile( "deno.json", await denoFmt( - JSON.stringify(config, null, 4) - ) + JSON.stringify(config, null, 4), + ), ); await saveImportMap(importMap); } @@ -279,7 +278,7 @@ async function denoFmt(code: string, ext = "json") { stdout: "piped", stderr: "null", }).spawn(); - + const raw = new ReadableStream({ start(controller) { controller.enqueue(new TextEncoder().encode(code)); @@ -288,7 +287,7 @@ async function denoFmt(code: string, ext = "json") { }); await raw.pipeTo(proc.stdin); const { stdout } = await proc.output(); - + const formattedStr = new TextDecoder().decode(stdout); return formattedStr; } @@ -346,8 +345,8 @@ async function saveImportMap(importMap: ImportMap): Promise { JSON.stringify( { ...importMap, imports: sortedImports, scopes: sortedScopes }, null, - 4 - ) + 4, + ), ), ); } @@ -507,8 +506,7 @@ if (import.meta.main) { const config = await getDenoConfig(); if (isNEString(config.importMap)) { imFilename = config.importMap; - } - else { + } else { imFilename = "deno.json"; } await commands[command as keyof typeof commands](...parseFlags(args)); @@ -517,3 +515,5 @@ if (import.meta.main) { throw error; } } + +export * from "https://esm.sh/v135/esm.sh@0.135.0";