Skip to main content

Built and signed on GitHub Actions

TypeScript + ES Modules Transpile TypeScript on the fly and serve it from your server as ES Modules.

This package works with DenoIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
JSR Score
100%
Published
3 hours ago (1.5.3)

ts-serve

Test codecov

TypeScript + ES Modules

Transpile TypeScript on the fly and serve it from your server as ES Modules.

import { serveDirWithTs } from "@ayame113/ts-serve";

Deno.serve((request) => serveDirWithTs(request));
// index.html
<script src="./main.ts" type="module"></script>;

// main.ts
console.log(1);
  • Supports ts, tsx and jsx transpiling.
  • The URL remains *.ts and will not be rewritten. That is, import "./foo.ts" and <script src="./foo.ts" type="module"> work on browser.
  • You can use import "./foo.ts", which has the same syntax as Deno. This means that you can use the completion and diagnostic features for frontend code by installing the Deno and Deno extensions in your editor.

Usage

As oak middleware:

import { Application } from "@oak/oak";
import { tsMiddleware } from "@ayame113/ts-serve";

const app = new Application();

// use middleware and transpile TS code
app.use(tsMiddleware);

// serve static file
app.use(async (ctx, next) => {
  try {
    await ctx.send({ root: "./" });
  } catch {
    await next();
  }
});
await app.listen({ port: 8000 });

As a replacement for the serveDir function in the Deno standard library:

import { serveDirWithTs } from "@ayame113/ts-serve";

Deno.serve((request) => serveDirWithTs(request));

As a replacement for the serveFile function in the Deno standard library:

import { serveFileWithTs } from "@ayame113/ts-serve";

Deno.serve((request) => serveFileWithTs(request, "./mod.ts"));

As Hono's handler:

import { Hono } from "@hono/hono";
import { serveDirWithTs } from "@ayame113/ts-serve";

const app = new Hono();
app.get("*", (c) => {
  return serveDirWithTs(c.req.raw);
});
Deno.serve(app.fetch);

forceInstantiateWasm function

Calling this function will load the wasm file used in the deno_emit of the dependency. Even if you don't call this function, if you call the transpile function, the wasm file will be read automatically at that timing.

However, performance can be an issue on the server as loading the wasm file takes time. In that case, calling this function in advance can speed up later calls to the transpile function.

import { forceInstantiateWasm, serveDirWithTs } from "@ayame113/ts-serve";

// load the wasm file in the background when the server starts.
forceInstantiateWasm();
Deno.serve((request) => serveDirWithTs(request));

develop

> deno task test
Built and signed on
GitHub Actions
View transparency log

Add Package

deno add jsr:@ayame113/ts-serve

Import symbol

import * as ts_serve from "@ayame113/ts-serve";

---- OR ----

Import directly with a jsr specifier

import * as ts_serve from "jsr:@ayame113/ts-serve";

Add Package

npx jsr add @ayame113/ts-serve

Import symbol

import * as ts_serve from "@ayame113/ts-serve";

Add Package

yarn dlx jsr add @ayame113/ts-serve

Import symbol

import * as ts_serve from "@ayame113/ts-serve";

Add Package

pnpm dlx jsr add @ayame113/ts-serve

Import symbol

import * as ts_serve from "@ayame113/ts-serve";

Add Package

bunx jsr add @ayame113/ts-serve

Import symbol

import * as ts_serve from "@ayame113/ts-serve";