Skip to content

Commit cf9b86a

Browse files
committed
dynamic imports
1 parent 27e4aad commit cf9b86a

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

jest.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ module.exports = {
1616
maxWorkers: '50%',
1717
testPathIgnorePatterns: ['/build/', '/node_modules/', '/docs/', '/e2e-tests/'],
1818
// The below resolves `jest-haste-map: Haste module naming collision: @substrate/api-sidecar`
19-
modulePathIgnorePatterns: ['/build']
19+
modulePathIgnorePatterns: ['/build'],
20+
// Transform ESM modules from @polkadot-api, @noble, and @scure packages
21+
transformIgnorePatterns: ['/node_modules/(?!(@polkadot-api|@noble|@scure)/)'],
2022
};

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,5 @@
9191
"polkadot",
9292
"kusama"
9393
],
94-
"packageManager": "[email protected]",
95-
"resolutions": {
96-
"@scure/base": "^1.2.6"
97-
}
94+
"packageManager": "[email protected]"
9895
}

src/services/transaction/TransactionMetadataBlobService.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,31 @@ import { ApiPromise } from '@polkadot/api';
1818
import { ApiDecoration } from '@polkadot/api/types';
1919
import type { BlockHash } from '@polkadot/types/interfaces';
2020
import { u8aToHex } from '@polkadot/util';
21-
import { merkleizeMetadata } from '@polkadot-api/merkleize-metadata';
2221
import { BadRequest, InternalServerError } from 'http-errors';
2322
import { ITransactionMetadataBlob } from 'src/types/responses';
2423

2524
import { MetadataBlobParams } from '../../controllers/transaction/TransactionMetadataBlobController';
2625
import { AbstractService } from '../AbstractService';
2726

27+
// Dynamic import helper that TypeScript won't transform to require()
28+
// This is necessary because @polkadot-api/merkleize-metadata is an ESM-only package
29+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
30+
const dynamicImport = new Function('specifier', 'return import(specifier)') as <T>(specifier: string) => Promise<T>;
31+
32+
// Cached module after first load
33+
let merkleizeMetadataModule: typeof import('@polkadot-api/merkleize-metadata') | null = null;
34+
35+
async function getMerkleizeMetadata(): Promise<
36+
(typeof import('@polkadot-api/merkleize-metadata'))['merkleizeMetadata']
37+
> {
38+
if (!merkleizeMetadataModule) {
39+
merkleizeMetadataModule = await dynamicImport<typeof import('@polkadot-api/merkleize-metadata')>(
40+
'@polkadot-api/merkleize-metadata',
41+
);
42+
}
43+
return merkleizeMetadataModule.merkleizeMetadata;
44+
}
45+
2846
export class TransactionMetadataBlobService extends AbstractService {
2947
/**
3048
* Fetch metadata blob (proof) for a given transaction.
@@ -87,6 +105,8 @@ export class TransactionMetadataBlobService extends AbstractService {
87105

88106
const base58Prefix = ss58Format?.toNumber() ?? 42;
89107

108+
// Use dynamic import for ESM module compatibility
109+
const merkleizeMetadata = await getMerkleizeMetadata();
90110
const merkleized = merkleizeMetadata(metadataV15Raw, {
91111
decimals,
92112
tokenSymbol,

yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2403,13 +2403,20 @@ __metadata:
24032403
languageName: node
24042404
linkType: hard
24052405

2406-
"@scure/base@npm:^1.2.6":
2406+
"@scure/base@npm:^1.1.1, @scure/base@npm:^1.1.7, @scure/base@npm:^1.2.6":
24072407
version: 1.2.6
24082408
resolution: "@scure/base@npm:1.2.6"
24092409
checksum: 10/c1a7bd5e0b0c8f94c36fbc220f4a67cc832b00e2d2065c7d8a404ed81ab1c94c5443def6d361a70fc382db3496e9487fb9941728f0584782b274c18a4bed4187
24102410
languageName: node
24112411
linkType: hard
24122412

2413+
"@scure/base@npm:^2.0.0":
2414+
version: 2.0.0
2415+
resolution: "@scure/base@npm:2.0.0"
2416+
checksum: 10/8fb86024f22e9c532d513b8df8a672252e58bd5695920ce646162287f0accd38e89cab58722a738b3d247b5dcf7760362ae2d82d502be7e62a555f5d98f8a110
2417+
languageName: node
2418+
linkType: hard
2419+
24132420
"@scure/sr25519@npm:^0.2.0":
24142421
version: 0.2.0
24152422
resolution: "@scure/sr25519@npm:0.2.0"

0 commit comments

Comments
 (0)