From 3bee4b4585ec0d2e55b66d2b05c6139d88c18801 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 21 Dec 2021 15:28:55 +0100 Subject: [PATCH] bundle bs58 to avoid pain of bundle transitive dependency for lib users bs58 depends on safe-buffer, which depends on buffer, which is a bit of a pain to bundle as it is a built-in node module. You'd typically replace buffer with a browser polyfill in your build system but: a) this is somewhat a pain to setup for simple apps b) the polyfill is way more than we need (6kb), so we prefer to bundle our minimal buffer replacement that uses Uint8Array. Since it is a transitive dependency, we need to bundle bs58 and all of its transitive dependencies (2.5kb) as well, so if users of hydrogen-sdk also use any of these, they'll be double included in their bundle. --- vite.common-config.js | 9 +++++++++ vite.config.js | 9 --------- vite.sdk-lib-config.js | 25 +++++++++---------------- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/vite.common-config.js b/vite.common-config.js index b253d1fe..bfade3e2 100644 --- a/vite.common-config.js +++ b/vite.common-config.js @@ -11,6 +11,15 @@ const commonOptions = { server: { hmr: false }, + resolve: { + alias: { + // these should only be imported by the base-x package in any runtime code + // and works in the browser with a Uint8Array shim, + // rather than including a ton of polyfill code + "safe-buffer": "./scripts/package-overrides/safe-buffer/index.js", + "buffer": "./scripts/package-overrides/buffer/index.js", + } + }, build: { emptyOutDir: true, assetsInlineLimit: 0, diff --git a/vite.config.js b/vite.config.js index e11c61d8..b6ec597d 100644 --- a/vite.config.js +++ b/vite.config.js @@ -9,15 +9,6 @@ export default defineConfig(({mode}) => { return mergeOptions(commonOptions, { root: "src/platform/web", base: "./", - resolve: { - alias: { - // these should only be imported by the base-x package in any runtime code - // and works in the browser with a Uint8Array shim, - // rather than including a ton of polyfill code - "safe-buffer": "./scripts/package-overrides/safe-buffer/index.js", - "buffer": "./scripts/package-overrides/buffer/index.js", - } - }, build: { outDir: "../../../target", minify: true, diff --git a/vite.sdk-lib-config.js b/vite.sdk-lib-config.js index bd2250af..c1678d8b 100644 --- a/vite.sdk-lib-config.js +++ b/vite.sdk-lib-config.js @@ -1,16 +1,16 @@ const path = require("path"); const mergeOptions = require('merge-options'); const commonOptions = require("./vite.common-config.js"); -const manifest = require("./package.json") - -// const srcDir = path.join(__dirname, "src/"); -// const modulesDir = path.join(srcDir, "node_modules/"); -// const mocksDir = path.join(srcDir, "mocks/"); -// const fixturesDir = path.join(srcDir, "fixtures/"); +const manifest = require("./package.json"); const externalDependencies = Object.keys(manifest.dependencies) + // just in case for safety in case fake-indexeddb wouldn't be + // treeshake'd out of the bundle .concat(Object.keys(manifest.devDependencies)) - .map(d => path.join(__dirname, "node_modules", d)); + // bundle bs58 because it uses buffer indirectly, which is a pain to bundle, + // so we don't annoy our library users with it. + .filter(d => d !== "bs58"); +const moduleDir = path.join(__dirname, "node_modules"); export default mergeOptions(commonOptions, { root: "src/", @@ -25,15 +25,8 @@ export default mergeOptions(commonOptions, { outDir: "../target/lib-build", // don't bundle any dependencies, they should be imported/required rollupOptions: { - external(id, parentId) { - const isRelativePath = id.startsWith("./") || id.startsWith("../"); - const isModuleIdentifier = !isRelativePath && !id.startsWith("/"); - const resolveId = isRelativePath ? path.join(path.dirname(parentId), id) : id; - const external = isModuleIdentifier || - externalDependencies.some(d => resolveId.startsWith(d)); - // resolveId.startsWith(fixturesDir) || - // resolveId.startsWith(mocksDir); - return external; + external(id) { + return externalDependencies.some(d => id === d || id.startsWith(d + "/")); }, /* don't bundle, so we can override imports per file at build time to replace components */ // output: {