npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kjn/tsc-dual-build

v1.1.0

Published

Quick and easy way to publish your TypeScript code as both Esmodule and CommonJS module

Downloads

5

Readme

@kjn/tsc-dual-build

semantic-release: angular

npm version

Helps you build ESModules and CommonJS modules with ease.

HOW-TO-USE

Install script using npm

npm i @kjn/tsc-dual-build@latest

Execute dual-build

npx tsc-dual-build ./tsconfig.json

Recommended

Update package.json > scripts > build

"scripts": {
  "build": "tsc-dual-build tsconfig.json"
}

Now you can simply npm run build to execute a dual build

> tsc-dual-build

2️⃣  tsc-dual-build <tsConfigFile>
🔨 tsc-build
  module: "es2022"
  outDir: "./dist/esm"
🔨 tsc-build
  module: "commonjs"
  outDir: "./dist/cjs"
⚙️ Exporting package.json
  created "dist/esm/package.json"
  created "dist/cjs/package.json"
🏁 tsc-dual-build finished

About

Will compile the following project structure

./src
  /**.*ts
./package.json
./tsconfig.json

into something like this

./dist
  /esm
    index.js
    package.json
  /cjs
    index.js
    package.json
  types.d.ts

The idea is that we'll create 2 seperate folders, 1 for ESM and 1 for CJS. In both these folders we'll place a local package.json file that contains the configuration solely for the module syntax of the directory.

e.g. ./dist/esm/package.json contains

  • type: "module"
  • main: "./dist/esm/index.js"

whereas ./dist/cjs/package.json contains

  • type: "commonjs"
  • main: "./dist/commonjs/index.js".

Additionally we assert that the root package.json contains the correct configuration to handle both imports and requires by enforcing the exports key to be present and setup correctly.

For tsc-dual-build to work you'd need 2 things

  • extend your tsconfig.json with the required properties
  • assert package.json contains the correct entry points for import and require

tsconfig.json

{
  "compilesOptions": {
    /** placeholder */
  },
  "include": [
    /** placeholder */
  ],

  /** Recommended setup */
  "tscDualBuild": {
    "esm": {
      "module": "es2022",
      "outDir": "./dist/esm"
    },
    "cjs": {
      "module": "commonjs",
      "outDir": "./dist/cjs"
    },
    "types": {
      "outDir": "./dist"
    }
  }
}

The (recommended) setup above will built

  • your ECMAScript module using es2022 syntax of tsconfig. and will write its output to the folder ./dist/esm/\*\*
  • your CommonJS module using CommonJS syntax of tsconfig. and will write its output to the folder ./dist/cjs/\*\*

The module and outDir properties are fully customisable aslong as they're valid tsconfig settings.

package.json

For the tsconfig.json above we can configure the root package.json as follows:

{
  "name": "<placeholder>",
  "version": "<placeholder>",
  "dependencies": {
    // <placeholder>
  },
  // ..
  // ..
  // etc..
  // ..
  "exports": {
    "import": "./dist/esm/index.js",
    "require": "./dist/cjs/index.js",
  },
  "types": "./dist/index.d.ts
}

Limitations

This project was build with a specific structure and setup in mind, therefore it might not be the golden hammer you're looking for.

Right now we're only supporting DUAL exports. There is simply no need to support more exports as most modern applications require either CommonJS OR ESM modules. Additionally at the time of writing we don't really care about old systems and many edge-cases that come with.

Another limitation that limits the use of this project would be that this setup assumes that there is only a single entry point for the codebase. IF you need more entry points this project won't work out of the box for you.