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

Support import.meta.glob without parent path #16130

Closed
4 tasks done
otomad opened this issue Mar 11, 2024 · 2 comments
Closed
4 tasks done

Support import.meta.glob without parent path #16130

otomad opened this issue Mar 11, 2024 · 2 comments
Labels
duplicate This issue or pull request already exists

Comments

@otomad
Copy link

otomad commented Mar 11, 2024

Description

When using import.meta.glob:

const getIcon = (name) =>
    import.meta.glob('/src/assets/icons/*.svg', {
        eager: true,
    })[`/src/assets/icons/${name}.svg`]

Did you see that, you have to write "/src/assets/icons/" twice. When this path is longer, it becomes more troublesome.

We known that we can't use variable in it, like this won't work:

const PATH = '/src/assets/icons/'
const getIcon = (name) =>
    import.meta.glob(`${PATH}*.svg`, {
        eager: true,
    })[`${PATH}${name}.svg`]

Suggested solution

How about add a new option, to match the asterisk in the result object key. For example:

const icons = import.meta.glob('/src/assets/icons/*.svg', {
    withoutParentPath: true, // or some key else
})

This will compile to:

const icons = {
    'foo': () => import('/src/assets/icons/foo.svg'),
    'bar': () => import('/src/assets/icons/bar.svg'),
}

Alternative

No response

Additional context

No response

Validations

@jophy-ye
Copy link

Personally, I come across the same issue when I have to import over 10 images. Personally, besides withoutParentPath which only returns the filename, I think we might also augment the glob syntax (e.g. with any brackets) to match but not include part of the path. This is already doable with regular expressions, but seems to be uncommon in the glob world.

Meanwhile, a work-around is to use ${PATH} for all except the initial import, where we write the full path. You still need to write it twice, but in a case like mine (over 10 imgs) it’s much better.

@sapphi-red
Copy link
Member

I'll close this as a duplicate of #17453 as that proposal is more generic than this one.

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Nov 12, 2024
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed enhancement: pending triage labels Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants