ecadlabs / taquito

A library for building dApps on the Tezos Blockchain - JavaScript / TypeScript
https://taquito.io
Apache License 2.0
298 stars 116 forks source link

Taquito first class support for deno #455

Closed jevonearth closed 1 year ago

jevonearth commented 4 years ago

Deno is a new runtime for JavaScript and TypeScript.

We want:

See: https://deno.land/

mweichert commented 3 years ago

Posting some thoughts I shared with @jevonearth when investigating how to add Deno as a build target for Taquito. Its important to differentiate between the following:

a) Consuming Taquito with Deno b) Adding Deno as a build target for Taquito

Consuming Taquito with Deno

Using an import map file, its relatively easy to make the following example work: import {TezosToolkit} from '@taquito/taquito'; console.log(TezosToolkit);

To make it work, you would map @taquito/taquito to https://cdn.skypack.dev/@taquito/taquito

As of right now, this won't actually work because Taquito makes use of require() and CJS. However, there are only a few places in various Taquito packages where CJS is used, and so adjusting is not difficult and something I was able to do quickly when prototyping.

Adding Deno as a build target for Taquito

Say you're working on a feature branch in Taquito, and want to target Deno? How do you do that? First, you'll need to write an import map which does the following:

That gets you most of the way there. However, there's one critical function that Skypack does for you when consuming that you have to do so yourself when targeting - local module resolution. For instance, one thing that Skypack does for you is transpile something like this:

import {split} from './util'

to:

import {split} from './util.ts'

Why? This is part of the ESM spec, and what browsers and Deno expect. They require fully resolved paths.

I'm surprised, but haven't found a third-party tool that does this kind of transpilation other than bundlers which accept TypeScript as input and output JavaScript. However, that has its own problems as the Deno runtime is stricter than the browser and Node. For example, take the following Typescript:

const foobar = (foo: string | undefined) => foo ? foo : "Foo Bar"
console.log(foobar()) // outputs "Foo Bar"

It's a common approach - a function will use a default value if no parameter is given. Running this code using a bundler like Rollup or Webpack produces the following output:

const foobar = foo => foo ? foo : "Foo Bar"
console.log(foobar())

The output is technically valid JavaScript, but it will not compile using Deno. Deno will throw a compilation error: foobar expects 1 argument but was called with 0 arguments.

I had a few ideas how to work around this:

michaelkernaghan commented 1 year ago

general agreement in the team that at this point Deno is not going to be implemented in Taquito.