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

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

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 Jan 15, 2025 9:59pm

@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
@JakobJingleheimer
Copy link
Member Author

I have some tsconfig feedback.

Thank you! TIL several things 😁

is it worth mentioning anything about package.json fields (i.e. that they should reference .js files, not .ts files)?

Oh! Yes!

"name": "example-ts-pkg",
"scripts": {
"test": "node --test './src/**/*.test.ts'",
"types:check": "tsc --noEmit"

Choose a reason for hiding this comment

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

I noted this in the other thread, but I would be cautious about this as a default; I really only see people setting noEmit when they're doing a quick check, or are using a bundler or something.

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 the type "test" command. Why would you want to emit a compilation?

Maybe the names could be better? When I have unit and end-to-end tests with different setups, I split those into different commands like:

  • test:unit
  • test:e2e

So in that scenario, it could make sense to name types:checktest:types.

But in the sample, there's no differentiation between units and e2e, so then what do I call what is currently test?

Choose a reason for hiding this comment

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

I mean, I guess it's fine, I am just wary of cases where tsc and tsc --noEmit output different errors because the former is doing more. Maybe you'd hit it on prepack and that's okay, but it's a little unfortunate to only hit an error when you go to release...

Copy link
Member Author

Choose a reason for hiding this comment

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

Is that likely? I've been doing this for years and never encountered that—am I just very lucky?

Choose a reason for hiding this comment

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

I'm not sure how to gauge "likely", probably unlikely, but they're cases like "tsc failed to write the files", along with potentially some declaration transform errors. (The latter shouldn't actually end up mattering by my reading of the code, though.)

Copy link
Member

Choose a reason for hiding this comment

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

The specific issues that come up that I can think of are when a declaration file can't reliably be generated because doing so might require referencing entities that are private or non-exported. Trying to figure out why this error is happening can be pretty frustrating, especially if you've been relying a specific pattern over time. Having a divergence between publish/CI probably just makes this even more confusing since most people outside of the person who set up the build won't be aware of any differences.

Copy link
Member Author

Choose a reason for hiding this comment

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

tsc failed to write the files

That does sound like an issue that you legitimately wouldn't catch with a dry-run, but also seems a very unlikely issue (and one that could still occur at publishing even if you were using non-dry-run for the test step). It seems an edge-case worth noting but not worth taking a hit every time to avoid something that likely will never happen (if it does, there are a very limited number of causes—two? permissions and storage availability).

The specific issues that come up that I can think of are when a declaration file can't reliably be generated because doing so might require referencing entities that are private or non-exported.

That sounds very detectable for --noEmit; if it doesn't do that, that sounds like a defect in tsc? Why would you need to writing to disk in order to discover it?


But maybe let's take a step back for a second: The reason I wrote the setup this way is because of performance—but perhaps my information is outdated. Last I heard, tsc --noEmit was significantly faster than with emit.

Choose a reason for hiding this comment

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

That sounds very detectable for --noEmit; if it doesn't do that, that sounds like a defect in tsc? Why would you need to writing to disk in order to discover it?

I think as of recent releases (since --isolatedDeclarations), we've actually always checked declaration errors without emit, so I think the only errors one can see differently are just the errors that happen while writing, which may not really be important except in the case where you've somehow accidentally marked output paths as readonly in the FS or something.

But maybe let's take a step back for a second: The reason I wrote the setup this way is because of performance—but perhaps my information is outdated. Last I heard, tsc --noEmit was significantly faster than with emit.

It can be, though I think at the scale of this demo, it's definitely not a big difference.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is the take-away then that what I have is fine? There is a potentially (and probably likely) significant perf savings, and there're basically no type-related errors this won't catch?


# Publishing a TypeScript project

This article augments TypeScript's own [Publishing guide](https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html) with specifics for native node support.

Choose a reason for hiding this comment

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

I am sort of questioning linking to this page; reading it, it's pretty out of date and is part of the declaration file section, so sort of misses out on other important stuff.

Copy link
Member Author

Choose a reason for hiding this comment

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

Happy to change it to a different one—is there one you had in mind?


- name: Publish with provenance
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Copy link
Member

Choose a reason for hiding this comment

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

Is NPM_TOKEN automatically provided somehow? I think we should mention how to get this or avoid automated publishing accordingly. having a separate guide on automated publishing is worthwhile.

Copy link
Member

Choose a reason for hiding this comment

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

t's not provide automatically. But this GA example from https://docs.npmjs.com/generating-provenance-statements#example-github-actions-workflow

Copy link
Member Author

@JakobJingleheimer JakobJingleheimer Jan 15, 2025

Choose a reason for hiding this comment

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

Would we have enough content for a dedicated guide on automated publishing?

For now, I think let's add a note explaining what the token is and where it comes from: 0f7f993

Copy link
Member

Choose a reason for hiding this comment

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

+1 for note

Copy link
Member

Choose a reason for hiding this comment

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

There are a ton of problems with automated publishing (discussed above) and I think a separate guide that can fully explain the pros and cons is definitely warranted.

Copy link
Member Author

@JakobJingleheimer JakobJingleheimer Jan 15, 2025

Choose a reason for hiding this comment

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

In that case, sure, happy to do subsequently (subsequently because otherwise we end up with a web of ½ finished, inter-dependent guides, and this doesn't seem like a show-stopper—unless someone strongly thinks that it needs to happen first).

Copy link
Member

Choose a reason for hiding this comment

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

I've made the case above that it would be better to give no publishing guidance prior to such a properly nuanced guide, then to provide instructions absent nuance - iow, I suggest removing this section entirely and hand-wave over publishing for the time being.

I don't think we can avoid interdependent guides; the reality is too complex to allow for a single "does everything" document imo.

Copy link
Member

Choose a reason for hiding this comment

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

why not just redirect to npm docs ?

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 for sure they'll be inter-dependent. I just want to avoid a bunch of unfinished inter-dependent guides that are stuck in a spiderman meme.

Copy link
Member

Choose a reason for hiding this comment

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

So let's remove provenance and add note or let the security team write another article about it

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.