From 4258dc473a3f624a9dc642d31de7816741996960 Mon Sep 17 00:00:00 2001 From: aptt Date: Thu, 19 Dec 2024 10:44:19 +0100 Subject: [PATCH] =?UTF-8?q?Revert=20"refactor(frontend):=20=F0=9F=92=A1=20?= =?UTF-8?q?remove=20hashconnect=20virtualization=20workaround"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit fe4730d054c2d4c86b2399550f9be7057dd8b230. --- frontend/src/lib/hashconnect/dappMetadata.ts | 2 +- .../src/lib/hashconnect/hashConnect.svelte.ts | 2 +- .../lib/hashconnect/virtual:hashconnect.d.ts | 3 +++ .../src/lib/hashconnect/virtualHashConnect.ts | 19 +++++++++++++++++++ frontend/vite.config.ts | 3 ++- 5 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 frontend/src/lib/hashconnect/virtual:hashconnect.d.ts create mode 100644 frontend/src/lib/hashconnect/virtualHashConnect.ts diff --git a/frontend/src/lib/hashconnect/dappMetadata.ts b/frontend/src/lib/hashconnect/dappMetadata.ts index f2914ad..0fddbef 100644 --- a/frontend/src/lib/hashconnect/dappMetadata.ts +++ b/frontend/src/lib/hashconnect/dappMetadata.ts @@ -1,4 +1,4 @@ -import type { DappMetadata } from 'hashconnect' +import type { DappMetadata } from 'virtual:hashconnect' export const dappMetadata: DappMetadata = { name: 'BIDI', diff --git a/frontend/src/lib/hashconnect/hashConnect.svelte.ts b/frontend/src/lib/hashconnect/hashConnect.svelte.ts index a43b7f8..4479ad0 100644 --- a/frontend/src/lib/hashconnect/hashConnect.svelte.ts +++ b/frontend/src/lib/hashconnect/hashConnect.svelte.ts @@ -5,9 +5,9 @@ import type { ExecuteTransaction } from '$lib/hedera/Execute' import { MirrorNodeClient } from '$lib/hedera/MirrorNodeClient' import { AccountId, LedgerId, TokenId } from '@hashgraph/sdk' import { tokenUtils as getTokenUtils } from '@tikz/hedera-mirror-node-ts' -import { HashConnect, type SessionData } from 'hashconnect' import type { HashConnectSigner } from 'hashconnect/dist/signer' import { untrack } from 'svelte' +import { HashConnect, type SessionData } from 'virtual:hashconnect' import { dappMetadata } from './dappMetadata' /** diff --git a/frontend/src/lib/hashconnect/virtual:hashconnect.d.ts b/frontend/src/lib/hashconnect/virtual:hashconnect.d.ts new file mode 100644 index 0000000..44670af --- /dev/null +++ b/frontend/src/lib/hashconnect/virtual:hashconnect.d.ts @@ -0,0 +1,3 @@ +declare module 'virtual:hashconnect' { + export * from 'hashconnect' +} diff --git a/frontend/src/lib/hashconnect/virtualHashConnect.ts b/frontend/src/lib/hashconnect/virtualHashConnect.ts new file mode 100644 index 0000000..f177f3c --- /dev/null +++ b/frontend/src/lib/hashconnect/virtualHashConnect.ts @@ -0,0 +1,19 @@ +import { type Plugin } from 'vite' + +/** + * a compatibility layer to import `hashconnect` through vite instead of allowing sveltekit to process it + * + * `hashconnect` can't be imported by node directly because it uses imports without extensions in its dist files. importing through sveltekit code doesn't work because of this, because sveltekit's processing is not as forgiving as vite's. but we can circumvent this by creating a virtual module that uses vite's module resolution to create a working bundle of `hashconnect` (as a pre-bundled dependency). + */ +export const getVirtualHashConnectPlugin = (): Plugin => { + const virtualModuleId = 'virtual:hashconnect' + + return { + name: 'virtual-hashconnect', // required, will show up in warnings and errors + async resolveId(id) { + if (id === virtualModuleId) { + return await this.resolve('hashconnect') + } + }, + } +} diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 3e83e90..636de58 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -1,9 +1,10 @@ import { sveltekit } from '@sveltejs/kit/vite' import { defineConfig } from 'vite' import { nodePolyfills } from 'vite-plugin-node-polyfills' +import { getVirtualHashConnectPlugin } from './src/lib/hashconnect/virtualHashConnect' export default defineConfig({ - plugins: [nodePolyfills(), sveltekit()], + plugins: [nodePolyfills(), getVirtualHashConnectPlugin(), sveltekit()], ssr: { noExternal: ['@tikz/hedera-mirror-node-ts'], },