From: Chris Duncan Date: Wed, 23 Jul 2025 05:46:04 +0000 (-0700) Subject: Separate node and browser options during build. Conditionally import lib based on... X-Git-Tag: v0.10.5~55^2~27 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=560f053efb0f49ba9941217af2eb5f1aaf5bfa0f;p=libnemo.git Separate node and browser options during build. Conditionally import lib based on environment when testing. --- diff --git a/esbuild-prod.mjs b/esbuild-prod.mjs index 64ee6e0..48c5654 100644 --- a/esbuild-prod.mjs +++ b/esbuild-prod.mjs @@ -10,18 +10,24 @@ options.minifySyntax = true options.minifyWhitespace = true // Node build -options.entryPoints = [ - { in: './src/main.ts', out: 'nodejs.min' } -] -options.dropLabels = ['BROWSER'] -options.external = ['node:worker_threads'] -await build(options) +const nodeOptions = { + ...options, + entryPoints: [ + { in: './src/main.ts', out: 'nodejs.min' } + ], + dropLabels: ['BROWSER'], + external: ['node:worker_threads'] +} +await build(nodeOptions) // Browser build -options.entryPoints = [ - { in: './src/main.ts', out: 'browser.min' }, - { in: './src/types.d.ts', out: 'types.d' } -] -options.dropLabels = ['NODE'] -options.inject = ['./buffer.mjs'] -await build(options) +const browserOptions = { + ...options, + entryPoints: [ + { in: './src/main.ts', out: 'browser.min' }, + { in: './src/types.d.ts', out: 'types.d' } + ], + dropLabels: ['NODE'], + inject: ['./buffer.mjs'] +} +await build(browserOptions) diff --git a/test/test.blake2b.mjs b/test/test.blake2b.mjs index 0609c90..01fe379 100644 --- a/test/test.blake2b.mjs +++ b/test/test.blake2b.mjs @@ -3,9 +3,9 @@ 'use strict' -import { assert, suite, test } from './GLOBALS.mjs' +import { assert, isNode, suite, test } from './GLOBALS.mjs' import { BLAKE2B_TEST_VECTORS } from './VECTORS.js' -import { Blake2b } from '../dist/main.min.js' +const { Blake2b } = await import(isNode ? '../dist/nodejs.min.js' : '../dist/browser.min.js') await Promise.all([ suite('BLAKE2b test vectors', async () => { diff --git a/test/test.blocks.mjs b/test/test.blocks.mjs index f03b7c8..82b9d45 100644 --- a/test/test.blocks.mjs +++ b/test/test.blocks.mjs @@ -3,9 +3,9 @@ 'use strict' -import { assert, suite, test } from './GLOBALS.mjs' +import { assert, isNode, suite, test } from './GLOBALS.mjs' import { NANO_TEST_VECTORS } from './VECTORS.js' -import { SendBlock, ReceiveBlock, ChangeBlock } from '../dist/main.min.js' +const { SendBlock, ReceiveBlock, ChangeBlock } = await import(isNode ? '../dist/nodejs.min.js' : '../dist/browser.min.js') await Promise.all([ suite('Block format', async () => { diff --git a/test/test.calculate-pow.mjs b/test/test.calculate-pow.mjs index ef1e73e..6c8331e 100644 --- a/test/test.calculate-pow.mjs +++ b/test/test.calculate-pow.mjs @@ -5,7 +5,7 @@ import { assert, isNode, suite, test } from './GLOBALS.mjs' import { NANO_TEST_VECTORS } from './VECTORS.js' -import { SendBlock, Blake2b } from '../dist/main.min.js' +const { SendBlock, Blake2b } = await import(isNode ? '../dist/nodejs.min.js' : '../dist/browser.min.js') await Promise.all([ suite('Calculate proof-of-work', { skip: isNode }, async () => { diff --git a/test/test.create-wallet.mjs b/test/test.create-wallet.mjs index 7429ca0..b3e4bad 100644 --- a/test/test.create-wallet.mjs +++ b/test/test.create-wallet.mjs @@ -3,9 +3,9 @@ 'use strict' -import { assert, suite, test } from './GLOBALS.mjs' +import { assert, isNode, suite, test } from './GLOBALS.mjs' import { NANO_TEST_VECTORS } from './VECTORS.js' -import { Bip44Wallet, Blake2bWallet } from '../dist/main.min.js' +const { Bip44Wallet, Blake2bWallet } = await import(isNode ? '../dist/nodejs.min.js' : '../dist/browser.min.js') await Promise.all([ suite('Create wallets', async () => { diff --git a/test/test.derive-accounts.mjs b/test/test.derive-accounts.mjs index a4949af..5dea003 100644 --- a/test/test.derive-accounts.mjs +++ b/test/test.derive-accounts.mjs @@ -3,9 +3,9 @@ 'use strict' -import { assert, suite, test } from './GLOBALS.mjs' +import { assert, isNode, suite, test } from './GLOBALS.mjs' import { NANO_TEST_VECTORS } from './VECTORS.js' -import { Bip44Wallet, Blake2bWallet } from '../dist/main.min.js' +const { Bip44Wallet, Blake2bWallet } = await import(isNode ? '../dist/nodejs.min.js' : '../dist/browser.min.js') await Promise.all([ suite('Derive accounts from BIP-44 wallet', async () => { diff --git a/test/test.import-wallet.mjs b/test/test.import-wallet.mjs index 7cc2929..ccd32e2 100644 --- a/test/test.import-wallet.mjs +++ b/test/test.import-wallet.mjs @@ -3,9 +3,9 @@ 'use strict' -import { assert, suite, test } from './GLOBALS.mjs' +import { assert, isNode, suite, test } from './GLOBALS.mjs' import { BIP32_TEST_VECTORS, CUSTOM_TEST_VECTORS, NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from './VECTORS.js' -import { Account, Bip44Wallet, Blake2bWallet } from '../dist/main.min.js' +const { Account, Bip44Wallet, Blake2bWallet } = await import(isNode ? '../dist/nodejs.min.js' : '../dist/browser.min.js') await Promise.all([ suite('Import wallets', async () => { diff --git a/test/test.ledger.mjs b/test/test.ledger.mjs index 68c1fcd..440168a 100644 --- a/test/test.ledger.mjs +++ b/test/test.ledger.mjs @@ -5,7 +5,7 @@ import { assert, click, env, isNode, suite, test } from './GLOBALS.mjs' import { NANO_TEST_VECTORS } from './VECTORS.js' -import { Account, LedgerWallet, ReceiveBlock, Rpc, SendBlock } from '../dist/main.min.js' +const { Account, LedgerWallet, ReceiveBlock, Rpc, SendBlock } = await import(isNode ? '../dist/nodejs.min.js' : '../dist/browser.min.js') const rpc = new Rpc(env.NODE_URL ?? '', env.API_KEY_NAME) diff --git a/test/test.lock-unlock.mjs b/test/test.lock-unlock.mjs index 74aedac..7f46349 100644 --- a/test/test.lock-unlock.mjs +++ b/test/test.lock-unlock.mjs @@ -3,9 +3,9 @@ 'use strict' -import { assert, suite, test } from './GLOBALS.mjs' +import { assert, isNode, suite, test } from './GLOBALS.mjs' import { NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from './VECTORS.js' -import { Bip44Wallet, Blake2bWallet } from '../dist/main.min.js' +const { Bip44Wallet, Blake2bWallet } = await import(isNode ? '../dist/nodejs.min.js' : '../dist/browser.min.js') await Promise.all([ suite('Lock and unlock wallets', async () => { diff --git a/test/test.manage-rolodex.mjs b/test/test.manage-rolodex.mjs index f32b2fa..61ef871 100644 --- a/test/test.manage-rolodex.mjs +++ b/test/test.manage-rolodex.mjs @@ -3,9 +3,9 @@ 'use strict' -import { assert, suite, test } from './GLOBALS.mjs' +import { assert, isNode, suite, test } from './GLOBALS.mjs' import { NANO_TEST_VECTORS } from './VECTORS.js' -import { Rolodex, Tools } from '../dist/main.min.js' +const { Rolodex, Tools } = await import(isNode ? '../dist/nodejs.min.js' : '../dist/browser.min.js') await Promise.all([ suite('Rolodex valid contact management', async () => { diff --git a/test/test.refresh-accounts.mjs b/test/test.refresh-accounts.mjs index 9ce6726..b4fb138 100644 --- a/test/test.refresh-accounts.mjs +++ b/test/test.refresh-accounts.mjs @@ -3,9 +3,9 @@ 'use strict' -import { assert, env, suite, test } from './GLOBALS.mjs' +import { assert, env, isNode, suite, test } from './GLOBALS.mjs' import { NANO_TEST_VECTORS } from './VECTORS.js' -import { Account, Bip44Wallet, Rpc } from '../dist/main.min.js' +const { Account, Bip44Wallet, Rpc } = await import(isNode ? '../dist/nodejs.min.js' : '../dist/browser.min.js') const rpc = new Rpc(env.NODE_URL ?? '', env.API_KEY_NAME) diff --git a/test/test.tools.mjs b/test/test.tools.mjs index 2982009..f9f54c1 100644 --- a/test/test.tools.mjs +++ b/test/test.tools.mjs @@ -3,9 +3,9 @@ 'use strict' -import { assert, env, suite, test } from './GLOBALS.mjs' +import { assert, env, isNode, suite, test } from './GLOBALS.mjs' import { RAW_MAX, NANO_TEST_VECTORS } from './VECTORS.js' -import { Bip44Wallet, Account, SendBlock, Rpc, Tools } from '../dist/main.min.js' +const { Bip44Wallet, Account, SendBlock, Rpc, Tools } = await import(isNode ? '../dist/nodejs.min.js' : '../dist/browser.min.js') const rpc = new Rpc(env?.NODE_URL ?? '', env?.API_KEY_NAME)