@fgiova/cjs-esm-ts-builder
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

Typescript CommonJS and ESM Builder for Node.js

GitHub license NPM version CI workflow TypeScript

Description

This module is a Typescript CommonJS and ESM Builder for Node.js. It provides a simple way to build your Typescript projects as CommonJS and ESM modules. It uses tsc and default tsconfig.json to build the project then rewrote import and export of esm files adding .mjs extension and indexes.

Install

npm i @fgiova/cjs-esm-ts-builder

CLI usage

npx @fgiova/cjs-esm-ts-builder

or

npm install --save-dev @fgiova/cjs-esm-ts-builder
esmBuilder

Cli options

  • --target: The target version of ECMAScript to compile to. Default is es2020.
  • --esmFolder: The folder where the ESM files will be generated. Default is esm.
  • --cjsFolder: The folder where the CommonJS files will be generated. Default is cjs.
  • --buildFolder: The folder where the build files will be generated. Default is dist.
  • --help: Show help message.

Programmatic usage

const { build } = require("@fgiova/cjs-esm-ts-builder");

const options = {
    target: "es2020",
    esmFolder: "esm",
    cjsFolder: "cjs",
    buildFolder: "dist"
};

build(options)
    .then(() => {
        console.log("Build completed");
    })
    .catch((err) => {
        console.error(err);
    });

Configure package.json

{
    ...
    "main": "./dist/cjs/index.js",
    "types": "./dist/cjs/index.d.ts",
    "module": "./dist/esm/index.mjs",
    "exports": {
        ".": {
            "import": {
                "types": "./dist/esm/index.d.mts",
                "default": "./dist/esm/index.mjs"
            },
            "require": {
                "types": "./dist/cjs/index.d.ts",
                "default": "./dist/cjs/index.js"
            }
        }
    },
    "files": [
        "dist"
    ],
    ...
}

Options

  • target: The target version of ECMAScript to compile to. Default is es2020.
  • esmFolder: The folder where the ESM files will be generated. Default is esm.
  • cjsFolder: The folder where the CommonJS files will be generated. Default is cjs.
  • buildFolder: The folder where the build files will be generated. Default is dist.

Notes

  • Tested with Node.js >= 20
  • Work only on *nix systems (Linux, MacOS, etc.)

License

Licensed under MIT.

Package Sidebar

Install

npm i @fgiova/cjs-esm-ts-builder

Weekly Downloads

7

Version

1.2.0

License

MIT

Unpacked Size

36.7 kB

Total Files

36

Last publish

Collaborators

  • fgiova_user