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: Fix missing support for named re-exports #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sai-cb
Copy link

@sai-cb sai-cb commented Dec 1, 2024

The library is currently missing re-export patterns where it isn't applying CJS interop correctly:

export { namedX } from "foo";

We discovered this upon investigating some build failures. Seemed like an helpful fix to bring back upstream.

});

expect(output.code).toBe(REEXPORT_OUTPUT);
});
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test case for the missing CJS interop wrapping, now supported.

minimatch(value, dependency),
);
}
return matchedDependencies[value];
Copy link
Author

@sai-cb sai-cb Dec 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unrelated optimization; Some memoizing here speeds the plugin to a good amount. This makes our builds 1s faster with no apparent downside.

const preambles: string[] = [];
let hasDynamicImportsToFix = false;
Copy link
Author

@sai-cb sai-cb Dec 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For simplicity, all fixes are included in toBeFixed.

The dynamicImportsToBeFixed is turned to a hasDynamicImportsToFix boolean to replace the one case where we used it as such (if dynamicImportsToBeFixed.length)

}
}
break;
default:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds support for ExportNamedDeclaration and applies the same logic to all nodes to be fixed to avoid duplicating code.

return;
}
const bottomUpToBeFixed = toBeFixed.reverse();

const ms = sourcemaps ? new MagicString(code) : null;
let counter = 1;
let specifierCounter = 1;
Copy link
Author

@sai-cb sai-cb Dec 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a unique counter for ExportNamedDeclaration specifiers.

code.slice(node.end);
}
continue;
}
Copy link
Author

@sai-cb sai-cb Dec 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was just moved. This is a slight behavior change; We no longer process dynamicImportsToBeFixed before bottomUpToBeFixed. Instead, we process all fixes in the (reversed) order that they are found in the module... Which seemed actually safer to me. But please review carefully.

continue;
}

if (node.type === "ExportNamedDeclaration") {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the code inside that if block is the new feature being added to handle re-exports.

@sai-cb sai-cb marked this pull request as ready for review December 1, 2024 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant