From 885e56f5e153297da9e012ba572981697eadd029 Mon Sep 17 00:00:00 2001 From: Marcelo Luiz Onhate Date: Sat, 2 Mar 2024 14:32:38 -0300 Subject: [PATCH] feat: esm + cjs --- package.json | 25 +++++++++++++++++-------- postbuild.sh | 8 ++++++++ tsconfig.json => tsconfig.base.json | 5 ++--- tsconfig.cjs.json | 8 ++++++++ tsconfig.dts.json | 8 ++++++++ tsconfig.esm.json | 8 ++++++++ 6 files changed, 51 insertions(+), 11 deletions(-) create mode 100755 postbuild.sh rename tsconfig.json => tsconfig.base.json (50%) create mode 100644 tsconfig.cjs.json create mode 100644 tsconfig.dts.json create mode 100644 tsconfig.esm.json diff --git a/package.json b/package.json index e24df65..e4d63b6 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,32 @@ { "name": "datelative", - "version": "0.0.3", + "version": "0.0.5", "description": "A library that converts relative string text to JavaScript dates", - "main": "index.js", - "types": "index.d.ts", - "type": "module", + "exports": { + ".": { + "types": "./dist/dts/index.d.ts", + "require": "./dist/cjs/index.js", + "import": "./dist/esm/index.js", + "default": "./dist/esm/index.js" + } + }, + "scripts": { "test": "node --test", "test:watch": "node --test --watch", - "prepublishOnly": "npx tsc" + "prepublishOnly": "npm run build", + "clear": "rm -rf dist/*", + "prebuild": "npm run clear", + "build": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.dts.json", + "postbuild": "./postbuild.sh" }, + "repository": { "type": "git", "url": "git+https://github.com/onhate/datelative.git" }, "files": [ - "index.js", - "index.d.ts", - "index.d.ts.map", + "dist/*", "README.md", "package.json" ], diff --git a/postbuild.sh b/postbuild.sh new file mode 100755 index 0000000..cf41689 --- /dev/null +++ b/postbuild.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +## So entire idea of this script is to generate separate package.json for ESM build (under build/esm directory) with the following content: +# { "type": "module" } +# This will tell the consumer build system that the underlying directory has modern EcmaScript modules. Otherwise, it will complain with: +# SyntaxError: Unexpected token 'export' + +echo '{ "type": "module" }' > dist/esm/package.json \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.base.json similarity index 50% rename from tsconfig.json rename to tsconfig.base.json index 712ee16..7c481a8 100644 --- a/tsconfig.json +++ b/tsconfig.base.json @@ -4,8 +4,7 @@ ], "compilerOptions": { "allowJs": true, - "declaration": true, - "emitDeclarationOnly": true, - "declarationMap": true + "pretty": true, + "strict": true } } \ No newline at end of file diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..7b18662 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "dist/cjs", + "target": "es2015" + } +} \ No newline at end of file diff --git a/tsconfig.dts.json b/tsconfig.dts.json new file mode 100644 index 0000000..2ea5290 --- /dev/null +++ b/tsconfig.dts.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "outDir": "dist/dts", + "declaration": true, + "emitDeclarationOnly": true + } +} \ No newline at end of file diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 0000000..f2923ba --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "module": "esnext", + "outDir": "dist/esm", + "target": "esnext" + } +} \ No newline at end of file