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

import.meta.globEager('../assets/svgIcon/*.svg?raw') #5304

Closed
4 tasks done
lihanspace opened this issue Oct 15, 2021 · 4 comments
Closed
4 tasks done

import.meta.globEager('../assets/svgIcon/*.svg?raw') #5304

lihanspace opened this issue Oct 15, 2021 · 4 comments
Labels
enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@lihanspace
Copy link

lihanspace commented Oct 15, 2021

Clear and concise description of the problem

将资源批量引入为其他格式。
Batch import resources into other formats.

Suggested solution

import svg from '../assets/svgIcon/github.svg?raw'
console.log(svg)
// Now results 现在的结果
// <svg t="1634195145119" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"...

const files = mport.meta.globEager('../assets/svgIcon/*.svg?raw')
console.log(files)
// Now results 现在的结果
// {}

// Expected results 期望的结果
/*
{
  '../assets/svgIcon/github.svg': {
    raw: '<svg t="1634195145119" class="icon" viewBox="0 0 1024 1024" version="1.1" ...',
    ...
  },
  '../assets/svgIcon/logo.svg': {
    raw: '<svg t="1634195051463" class="icon" viewBox="0 0 1024 1024" version="1.1" ...',
    ...
  }
}
*/

Alternative

No response

Additional context

No response

Validations

@windole
Copy link

windole commented Oct 15, 2021

The glob patterns are treated like import specifiers: they must be either relative (start with ./) or absolute (start with /, resolved relative to project root).

@lihanspace
Copy link
Author

lihanspace commented Oct 15, 2021

The glob patterns are treated like import specifiers: they must be either relative (start with ./) or absolute (start with /, resolved relative to project root).

I know, but what I want is to batch import files from folders instead of batch convert paths. Like imoort svg from './assets/svgIcon/*.svg?raw', the path is followed by '?raw', which can import resources as strings, or vite can provide a new method to batch import static resources as strings.

@patak-dev
Copy link
Member

You can achieve this for regular glob using

const modules = import.meta.glob('./assets/svgIcon/*.svg')
const rawModules = {}
for (const path in modules) {
  rawModules[path] = () => import(`${path}?raw`)
}

But the same can't be done for globEager. And several users bumped into this already so I think we should provide support for this. It would be good if someone would issue a PR so we can discuss the details of the API there. Your code snippet looks like the correct way to add this feature but ? is a valid character in minimatch so we should check if we want to use ?raw here

const files = mport.meta.globEager('../assets/svgIcon/*.svg?raw')

Or pass this flag as a parameter like

const files = mport.meta.globEager('../assets/svgIcon/*.svg','raw')

@patak-dev patak-dev added enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority) and removed enhancement: pending triage labels Oct 15, 2021
@boehs
Copy link
Contributor

boehs commented Dec 29, 2021

@patak-dev closed!

@Niputi Niputi closed this as completed Dec 29, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jan 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

No branches or pull requests

5 participants