Skip to content

Ignore TypeScript type-only exports/imports when transforming barrels #15

@rtatarinov

Description

@rtatarinov

Right now, babel-plugin-transform-barrels tries to process every exported/imported symbol from a barrel, including TypeScript type and interface declarations (type-only entities).
This leads to runtime errors such as:

barrelFile.getDirectSpecifierObject(...).toImportSpecifier is not a function

when a barrel contains export type {...} or export interface ... (or via export *) and the consumer imports it (directly or indirectly).

Expected behavior:
The plugin should skip type-only exports/imports (those with importKind: 'type' or exportKind: 'type') when generating transformed imports, because they have no runtime value.

Suggested fix:
Add a check in importDeclarationVisitor to skip processing if barrelFile.getDirectSpecifierObject(importedName) returns a falsy value (e.g., type-only).

Example patch:

--- a/node_modules/babel-plugin-transform-barrels/src/main.js
+++ b/node_modules/babel-plugin-transform-barrels/src/main.js
@@ -22,7 +22,13 @@ const importDeclarationVisitor = (path, state) => {
   const directSpecifierASTArray = []
   for (const specifier of importsSpecifiers) {
     const importedName = specifier?.imported?.name || "default";
-    const importSpecifier = barrelFile.getDirectSpecifierObject(importedName).toImportSpecifier();
+      const obj = barrelFile.getDirectSpecifierObject(importedName);
+
+      if (!obj) {
+          continue;
+      }
+
+      const importSpecifier = obj.toImportSpecifier();
     if (!importSpecifier.path) return;
     importSpecifier.localName = specifier.local.name;
     const transformedASTImport = AST.createASTImportDeclaration(importSpecifier);

This will prevent crashes when a barrel re-exports types/interfaces and allow the plugin to work correctly in TypeScript projects without manual patching.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions