From ff09f82f819fc6185fbc5a842267fc9cffadd711 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 16 Sep 2020 16:58:35 +0200 Subject: [PATCH] add bs58 as a dependency override some of its imports as they would just add a lot of nodejs fluff that needs stubbing out for a browser. --- package.json | 1 + scripts/package-overrides/buffer/index.js | 6 +++++ .../package-overrides/safe-buffer/index.js | 1 + scripts/post-install.mjs | 26 +++++++++++++++++-- yarn.lock | 19 ++++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 scripts/package-overrides/buffer/index.js create mode 100644 scripts/package-overrides/safe-buffer/index.js diff --git a/package.json b/package.json index 9e22569a..7faaea2c 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ }, "dependencies": { "another-json": "^0.2.0", + "bs58": "^4.0.1", "olm": "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz" } } diff --git a/scripts/package-overrides/buffer/index.js b/scripts/package-overrides/buffer/index.js new file mode 100644 index 00000000..951b344b --- /dev/null +++ b/scripts/package-overrides/buffer/index.js @@ -0,0 +1,6 @@ +module.exports = class Buffer { + static isBuffer() {return false;} + static from(arrayBuffer) {return arrayBuffer;} + static allocUnsafe(size) {return Buffer.alloc(size);} + static alloc(size) {return new Uint8Array(size);} +}; diff --git a/scripts/package-overrides/safe-buffer/index.js b/scripts/package-overrides/safe-buffer/index.js new file mode 100644 index 00000000..93a10446 --- /dev/null +++ b/scripts/package-overrides/safe-buffer/index.js @@ -0,0 +1 @@ +module.exports.Buffer = require("buffer"); diff --git a/scripts/post-install.mjs b/scripts/post-install.mjs index 328f8c7c..16332c5d 100644 --- a/scripts/post-install.mjs +++ b/scripts/post-install.mjs @@ -22,18 +22,34 @@ import { fileURLToPath } from 'url'; import { dirname } from 'path'; // needed to translate commonjs modules to esm import commonjs from '@rollup/plugin-commonjs'; -// multi-entry plugin so we can add polyfill file to main +import { nodeResolve } from '@rollup/plugin-node-resolve'; import {removeDirIfExists} from "./common.mjs"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const projectDir = path.join(__dirname, "../"); +/** function used to resolve common-js require calls below. */ +function packageIterator(request, start, defaultIterator) { + // this is just working for bs58, would need to tune it further for other dependencies + if (request === "safe-buffer") { + return [path.join(projectDir, "/scripts/package-overrides/safe-buffer")]; + } else if (request === "buffer/") { + return [path.join(projectDir, "/scripts/package-overrides/buffer")]; + } else { + return defaultIterator(); + } +} + async function commonjsToESM(src, dst) { // create js bundle const bundle = await rollup({ input: src, - plugins: [commonjs()] + plugins: [commonjs(), nodeResolve({ + browser: true, + preferBuiltins: false, + customResolveOptions: {packageIterator} + })] }); const {output} = await bundle.generate({ format: 'es' @@ -59,6 +75,12 @@ async function populateLib() { path.join(modulesDir, 'another-json/another-json.js'), path.join(libDir, "another-json/index.js") ); + // transpile bs58 to esm + await fs.mkdir(path.join(libDir, "bs58/")); + await commonjsToESM( + path.join(modulesDir, 'bs58/index.js'), + path.join(libDir, "bs58/index.js") + ); } populateLib(); diff --git a/yarn.lock b/yarn.lock index d1bf27a2..89891ce7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -931,6 +931,13 @@ balanced-match@^1.0.0: resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c= +base-x@^3.0.2: + version "3.0.8" + resolved "https://registry.yarnpkg.com/base-x/-/base-x-3.0.8.tgz#1e1106c2537f0162e8b52474a557ebb09000018d" + integrity sha512-Rl/1AWP4J/zRrk54hhlxH4drNxPJXYUaKffODVI53/dAsV4t9fBxyxYKAVPU1XBHxYwOWP9h9H0hM2MVw4YfJA== + dependencies: + safe-buffer "^5.0.1" + boolbase@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/boolbase/-/boolbase-1.0.0.tgz#68dff5fbe60c51eb37725ea9e3ed310dcc1e776e" @@ -954,6 +961,13 @@ browserslist@^4.12.0, browserslist@^4.8.5: escalade "^3.0.2" node-releases "^1.1.60" +bs58@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/bs58/-/bs58-4.0.1.tgz#be161e76c354f6f788ae4071f63f34e8c4f0a42a" + integrity sha1-vhYedsNU9veIrkBx9j806MTwpCo= + dependencies: + base-x "^3.0.2" + builtin-modules@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.1.0.tgz#aad97c15131eb76b65b50ef208e7584cd76a7484" @@ -1726,6 +1740,11 @@ rollup@^2.26.4: optionalDependencies: fsevents "~2.1.2" +safe-buffer@^5.0.1: + version "5.2.1" + resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6" + integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ== + safe-buffer@~5.1.1: version "5.1.2" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"