jaredpalmer / tsdx

Zero-config CLI for TypeScript package development
https://tsdx.io
MIT License
11.26k stars 508 forks source link

Show usage of a tsdx-created lib with deno 🦖 #722

Closed deanrad closed 4 years ago

deanrad commented 4 years ago

If I figure it out first, I'll answer this, but any pointers would be appreciated!

deanrad commented 4 years ago

So 6 minutes later, I have run code from a tsdx project through deno! (Kinda)..

This worked to print Hello World..

// poly-deno.js
import { channel } from 'https://raw.githubusercontent.com/deanius/monolog/master/polyrhythm.1.0.0.development.js';
channel.listen('greet', () => console.log('World'));
channel.filter(true, () => console.log('Hello'));
channel.trigger('greet');

I ran the file successfully with:

deno run --allow-net=githubusercontent.com examples/poly-deno.ts
Compile file:///Users/deanius/src/polyrhythm/examples/poly-deno.ts
Download https://raw.githubusercontent.com/deanius/monolog/master/polyrhythm.1.0.0.development.js
... # lots of downloaded files
Hello
World

The file polyrhythm.1.0.0.development.jswas taken from a tsdx build, and its local dependency names manually replaced with URLs.. It was originally used in a browser like this:

<script type="module">
      import {  channel } from './polyrhythm.1.0.0.development.js';

This is the part that is suboptimal, that I'm looking for help on. If I used the actual typescript files raw from the project, deno complained with:

error: Uncaught URIError: relative import path "rxjs" not prefixed with / or ./ or ../ Imported from "https://raw.githubusercontent.com/deanius/polyrhythm/master/src/channel.ts"

So it's a good start, but a ways to go - I'm sure if one of the tsdx-built files is, or can be made deno compatible, then we'll be off and running..

agilgur5 commented 4 years ago

Yea so Deno is basically incompatible with most existing JS/TS libraries because the way it does dependencies doesn't align with the ecosystem. Only if you're installing a package with 0 dependencies will it work ok, otherwise as soon as you even have one dependency it falls over because the import isn't a URL.

Deno still transpiles your code and with its own tsconfig settings at that, so providing it raw TS is likely not as efficient or compatible as JS with type declarations (i.e. TSDX's existing output). Meaning that to support Deno, we just need to support it's incompatible dependency system, so we'd need some Babel transform (or Rollup) that converts things like:

import { blah } from 'rxjs'

to something like:

import { blah } 'https://unpkg.com/rxjs@6.5.5'

with the same version as the one in your package.json

Would take a PR to support Deno format with some plugin, but supporting incompatible platforms isn't on the top of my priorities.

deanrad commented 4 years ago

Thanks for the thorough reply. I won't hold my breath (i wasn't!) but the idea of a transform upon build is interesting..