@ajar/timescript
v1.4.0
Published
A simple time script
Readme
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.
run
npm install -D tsxadd a
devscript topackage.jsonto runtsxinwatchmode."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.
install the following
dev dependencies- typescript - the
tsccompiler for our build step - @types/node - types for node.js
- rimraf - a
rm -rfutil for node.js that works on windows too
- typescript - the
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
