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

feat(learn): add article for publishing a typescript package #7279

Merged
merged 36 commits into from
Feb 5, 2025

Conversation

JakobJingleheimer
Copy link
Member

@JakobJingleheimer JakobJingleheimer commented Nov 23, 2024

Description

Document the recommended way to publish a typescript package

Validation

Related Issues

nodejs/typescript#19

Depends on #7229

Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.
  • I have run npm run format to ensure the code follows the style guide.
  • I have run npm run test to check if all tests are passing.
  • I have run npx turbo build to check if the website builds without errors.
  • I've covered new added functionality with unit tests if necessary.

Copy link

vercel bot commented Nov 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nodejs-org ✅ Ready (Inspect) Visit Preview Feb 4, 2025 10:19am

@AugustinMauroy
Copy link
Member

AugustinMauroy commented Dec 29, 2024

Jacob you should:

apps/site/pages/en/learn/typescript/publishing.md Outdated Show resolved Hide resolved
apps/site/pages/en/learn/typescript/publishing.md Outdated Show resolved Hide resolved
apps/site/pages/en/learn/typescript/publishing.md Outdated Show resolved Hide resolved
apps/site/pages/en/learn/typescript/publishing.md Outdated Show resolved Hide resolved
@JakobJingleheimer

This comment was marked as outdated.

Co-authored-by: Augustin Mauroy <[email protected]>
Signed-off-by: Jacob Smith <[email protected]>
@JakobJingleheimer JakobJingleheimer marked this pull request as ready for review January 8, 2025 19:20
@JakobJingleheimer JakobJingleheimer requested a review from a team as a code owner January 8, 2025 19:20
@ljharb
Copy link
Member

ljharb commented Feb 3, 2025

It's not a clean mapping; unfortunately TS doesn't have a babel-preset-env-like setting where it can infer the parser settings from your supported envs.

@JakobJingleheimer
Copy link
Member Author

It's not a clean mapping; unfortunately TS doesn't have a babel-preset-env-like setting where it can infer the parser settings from your supported envs.

Looks like there is something: https://www.typescriptlang.org/tsconfig/#targethttps://www.npmjs.com/package/@tsconfig/node18

@ljharb
Copy link
Member

ljharb commented Feb 3, 2025

That's great, thanks!

Copy link
Member

@AugustinMauroy AugustinMauroy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM !!!! WOW after some interation

Copy link
Contributor

github-actions bot commented Feb 4, 2025

Lighthouse Results

URL Performance Accessibility Best Practices SEO Report
/en 🔴 60 🟢 100 🟢 100 🟢 91 🔗
/en/about 🟢 100 🟢 100 🟢 100 🟢 91 🔗
/en/about/previous-releases 🟢 99 🟢 100 🟢 100 🟢 92 🔗
/en/download 🟢 92 🟢 100 🟢 96 🟢 91 🔗
/en/blog 🟢 100 🟢 100 🟢 96 🟢 92 🔗

Copy link
Contributor

github-actions bot commented Feb 4, 2025

Unit Test Coverage Report

Lines Statements Branches Functions
Coverage: 85%
83.85% (587/700) 64.74% (180/278) 82.7% (110/133)

Unit Test Report

Tests Skipped Failures Errors Time
123 0 💤 0 ❌ 0 🔥 4.908s ⏱️

@bmuenzenmeyer
Copy link
Collaborator

merging per a conversation with @JakobJingleheimer

@bmuenzenmeyer bmuenzenmeyer added this pull request to the merge queue Feb 5, 2025
Merged via the queue into main with commit 021bf3c Feb 5, 2025
22 of 24 checks passed
@bmuenzenmeyer bmuenzenmeyer deleted the art/publishing-typescript branch February 5, 2025 16:19
@JakobJingleheimer
Copy link
Member Author

Thanks all! Happy to iterate if needed 🙂

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks great! Just a few comments (sorry they came in post-merge)


