Skip to content

customizing object keys for import.meta.glob #10472

@randName

Description

@randName

Description

When using glob import I'm usually trying to do some manipulation of the path. This is the pattern which appears a lot of the time

const globbedArray = Object.entries(import.meta.glob('./routes/*.js'))
const routes = globbedArray.map(([name, item]) => {
  path = name.split('/').at(-1).split('.')[0]
  return [path, item]
})

which works great but the compiled output will contain those paths which aren't needed.

Suggested solution

const routes = import.meta.glob('./routes/*.js', {
  array: true,
  key: (path) => path.split('/').at(-1).split('.')[0],
})

where array: true just uses the Object.entries of the existing output, and key transforms the key used in the object (duplicate handling shouldn't be too much of a concern as it is up to the dev to write it appropriately).

looking at the current importMetaGlob.ts plugin it looks possible to do. if this proposal is accepted I am willing to submit a pr

Additional context

Apologies if I had missed any earlier discussion that is similar to this, most of the import glob issues that I see have been resolved by the current implementation

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions