diff --git a/.changeset/hungry-noodle-dryer.md b/.changeset/hungry-noodle-dryer.md new file mode 100644 index 000000000..610cbb926 --- /dev/null +++ b/.changeset/hungry-noodle-dryer.md @@ -0,0 +1,5 @@ +--- +'typechain': patch +--- + +Fix the import syntax for barrel file imports referring to type-only files. Fixes a problem with ESM compatibility when using the `--node16-modules` flag. \ No newline at end of file diff --git a/packages/typechain/src/codegen/createBarrelFiles.ts b/packages/typechain/src/codegen/createBarrelFiles.ts index f1e764db6..2df569e12 100644 --- a/packages/typechain/src/codegen/createBarrelFiles.ts +++ b/packages/typechain/src/codegen/createBarrelFiles.ts @@ -54,16 +54,15 @@ export function createBarrelFiles( .map((p) => { const namespaceIdentifier = normalizeDirName(p) + const fromFilePath = moduleSuffix ? `'./${p}/index${moduleSuffix}'` : `'./${p}'` + if (typeOnly) return [ - `import type * as ${namespaceIdentifier} from './${p}';`, + `import type * as ${namespaceIdentifier} from ${fromFilePath};`, `export type { ${namespaceIdentifier} };`, ].join('\n') - if (moduleSuffix) { - return `export * as ${namespaceIdentifier} from './${p}/index${moduleSuffix}';` - } - return `export * as ${namespaceIdentifier} from './${p}';` + return `export * as ${namespaceIdentifier} from ${fromFilePath};` }) .join('\n')