tbtc.js icon indicating copy to clipboard operation
tbtc.js copied to clipboard

Convert to commonjs for Node

Open dmihal opened this issue 4 years ago • 4 comments

The library uses ES modules, which requires a transpiling with something like babel. Thus, this can't be used easily in Node.js.

dmihal avatar May 16 '20 00:05 dmihal

Hey @dmihal - we've been developing/testing tbtc.js with some of the latest versions of node (v13), which include native support for ES modules. I believe you still need a flag for the JSON artifacts to load - --experimental-json-modules.

If you have Node v13, running node --experimental-json-modules example.js should work.

liamzebedee avatar May 18 '20 04:05 liamzebedee

That's probably fine for now since tBTC is still in beta. But long term, limiting the library to only work with an experimental flag will probably prevent a lot of developers from using the library.

I've written a number of NPM libraries, I know how frustrating it is trying to get a library to play nicely with both Web/Babel & Node 😩

dmihal avatar May 19 '20 15:05 dmihal

I know how frustrating it is trying to get a library to play nicely with both Web/Babel & Node 😩

Oh, I agree @dmihal, it's a nightmare. Sourcemaps in particular are something that still scare me re: transpilation.

I don't know so much about bundling, aside from the requirements varying between every system I've used (Next.js, create-react-app, webpack, the list goes on). That being said - we should be able to make this usable from the get-go. I think in the coming weeks we'll explore some approaches, and if you have any recommendations, please feel free to contribute them. 🙂

liamzebedee avatar May 20 '20 09:05 liamzebedee

No current plans to convert away from ES modules here, so I dropped the tags in favor of an ecosystem tag---we use ES modules expectedly and intentionally, but a switch away from them might be dictated by ecosystem concerns.

To elaborate a bit, the decision to use ES modules was rooted in the goals of running on recent versions of Node, working for modern browser versions, and not requiring babel, while still supporting a relatively modern development environment. This is why all typing is done in JSDoc rather than TypeScript, for example. The fact that node currently tucks this behind a flag is annoying, but note that if an npm package is marked as type: "module", recent versions of Node, including the latest LTS version, should handle things transparently, and all bin/ scripts have a shebang that specifies the appropriate flag.

We're open to feedback on specific trip-ups folks have integrating here, and definitely open to convert back to standard CommonJS if the downsides prove too great.

Shadowfiend avatar May 20 '20 16:05 Shadowfiend