Skip to content

Commit

Permalink
Add ESM build (#141)
Browse files Browse the repository at this point in the history
* Add ESM build

* Update .gitignore

* Fix export path

* Use CJS types as fallback
  • Loading branch information
Mrtenz authored Mar 8, 2024
1 parent d3f56aa commit 95276c8
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 60 deletions.
91 changes: 37 additions & 54 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,41 +1,56 @@
*.js
*.d.ts
*.map
.DS_Store
dist/
coverage/
docs/

# Logs
logs
*.log
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
*.pid
*.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

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

Expand All @@ -47,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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v12
lts/*
26 changes: 21 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/cjs/index.d.ts",
"exports": {
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.mjs"
},
"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",
Expand All @@ -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"
},
Expand Down
13 changes: 13 additions & 0 deletions scripts/rename-esm.sh
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"declaration": true,
"module": "ES6",
"moduleResolution": "Node",
"outDir": "dist/esm",
"sourceMap": true,
"strict": true,
"target": "ES2017"
},
"include": [
"./**/*.ts"
]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"declaration": true,
"module": "CommonJS",
"outDir": "dist/cjs",
"sourceMap": true,
"strict": true,
"target": "ES2017"
Expand Down

0 comments on commit 95276c8

Please sign in to comment.