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

feat: support ignore option for glob import #2495

Closed

Conversation

vimcaw
Copy link

@vimcaw vimcaw commented Mar 13, 2021

Motivation

Sometimes we need to ignore some files or directories when using import.meta.glob, such as ignoring components or utils directories, files beginning with _ or ., etc.

A typical application scenario is:

In a file-based routing system, all files starting with lowercase letters and numbers in the pages directory will be automatically imported as routers by src/pages/**/[a-z0-9]*.tsx, and the following files or directory will be ignored:

  • Directories or files beginning with a capital letter (this will be regarded as a component)
  • components or utils directory
  • Files or directories beginning with . or _
  • Type definition files ending in d.ts
  • Test files ending with test.ts, spec.ts, e2e.ts

That will be difficult to achieve if only one pattern can be specified (make duplicated multiple level path rule in glob pattern is very difficult, See mrmlnc/fast-glob#306), but that can be easy if the ignore option of fast-glob can be specified.

Document

You can also pass the second argument to ignore some files:

const modules = import.meta.glob('./dir/*.js', '**/b*.*')

That will ignore all files whose name starts with b, the file ./dir/bar.js will be ignored, the above will be transformed into the following:

// code produced by vite
const modules = {
  './dir/foo.js': () => import('./dir/foo.js')
}

@vimcaw
Copy link
Author

vimcaw commented Mar 13, 2021

Seems it will be better to support multiple patterns instead of the ignore option, multiple patterns can also achieve the same effect of the ignore option and will be more powerful according to https://github.com/mrmlnc/fast-glob#ignore:

dir/
├── package-lock.json
└── package.json
fg.sync(['*.json', '!package-lock.json']);            // ['package.json']
fg.sync('*.json', { ignore: ['package-lock.json'] }); // ['package.json']

I will try to support multiple patterns.

@vimcaw
Copy link
Author

vimcaw commented Mar 13, 2021

Oops, this PR lacks some processing of HMR, I will add them when I have time.

@antfu antfu added the enhancement New feature or request label Mar 13, 2021
@yyx990803 yyx990803 added the p2-nice-to-have Not breaking anything but nice to have (priority) label Mar 15, 2021
@luoway
Copy link

luoway commented Aug 12, 2021

It will be helpful for me, to write like

import.meta.glob(`./**/index.vue`, { deep: 1 })

@hyf0
Copy link
Contributor

hyf0 commented Aug 22, 2021

I will suggest only support pattern like import.meta.glob('...', { ignore: ['....'] }) for better flexibility and consistency.

@@ -59,7 +60,7 @@ export async function transformImportGlob(
}
const files = glob.sync(pattern, {
cwd: base,
ignore: ['**/node_modules/**']
ignore: ['**/node_modules/**', ...(ignore ? [ignore] : [])]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also overwrite the default ignore option if the user explicit set ignore option. In that case, we could also close #1903 by import.meta.glob('...', { ignore: [] }).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other solution of #1903 would be something like import.meta.glob('...', { node_modules: true })

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will implement it later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should leave a ignoreNodeModules option (like your second solution) to support this, otherwise, the user will have to manually add the node_modules item every time the ignore option is used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm also prefer option like import.meta.glob('...', { node_modules: true }). We don't need to solve #1903 in this PR. Maybe it's better to let the vite team consider weather should and how to support the feature in #1903.

@antfu
Copy link
Member

antfu commented May 8, 2022

Close in favor of #7537 (targeting v3.0)

@antfu antfu closed this May 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants