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

new URL ,The packing volume increases 打包体积增大 #16506

Closed
7 tasks done
a982246809 opened this issue Apr 23, 2024 · 4 comments
Closed
7 tasks done

new URL ,The packing volume increases 打包体积增大 #16506

a982246809 opened this issue Apr 23, 2024 · 4 comments

Comments

@a982246809
Copy link

a982246809 commented Apr 23, 2024

Describe the bug

function getImageUrl(name) {
  return new URL(`./assets/imgs/${name}`, import.meta.url).href;
}
getImageUrl('home/aaa.jpg');

It seems to pack all the image addresses under imgs.
似乎会把imgs下的所有图片地址都打包
image

Reproduction

https://stackblitz.com/edit/vitejs-vite-irxn4k?file=main.js,package.json&terminal=dev

Steps to reproduce

npm run build

System Info

https://stackblitz.com/edit/vitejs-vite-irxn4k?file=main.js,package.json&terminal=dev

Used Package Manager

npm

Logs

No response

Validations

Copy link

stackblitz bot commented Apr 23, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sapphi-red
Copy link
Member

This is expected. Vite cannot analyze that name can only be home/aaa.jpg and thus it bundles all files in the imgs directory.

@a982246809
Copy link
Author

a982246809 commented Apr 25, 2024

@sapphi-red
Is there any way to solve it? I have used the getImageUrl method extensively in the project.
有什么可以办法可以解决吗 , 我已经在项目中大面积使用 getImageUrl 方法了

@sapphi-red
Copy link
Member

sapphi-red commented Apr 26, 2024

I guess using import.meta.glob would work. With the following code, Vite will only bundle a.jpg and b.jpg and c.jpg.

const modules = import.meta.glob('./assets/img/{a,b,c}.jpg', { eager: true })
function getImageUrl(name) {
  return modules[`./assets/imgs/${name}`]
}

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

No branches or pull requests

2 participants