From e886df7be7cc8ce0587ac6be1e51e06e4b36c3f6 Mon Sep 17 00:00:00 2001 From: Moe Jangda Date: Fri, 13 Oct 2023 17:10:15 -0500 Subject: [PATCH] update how buffer is polyfilled to match approach taken by other packages in this repo --- package-lock.json | 1 + packages/dids/build/buffer-polyfill.cjs | 3 --- packages/dids/build/esbuild-browser-config.cjs | 18 +++++++++++++++++- packages/dids/package.json | 1 + 4 files changed, 19 insertions(+), 4 deletions(-) delete mode 100644 packages/dids/build/buffer-polyfill.cjs diff --git a/package-lock.json b/package-lock.json index c5803604c..e6267fa5d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10795,6 +10795,7 @@ "eslint": "8.47.0", "karma": "6.4.1", "mocha": "10.2.0", + "node-stdlib-browser": "1.2.0", "rimraf": "4.4.0", "sinon": "15.0.2", "typescript": "5.1.6" diff --git a/packages/dids/build/buffer-polyfill.cjs b/packages/dids/build/buffer-polyfill.cjs deleted file mode 100644 index 8bd6256c2..000000000 --- a/packages/dids/build/buffer-polyfill.cjs +++ /dev/null @@ -1,3 +0,0 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const { Buffer } = require('buffer'); -global.Buffer = Buffer; diff --git a/packages/dids/build/esbuild-browser-config.cjs b/packages/dids/build/esbuild-browser-config.cjs index 599cae57b..bbd62fbcb 100644 --- a/packages/dids/build/esbuild-browser-config.cjs +++ b/packages/dids/build/esbuild-browser-config.cjs @@ -1,3 +1,18 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const polyfillProviderPlugin = require('node-stdlib-browser/helpers/esbuild/plugin'); +const stdLibBrowser = require('node-stdlib-browser'); + +const requiredPolyfills = new Set(['buffer']); + +// populate object containing lib -> polyfill path +const polyfills = {}; +for (let lib in stdLibBrowser) { + if (requiredPolyfills.has(lib)) { + const polyfill = stdLibBrowser[lib]; + polyfills[lib] = polyfill; + } +} + /** @type {import('esbuild').BuildOptions} */ module.exports = { entryPoints : ['./src/index.ts'], @@ -7,7 +22,8 @@ module.exports = { minify : true, platform : 'browser', target : ['chrome101', 'firefox108', 'safari16'], - inject : ['./build/buffer-polyfill.cjs'], + inject : [require.resolve('node-stdlib-browser/helpers/esbuild/shim')], + plugins : [polyfillProviderPlugin(polyfills)], define : { 'global': 'globalThis', }, diff --git a/packages/dids/package.json b/packages/dids/package.json index cac90d14c..2fd318563 100644 --- a/packages/dids/package.json +++ b/packages/dids/package.json @@ -102,6 +102,7 @@ "eslint": "8.47.0", "karma": "6.4.1", "mocha": "10.2.0", + "node-stdlib-browser": "1.2.0", "rimraf": "4.4.0", "sinon": "15.0.2", "typescript": "5.1.6"