diff --git a/.gitignore b/.gitignore index 9a630cb59..9edc6872c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /*.ts coverage/ dist/ +cjs/ node_modules/ !auto.config.ts /.idea/ @@ -10,4 +11,4 @@ node_modules/ # Other package managers pnpm-lock.yaml -package-lock.json +package-lock.json \ No newline at end of file diff --git a/build/pkg-version.js b/build/pkg-version.js new file mode 100644 index 000000000..82d7d7e0e --- /dev/null +++ b/build/pkg-version.js @@ -0,0 +1,18 @@ +// Having the same source code to work with Node16's ESM and Node's CJS is a pain. +// this script simply copies the packageJson version inside the main ts-json-schema-generator.js file + +import fs from "node:fs"; +import path from "node:path"; +import pkg from "../package.json" with { type: "json" }; + +function replaceVersion(source, version) { + return source.replace(/const pkgVersion = "0.0.0";/, `const pkgVersion = "${version}";`); +} + +function replaceFile(path) { + const source = fs.readFileSync(path, "utf-8"); + fs.writeFileSync(path, replaceVersion(source, pkg.version)); +} + +replaceFile(path.resolve("cjs/ts-json-schema-generator.js")); +replaceFile(path.resolve("dist/ts-json-schema-generator.js")); diff --git a/eslint.config.js b/eslint.config.js index 1963ae3ee..74c0e841a 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -6,7 +6,7 @@ import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended" /** @type {import('@types/eslint').Linter.FlatConfig[]} */ export default tseslint.config( { - ignores: ["dist"], + ignores: ["dist", "cjs", "build"], }, eslint.configs.recommended, { diff --git a/package.json b/package.json index 4598454e9..7c0b15eba 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "2.0.0", "description": "Generate JSON schema from your Typescript sources", "main": "dist/index.js", + "module": "dist/index.js", "types": "dist/index.d.ts", "type": "module", "bin": { @@ -10,6 +11,7 @@ }, "files": [ "dist", + "cjs", "src", "factory", "index.*", @@ -44,6 +46,10 @@ "engines": { "node": ">=18.0.0" }, + "exports": { + "import": "./dist/index.js", + "require": "./cjs/index.js" + }, "dependencies": { "@types/json-schema": "^7.0.15", "commander": "^12.0.0", @@ -51,6 +57,7 @@ "json5": "^2.2.3", "normalize-path": "^3.0.0", "safe-stable-stringify": "^2.4.3", + "tslib": "^2.6.2", "typescript": "^5.4.5" }, "devDependencies": { @@ -84,7 +91,10 @@ }, "scripts": { "prepublishOnly": "yarn build", - "build": "tsc", + "build": "npm run build:cjs && npm run build:esm", + "build:cjs": "tsc -p tsconfig.cjs.json", + "build:esm": "tsc -p tsconfig.json", + "postbuild": "node build/pkg-version.js", "watch": "tsc -w", "lint": "eslint", "format": "eslint --fix", diff --git a/ts-json-schema-generator.ts b/ts-json-schema-generator.ts index 8b28fc575..9cd456e5a 100644 --- a/ts-json-schema-generator.ts +++ b/ts-json-schema-generator.ts @@ -1,12 +1,14 @@ +import { mkdirSync, writeFileSync } from "node:fs"; +import { dirname } from "node:path"; import { Command, Option } from "commander"; import stableStringify from "safe-stable-stringify"; import { createGenerator } from "./factory/generator.js"; -import { Config } from "./src/Config.js"; +import type { Config } from "./src/Config.js"; import { BaseError } from "./src/Error/BaseError.js"; import { formatError } from "./src/Utils/formatError.js"; -import pkg from "./package.json" with { type: "json" }; -import { dirname } from "path"; -import { mkdirSync, writeFileSync } from "fs"; + +// This constant gets replaced by the build script +const pkgVersion = "0.0.0"; const args = new Command() .option("-p, --path ", "Source file path") @@ -49,7 +51,7 @@ const args = new Command() [], ) .option("--additional-properties", "Allow additional properties for objects with no index signature", false) - .version(pkg.version) + .version(pkgVersion) .parse(process.argv) .opts(); diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 000000000..de02fb0c0 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,12 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "target": "ES2022", + "module": "CommonJS", + "moduleResolution": "Node", + "outDir": "cjs" + }, + "files": ["ts-json-schema-generator.ts", "index.ts"], + "include": ["src/**/*.ts", "factory/**/*.ts"], + "exclude": ["node_modules", "dist", "cjs"] +} diff --git a/tsconfig.json b/tsconfig.json index 6d82b123a..d0f20b4a0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,9 +22,10 @@ "pretty": true, "typeRoots": ["node_modules/@types"], "outDir": "dist", - "incremental": true + "incremental": true, + "importHelpers": true }, "files": ["ts-json-schema-generator.ts", "index.ts"], "include": ["src/**/*.ts", "factory/**/*.ts"], - "exclude": ["node_modules", "dist"] + "exclude": ["node_modules", "dist", "cjs"] }