Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for barrel file codegen of type-only files #898

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hungry-noodle-dryer.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 4 additions & 5 deletions packages/typechain/src/codegen/createBarrelFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down