Skip to content

Commit 21e83a0

Browse files
committed
fix(hardhat-viem): add resolution-mode to viem type imports
A fresh install resolves viem to a release that ships a nested _types/package.json with type: module, which makes TypeScript treat viem's type declarations as belonging to an ES module. Under this package's CommonJS/Node16 setup, every type-only import from viem then needs an explicit resolution-mode import attribute, or tsc fails with TS1541/TS1542. Add the attribute to the affected type-only imports and type queries in src and test. No dependency version changes, so this stays compatible with both the currently pinned viem and future releases.
1 parent a0a24d5 commit 21e83a0

9 files changed

Lines changed: 47 additions & 18 deletions

File tree

‎packages/hardhat-viem/src/internal/accounts.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { EthereumProvider } from "hardhat/types";
2-
import type { Address } from "viem";
2+
import type { Address } from "viem" with { "resolution-mode": "import" };
33

44
import memoize from "lodash.memoize";
55

‎packages/hardhat-viem/src/internal/bytecode.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type * as viemT from "viem";
1+
import type * as viemT from "viem" with { "resolution-mode": "import" };
22
import type { Artifact } from "hardhat/types/artifacts";
33

44
import {
@@ -22,7 +22,9 @@ export async function linkBytecode(
2222
artifact: Artifact,
2323
libraries: Link[]
2424
): Promise<viemT.Hex> {
25-
const { isHex } = require("viem") as typeof import("viem");
25+
const { isHex } = require("viem") as typeof import("viem", {
26+
with: { "resolution-mode": "import" },
27+
});
2628
let bytecode = artifact.bytecode;
2729

2830
// TODO: measure performance impact

‎packages/hardhat-viem/src/internal/chains.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import type { EthereumProvider } from "hardhat/types";
2-
import type { Chain } from "viem";
2+
import type { Chain } from "viem" with { "resolution-mode": "import" };
33
import type { TestClientMode } from "../types";
44

55
import memoize from "lodash.memoize";
66

77
import { UnknownDevelopmentNetworkError, NetworkNotFoundError } from "./errors";
88

99
export async function getChain(provider: EthereumProvider): Promise<Chain> {
10-
const { extractChain } = require("viem") as typeof import("viem");
11-
const chainsModule = require("viem/chains") as typeof import("viem/chains");
10+
const { extractChain } = require("viem") as typeof import("viem", {
11+
with: { "resolution-mode": "import" },
12+
});
13+
const chainsModule = require("viem/chains") as typeof import("viem/chains", {
14+
with: { "resolution-mode": "import" },
15+
});
1216
const chains = Object.values(chainsModule) as Chain[];
1317
const chainId = await getChainId(provider);
1418

‎packages/hardhat-viem/src/internal/clients.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
PublicClientConfig,
66
WalletClientConfig,
77
TestClientConfig,
8-
} from "viem";
8+
} from "viem" with { "resolution-mode": "import" };
99
import type {
1010
PublicClient,
1111
TestClient,
@@ -56,7 +56,9 @@ export async function innerGetPublicClient(
5656
chain: Chain,
5757
publicClientConfig?: Partial<PublicClientConfig>
5858
): Promise<PublicClient> {
59-
const viem = require("viem") as typeof import("viem");
59+
const viem = require("viem") as typeof import("viem", {
60+
with: { "resolution-mode": "import" },
61+
});
6062
const { clientParameters, transportParameters } = await getParameters(
6163
chain,
6264
publicClientConfig
@@ -97,7 +99,9 @@ export async function innerGetWalletClients(
9799
accounts: Address[],
98100
walletClientConfig?: Partial<WalletClientConfig>
99101
): Promise<WalletClient[]> {
100-
const viem = require("viem") as typeof import("viem");
102+
const viem = require("viem") as typeof import("viem", {
103+
with: { "resolution-mode": "import" },
104+
});
101105
const { clientParameters, transportParameters } = await getParameters(
102106
chain,
103107
walletClientConfig
@@ -161,7 +165,9 @@ export async function innerGetTestClient(
161165
mode: TestClientMode,
162166
testClientConfig?: Partial<TestClientConfig>
163167
): Promise<TestClient> {
164-
const viem = require("viem") as typeof import("viem");
168+
const viem = require("viem") as typeof import("viem", {
169+
with: { "resolution-mode": "import" },
170+
});
165171
const { clientParameters, transportParameters } = await getParameters(
166172
chain,
167173
testClientConfig

‎packages/hardhat-viem/src/internal/contracts.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import type {
22
EthereumProvider,
33
HardhatRuntimeEnvironment,
44
} from "hardhat/types";
5-
import type { Abi, Address, Hex } from "viem";
5+
import type { Abi, Address, Hex } from "viem" with {
6+
"resolution-mode": "import",
7+
};
68
import type {
79
DeployContractConfig,
810
GetContractAtConfig,
@@ -191,7 +193,9 @@ async function innerSendDeploymentTransaction(
191193
hash: deploymentTxHash,
192194
});
193195

194-
const { getContractAddress } = require("viem") as typeof import("viem");
196+
const { getContractAddress } = require("viem") as typeof import("viem", {
197+
with: { "resolution-mode": "import" },
198+
});
195199
const contractAddress = getContractAddress({
196200
from: walletClient.account.address,
197201
nonce: BigInt(deploymentTx.nonce),
@@ -234,7 +238,9 @@ async function innerGetContractAt(
234238
contractAbi: Abi,
235239
address: Address
236240
): Promise<GetContractReturnType> {
237-
const viem = require("viem") as typeof import("viem");
241+
const viem = require("viem") as typeof import("viem", {
242+
with: { "resolution-mode": "import" },
243+
});
238244
const contract = viem.getContract({
239245
address,
240246
client: {

‎packages/hardhat-viem/src/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type * as viemT from "viem";
1+
import type * as viemT from "viem" with { "resolution-mode": "import" };
22
import type { ArtifactsMap } from "hardhat/types/artifacts";
33
import type { Libraries } from "./internal/bytecode";
44

‎packages/hardhat-viem/test/chains.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { EthereumProvider } from "hardhat/types";
22

33
import { expect, assert } from "chai";
44
import sinon from "sinon";
5-
import * as chains from "viem/chains";
65

76
import {
87
getChain,
@@ -11,6 +10,10 @@ import {
1110
} from "../src/internal/chains";
1211
import { EthereumMockedProvider } from "./mocks/provider";
1312

13+
const chains = require("viem/chains") as typeof import("viem/chains", {
14+
with: { "resolution-mode": "import" },
15+
});
16+
1417
describe("chains", () => {
1518
describe("getChain", () => {
1619
afterEach(sinon.restore);

‎packages/hardhat-viem/test/clients.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { EthereumProvider } from "hardhat/types";
22

33
import { assert, expect } from "chai";
4-
import * as chains from "viem/chains";
54

65
import {
76
innerGetPublicClient,
@@ -10,6 +9,10 @@ import {
109
} from "../src/internal/clients";
1110
import { EthereumMockedProvider } from "./mocks/provider";
1211

12+
const chains = require("viem/chains") as typeof import("viem/chains", {
13+
with: { "resolution-mode": "import" },
14+
});
15+
1316
describe("clients", () => {
1417
describe("innerGetPublicClient", () => {
1518
it("should return a public client", async () => {

‎packages/hardhat-viem/test/integration.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import type { Hex, TransactionReceipt } from "viem";
1+
import type { Hex, TransactionReceipt } from "viem" with {
2+
"resolution-mode": "import",
3+
};
24
import type { EthereumProvider } from "hardhat/types";
35

46
import path from "path";
57
import { assert, expect } from "chai";
68
import sinon from "sinon";
7-
import { getAddress, parseEther } from "viem";
89

910
import { TASK_CLEAN, TASK_COMPILE } from "hardhat/builtin-tasks/task-names";
1011
import { deployContract, innerDeployContract } from "../src/internal/contracts";
1112
import { EthereumMockedProvider } from "./mocks/provider";
1213
import { assertSnapshotMatch, sleep, useEnvironment } from "./helpers";
1314

15+
const { getAddress, parseEther } = require("viem") as typeof import("viem", {
16+
with: { "resolution-mode": "import" },
17+
});
18+
1419
describe("Integration tests", function () {
1520
afterEach(function () {
1621
sinon.restore();

0 commit comments

Comments
 (0)