From 59567ce1089f75d62a791b0cf18171a0053db5c9 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Fri, 8 Mar 2024 11:13:39 +0100 Subject: [PATCH 1/4] Add ESM build --- .gitignore | 3 ++- .nvmrc | 2 +- package.json | 26 +++++++++++++++++++++----- scripts/rename-esm.sh | 13 +++++++++++++ tsconfig.esm.json | 14 ++++++++++++++ tsconfig.json | 1 + 6 files changed, 52 insertions(+), 7 deletions(-) create mode 100755 scripts/rename-esm.sh create mode 100644 tsconfig.esm.json diff --git a/.gitignore b/.gitignore index 99b9e9b..04b899e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.js *.d.ts *.map +dist # Logs logs @@ -70,7 +71,7 @@ typings/ .LSOverride # Icon must end with two \r -Icon +Icon # Thumbnails ._* diff --git a/.nvmrc b/.nvmrc index dae199a..b009dfb 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -v12 +lts/* diff --git a/package.json b/package.json index 05ba330..6b99ee4 100644 --- a/package.json +++ b/package.json @@ -2,15 +2,28 @@ "name": "@metamask/safe-event-emitter", "version": "3.0.0", "description": "An EventEmitter that isolates the emitter from errors in handlers", - "main": "index.js", + "main": "dist/cjs/index.js", + "module": "dist/esm/index.js", + "types": "dist/esm/index.d.ts", + "exports": { + ".": { + "import": { + "types": "./dist/esm/index.d.ts", + "default": "./dist/esm/index.js" + }, + "require": { + "types": "./dist/cjs/index.d.ts", + "default": "./dist/cjs/index.js" + } + }, + "./package.json": "./package.json" + }, "publishConfig": { "registry": "https://registry.npmjs.org/", "access": "public" }, "files": [ - "index.js", - "index.d.ts", - "index.js.map" + "dist" ], "repository": { "type": "git", @@ -21,7 +34,10 @@ }, "scripts": { "prepublishOnly": "yarn build", - "build": "tsc --project .", + "build": "yarn build:cjs && yarn build:esm", + "build:cjs": "tsc --project .", + "build:esm": "tsc --project tsconfig.esm.json && yarn build:esm:rename", + "build:esm:rename": "./scripts/rename-esm.sh", "test": "jest", "lint": "eslint . --ext .ts,.js" }, diff --git a/scripts/rename-esm.sh b/scripts/rename-esm.sh new file mode 100755 index 0000000..526904d --- /dev/null +++ b/scripts/rename-esm.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -e +set -u +set -o pipefail + +if [[ ${RUNNER_DEBUG:-0} == 1 ]]; then + set -x +fi + +for file in dist/esm/*.js; do + mv "$file" "${file%.js}.mjs" +done diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 0000000..e2b6d0b --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "declaration": true, + "module": "ES6", + "moduleResolution": "Node", + "outDir": "dist/esm", + "sourceMap": true, + "strict": true, + "target": "ES2017" + }, + "include": [ + "./**/*.ts" + ] +} diff --git a/tsconfig.json b/tsconfig.json index c08a0fa..413414b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "declaration": true, "module": "CommonJS", + "outDir": "dist/cjs", "sourceMap": true, "strict": true, "target": "ES2017" From f33abb9d3047f68c55e305793a96934dccc5721f Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Fri, 8 Mar 2024 11:15:59 +0100 Subject: [PATCH 2/4] Update .gitignore --- .gitignore | 92 ++++++++++++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 55 deletions(-) diff --git a/.gitignore b/.gitignore index 04b899e..d54c2ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ -*.js -*.d.ts -*.map -dist +.DS_Store +dist/ +coverage/ +docs/ # Logs logs @@ -9,6 +9,10 @@ logs npm-debug.log* yarn-debug.log* yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json # Runtime data pids @@ -16,20 +20,24 @@ pids *.seed *.pid.lock -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - # Coverage directory used by tools like istanbul coverage +*.lcov # nyc test coverage .nyc_output +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + # Dependency directories node_modules/ -# TypeScript v1 declaration files -typings/ +# TypeScript cache +*.tsbuildinfo # Optional npm cache directory .npm @@ -37,6 +45,12 @@ typings/ # Optional eslint cache .eslintcache +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + # Optional REPL history .node_repl_history @@ -48,49 +62,17 @@ typings/ # dotenv environment variables file .env - -# parcel-bundler cache (https://parceljs.org/) -.cache - -# next.js build output -.next - -# nuxt.js build output -.nuxt - -# vuepress build output -.vuepress/dist - -# Serverless directories -.serverless - -### OSX ### -# General -.DS_Store -.AppleDouble -.LSOverride - -# Icon must end with two \r -Icon - -# Thumbnails -._* - -# Files that might appear in the root of a volume -.DocumentRevisions-V100 -.fseventsd -.Spotlight-V100 -.TemporaryItems -.Trashes -.VolumeIcon.icns -.com.apple.timemachine.donotpresent - -# Directories potentially created on remote AFP share -.AppleDB -.AppleDesktop -Network Trash Folder -Temporary Items -.apdisk - - -# End of https://www.gitignore.io/api/osx,node +.env.test + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v3 (w/o zero-install) +# See: https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored +.pnp.* +.yarn/* +!.yarn/patches +!.yarn/plugins +!.yarn/releases +!.yarn/sdks +!.yarn/versions From 09df896183ad8fc977e3a00aa41bab681fccd543 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Fri, 8 Mar 2024 11:20:54 +0100 Subject: [PATCH 3/4] Fix export path --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6b99ee4..0cef0b8 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ ".": { "import": { "types": "./dist/esm/index.d.ts", - "default": "./dist/esm/index.js" + "default": "./dist/esm/index.mjs" }, "require": { "types": "./dist/cjs/index.d.ts", From 28aa914c86f41876ccab8d75886f1ba358170adb Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Fri, 8 Mar 2024 11:27:32 +0100 Subject: [PATCH 4/4] Use CJS types as fallback --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0cef0b8..2196e7c 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "An EventEmitter that isolates the emitter from errors in handlers", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", - "types": "dist/esm/index.d.ts", + "types": "dist/cjs/index.d.ts", "exports": { ".": { "import": {