deno_emit icon indicating copy to clipboard operation
deno_emit copied to clipboard

Transpile and bundle JavaScript and TypeScript under Deno and Deno Deploy

emit

deno emit Twitter handle Discord Chat

Transpile and bundle JavaScript and TypeScript in Deno and Deno Deploy.

This is an unstable module, where the API is likely to change over time.

Why was Deno.emit() removed from Deno?

In Deno 1.22 the unstable API Deno.emit() was removed. This module provides similar functionality as an optional user loaded module.

Having any APIs that are platform specific (e.g. in the Deno namespace) makes code a lot less portable. We have generally avoided adding APIs to the Deno namespace if we can. While Deno.emit() and similar APIs have been part of the unstable features for an extended period of time, creating deno_emit, deno_doc and deno_cache, which expose some of the internal Deno APIs as user loadable modules has proven to be a good pattern for giving the command line power of the Deno CLI to build end-user solutions, which also then can work on edge compute like Deno Deploy.

So while taking away functionality from the Deno CLI may seem like a strange decision, by moving it to a user loadable module, it provides users more choice and flexibility.

Note: the examples below use "unpinned" version of the module. Consider using a "pinned" version (e.g. https://deno.land/x/[email protected]/mod.ts instead.)

Transpiling

Take individual modules that are JavaScript or TypeScript and emit them in a transpiled fashion. An example of taking some TypeScript and transpiling to JavaScript:

import { emit } from "https://deno.land/x/emit/mod.ts";

const url = new URL("./testdata/mod.ts", import.meta.url);
const result = await emit(url);

const code = result[url.href];
console.log(code.includes("export default function hello()"));

Bundle

Take a root module and all its dependencies and emit a single JavaScript bundle. This is similar to the functionality provided by deno bundle on the Deno command line. An example:

import { bundle } from "https://deno.land/x/emit/mod.ts";
const result = await bundle(
  new URL("https://deno.land/[email protected]/examples/chat/server.ts"),
);

const { code } = result;
console.log(code);

Type checking

Currently emit does not perform type checking, while the prior Deno.emit() could. Getting type checking to run under Deno is a complex process. While we want to provide a user loadable solution for this, it is unlikely that it will be provided as part of this module, or would exist as a separate loadable module as part of this one.


Copyright 2018-2022 the Deno authors. All rights reserved. MIT License.