Is Using `__deps__` for Module Dependencies in ESM a Smart Move?

0
6
Asked By CleverFox123 On

I've been experimenting with a new approach to handle module dependencies in a way that works across different runtimes, like Node.js and the browser. Instead of using standard import statements, I want to declare dependencies as data in my module. Here's an example:

```js
export const __deps__ = {
fs: "node:fs",
logger: "./logger.mjs",
};

export default function makeService({ fs, logger }) {
// ...
}
```

With this setup, the module doesn't import anything directly, but instead, it gets its dependencies passed from a higher level in the application. For example:

In Node:
```js
makeService({ fs, logger });
```

In browsers:
```js
makeService({ fs: fsAdapter, logger });
```

This pattern is similar to standard Dependency Injection, and my goal is to keep the modules flexible and runtime-agnostic. However, I'm aware there are trade-offs, like reduced static analyzability and the need for more architectural discipline. So, do you think this pattern is a valid approach for cross-runtime modules, or is it just unnecessary complexity compared to using import maps or conditional builds?

3 Answers

Answered By FutureDev On

I can see why you'd want to avoid static imports. It gives you more control over your dependencies at runtime, and that's a smart move for cross-platform code. I just wonder if the trade-offs are worth it for your app's size and complexity.

Answered By TechTrendsGuy On

I think what you're doing with the `makeService` function is interesting, but I'm not fully sold on the `__deps__` variable. Isn't the logger already inherently runtime-agnostic? It feels like you could simplify things by just using import statements instead of waving around data like that.

Answered By CreativeCoder99 On

Not a huge fan of that abstraction. What’s the point of exporting that `__deps__` object? If you want the function to handle everything, then just keep it simple. Either go all in on abstraction or skip it entirely, right?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.