unjs/unimport

Don't camel-case default exports if they're already in correct case

lehni opened this issue · 0 comments

lehni commented

Describe the bug

In a Nuxt 3 project I am trying to use the following config to make SFC components auto-importable in JS code:

export default defineNuxtConfig({
  imports: {
    dirs: ['components/**/*'],
  }

This almost works, but the components have to be referenced with camel-case names instead of their original pascal-case names, e.g. (from .nuxt/types/imports.d.ts):

const pageFooter: typeof import('../../components/PageFooter')['default']

This is caused by the use of camelCase() in this line here:

imports.push({ name: 'default', as: camelCase(name), from: filepath })

I looked at scule and it appears to split at '-', '_', '/', '.' to do the transformation:
https://github.com/unjs/scule/blob/249072ac10d1f441a23eb9df7bcb1bfc1e13d419/src/index.ts#L4

I'd like to suggest a change where the name is first inspected, and only if it contains any of these characters, will it be transformed using camelCase(). Otherwise it is passed on unmodified:

// Only camelize name if it contains separators by which scule would split,
// see STR_SPLITTERS: https://github.com/unjs/scule/blob/main/src/index.ts
const as = /[-_.]/.test(name) ? camelCase(name) : name
imports.push({ name: 'default', as, from: filepath })

Note that there is no need to check for / since it's only a file-name, not a path, at this point.