From c31cfc81582dd7b43a0d2e6df535af48acb58979 Mon Sep 17 00:00:00 2001 From: dsinghvi Date: Tue, 28 May 2024 14:02:57 +0200 Subject: [PATCH] (fix, template-resolver): publish ESM + CJS compatible lib --- packages/template-resolver/package.json | 12 ++++++--- .../scripts/writePackageJson.js | 25 +++++++++++++++++++ packages/template-resolver/tsconfig.cjs.json | 6 +++++ .../{tsconfig.json => tsconfig.esm.json} | 2 +- .../template-resolver/tsconfig.types.json | 6 +++++ 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 packages/template-resolver/scripts/writePackageJson.js create mode 100644 packages/template-resolver/tsconfig.cjs.json rename packages/template-resolver/{tsconfig.json => tsconfig.esm.json} (69%) create mode 100644 packages/template-resolver/tsconfig.types.json diff --git a/packages/template-resolver/package.json b/packages/template-resolver/package.json index da61e3905c..9672fbf0dc 100644 --- a/packages/template-resolver/package.json +++ b/packages/template-resolver/package.json @@ -6,11 +6,9 @@ "files": [ "dist" ], - "type": "module", - "module": "dist/src/index.js", "source": "src/index.ts", "scripts": { - "compile": "tsc --build", + "compile": "tsc --build ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json && node ./scripts/writePackageJson.js ", "clean": "rm -rf ./dist && tsc --build --clean", "format": "prettier --write --ignore-unknown --ignore-path ../../shared/.prettierignore \"**\"", "format:check": "prettier --check --ignore-unknown --ignore-path ../../shared/.prettierignore \"**\"", @@ -36,5 +34,13 @@ "organize-imports-cli": "^0.10.0", "prettier": "^2.7.1", "typescript": "5.4.3" + }, + "exports": { + ".": { + "types": "./dist/types/index.d.ts", + "require": "./dist/cjs/index.js", + "import": "./dist/esm/index.js", + "default": "./dist/esm/index.js" + } } } diff --git a/packages/template-resolver/scripts/writePackageJson.js b/packages/template-resolver/scripts/writePackageJson.js new file mode 100644 index 0000000000..ee709aa229 --- /dev/null +++ b/packages/template-resolver/scripts/writePackageJson.js @@ -0,0 +1,25 @@ +const fs = require("fs"); +const path = require("path"); + +const buildDir = "./dist"; +function createEsmModulePackageJson() { + fs.readdir(buildDir, function (err, dirs) { + if (err) { + throw err; + } + dirs.forEach(function (dir) { + if (dir === "esm") { + var packageJsonFile = path.join(buildDir, dir, "/package.json"); + if (!fs.existsSync(packageJsonFile)) { + fs.writeFile(packageJsonFile, new Uint8Array(Buffer.from('{"type": "module"}')), function (err) { + if (err) { + throw err; + } + }); + } + } + }); + }); +} + +createEsmModulePackageJson(); diff --git a/packages/template-resolver/tsconfig.cjs.json b/packages/template-resolver/tsconfig.cjs.json new file mode 100644 index 0000000000..af330e380e --- /dev/null +++ b/packages/template-resolver/tsconfig.cjs.json @@ -0,0 +1,6 @@ +{ + "extends": "@fern-platform/configs/tsconfig/library.json", + "compilerOptions": { "outDir": "./dist/cjs", "rootDir": ".", "module": "commonjs", "moduleResolution": "node" }, + "include": ["./src/**/*"], + "references": [{ "path": "../commons/core-utils" }] +} diff --git a/packages/template-resolver/tsconfig.json b/packages/template-resolver/tsconfig.esm.json similarity index 69% rename from packages/template-resolver/tsconfig.json rename to packages/template-resolver/tsconfig.esm.json index daace200d4..0e87e1ab12 100644 --- a/packages/template-resolver/tsconfig.json +++ b/packages/template-resolver/tsconfig.esm.json @@ -1,6 +1,6 @@ { "extends": "@fern-platform/configs/tsconfig/library.json", - "compilerOptions": { "outDir": "./dist", "rootDir": "." }, + "compilerOptions": { "outDir": "./dist/esm", "rootDir": "." }, "include": ["./src/**/*"], "references": [{ "path": "../commons/core-utils" }] } diff --git a/packages/template-resolver/tsconfig.types.json b/packages/template-resolver/tsconfig.types.json new file mode 100644 index 0000000000..d633773aa3 --- /dev/null +++ b/packages/template-resolver/tsconfig.types.json @@ -0,0 +1,6 @@ +{ + "extends": "@fern-platform/configs/tsconfig/library.json", + "compilerOptions": { "outDir": "./dist/types", "declaration": true, "emitDeclarationOnly": true }, + "include": ["./src/**/*"], + "references": [{ "path": "../commons/core-utils" }] +}