From 39aca167fb94d7a88343ecd0765676b676760cfa Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 19 Dec 2022 15:46:31 -0300 Subject: [PATCH] build for commonjs, esm and a standalone bundle. --- .gitignore | 4 +++- build.cjs | 29 ++++++++++++++++++++--------- package.json | 10 +++++----- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 290a877..f86ec31 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,7 @@ node_modules dist yarn.lock package-lock.json -nostr.js .envrc +standalone +cjs +esm diff --git a/build.cjs b/build.cjs index d2f852d..a6a625b 100755 --- a/build.cjs +++ b/build.cjs @@ -2,24 +2,35 @@ const esbuild = require('esbuild') const alias = require('esbuild-plugin-alias') -const nodeGlobals = require('@esbuild-plugins/node-globals-polyfill').default -const buildOptions = { - entryPoints: ['index.js'], - outfile: 'nostr.js', +let common = { + entryPoints: ['index.ts'], bundle: true, - format: 'esm', plugins: [ alias({ stream: require.resolve('readable-stream') - }), - nodeGlobals({buffer: true}) + }) ], define: { window: 'self', global: 'self' }, - loader: {'.js': 'jsx'} + sourcemap: 'external' } -esbuild.build(buildOptions).then(() => console.log('build success.')) +esbuild + .build({...common, outdir: 'esm/', format: 'esm', packages: 'external'}) + .then(() => console.log('esm build success.')) + +esbuild + .build({...common, outdir: 'cjs/', format: 'cjs', packages: 'external'}) + .then(() => console.log('cjs build success.')) + +esbuild + .build({ + ...common, + outdir: 'standalone/', + format: 'iife', + globalName: 'NostrTools' + }) + .then(() => console.log('standalone build success.')) diff --git a/package.json b/package.json index d80ef19..ec0b2b7 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "type": "git", "url": "https://github.com/fiatjaf/nostr-tools.git" }, - "type": "module", + "main": "cjs/index.js", + "module": "esm/index.js", "dependencies": { "@noble/hashes": "^0.5.7", "@noble/secp256k1": "^1.5.2", @@ -25,11 +26,10 @@ "nostr" ], "devDependencies": { - "@esbuild-plugins/node-globals-polyfill": "^0.1.1", "@types/node": "^18.0.3", "@typescript-eslint/eslint-plugin": "^5.46.1", "@typescript-eslint/parser": "^5.46.1", - "esbuild": "^0.14.38", + "esbuild": "0.16.9", "esbuild-plugin-alias": "^0.2.1", "eslint": "^8.30.0", "eslint-plugin-babel": "^5.3.1", @@ -39,7 +39,7 @@ "typescript": "^4.9.4" }, "scripts": { - "prepublish": "node build.cjs", - "check-ts": "tsd && node --no-warnings --loader=esm-loader-typescript index.test-d.ts" + "build": "node build.cjs", + "prepublish": "yarn run build" } }