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

feat(findExports): extract name of default exports #179

Merged
merged 7 commits into from Jan 11, 2024

Conversation

mateenagy
Copy link
Contributor

Hi!

I am using unplugin-auto-import which rely on unimport which using this package findExports method. And found an edge case when it extract export default. The unplugin-auto-import can prevent using filename except when we use export default. In that case it will always looking for the file name and use it, but for example I use named default export like this:

exampleStore.ts:

const useExampleStore = defineStore({});
export default useExampleStore;

In the current version the findExports method it will always return the filename (exampleStore). In my case I want to get the name from the default (useExampleStore).

For this

  • I update the ESMExport interface with a new namedDefault in type.
  • I create a new regex for named default export and also update the EXPORT_DEFAULT_RE. I update the basic one to prevent duplication in the final exports. (I am not the best with regex so I think it can be written better but it works with every test without duplication).
  • Write the search in the findExports method and add it to the exports array

@mateenagy mateenagy mentioned this pull request Jul 10, 2023
@codecov
Copy link

codecov bot commented Jul 18, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (d212da8) 86.96% compared to head (8edaa84) 86.98%.

❗ Current head 8edaa84 differs from pull request most recent head 2f9f99c. Consider uploading reports for the commit 2f9f99c to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #179      +/-   ##
==========================================
+ Coverage   86.96%   86.98%   +0.01%     
==========================================
  Files           8        8              
  Lines        1036     1037       +1     
  Branches      168      168              
==========================================
+ Hits          901      902       +1     
  Misses        135      135              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pi0
Copy link
Member

pi0 commented Jan 11, 2024

Hi dear @mateenagy thanks for your pull request sorry for late review.

I think it is a really good idea to allow accessing variable name of default export but perhaps we don't need to introduce a new export type namedDefault.

We can detect both export default <name> and export { <name> as default } and add it as a new meta property to the results.

@pi0 pi0 changed the title feat(findExports): Support named default export feat(findExports): extract name of default exports Jan 11, 2024
@pi0 pi0 force-pushed the named-default-export branch 2 times, most recently from 3318e45 to e602bc1 Compare January 11, 2024 11:34
@pi0 pi0 merged commit 5213717 into unjs:main Jan 11, 2024
2 checks passed
pi0 added a commit that referenced this pull request Jan 11, 2024
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.

None yet

2 participants