All Projects β†’ fromdeno β†’ deno2node

fromdeno / deno2node

Licence: MIT license
Compile your Deno project to run on Node.js.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to deno2node

deno-dex
πŸ¦• A dexterous deno executor
Stars: ✭ 19 (-71.21%)
Mutual labels:  deno
now-deno-example
A demo application of now-deno and deno-postgres.
Stars: ✭ 30 (-54.55%)
Mutual labels:  deno
deno-x-ranking
πŸ¦• Deno Third Party Modules Ranking πŸ‘‘
Stars: ✭ 28 (-57.58%)
Mutual labels:  deno
babel-loader-lerna-cra
Transpile Create-React-App imports in Lerna projects.
Stars: ✭ 30 (-54.55%)
Mutual labels:  transpile
kt2ts-gradle-plugin
Generate TypeScript from Kotlin (or any jvm language) by annotations
Stars: ✭ 15 (-77.27%)
Mutual labels:  transpile
prettyBenching
πŸ¦• A small lib to make your Deno benchmarking progress and results look pretty
Stars: ✭ 23 (-65.15%)
Mutual labels:  deno
deno sass
πŸš€ Cute Sass compiler bindings to Deno.
Stars: ✭ 13 (-80.3%)
Mutual labels:  deno
deno-canvas
Canvas API for Deno, ported from canvaskit-wasm (Skia).
Stars: ✭ 124 (+87.88%)
Mutual labels:  deno
azure-webapps-deno-deploy
A GitHub Action for deploying Deno πŸ¦• web apps to Azure ☁️
Stars: ✭ 27 (-59.09%)
Mutual labels:  deno
scuttlesaurus
retog.github.io/scuttlesaurus/
Stars: ✭ 21 (-68.18%)
Mutual labels:  deno
drash
A microframework for Deno's HTTP server with zero third-party dependencies
Stars: ✭ 1,013 (+1434.85%)
Mutual labels:  deno
mock
Utilities to help mock behavior, spy on function calls, stub methods, and fake time for tests.
Stars: ✭ 31 (-53.03%)
Mutual labels:  deno
slack-web-api
Simple, convenient, and configurable HTTP client for making requests to Slack’s Web API. Deno port of @slack/web-api
Stars: ✭ 16 (-75.76%)
Mutual labels:  deno
carol
A Deno port of carlo
Stars: ✭ 63 (-4.55%)
Mutual labels:  deno
deno deploy versailles
凑尔衛语录(ιƒ¨η½²εœ¨ Deno Deploy)
Stars: ✭ 18 (-72.73%)
Mutual labels:  deno
http compression
πŸ—œοΈ Deno HTTP compression middleware
Stars: ✭ 34 (-48.48%)
Mutual labels:  deno
hatcher
🐣 Registries toolbox & update notifications for your CLI
Stars: ✭ 14 (-78.79%)
Mutual labels:  deno
cli badges
Generate Badges for your CLI written in πŸ¦• Deno and TS
Stars: ✭ 20 (-69.7%)
Mutual labels:  deno
deno desktop
Windowing support for Deno WebGPU.
Stars: ✭ 49 (-25.76%)
Mutual labels:  deno
ssvm-deno-starter
A template project to run Rust functions in Deno through the Second State WebAssembly engine.
Stars: ✭ 50 (-24.24%)
Mutual labels:  deno

deno2node

Compile your Deno project to run on Node.js.

Because Deno's tooling is way simpler than Node's

Quick Setup

Run npx deno2node --init in an empty directory.

CLI Usage From Deno

No installation needed. Simply cd into the directory of your project, and run:

deno run --no-check --allow-read=. --allow-write=. \
  https://deno.land/x/deno2node/src/cli.ts <tsConfigFilePath>

You need to substitute <tsConfigFilePath> by a path to your tsconfig.json file. deno2node passes it on to the shipped tsc for compiling your code.

If you don't already have a package.json, you may find dnt easier to use.

CLI Usage From Node.js

As a by-product of end-to-end testing, a Node.js build is also available:

# New features or TypeScript upgrades
# may alter output or diagnostics across minor versions.
npm install --save-dev --save-prefix='~' deno2node

Now add a script to package.json so you can run it with npm run prepare:

// @filename: package.json
{
  // yada yada ...
  "scripts": {
    "prepare": "deno2node <tsConfigFilePath>"
  }
}

You can also run it directly:

npx deno2node <tsConfigFilePath>

How It Works

There are three main steps to this.

  1. Transform the code base in-memory, by rewriting all import statements.
  2. Typecheck the code.
  3. Emit .js and .d.ts files. These files can directly be run by Node or published on npm.

deno2node uses ts-morph under the hood, which in turn builds on top of the TypeScript compiler tsc. Hence, you get the same behaviour as if you had developed your code directly for Node.

deno2node can perform more powerful transpilation steps that make it flexible enough for most needs.

Shimming

Some things are global in Deno, but not in Node.js. One example for this is fetch. Consequently, you need to shim them, i.e. provide code that supplements the missing globals.

Note that deno2node does not actually touch global definitions. Instead, it only injects import statements in the respective modules.

Install the packages providing the shims.

Now, create a file that exports the globals you need:

// @filename: src/shim.node.ts
export { Blob } from "buffer";
export { webcrypto as crypto } from "crypto";

export { fetch, File, FormData, Headers, Request, Response } from "undici";
export { Deno } from "@deno/shim-deno";
export { alert, confirm, prompt } from "@deno/shim-prompts";

Lastly, you need to register your shims in tsconfig.json so deno2node can inject them for you:

// @filename: tsconfig.json
{
  "deno2node": {
    "shim": "src/shim.node.ts" // path to shim file, relative to tsconfig
  }
}

Runtime-specific code

In same cases you may want to have two different implementations, depending on whether you're running on Deno or on Node. When shimming is not enough, you can provide a Node-specific <anything>.node.ts and a Deno-specific <anything>.deno.ts version of any file. They need to reside next to each other in the same directory.

deno2node will ignore the Deno version and rewrite imports to use the Node.js version instead. Thus, the Deno-specific file will not be part of the build output.

For example, provide greet.deno.ts for Deno:

// @filename: src/greet.deno.ts
export function greet() {
  console.log("Hello Deno!");
  // access Deno-specific APIs here
}

Now, provide greet.node.ts for Node:

// @filename: src/greet.node.ts
export function greet() {
  console.log("Hello Node!");
  // access Node-specific APIs here
}

Finally, use it in foo.ts:

import { greet } from "./platform.deno.ts";

// Prints "Hello Deno!" on Deno,
// and "Hello Node!" on Node:
greet();

This technique has many uses. deno2node itself uses it to import from https://deno.land/std. The Telegram bot framework grammY uses it to abstract away platform-specific APIs.

Vendoring

To import a module which has no npm equivalent, first set up vendorDir.

// @filename: tsconfig.json
{
  "deno2node": {
    "vendorDir": "src/vendor/" // path within `rootDir`
  }
}

Then, populate it: deno vendor src/deps.vendor.ts --output src/vendor/.

Vendoring is still experimental, so be welcome to open an issue if you encounter a problem with it!

Also, consider recommending pnpm to users of your library. It might be able to deduplicate vendored files across packages.

API

Confer the automatically generated API Reference if you want to use deno2node from code.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].