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"