-
Notifications
You must be signed in to change notification settings - Fork 4
Description
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.