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.
This commit is contained in:
Bruno Windels 2021-12-21 15:28:55 +01:00
parent b48280905e
commit 3bee4b4585
3 changed files with 18 additions and 25 deletions

View File

@ -11,6 +11,15 @@ const commonOptions = {
server: { server: {
hmr: false 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: { build: {
emptyOutDir: true, emptyOutDir: true,
assetsInlineLimit: 0, assetsInlineLimit: 0,

View File

@ -9,15 +9,6 @@ export default defineConfig(({mode}) => {
return mergeOptions(commonOptions, { return mergeOptions(commonOptions, {
root: "src/platform/web", root: "src/platform/web",
base: "./", 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: { build: {
outDir: "../../../target", outDir: "../../../target",
minify: true, minify: true,

View File

@ -1,16 +1,16 @@
const path = require("path"); const path = require("path");
const mergeOptions = require('merge-options'); const mergeOptions = require('merge-options');
const commonOptions = require("./vite.common-config.js"); const commonOptions = require("./vite.common-config.js");
const manifest = require("./package.json") 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 externalDependencies = Object.keys(manifest.dependencies) 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)) .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, { export default mergeOptions(commonOptions, {
root: "src/", root: "src/",
@ -25,15 +25,8 @@ export default mergeOptions(commonOptions, {
outDir: "../target/lib-build", outDir: "../target/lib-build",
// don't bundle any dependencies, they should be imported/required // don't bundle any dependencies, they should be imported/required
rollupOptions: { rollupOptions: {
external(id, parentId) { external(id) {
const isRelativePath = id.startsWith("./") || id.startsWith("../"); return externalDependencies.some(d => id === d || id.startsWith(d + "/"));
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;
}, },
/* don't bundle, so we can override imports per file at build time to replace components */ /* don't bundle, so we can override imports per file at build time to replace components */
// output: { // output: {