ansi-escapes icon indicating copy to clipboard operation
ansi-escapes copied to clipboard

Named exports instead of default export?

Open fisker opened this issue 3 years ago • 9 comments

For better tree-shaking.

fisker avatar Apr 02 '22 10:04 fisker

I don't see how that matters in this case as it's for Node.js, not browsers.

sindresorhus avatar Apr 02 '22 12:04 sindresorhus

Bundling for the desktop is valid, especially in end-user applications. I'd be :+1: for this if @sindresorhus also agreed and if someone did a PR.

Qix- avatar Apr 02 '22 12:04 Qix-

I need to bundle this package, and I only want use clearScreen.

fisker avatar Apr 02 '22 12:04 fisker

I'm neutral about it. Many of the exports have too general names when imported without the namespace though. ansiEscapes.image() makes it clear what it is, but image() could be anything.

sindresorhus avatar Apr 02 '22 14:04 sindresorhus

For those users we'd just rec using import * as ansiEscapes from 'ansi-escapes'. I would imagine in some cases the bundler would still be able to do DCR even then.

Qix- avatar Apr 02 '22 14:04 Qix-

@Qix- It's not just image(). It applies to most of the exports.

sindresorhus avatar Apr 06 '22 15:04 sindresorhus

I don't see how that negates what I said though. You can still import as a namespace. It's up to users to use proper naming. If they just want to import image then they should import { image as ansiImage } from 'ansi-escapes' if they want it to be clean.

My point was more that even using a wildcard import (import * as ansiEscapes) might still allow the bundlers/compilers to do dead code removal ("tree shaking" as the JS community seems to call it) even though it's done via indirection.

Qix- avatar Apr 06 '22 22:04 Qix-

@sindresorhus Would you be open to a PR?

I think the argument for export names not being self-descriptive without the context of the package name applies to most ESM packages, including native ones:

// both `access` and `constants` are very vague without knowing it comes from `fs`
import { access, constants } from 'node:fs/promises';

privatenumber avatar Nov 21 '22 02:11 privatenumber

Would you be open to a PR?

Yes, but it should preserve the default export too (can be done with re-exporting). Most users don't care about tree-shaking this, so the main docs should remain the default export too. It can be a tip mention in the docs that this can also be imported using named imports.

I think the argument for export names not being self-descriptive without the context of the package name applies to most ESM packages, including native ones:

Node.js APIs are not very good, so I wouldn't use that as an example of great API design.

sindresorhus avatar Dec 04 '22 17:12 sindresorhus