Skip to content

Support publishing @typescript/sandbox package to NPM #2437

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

Merged
merged 4 commits into from
Jul 1, 2022

Conversation

oliverdunk
Copy link
Contributor

Overview

I was recently working on a project (https://github.com/oliverdunk/web-extension-playground) and to use the @typescript/sandbox package, I had to load it from the CDN and then vendor the types: https://github.com/oliverdunk/web-extension-playground/blob/main/src/utils/sandbox.ts#L39. I spoke to @andrewbranch about this, and he encouraged me to open a PR making this package publishable to NPM.

Description

The changes here are fairly simple:

  • Use tsdx (already used by @typescript/vfs) to build both a CJS and ESM bundle
  • Add @rollup/plugin-commonjs to allow lzstring to be correctly loaded
  • Update the package.json file to add more useful metadata for NPM

A potentially surprising change here is the rename from releases.ts to release_data.ts. Since there was both releases.ts and releases.json in the same directory, Rollup was resolving import ... from "./releases as a JSON import and failing the build. I looked at solving this but it ended up with a lot of deep configuration overriding to prefer TS files over JSON and didn't feel worth the effort when I could easily rename one of the files.

Publishing to NPM

I did a bit of investigation to see how packages are published to NPM and what setup would be needed. This appears to be handled by a package called pleb: https://github.com/microsoft/TypeScript-Website/blob/v2/.github/workflows/v2-merged-staging.yml#L65. As far as I can tell, simply removing the private field from the package.json (as done here) should be enough to get things going.

Note: The README mentioned wanting tests to ensure a packaged build wouldn't break. I'm not quite sure what that would look like so I haven't done it here. I'm happy to add them if it's something we want and there is a clear idea about how to do it. At the same time, I can't see anything like that for the other packages so I feel like it might be fine without.

@ghost
Copy link

ghost commented Jun 30, 2022

CLA assistant check
All CLA requirements met.

@orta
Copy link
Contributor

orta commented Jun 30, 2022

Yeah, deploy infra doesn't need tests 👍🏻

Structurally, this looks fine and because the sandbox is independent of the typescript version then we don't to find a way to make it update with each TS version update, so this PR should be cool to go.

That said, I had been recently wondering about moving this repo in the opposite direction and try to remove deployment to npm from the TS website repo. Since I left there's basically been no documentation update PRs merged and the more deployment infra on this repo means the less chance of me being able to get #2286 merged so that we can get ~80 PRs since feb upstreamed.

Though the sandbox isn't in too active development, so its doesn't hurt to send it off an npm deploy anyway while that is still getting figured out

"url": "https://github.com/microsoft/TypeScript-Website/issues"
},
"main": "./dist/index.js",
"module": "./dist/sandbox.esm.js",
Copy link
Member

Choose a reason for hiding this comment

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

Did you know that module is something bundlers made up out of thin air? 🙃 It’s ok to keep it, but we should use the new standard exports too:

"exports": {
  ".": {
    "types": "./dist/index.d.ts",
    "import": "./dist/sandbox.esm.js",
    "default": "./dist/index.js"
  }
}

This is assuming that the index.d.ts file works equally well for the ESM and CJS file, which is probably true if there’s no default export. If there are appreciable differences, there should be a separate sandbox.esm.d.ts. (By the way, why not just sandbox.mjs or index.mjs for that?)

Copy link
Contributor

Choose a reason for hiding this comment

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

This is likely to keep it consistent with the rest of the packages in this repo, it's safe to say that tsdx has not kept up with the times: https://github.com/jaredpalmer/tsdx/releases

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have to admit, my experience publishing packages to NPM is fairly limited! I didn't know about that key so good to know. Left the file extensions to keep things consistent per @orta's comment.

@andrewbranch
Copy link
Member

I can reproduce the same build error that CI is getting:

(commonjs plugin) Error: Insufficient rollup version: "@rollup/plugin-commonjs" requires at least [email protected] but found [email protected].

@oliverdunk
Copy link
Contributor Author

I can reproduce the same build error that CI is getting:

(commonjs plugin) Error: Insufficient rollup version: "@rollup/plugin-commonjs" requires at least [email protected] but found [email protected].

Super strange! This continued to work for me locally until I re-cloned, so I'm not sure what happened there. Sticking to the theme of consistency I've continued using tsdx but downgraded that package to the most recent compatible version.

I've also added the exports key, hopefully the tests will pass. This should be good for another look!

@oliverdunk
Copy link
Contributor Author

Pushed some fixes for conflicting types, and also updated create-typescript-playground-plugin. While that plugin could arguably use @typescript/sandbox from NPM now, I think that would be a good follow-up PR. It would be nice to wait until the package is live and there are existing issues that mean it may need additional work: #2441

@andrewbranch
Copy link
Member

A question for @orta or @oliverdunk; if we merge this and I want to verify that it didn’t break the site, what should I look for? I assume the Playground would be totally busted?

@orta
Copy link
Contributor

orta commented Jul 1, 2022

Yeah, the playground wouldn't build I imagine - all the references which are changed are type ones, so I'd expect CI would raise that. If the playground runs on staging, then we've definitely not broke web consumers

@oliverdunk
Copy link
Contributor Author

That sounds reasonable to me. @orta, I assume since you didn't mention it the releases.ts rename seems ok? It doesn't feel impossible that people were depending on that file location to load releases data, but at the same time that seems like a slightly odd thing to do (especially with a JSON equivalent existing) - and I don't know if the location and contents of a non-index JS file are something that should be expected to stay the same 🤔

I can look at changing that back if we want to, I'm 50/50 on it. It just got fiddly with tsdx and rollup...

@orta
Copy link
Contributor

orta commented Jul 1, 2022

Yeah, I've always recommend folks use the CDN URLs for that data: https://typescript.azureedge.net/indexes/releases.json - I think the path for that file can generally be considered internal details

@andrewbranch andrewbranch merged commit d2e4740 into microsoft:v2 Jul 1, 2022
@andrewbranch
Copy link
Member

Lol

image

@oliverdunk
Copy link
Contributor Author

Is the budget getting low? 😜 You may have seen this but it looks like we can either publish once from the command line or add something to the package.json to explicitly make it public: https://stackoverflow.com/questions/41981686/getting-error-402-while-publishing-package-using-npm

I wonder if we could also just create a project manually in the account but not sure about that, and maybe you don't have easy access to the credentials...

@andrewbranch
Copy link
Member

Oh yeah, for personal packages I’ve always added the publishConfig. I could dig through the keyvaults for the token and run some manual stuff, but I’d rather not as that seems more error prone. Mind opening a follow-up PR?

@orta
Copy link
Contributor

orta commented Jul 1, 2022

add:

  "publishConfig": {
    "access": "public"
  }

I tried to get the error message to note this in npm, but it didn't get much momentum: npm/feedback#583

@oliverdunk
Copy link
Contributor Author

PR is here: #2442

@oliverdunk
Copy link
Contributor Author

It's live (https://www.npmjs.com/package/@typescript/sandbox)! Thank you both so much for the help here. I really appreciate how quickly you were able to help me merge it.

oliverdunk added a commit to oliverdunk/web-extension-playground that referenced this pull request Jul 1, 2022

Verified

This commit was signed with the committer’s verified signature.
oliverdunk Oliver Dunk
I was able to get this published in
microsoft/TypeScript-Website#2437 which
means we no longer need to vendor the types or load the
script from a CDN.
@andrewbranch
Copy link
Member

Thanks @oliverdunk!

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.

None yet

3 participants