The article is read and translated fromThe 30 second guide to publishing a typescript package to npmPart of it has been revised.
This article requires you to have a certain knowledge of JS, TS and NPM. it would be better if you had written an ordinary NPM package. if not, there are many tutorials on the internet, which are very simple ~
Students who have published npm packages all know that to initiate an npm project, use it directlynpm init -y
That’s all right. What about ts? Directlytsc --init
Just. These two operations generatepackage.json
Andtsconfig.json
Documents. Then we will modify the configuration as follows:
1. Add"declaration": true
To youtsconfig.json
This line tells TypsScript to automatically generate it for you at compile timed.ts
Documents. It should be noted that when you use a private type, but this type is also part of the external API, the user of this package will not be able to infer the type of this type, and the TS compiler will also issue a warning. At this time, you only need to add before this typeexport
Just.
2. Add"types": "index.d.ts"
To youpackage.json
When others import your package, this sentence tells the TS compiler where to find the type definition file. Here.d.ts
Documents andmain
The entry points to a folder, so normally your package.json will contain the following two lines:
"main": "dist/index.js",
"types": "dist/index.d.ts"
(the above default you intsconfig.json
Configured inoutDir
Point todist
Directory)
3. Promise youdist
Directory added to.gitignore
In our code warehouse, we generally do not need to include compiled code, only need to include source code. So we are wrongdist
The catalog does version management. And this does not affect our releasedist
Directory to npm.
4. Run the build command
runtsc
All source codes can be compiled. This is very convenient, usually we can add a command topackage.json
:
"build": "tsc"
5. run npm publish.
Next is to be released to npm, inpackage.json
Add the following command:
"release": "tsc && npm publish"
I am used to using it.standard-version
coordinatecommitizen
To release the npm package, interested students can learn about it by themselves ~
If you want to debug the local package before publishing, you can use thenpm link
Command, executed at the root directory of this projectnpm link
And then execute it in the demo project to be debuggednpm link <your package name>
, and then directly introduced into the code.
JSON after the configuration is completed:
{
"name": "my-ts-lib",
"version": "1.0.0",
"description": "My npm package written in TS",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"release": "tsc && npm publish"
},
"author": "savokiss",
"license": "MIT",
"devDependencies": {
"typescript": "^3.5.3"
}
}
Json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es2017", "es7", "es6", "dom"],
"declaration": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"exclude": ["node_modules", "dist"]
}