unjs/unimport

`toExports` strips anything that look like an extension from package names

GerryWilko opened this issue · 2 comments

Environment

unimport: 3.0.11
node: v18.16.1

Reproduction

I raised this issue initially in Nuxt but soon realised the issue appears to come from this package.

nuxt/nuxt#23930

When using toExports with any export name containing anything that looks like an extension such as chart.js the issue presents.

Describe the bug

The issue is caused by:

name = name.replace(/\.[a-z]+$/, '')

This attempts to strip anything that looks like an extension from the name resulting in the incorrectly generated import from something like chart.js of just chart.

As a temporary workaround you can simply change it to chart.JS which avoids this regex matching. However this may also be an issue that this extension stripping should perhaps not be case-sensitive?

Not sure how to achieve this but the extension stripping should never be done on packages. Could we perhaps change the matcher to only concern itself with something which starts with ./ or a multi part filepath always ignoring the first segment?

Additional context

No response

Logs

No response

Just a note for anyone that tries the workaround it may not be successful in CI as both my GitHub builds and Vercel builds fail to find chart.JS due to casing of the JS.

@manniL is there anyone who might be able to take a look at the PR I raised over the weekend? Thanks!