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

fix: make glob pattern supports nested paths #19622

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

OnlyWick
Copy link
Contributor

Description

closes: #19605

This fix supports matching nested paths by adding /** in the path.

I think it would be better to convert it like this.

before:

./foo/${filePath}   =>   ./foo/*
./foo/${dir}/index.js   =>   ./foo/*/index.js
./foo/${dir}.js   =>   ./foo/*.js
./foo/${dir}${file}.js   =>   ./foo/*.js
${file}.js   =>   *.js

after:

./foo/${filePath}   =>   ./foo/**
./foo/${dir}/index.js   =>   ./foo/**/index.js
./foo/${dir}.js   =>   ./foo/**/*.js
./foo/${dir}${file}.js   =>   ./foo/**/*.js
${file}.js   =>   *.js

The execution result is as follows.

Before

import micromatch from "micromatch";

const globs = ["/foo/*", "/foo/*.js", "/foo/*/foo.js"];
const paths = ["/foo/foo.js", "/foo/dir/foo.js"];

for (const glob of globs) {
  const matcher = micromatch.matcher(glob);
  const result = paths.map((p) => [p, matcher(p)]);

  console.log(`# ${glob}`);
  for (const res of result) {
    console.log(`- ${res[0]}: ${res[1]}`);
  }
  console.log();
}
# /foo/*
- /foo/foo.js: true
- /foo/dir/foo.js: false

# /foo/*.js
- /foo/foo.js: true
- /foo/dir/foo.js: false

# /foo/*/foo.js
- /foo/foo.js: false
- /foo/dir/foo.js: true
import micromatch from "micromatch";

const globs = ["/foo/**", "/foo/**/*.js", "/foo/**/foo.js"];
const paths = ["/foo/foo.js", "/foo/dir/foo.js"];

for (const glob of globs) {
  const matcher = micromatch.matcher(glob);
  const result = paths.map((p) => [p, matcher(p)]);

  console.log(`# ${glob}`);
  for (const res of result) {
    console.log(`- ${res[0]}: ${res[1]}`);
  }
  console.log();
}
# /foo/**
- /foo/foo.js: true
- /foo/dir/foo.js: true

# /foo/**/*.js
- /foo/foo.js: true
- /foo/dir/foo.js: true

# /foo/**/foo.js
- /foo/foo.js: true
- /foo/dir/foo.js: true

@OnlyWick
Copy link
Contributor Author

It seems to be a Windows path issue🤔. I don't have a computer with Windows.

@hi-ogawa
Copy link
Collaborator

Hey, thanks for the PR 👋

Choosing * over **/* was a deliberate decision to match import(`./some/${file}.js`) globbing behavior as discussed in #18194. So, we don't consider the current behavior as a bug. We'll need to discuss whether changing it back would make sense.

@OnlyWick
Copy link
Contributor Author

OK, it's really frustrating when recursive directory matching is not supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vite 6 does not load /assets/img/** as Vite 5 does
2 participants