Skip to content

Commit 06fadfb

Browse files
committed
Start dynamic import implementation
1 parent 4ce0190 commit 06fadfb

18 files changed

+269
-443
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"blocknative",
99
"wallet"
1010
],
11-
"main": "dist/onboard.umd.js",
12-
"module": "dist/onboard.esm.js",
11+
"main": "dist/cjs/onboard.js",
12+
"module": "dist/esm/onboard.js",
1313
"typings": "dist/src/onboard.d.ts",
1414
"files": [
1515
"dist"
@@ -29,6 +29,7 @@
2929
"@rollup/plugin-json": "^4.0.0",
3030
"@types/node": "^12.12.3",
3131
"babel-plugin-external-helpers": "^6.18.0",
32+
"rimraf": "^3.0.0",
3233
"rollup": "^1.12.0",
3334
"rollup-plugin-commonjs": "^10.0.0",
3435
"rollup-plugin-img": "^1.1.0",
@@ -52,7 +53,7 @@
5253
"regenerator-runtime": "^0.13.3"
5354
},
5455
"scripts": {
55-
"build": "rollup -c && babel dist/onboard.esm.js --out-file dist/onboard.esm.js && babel dist/onboard.umd.js --out-file dist/onboard.umd.js --minified",
56+
"build": "rimraf dist && rollup -c && babel dist/cjs --out-dir dist/cjs && babel dist/esm --out-dir dist/esm",
5657
"test": "echo \"TBD\" && exit 0"
5758
}
5859
}

rollup.config.js

Lines changed: 43 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -23,83 +23,49 @@ const opts = {
2323
}
2424
}
2525

