Skip to content

Commit 0596631

Browse files
authored
Dynamic imports: only use .default when present (Uniswap#115)
* Dynamic imports: only use .default when present This solves an issue where in some cases, the imported module returns the default export directly, rather than being on the .default property. In CJS, the import() calls get transformed into Promise.resolve(require("dependency")) calls. Bundlers seem to stick to CJS in this case, and pick the `main` field of the dependency, which is usually CJS with a .default prop. In ESM, the import() calls don’t get transformed. In this case, bundlers seem to behave in different ways, by following one of the `main`, `module` and `browser` fields. In these two last cases, the default export is available directly and not on the .default property. By checking if .default is defined and using the imported value otherwise, we make these imports compatible with these different scenarios. * Prettier fix
1 parent f52b5d0 commit 0596631

File tree

8 files changed

+8
-8
lines changed

8 files changed

+8
-8
lines changed

packages/authereum-connector/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class AuthereumConnector extends AbstractConnector {
2929

3030
public async activate(): Promise<ConnectorUpdate> {
3131
if (!this.authereum) {
32-
const { default: Authereum } = await import('authereum')
32+
const Authereum = await import('authereum').then(m => m?.default ?? m)
3333
this.authereum = new Authereum({
3434
networkName: chainIdToNetwork[this.chainId],
3535
...this.config

packages/fortmatic-connector/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class FortmaticConnector extends AbstractConnector {
3030

3131
public async activate(): Promise<ConnectorUpdate> {
3232
if (!this.fortmatic) {
33-
const { default: Fortmatic } = await import('fortmatic')
33+
const Fortmatic = await import('fortmatic').then(m => m?.default ?? m)
3434
this.fortmatic = new Fortmatic(
3535
this.apiKey,
3636
this.chainId === 1 || this.chainId === 4 ? undefined : chainIdToNetwork[this.chainId]

packages/portis-connector/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class PortisConnector extends AbstractConnector {
7777

7878
public async activate(): Promise<ConnectorUpdate> {
7979
if (!this.portis) {
80-
const { default: Portis } = await import('@portis/web3')
80+
const Portis = await import('@portis/web3').then(m => m?.default ?? m)
8181
this.portis = new Portis(
8282
this.dAppId,
8383
typeof this.networks[0] === 'number' ? chainIdToNetwork[this.networks[0]] : (this.networks[0] as any),

packages/squarelink-connector/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class SquarelinkConnector extends AbstractConnector {
4141

4242
public async activate(): Promise<ConnectorUpdate> {
4343
if (!this.squarelink) {
44-
const { default: Squarelink } = await import('squarelink')
44+
const Squarelink = await import('squarelink').then(m => m?.default ?? m)
4545
this.squarelink = new Squarelink(
4646
this.clientId,
4747
typeof this.networks[0] === 'number' ? chainIdToNetwork[this.networks[0]] : this.networks[0],

packages/torus-connector/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class TorusConnector extends AbstractConnector {
2727

2828
public async activate(): Promise<ConnectorUpdate> {
2929
if (!this.torus) {
30-
const { default: Torus } = await import('@toruslabs/torus-embed')
30+
const Torus = await import('@toruslabs/torus-embed').then(m => m?.default ?? m)
3131
this.torus = new Torus(this.constructorOptions)
3232
await this.torus.init(this.initOptions)
3333
}

packages/trezor-connector/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class TrezorConnector extends AbstractConnector {
4848

4949
public async activate(): Promise<ConnectorUpdate> {
5050
if (!this.provider) {
51-
const { default: TrezorConnect } = await import('trezor-connect')
51+
const TrezorConnect = await import('trezor-connect').then(m => m?.default ?? m)
5252
TrezorConnect.manifest({
5353
email: this.manifestEmail,
5454
appUrl: this.manifestAppUrl

packages/walletconnect-connector/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class WalletConnectConnector extends AbstractConnector {
7373

7474
public async activate(): Promise<ConnectorUpdate> {
7575
if (!this.walletConnectProvider) {
76-
const { default: WalletConnectProvider } = await import('@walletconnect/web3-provider')
76+
const WalletConnectProvider = await import('@walletconnect/web3-provider').then(m => m?.default ?? m)
7777
this.walletConnectProvider = new WalletConnectProvider({
7878
bridge: this.bridge,
7979
rpc: this.rpc,

packages/walletlink-connector/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class WalletLinkConnector extends AbstractConnector {
3030

3131
public async activate(): Promise<ConnectorUpdate> {
3232
if (!this.walletLink) {
33-
const { default: WalletLink } = await import('walletlink')
33+
const WalletLink = await import('walletlink').then(m => m?.default ?? m)
3434
this.walletLink = new WalletLink({
3535
appName: this.appName,
3636
darkMode: this.darkMode,

0 commit comments

Comments
 (0)