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

base or cwd option for import.meta.glob #17453

Open
4 tasks done
Rich-Harris opened this issue Jun 12, 2024 · 0 comments · May be fixed by #17625
Open
4 tasks done

base or cwd option for import.meta.glob #17453

Rich-Harris opened this issue Jun 12, 2024 · 0 comments · May be fixed by #17625
Labels
enhancement New feature or request good first issue Good for newcomers p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@Rich-Harris
Copy link
Contributor

Description

Today, import.meta.glob returns an object whose keys are relative paths (unless using an alias, in which case the behaviour is slightly unexpected — #12180):

const stuff = import.meta.glob('./path/to/my/stuff/**/*.md', {
  eager: true,
  query: '?url'
});

/*
{
  './path/to/my/stuff/a/b/c.md': {...},
  './path/to/my/stuff/d/e/f.md': {...}
}
*/

Working with this object is slightly cumbersome. If you want a specific module, you have to do this...

const abc = stuff[`./path/to/my/stuff/a/b/c.md`];

...and if you're iterating over the keys you have to do some ugly string manipulation:

for (const key in stuff) {
  const slug = key.slice('./path/to/my/stuff/'.length, -'.md'.length);
  // ...
}

If you're trying to make that logic reusable, you have to pass around a prefix — this is brittle, because you could easily change the argument to glob (because you moved some files) but forget to update base:

const index = createIndex(stuff, {
  base: './path/to/my/stuff'
});

Suggested solution

A straightforward solution that would result in cleaner code and that could (I think?) be implemented without a breaking change could be to add a base option to the glob arguments:

const stuff = import.meta.glob('**/*.md', {
  base: './path/to/my/stuff',
  eager: true,
  query: '?url'
});

/*
{
  'a/b/c.md': {...},
  'd/e/f.md': {...}
}
*/

const abc = stuff[`a/b/c.md`];

for (const key in stuff) {
  const slug = key.slice(0, -'.md'.length);
  // ...
}

const index = createIndex(stuff);

Every globbing library (including fast-glob which Vite uses IIUC) has some version of this — the option is usually called cwd, and as such there's an argument for calling it cwd rather than base. The counterargument is that cwd is always resolved relative to the actual cwd, rather than the file in which it's used, and so maybe there's a slight conceptual mismatch. I think either would be fine.

Alternative

Taking the idea further, it could be prefix and suffix (i.e. allowing you to automatically remove suffixes like .md), though I suspect that's unnecessary complexity.

Additional context

No response

Validations

@benmccann benmccann added enhancement New feature or request good first issue Good for newcomers p2-nice-to-have Not breaking anything but nice to have (priority) and removed enhancement: pending triage labels Jul 5, 2024
Nullaha added a commit to Nullaha/vite that referenced this issue Jul 6, 2024
@Nullaha Nullaha linked a pull request Jul 6, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants