egoist / ideas

💡ideas / thoughts / polls from egoist
101 stars 2 forks source link

Making Node.js code compatible with Deno #2

Open egoist opened 5 years ago

egoist commented 5 years ago

Problem

There are many great Node.js libraries that are also useful in Deno ecosystem, it would be a waste of time to reinvent the wheels from scratch for Deno.

Solution

Creating a compiler, probably on top of webpack, to generate Deno-compatible code from Node.js code.

// index.js
const chalk = require('chalk')
module.exports = chalk
denoify ./index.js -o ./dist/index.js
deno dist/index.js # it works

👍 If you like this idea

zzz6519003 commented 5 years ago

their difference in appearance?

motss commented 5 years ago

How about those rely on Node's core API?

egoist commented 5 years ago

@motss via polyfills, path url and some other core modules can be shimmed easily. Deno may not have all the APIs that we have in Node.js, we can try to support as many as possible.

aalemayhu commented 5 years ago

Sounds interesting. Considering that Deno has a different security model would the compiler or whatever is generating the code be aware of this? Or are you thinking of making the generated code "less secure" by being permissive (--allow-all)? Just curious if you have thought about it.

egoist commented 5 years ago

@scanf The generated code should work the same as normal Deno code, Deno will only ask for permissions when needed.

motss commented 5 years ago

How about working with the package author to support Deno?

egoist commented 5 years ago

@motss that's like working with the package author to support TypeScript 😂which won't work since not everyone has the time / interest in doing so.

vladimyr commented 5 years ago
denoify ./index.js -o ./dist/index.js
deno dist/index.js # it works

Which is more or less same as:

npx webrunify ./index.js > ./dist/index.js
deno dist/index.js

Where webrunify is bundler made on top of browserify that produces ES module bundles in rollup like fashion: https://github.com/brechtcs/webrunify

However, bundling stuff together is an easy part. Next wall you'll gonna hit is unfortunate fact that your compiler can't produce bundles that are guaranteed to pass typescript's type checking. That is important because deno forces type checking of js files (aka your resulting bundle): https://github.com/denoland/deno/blob/9dfebbc/js/compiler.ts#L162 (docs)

So you essentially need to prepend your bundle with // @ts-nocheck pragma :weary:

vladimyr commented 5 years ago

There is also webpack powered PoC: https://github.com/reggi/node-to-deno

Also let me point you to two related deno issues/discussions:

Discussion: Use of NPM modules within deno https://github.com/denoland/deno/issues/1397

No typing check for js files https://github.com/denoland/deno/issues/1432

But I'm just a reader who did few local tests, @reggi knows all nasty details and what is needed here is his expertise :neckbeard: