Skip to content

Commit 4ac21b2

Browse files
feat: add back cjs output (#1964)
* initial config * fix: lint * fix * fixed esm/cjs fuckery * fix: removed unused comment * feat: importHelpers for esm * remove build script and just read package.json file --------- Co-authored-by: Arthur Fiorette <[email protected]> Co-authored-by: Arthur Fiorette <[email protected]>
1 parent 1b27258 commit 4ac21b2

6 files changed

+34
-10
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*.ts
22
coverage/
33
dist/
4+
cjs/
45
node_modules/
56
!auto.config.ts
67
/.idea/
@@ -10,4 +11,4 @@ node_modules/
1011

1112
# Other package managers
1213
pnpm-lock.yaml
13-
package-lock.json
14+
package-lock.json

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended"
66
/** @type {import('@types/eslint').Linter.FlatConfig[]} */
77
export default tseslint.config(
88
{
9-
ignores: ["dist"],
9+
ignores: ["dist", "cjs", "build"],
1010
},
1111
eslint.configs.recommended,
1212
{

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
"name": "ts-json-schema-generator",
33
"version": "2.0.0",
44
"description": "Generate JSON schema from your Typescript sources",
5-
"main": "dist/index.js",
5+
"module": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"type": "module",
88
"bin": {
99
"ts-json-schema-generator": "./bin/ts-json-schema-generator.js"
1010
},
1111
"files": [
1212
"dist",
13+
"cjs",
1314
"src",
1415
"factory",
1516
"index.*",
@@ -44,13 +45,18 @@
4445
"engines": {
4546
"node": ">=18.0.0"
4647
},
48+
"exports": {
49+
"import": "./dist/index.js",
50+
"require": "./cjs/index.js"
51+
},
4752
"dependencies": {
4853
"@types/json-schema": "^7.0.15",
4954
"commander": "^12.0.0",
5055
"glob": "^10.3.12",
5156
"json5": "^2.2.3",
5257
"normalize-path": "^3.0.0",
5358
"safe-stable-stringify": "^2.4.3",
59+
"tslib": "^2.6.2",
5460
"typescript": "^5.4.5"
5561
},
5662
"devDependencies": {
@@ -84,7 +90,9 @@
8490
},
8591
"scripts": {
8692
"prepublishOnly": "yarn build",
87-
"build": "tsc",
93+
"build": "npm run build:cjs && npm run build:esm",
94+
"build:cjs": "tsc -p tsconfig.cjs.json",
95+
"build:esm": "tsc -p tsconfig.json",
8896
"watch": "tsc -w",
8997
"lint": "eslint",
9098
"format": "eslint --fix",

ts-json-schema-generator.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { mkdirSync, writeFileSync } from "node:fs";
2+
import { dirname } from "node:path";
13
import { Command, Option } from "commander";
24
import stableStringify from "safe-stable-stringify";
35
import { createGenerator } from "./factory/generator.js";
4-
import { Config } from "./src/Config.js";
6+
import type { Config } from "./src/Config.js";
57
import { BaseError } from "./src/Error/BaseError.js";
68
import { formatError } from "./src/Utils/formatError.js";
7-
import pkg from "./package.json" with { type: "json" };
8-
import { dirname } from "path";
9-
import { mkdirSync, writeFileSync } from "fs";
9+
import fs from "node:fs";
10+
11+
const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"));
1012

1113
const args = new Command()
1214
.option("-p, --path <path>", "Source file path")

tsconfig.cjs.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"target": "ES2022",
5+
"module": "CommonJS",
6+
"moduleResolution": "Node",
7+
"outDir": "cjs"
8+
},
9+
"files": ["ts-json-schema-generator.ts", "index.ts"],
10+
"include": ["src/**/*.ts", "factory/**/*.ts"],
11+
"exclude": ["node_modules", "dist", "cjs"]
12+
}

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
"pretty": true,
2323
"typeRoots": ["node_modules/@types"],
2424
"outDir": "dist",
25-
"incremental": true
25+
"incremental": true,
26+
"importHelpers": true
2627
},
2728
"files": ["ts-json-schema-generator.ts", "index.ts"],
2829
"include": ["src/**/*.ts", "factory/**/*.ts"],
29-
"exclude": ["node_modules", "dist"]
30+
"exclude": ["node_modules", "dist", "cjs"]
3031
}

0 commit comments

Comments
 (0)