- Use [dependabot](https://docs.github.com/en/code-security/dependabot) to keep your dependencies current, including those in github actions. It's a very easy set-and-forget configuration.

- `.nvmrc` comes from [NVM](https://github.com/nvm-sh/nvm), a multi-version manager for node. It allows you to specify the version of node the project should generally use.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `.nvmrc` comes from [NVM](https://github.com/nvm-sh/nvm), a multi-version manager for node. It allows you to specify the version of node the project should generally use.
- `.nvmrc` comes from [`nvm`](https://github.com/nvm-sh/nvm), a multi-version manager for node. It allows you to specify the version of node the project should generally use.


TypeScript has warned that the above code will not behave as intended, just like a unit test warns that code does not behave as intended. They are complementary and verify different things—you should have both.

Your editor (ex VS Code) likely has built-in support for TypeScript, displaying errors as you work. If not, and/or you missed those, CI will have your back.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Your editor (ex VS Code) likely has built-in support for TypeScript, displaying errors as you work. If not, and/or you missed those, CI will have your back.
Your editor (eg VS Code) likely has built-in support for TypeScript, displaying errors as you work. If not, and/or you missed those, CI will have your back.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the same 😜 One is english, the other is latin:

exexample
egexempli gratia

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, but "ex" reads to me as "formerly", "eg" doesn't have the same confusion

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay that's better then 🙂


# This is mostly boilerplate.

name: Publish to NPM
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: Publish to NPM
name: Publish to npm

(npm should always be lowercased)


#### Breaking this down

Generating type declarations is deterministic: you'll get the same output from the same input, every time. So there is no need to commit these to git.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm pretty sure this isn't actually true, but either way generated output should never be committed to source control.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Oh? I can't think of how it isn't—could you enlighten me?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure it's semantically deterministic, but I would be very surprised if the output bytes were actually deterministic (mangled variable names, etc) - and either way, nondeterminism isn't why they shouldn't be committed, "they're generated" is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is type def generation, no transpiling the typescript (which is definitely not deterministic, as indeed variable name mangling etc). Type names are not changed when the def files are generated (that would break the hell out of the type defs).

nondeterminism isn't why they shouldn't be committed, "they're generated" is.

True, but if it's nondeterministic, I shouldn't say it is deterministic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhh that's fair. you might be right that the type defs are deterministic! but i'd still say that's not important to mention because it's not why they shouldn't be committed.


[`npm publish`](https://docs.npmjs.com/cli/commands/npm-publish) grabs everything applicable and available at the moment the command is run; so generating type declarations immediately before means those are available and will get picked up.

By default, `npm publish` grabs (almost) everything (see [Files included in package](https://docs.npmjs.com/cli/commands/npm-publish#files-included-in-package)). In order to keep your published package minimal (see the "Heaviest Objects in the Universe" meme about `node_modules`), you want to exclude certain files (like tests and test fixtures) from from packaging. Add these to the opt-out list specified in [`.npmignore`](https://docs.npmjs.com/cli/using-npm/developers#keeping-files-out-of-your-package); ensure the `!*.d.ts` exception is listed, or the generated type declartions will not be published! Alternatively, you can use [package.json "files"](https://docs.npmjs.com/cli/configuring-npm/package-json#files) to create an opt-in list.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
By default, `npm publish` grabs (almost) everything (see [Files included in package](https://docs.npmjs.com/cli/commands/npm-publish#files-included-in-package)). In order to keep your published package minimal (see the "Heaviest Objects in the Universe" meme about `node_modules`), you want to exclude certain files (like tests and test fixtures) from from packaging. Add these to the opt-out list specified in [`.npmignore`](https://docs.npmjs.com/cli/using-npm/developers#keeping-files-out-of-your-package); ensure the `!*.d.ts` exception is listed, or the generated type declartions will not be published! Alternatively, you can use [package.json "files"](https://docs.npmjs.com/cli/configuring-npm/package-json#files) to create an opt-in list.
By default, `npm publish` grabs (almost) everything (see [Files included in package](https://docs.npmjs.com/cli/commands/npm-publish#files-included-in-package)). In order to keep your published package minimal (see the "Heaviest Objects in the Universe" meme about `node_modules`), you want to exclude certain files (like tests and test fixtures) from from packaging. Add these to the opt-out list specified in [`.npmignore`](https://docs.npmjs.com/cli/using-npm/developers#keeping-files-out-of-your-package); ensure the `!*.d.ts` exception is listed, or the generated type declartions will not be published! Alternatively, you can use [package.json "files"](https://docs.npmjs.com/cli/configuring-npm/package-json#files) to create an opt-in list - but if a mistake is made and a file accidentally omitted, your package may be broken for downstream users, so this is a less safe option.

@JakobJingleheimer
Copy link
Member Author

Thanks! I'll open a follow-up soon (on quasi-holiday til this weekend, so not sure if/when I'll have time before then).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.