From 3b487f7c69b6ddec30e1759dcecfb3e27c1dc458 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo@trentesaux.fr>
Date: Thu, 18 Jul 2024 08:57:51 +0200
Subject: [PATCH] wip dynamic import for web3

---
 src/common.ts | 6 +++---
 src/login.ts  | 4 ++--
 src/main.ts   | 8 ++++----
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/common.ts b/src/common.ts
index 05996f3..16cadba 100644
--- a/src/common.ts
+++ b/src/common.ts
@@ -1,11 +1,9 @@
 import type { ApiPromise } from '@polkadot/api'
-import { web3FromAddress } from '@polkadot/extension-dapp'
 import type { Vec, u32 } from '@polkadot/types-codec'
 import type { ISubmittableResult, RegistryError } from '@polkadot/types/types'
 import type { Ref } from 'vue'
 import { loginAccount } from './global'
 import type { SubmittableExtrinsic } from '@polkadot/api/promise/types'
-import type { AugmentedSubmittable } from '@polkadot/api-base/types'
 
 /// fetch authorities
 export function fetchAuthorities(
@@ -36,7 +34,9 @@ export async function signAndSend(
   successCallback: () => void
 ) {
   const address = loginAccount.value!.address
-  const injector = await web3FromAddress(address)
+  const injector = await import('@polkadot/extension-dapp').then((web3) =>
+    web3.web3FromAddress(address)
+  )
   try {
     await tx.signAndSend(
       address,
diff --git a/src/login.ts b/src/login.ts
index f3257ce..33257a1 100644
--- a/src/login.ts
+++ b/src/login.ts
@@ -17,7 +17,6 @@ import type {
 import type { InjectedAccountWithMeta } from '@polkadot/extension-inject/types'
 import type { ApiPromise } from '@polkadot/api'
 import { type u32, Option } from '@polkadot/types-codec'
-import { web3Accounts } from '@polkadot/extension-dapp'
 import type { IdtyNameQuery } from '@/generated/squid'
 import { getItem, setItem, sk } from './storage'
 import { IdtyName } from '@/squid/wot.gql'
@@ -31,7 +30,8 @@ export async function tryConnectAccount(
   api: ApiPromise,
   apollo: ApolloClient<NormalizedCacheObject>
 ) {
-  if (allAccounts.value.length == 0) allAccounts.value = await web3Accounts()
+  if (allAccounts.value.length == 0)
+    allAccounts.value = await import('@polkadot/extension-dapp').then((web3) => web3.web3Accounts())
   const allAcc = allAccounts.value
   allAccounts.value = allAcc
   const defaultLogin = getItem<string>(sk.defaultAccount)
diff --git a/src/main.ts b/src/main.ts
index 02a7335..54dde6e 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -3,7 +3,6 @@ import { createApp } from 'vue'
 import App from './App.vue'
 import router from './router'
 import { initStateFromStorage, initStateFromApis } from './global'
-import { web3Enable } from '@polkadot/extension-dapp'
 import { initLocalStorage } from './storage'
 import { initDatapod, initRpc, initSquid, initp2p } from './network'
 import { ApolloClients } from '@vue/apollo-composable'
@@ -12,9 +11,6 @@ import { ApolloClients } from '@vue/apollo-composable'
 initLocalStorage()
 initStateFromStorage()
 
-// ask browser extension for web3 support
-web3Enable('duniter-vue')
-
 // ===== APP =====
 
 const app = createApp(App)
@@ -32,3 +28,7 @@ Promise.all([initRpc(), initSquid(), initDatapod()]).then(([api, apolloSquid, ap
 
 // also provide datapod ipfs stack
 initp2p().then((p2p) => app.provide('p2p', p2p))
+
+// === dynamic imports ===
+// ask browser extension for web3 support
+import('@polkadot/extension-dapp').then((web3) => web3.web3Enable('duniter-vue'))
-- 
GitLab