#Best output format for a TypeScript library

35 messages · Page 1 of 1 (latest)

grim gale
#

Hello, I'm writing a JS library, using TypeScript (because of static typing) and ES6 -style modules.

I'm wondering what is the best format to package my library. My target are all modern browsers and Node.js, I've read ES6 modules are now broadly supported by browsers and Node.js; thus, is it a good choice?

Sadly I've seen that the test framework I'm using (jest) doesn't support ES6 modules. Therefore I think I have to fallback on commonjs. But commonjs can't be used on browsers, so shoud I make two different outputs? 🤔

robust geode
#

are you stuck using jest? it should be pretty easy to swap it out with a different test runner, and only having to publish a single module format would make your life significantly easier

grim gale
#

Node.js ES6 support still seems experimental

#

so I would still rely on commonjs for Node.js

robust geode
#

it all depends on who your target audience is, but if it's users of modern-ish versions of node.js and browsers, then ESM-only seems like a fine choice

#

i think if i were publishing a general-purpose npm package today i wouldn't bother supporting commonjs until a user specifically asked for it

grim gale
robust geode
#

what version of node are you using?

grim gale
#

v18.13.0

robust geode
grim gale
#

oh, v23 now

robust geode
#

i think it's hard for them because they do a lot of runtime magic

#

if you use a simpler test runner it's not a problem

grim gale
#

do you suggest any simpler test runner?

#

ah okay

robust geode
#

it's also one less dependency to manage, which is nice

grim gale
#

Just found out I have to add .js extension to every import because TypeScript transpiler doesn't add it

#

and then module resolution fails on browser

#

very bad...

robust geode
#

yeah TS never touches your import paths. what you write is what you get

#

what problem are you having in browsers though?

#

that should work fine as long as your web server is configured to serve the files

grim gale
#

so import { Class } from './Class' would fail

#

but import { Class } from './Class.js' would not, so I'm appending .js also in TypeScript code

robust geode
#

or was that without .js?

grim gale
#

ah no I mean without

robust geode
#

ah gotcha

#

yeah just use .js everywhere