Coder Social home page Coder Social logo

how-to-lib's Introduction

Creating a TypeScript Calculator Library

Setting up the project

  1. Create an empty directory
mkdir fantastic-calculator; cd $_
  1. Initialize a node project
npm init -y
  1. Install the dependencies
npm i -D tsup typescript @changesets/cli vitest
  1. Add an indxe.ts file. This will be the entry point of our library
mkdir src; touch $_/index.ts
  1. Add entry points to package.json
npm pkg set type="module" main="dist/index.cjs" module="dist/index.mjs" types="dist/index.d.ts"
  1. Create a tsup.config.ts. It could look like this
import { defineConfig } from "tsup";

export default defineConfig({
  entry: ["src/index.ts"],
  splitting: true,
  // sourcemap: true,
  clean: true,
  target: "es2022",
  minify: true,
  outDir: "dist",
  format: ["cjs", "esm"],
  outExtension: ({ format }) => {
    if (format === "esm") return { js: ".mjs", dts: ".d.ts" };
    return { js: ".cjs", dts: ".d.ts" };
  },
  bundle: true,
  dts: true,
});

This config will be used by tsup to build our library

  1. Add scripts to package.json
npm pkg set scripts.build="tsup" scripts.test="vitest" scripts.lint="tsc"

We use the TypeScript compiler tsc to lint our project. When running it, it will throw any error found.

  1. Add a tsconfig.json. It could look like this:
{
  "compilerOptions": {
    "target": "ES2022",
    "lib": ["ESNext"],
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "strict": true,
    "declaration": true
  }
}
  1. Finally create a first test in test/addition.test.ts and run it with npm run test

Build the dist

After having implemented the library code and export it in src/index.ts, the library can be built.

Simply run:

npm run build

Now you should see a dist directory which contains our .mjs, .cjs and .d.ts files.

Testing the library locally

npm link

Resources

how-to-lib's People

Contributors

s0h311 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.