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
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.
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.
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
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically