tshy - TypeScript Hybridizer

4.1.1 · active · verified Sun Apr 19

tshy (TypeScript Hybridizer) is a build tool designed to create hybrid npm packages that support both CommonJS (CJS) and ECMAScript Modules (ESM) simultaneously, primarily for Node.js environments. The current stable version is 4.1.1. It maintains an active development pace, with updates often aligning with new TypeScript releases. It differentiates itself by building the source code twice, once for ESM and once for CJS, using `tsc` (TypeScript compiler) versions 5.2+ (v4 defaults to TypeScript 6). It automatically manages the `exports` field in `package.json` to correctly point to the respective module entry points. Unlike some tools that attempt to mitigate the 'Dual Package Hazard,' tshy acknowledges it as an inherent aspect of Node.js module resolution and provides guidance on how package authors can handle potential duplicate module loading scenarios by leveraging global state rather than relying on strict singleton behavior within the module graph.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure tshy in your `package.json` to build a hybrid TypeScript package, including a simple `src/index.ts` example. Running `npm install` will trigger tshy to compile your code into both CommonJS and ESM, updating the `package.json` exports accordingly.

{
  // package.json
  "name": "my-hybrid-package",
  "version": "1.0.0",
  "description": "A package built with tshy",
  "main": "dist/commonjs/index.js",
  "type": "commonjs", // or module
  "files": [
    "dist"
  ],
  "scripts": {
    "build": "tshy",
    "prepare": "npm run build" // Runs tshy on npm install
  },
  "devDependencies": {
    "tshy": "^4.0.0",
    "typescript": "^5.2.2" // Optional: specify your TS version
  },
  "exports": {},
  "types": "dist/commonjs/index.d.ts"
}

// src/index.ts
export const greet = (name: string): string => {
  return `Hello, ${name}! This is a hybrid module built by tshy.`;
};

export class Greeter {
  private message: string;
  constructor(name: string) {
    this.message = `Greetings from ${name}.`;
  }
  sayHello(): string {
    return this.message;
  }
}

// To run this quickstart:
// 1. Create a new directory and `npm init -y`
// 2. Paste the package.json content above.
// 3. Create a `src/index.ts` file with the content above.
// 4. Run `npm install` (this will trigger the 'prepare' script).
// 5. Observe 'dist/commonjs' and 'dist/esm' directories, and updated 'exports' in package.json.

view raw JSON →