this post was submitted on 23 Jan 2024
9 points (100.0% liked)

JavaScript

2000 readers
1 users here now

founded 1 year ago
MODERATORS
 

I've decided to write my first library in Typescript, which is here.

I've got some questions that I don't think is suitable for StackOverflow because it's quite case-specific rather than being generic and I've got a couple of them rather than one.

I'm trying to wrap my head around JS/TS module system for some while. There are some problems with my library:

  1. If a user imports a hook, they have to do import { useDocument } from 'firereact/firestore/useDocument', but it'd be much better if they could do import { useDocument } from 'firereact/firestore'. I've tried many ways but I couldn't export it to firestore/index.ts I guess. What am I doing wrong?
  2. I have realized that consumers can also import test modules and firebase.ts, which are only used for testing and it is not desirable for them to be imported by the consumers. How can I ignore some specific exports while bundling? They are meant to be used internally.

Thanks in advance. And btw, extra reviews and critics are appreciated since this is going to be my first library.

top 4 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 10 months ago (1 children)

In all seriousness, this is a fine scenario for AI guided learning. I gave Bing Copilot your question, and the response was very helpful.

Here's the trick though:

// In 'firereact/firestore/index.ts'
export { useDocument } from './useDocument';

and:

// In 'tsconfig.json'
{
  "compilerOptions": {
    // ...
  },
  "exclude": ["**/*.test.ts", "**/*.spec.ts", "path/to/firebase.ts"]
}
[–] erayerdin 1 points 10 months ago

Yep, tried ChatGPT on that but screwed up the project and had to revert back to an older tag for it, but I will try this surely.

[–] [email protected] 3 points 10 months ago (1 children)

I don’t really understand the first question because you have elided some important details, but for the second question, there’s a “files” key you can set in package.json that specifies which files to include in the package tarball. If you set that to some pattern that excludes your tests, they will not be included. Alternatively, you can create a .npmignore file.

[–] erayerdin 1 points 9 months ago

Thank you, I've used files to exclude them from the bundle. :)