From 94fdd6ff08d632b91bce18965ca4b38dc612ae43 Mon Sep 17 00:00:00 2001 From: James Yeoman Date: Wed, 6 Mar 2024 12:30:57 +0000 Subject: [PATCH] Provide a CJS build --- package.json | 14 +++++++++++--- test/esbuild.js | 2 +- tsconfig.cjs.json | 15 +++++++++++++++ tsconfig.esm.json | 8 ++++++++ tsconfig.build.json => tsconfig.types.json | 7 +++---- 5 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 tsconfig.cjs.json create mode 100644 tsconfig.esm.json rename tsconfig.build.json => tsconfig.types.json (56%) diff --git a/package.json b/package.json index 511e4fc..3176835 100644 --- a/package.json +++ b/package.json @@ -18,14 +18,22 @@ "license": "MIT", "author": "Robin Löffel (https://robinloeffel.ch) ", "type": "module", - "main": "dist/index.js", - "types": "dist/index.d.ts", + "main": "dist/esm/index.js", + "types": "dist/types/index.d.ts", + "exports": { + "types": "./dist/types/index.d.ts", + "require": "./dist/cjs/index.js", + "default": "./dist/esm/index.js" + }, "files": [ "src", "dist" ], "scripts": { - "build": "rm -rf dist && tsc --project tsconfig.build.json", + "build": "rm -rf dist && concurrently npm:build:*", + "build:cjs": "tsc --project tsconfig.cjs.json", + "build:esm": "tsc --project tsconfig.esm.json", + "build:types": "tsc --project tsconfig.types.json", "prepack": "npm run build", "start": "npm run watch", "watch": "concurrently npm:watch:*", diff --git a/test/esbuild.js b/test/esbuild.js index 2a59be2..d5b717f 100644 --- a/test/esbuild.js +++ b/test/esbuild.js @@ -1,5 +1,5 @@ import { build } from "esbuild"; -import eslint from "../dist/index.js"; +import eslint from "../dist/esm/index.js"; const absolute = relative => new URL(relative, import.meta.url).pathname; diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..a8410b5 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,15 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "noEmit": false, + "outDir": "./dist/cjs", + + // Enable CJS output + "module": "CommonJS", + "moduleResolution": "Node", + + // verbatimModuleSyntax doesn't transform import to require + "verbatimModuleSyntax": false + }, + "include": ["./src"] +} diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 0000000..622d986 --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "noEmit": false, + "outDir": "./dist/esm" + }, + "include": ["./src"] +} diff --git a/tsconfig.build.json b/tsconfig.types.json similarity index 56% rename from tsconfig.build.json rename to tsconfig.types.json index bf59e32..7b25786 100644 --- a/tsconfig.build.json +++ b/tsconfig.types.json @@ -3,9 +3,8 @@ "compilerOptions": { "noEmit": false, "declaration": true, - "outDir": "./dist" + "emitDeclarationOnly": true, + "outDir": "./dist/types" }, - "include": [ - "./src" - ] + "include": ["./src"] }