Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommendations on exporting multiple functions in a utility library #713

Closed
Svish opened this issue May 8, 2020 · 1 comment
Closed
Labels
kind: question This is a usage or similar question kind: support Asking for support with something or a specific use case

Comments

@Svish
Copy link

Svish commented May 8, 2020

Not sure if this is a general javascript/typescript question or a tsdx question, but I needed to create a @my-org/common utility package (custom formatting, validation, some special array functions, etc., etc.), and, well, I've hit at least more than 1 of the "things to screw up" with no idea of how to do it right.

So, tsdx looks promising, but I have a question about how one would write/structure a general utility library. What does one do about the main index.ts file? Especially in relation to tree-shaking and other related things.

Do I need to export every single utility function from index.js? And if not, how would one import one of these functions?

I've tried to look for examples, but most seem to be single function or simple react component type packages, where of course the main index.ts makes perfect sense. 🤔

@agilgur5 agilgur5 added the kind: question This is a usage or similar question label May 9, 2020
@agilgur5
Copy link
Collaborator

It is indeed more of a general JS question.

Yes, exporting every utility function from index.ts is one way of doing it. Bundlers should normally tree-shake out any unused imports, but this apparently does not always seem to be true unless you also set preserveModules: true for Rollup (see that linked issue for details on that).

Multi-entry is the other option, that is, having different files containing different imports, i.e. @my-org/common/validation, @my-org/common/array-func-a, etc. Support is still pending for this in TSDX (#367 ) because it is a bit breaking and it is very manual currently.

@agilgur5 agilgur5 changed the title Recommendations on util type library Recommendations on exporting multiple functions from a utility library May 23, 2020
@agilgur5 agilgur5 changed the title Recommendations on exporting multiple functions from a utility library Recommendations on exporting multiple functions in a utility library May 23, 2020
@agilgur5 agilgur5 added the kind: support Asking for support with something or a specific use case label Aug 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question This is a usage or similar question kind: support Asking for support with something or a specific use case
Projects
None yet
Development

No branches or pull requests

2 participants