sveltejs / svelte-loader

Webpack loader for svelte components.
MIT License
594 stars 73 forks source link

How can i use <img src='xxx.png'> without require? #196

Closed BaronJHYu closed 2 years ago

BaronJHYu commented 2 years ago

image image

only ‘require’ usable. Is it some wrong with svelte-loader?

BaronJHYu commented 2 years ago

or how can i config to make it usable?

mcuppi commented 2 years ago

@BaronJHYu If I understand your question correctly, you're asking why src="../images/close.png" fails, but src={closePic} works.

The reason the latter example works is because you make Webpack "aware" of the asset. By importing the image, you're defining an explicit dependency. This allows Webpack to "see" the asset and handle it according to the rules you defined in the configuration file.

Webpack won't be able to manage your assets if you just reference them as string somewhere in your code (e.g., src="../images/close.png"). As far as I understand, Webpack doesn't automatically parse through all of your code in an attempt to find strings that look like asset URLs.

For more information about asset management, please refer to Webpack's asset management documentation.

@dummdidumm; I believe this issue should be closed as I'm fairly confident this question has more to do with Webpack than svelte-loader. Additionally, if we want to track this as a feature request, #114 would probably be a better issue to use.