@asryab/timescript v1.2.0
Creating an npm package
initial files and folders
- Create a new package project in a new folder
npm init -y - Create an
srcfolder and aindex.tsfile inside it.
Running Typescript in Node.js while developing the package
We will start by installing tsx.
It uses esbuild to run typescript files on the fly in node.js with zero configuration.
1. run npm install -D tsx
2. add a dev script to package.json to run tsx in watch mode.
```json
"scripts": {
"dev": "tsx watch ./src/index.ts",
}
```- Write some typescript code in
index.tsand runnpm run devto see it in action.
Building the package
While esbuild is orders-of-magnitude faster then tsc it doesn't type-check our code.
Although there are configuration options to use tsc only for type-checking in tandam with esbuild for the actual compilation, I want to keep our config simple and not rely too much on esbuild other then spontanious zero-config development. So let's use tsc to build our package for production.
1. install the following dev dependencies
1. typescript - the tsc compiler for our build step
2. @types/node - types for node.js
3. rimraf - a rm -rf util for node.js that works on windows too
Add a
tsconfig.jsonfile to the root of the project with the following code. If you're curious about the configuration options, you can read about them here, or runtsc --initto generate atsconfig.jsonfile with comments explaining each option...{ "compilerOptions": { "target": "esnext", "module": "esnext", "outDir": "dist", "rootDir": "src", "strict": true, "noEmitOnError": true, "moduleResolution": "node", "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true }, "exclude": ["**/*.spec.ts"] }Add a
buildscript topackage.jsonto runtsc."scripts": { "build": "rimraf dist && tsc", }
unit tests and code coverage
- We'll use
vitest- a lightweight next generation drop-in replacement for jest