26-
export default [
27-
{
28-
input: 'src/onboard.ts',
29-
output: {
30-
format: 'umd',
31-
name: 'onboard',
32-
file: 'dist/onboard.umd.js'
33-
},
34-
moduleContext: id => {
35-
const thisAsWindowForModules = [
36-
'node_modules/intl-messageformat/lib/core.js',
37-
'node_modules/intl-messageformat/lib/compiler.js'
38-
]
39-
40-
if (thisAsWindowForModules.some(id_ => id.trimRight().endsWith(id_))) {
41-
return 'window'
42-
}
43-
},
44-
plugins: [
45-
image(),
46-
json(),
47-
svelte({
48-
preprocess: preprocess(opts)
49-
}),
50-
resolve({
51-
browser: true,
52-
preferBuiltins: true,
53-
dedupe: importee =>
54-
importee === 'svelte' || importee.startsWith('svelte/')
55-
}),
56-
commonjs(),
57-
globals(),
58-
builtins(),
59-
typescript()
60-
]
61-
},
62-
{
63-
input: 'src/onboard.ts',
64-
output: {
26+
export default {
27+
input: 'src/onboard.ts',
28+
output: [
29+
{
6530
format: 'es',
66-
file: 'dist/onboard.esm.js'
31+
dir: 'dist/esm/'
6732
},
68-
moduleContext: id => {
69-
const thisAsWindowForModules = [
70-
'node_modules/intl-messageformat/lib/core.js',
71-
'node_modules/intl-messageformat/lib/compiler.js'
72-
]
73-
74-
if (thisAsWindowForModules.some(id_ => id.trimRight().endsWith(id_))) {
75-
return 'window'
76-
}
77-
},
78-
plugins: [
79-
json(),
80-
image(),
81-
svelte({
82-
preprocess: preprocess(opts)
83-
}),
84-
resolve({
85-
browser: true,
86-
dedupe: importee =>
87-
importee === 'svelte' || importee.startsWith('svelte/')
88-
}),
89-
commonjs(),
90-
typescript()
91-
],
92-
external: [
93-
'bowser',
94-
'bnc-sdk',
95-
'bignumber.js',
96-
'promise-cancelable',
97-
'@portis/web3',
98-
'@walletconnect/web3-provider',
99-
'fortmatic',
100-
// 'squarelink',
101-
'authereum',
102-
'regenerator-runtime/runtime'
33+
{ format: 'cjs', dir: 'dist/cjs/' }
34+
],
35+
moduleContext: id => {
36+
const thisAsWindowForModules = [
37+
'node_modules/intl-messageformat/lib/core.js',
38+
'node_modules/intl-messageformat/lib/compiler.js'
10339
]
104-
}
105-
]
40+
41+
if (thisAsWindowForModules.some(id_ => id.trimRight().endsWith(id_))) {
42+
return 'window'
43+
}
44+
},
45+
plugins: [
46+
json(),
47+
image(),
48+
svelte({
49+
preprocess: preprocess(opts)
50+
}),
51+
resolve({
52+
browser: true,
53+
dedupe: importee =>
54+
importee === 'svelte' || importee.startsWith('svelte/')
55+
}),
56+
commonjs(),
57+
typescript()
58+
],
59+
external: [
60+
'bowser',
61+
'bnc-sdk',
62+
'bignumber.js',
63+
'promise-cancelable',
64+
'@portis/web3',
65+
'@walletconnect/web3-provider',
66+
'fortmatic',
67+
'squarelink',
68+
'authereum',
69+
'regenerator-runtime/runtime'
70+
]
71+
}

src/interfaces.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export interface Initialization {
2-
networkId: number
32
dappId: string
3+
networkId: number
44
subscriptions: Subscriptions
55
modules: Modules
66
}
@@ -21,10 +21,7 @@ interface Modules {
2121
export interface WalletSelectModule {
2222
heading: string
2323
description: string
24-
wallets: {
25-
mobile: WalletModule[]
26-
desktop: WalletModule[]
27-
}
24+
wallets: WalletModule[]
2825
}
2926

3027
export interface WalletReadyModule {
@@ -88,6 +85,8 @@ export interface WalletModule {
8885
selectedWallet: string
8986
}) => string
9087
preferred?: boolean
88+
desktop?: boolean
89+
mobile?: boolean
9190
}
9291

9392
export interface Helpers {
@@ -137,15 +136,12 @@ export interface WalletConnectOptions {
137136
infuraKey: string
138137
}
139138

140-
export interface SelectDefaultsOptions {
141-
heading?: string
142-
description?: string
143-
networkId: number
144-
fortmaticInit?: { apiKey: string }
145-
portisInit?: { apiKey: string }
146-
squarelinkInit?: { apiKey: string }
147-
walletConnectInit?: WalletConnectOptions
148-
preferredWallets?: string[]
139+
export interface WalletInit {
140+
name: string
141+
preferred?: boolean
142+
apiKey?: string
143+
infuraKey?: string
144+
networkId?: number
149145
}
150146

151147
export interface WalletSelectFunction {

src/modules/select/index.ts

Lines changed: 35 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,42 @@
1-
import metamask from './wallets/metamask'
2-
import dapper from './wallets/dapper'
3-
import walletConnect from './wallets/wallet-connect'
4-
import coinbase from './wallets/coinbase'
5-
import trust from './wallets/trust'
6-
import portis from './wallets/portis'
7-
import fortmatic from './wallets/fortmatic'
8-
import authereum from './wallets/authereum'
9-
// import squarelink from './wallets/squarelink'
10-
import opera from './wallets/opera'
11-
import operaTouch from './wallets/opera-touch'
1+
import { WalletInit } from '../../interfaces'
122

13-
import { validateSelectDefaultsOptions } from '../../validation'
14-
import { SelectDefaultsOptions, WalletSelectModule } from '../../interfaces'
3+
function wallets(wallets: WalletInit[]) {
4+
return Promise.all(
5+
wallets.map((wallet: WalletInit) => {
6+
const { name, ...initParams } = wallet
157

16-
function defaults(options: SelectDefaultsOptions): WalletSelectModule {
17-
validateSelectDefaultsOptions(options)
18-
19-
const {
20-
heading,
21-
description,
22-
networkId,
23-
fortmaticInit,
24-
portisInit,
25-
// squarelinkInit,
26-
walletConnectInit,
27-
preferredWallets
28-
} = options
29-
30-
let desktopWallets = [metamask(), dapper(), opera()]
31-
let mobileWallets = [coinbase(), trust(), operaTouch()]
32-
33-
if (portisInit) {
34-
desktopWallets.push(portis({ ...portisInit, networkId }))
35-
mobileWallets.push(portis({ ...portisInit, networkId }))
36-
}
37-
38-
if (fortmaticInit) {
39-
desktopWallets.push(fortmatic({ ...fortmaticInit, networkId }))
40-
mobileWallets.push(fortmatic({ ...fortmaticInit, networkId }))
41-
}
42-
43-
if (walletConnectInit) {
44-
desktopWallets.push(
45-
walletConnect({ infuraKey: walletConnectInit.infuraKey })
46-
)
47-
mobileWallets.push(
48-
walletConnect({ infuraKey: walletConnectInit.infuraKey })
49-
)
50-
}
51-
52-
// if (squarelinkInit) {
53-
// desktopWallets.push(squarelink({ ...squarelinkInit, networkId }))
54-
// mobileWallets.push(squarelink({ ...squarelinkInit, networkId }))
55-
// }
56-
57-
desktopWallets.push(authereum({ networkId }))
58-
mobileWallets.push(authereum({ networkId }))
59-
60-
//set preferred wallets if provided
61-
if (preferredWallets) {
62-
desktopWallets = desktopWallets.map(wallet => {
63-
if (preferredWallets.includes(wallet.name)) {
64-
wallet.preferred = true
65-
}
66-
67-
return wallet
8+
return getModule(name).then((module: any) => module.default(initParams))
689
})
10+
)
11+
}
6912

70-
mobileWallets = mobileWallets.map(wallet => {
71-
if (preferredWallets.includes(wallet.name)) {
72-
wallet.preferred = true
73-
}
74-
75-
return wallet
76-
})
77-
}
78-
79-
return {
80-
heading: heading || 'Select a Wallet',
81-
description:
82-
description ||
83-
'Please select the wallet that you would like to use with this dapp:',
84-
wallets: {
85-
mobile: mobileWallets,
86-
desktop: desktopWallets
87-
}
13+
function getModule(name: string): Promise<any> {
14+
switch (name) {
15+
case 'metamask':
16+
return import('./wallets/metamask')
17+
case 'dapper':
18+
return import('./wallets/dapper')
19+
case 'portis':
20+
return import('./wallets/portis')
21+
case 'fortmatic':
22+
return import('./wallets/fortmatic')
23+
case 'squarelink':
24+
return import('./wallets/squarelink')
25+
case 'authereum':
26+
return import('./wallets/authereum')
27+
case 'trust':
28+
return import('./wallets/trust')
29+
case 'coinbase':
30+
return import('./wallets/coinbase')
31+
case 'walletConnect':
32+
return import('./wallets/wallet-connect')
33+
case 'opera':
34+
return import('./wallets/opera')
35+
case 'operaTouch':
36+
return import('./wallets/opera-touch')
37+
default:
38+
return Promise.reject('invalid wallet name')
8839
}
8940
}
9041

91-
export default {
92-
defaults,
93-
metamask,
94-
dapper,
95-
walletConnect,
96-
coinbase,
97-
trust,
98-
portis,
99-
fortmatic,
100-
authereum,
101-
// squarelink,
102-
opera,
103-
operaTouch
104-
}
42+
export default wallets

src/modules/select/wallets/authereum.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ function authereum(options: { networkId: number }): WalletModule {
4747
}
4848
}
4949
}
50-
}
50+
},
51+
desktop: true,
52+
mobile: true
5153
}
5254
}
5355

src/modules/select/wallets/coinbase.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ function coinbase(): WalletModule {
2020
}
2121
},
2222
link: 'https://go.cb-w.com/',
23-
installMessage: mobileWalletInstallMessage
23+
installMessage: mobileWalletInstallMessage,
24+
mobile: true
2425
}
2526
}
2627

src/modules/select/wallets/dapper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function dapper(): WalletModule {
2222
}
2323
},
2424
link: 'https://www.meetdapper.com/',
25-
installMessage: extensionInstallMessage
25+
installMessage: extensionInstallMessage,
26+
desktop: true
2627
}
2728
}
2829

src/modules/select/wallets/fortmatic.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ function fortmatic(options: SdkWalletOptions): WalletModule {
4949
}
5050
}
5151
}
52-
}
52+
},
53+
desktop: true,
54+
mobile: true
5355
}
5456
}
5557

src/modules/select/wallets/metamask.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function metamask(): WalletModule {
3232
}
3333
},
3434
link: 'https://metamask.io/',
35-
installMessage: extensionInstallMessage
35+
installMessage: extensionInstallMessage,
36+
desktop: true
3637
}
3738
}
3839

0 commit comments

Comments
 (0)