Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Built-in module auto porting #39

Closed
Artrix9095 opened this issue Feb 9, 2022 · 2 comments
Closed

Built-in module auto porting #39

Artrix9095 opened this issue Feb 9, 2022 · 2 comments

Comments

@Artrix9095
Copy link

I have a scenario. So, lets say your using node-fetch for your NodeJS version of your library, but you want to use the built-in fetch module in deno. Or lets say you use ws for nodejs and the built-in websocket module in deno. I currently have a very hacky solution

const isDeno = () => {
    // @ts-ignore
    try {
        window.fetch; 
        return true
    } catch { return false }
}
// @ts-ignore
const WebSocket = isDeno() ? window.WebSocket : require('ws')


...

Cons of this method is... I no longer get intellisense for the WebSocket variable.

I think something like this would be nice in ports

{
    "ports": {
        "ws": "builtins://WebSocket"
    }
}

IF this is possible this would be a huge help to me and my team.

Thanks!

@garronej
Copy link
Owner

garronej commented Feb 9, 2022

Hi @Artrix9095,

Thanks for reaching out. I think there is something in Denoify already that will fit your needs.
Do you know about the .deno.ts files?

In my_dummy_npm_and_deno_module
image

In your cas you could have a file for node:
tools/WebSocket.ts

import WebSocket from 'ws';
export { WebSocket };

And a file for deno
tools/WebSocket.deno.ts

export const { WebSocket } = window;

And then whenever you need WebSocket import it just like:

import { WebSocket } from "../tools/WebSocket";

If you are running on Deno it will import WebSocket.deno.ts, on node it will be WebSocket.ts

Hopes it fit your needs.

Best regards,

@Artrix9095
Copy link
Author

Thanks for the fast reply, this should work. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants