Skip to main content
Home

Ad hoc (client) JavaScript from typed (server) code

This package works with DenoIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun, Browsers
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
It is unknown whether this package works with Browsers
JSR Score
94%
Published
5 months ago (0.1.15)

Generate typed JS on the fly.

Keeps track of manipulated references, outputs compact code.

Examples

Provide time in target platform

import { js } from "@classic/js";

const now = js<Date>`new Date()`;
const time = js.string`Time is ${now.getHours()}:${now.getMinutes()}`;

// Generated code looks like:
// let now = new Date();
// let time = `Time is ${now.getHours()}:${now.getMinutes()}`;

Render a react app

import { js, type JS } from "@classic/js";

import type React from "npm:react";
import type { Root } from "npm:react-dom/client";

const react = js.module<typeof import("npm:react-dom/client")>("npm:react-dom/client");
const app = js.module<{ render: (root: Root) => void }>(import.meta.resolve("./app.tsx"));

const clientJs = app.render(react.createRoot(js.window.document.body));

Prevent clicks on every link of the document

import { js, type JS } from "@classic/js";

const disableClicks = js.window.document.querySelectorAll("a").forEach((a) =>
  a.addEventListener("click", (e: JS<MouseEvent>) => [
    e.preventDefault(),
  ])
);

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@classic/js

Import symbol

import * as js from "@classic/js";
or

Import directly with a jsr specifier

import * as js from "jsr:@classic/js";

Add Package

pnpm i jsr:@classic/js
or (using pnpm 10.8 or older)
pnpm dlx jsr add @classic/js

Import symbol

import * as js from "@classic/js";

Add Package

yarn add jsr:@classic/js
or (using Yarn 4.8 or older)
yarn dlx jsr add @classic/js

Import symbol

import * as js from "@classic/js";

Add Package

vlt install jsr:@classic/js

Import symbol

import * as js from "@classic/js";

Add Package

npx jsr add @classic/js

Import symbol

import * as js from "@classic/js";

Add Package

bunx jsr add @classic/js

Import symbol

import * as js from "@classic/js";