Introduction
When using packages, one often wonders about the general process of managing package development. Today, I encountered Matt Pocock’s Blazing Fast Tips: Publishing to NPM tutorial and implemented it, also publishing a TS package based on my previous needs: edit-element.
1. Install TypeScript
- Initialize a new NPM package.
- Install the latest version of TypeScript.
- Initialize a basic TypeScript configuration file, which will generate a default
tsconfig.json
that can be modified as needed.
Finally, you can directly execute tsc file location
to transpile the .ts
files into .js
:
2. Convert Module Format
The awkward part is that the JavaScript ecosystem has two module systems: CommonJS and ES Modules. Therefore, we use the tsup package to quickly bundle the ts
files into the required format. After installation, set the script to be triggered in the NPM Scripts section of package.json
.
The setup is also very simple; just specify the location of the TS files and the types to convert, with the default output in the dist
folder. After generating different types of package files, remember to label the files corresponding to different module systems.
3. CI Process
Now you can perfectly edit index.ts
and push your package to NPM using tsup! However, the entire code release process is still not very smart. It’s best to perform a check and build on the code every time there is a code push. GitHub Actions can conveniently achieve this by creating a simple workflow file in .github/workflows
:
4. Automate Semantic Versioning Releases with Changeset GitHub Actions
With the CI process in place, we can further consider how to automate the deployment process as well. Install @changesets/cli and add a new Workflow to automate using changesets/action:
Regarding the environment variables that will be used:
GITHUB_TOKEN
- Grant necessary GitHub permissions by settingpermissions
. This variable is preset.NPM_TOKEN
- Log in to NPM > Access Tokens to set up a key for read and write access to your account, and place it in GitHub Env.
Once set up, you can run npm run changeset
anytime you want to change the project version and input the types and details of the package versions to be updated, which will automatically generate a temporary markdown file.
To push the version later, you can use npm run changeset version
, which will automatically update the package version. After committing, the previously written Publish process will detect version differences and automatically deploy to NPM and GitHub Releases.
Summary
If you prefer learning through videos, I recommend watching Matt Pocock’s Blazing Fast Tips: Publishing to NPM tutorial, but be aware that the package version is lower, and some adjustments are needed. The latest progress should be checked in the mattpocock/pkg-demo repository.
Through this tutorial, I also created my own package: edit-element, and expanded it with additional testing processes and environments for reference.