Skip to content

Automatically static asset handling #863

Open
@alkorlos

Description

@alkorlos

Describe the problem

In Svetle, basic and important functionality from Vite doesn't work — automatic handling of static resources from component folder only using HTML.

When initializing a new project in Svelte, there will be such lines:

<script>
  import svelteLogo from './assets/svelte.svg'
</script>

<img src={svelteLogo} class="logo svelte" alt="Svelte Logo" />

It works, but I would like to have the ability to use notation like native HTML:

<img src="./assets/svelte.svg" class="logo svelte" alt="Svelte Logo" />

But this does not work.

In the documentation for Svelte and SvelteKit, I found information about this only in one place, in SvelteKit Images (I didn't find anything in the Svelte documentation on this topic). It says that when working with static resources, Svelte uses the Vite mechanism, which optimizes images: hashing in the name and assetsInlineLimit.

But in Vite, both methods work: importing in JS and specifying the path directly in src , and both trigger the optimizations mentioned above.

Documentation Vite Static Asset Handling about this:

Common image, media, and font filetypes are detected as assets automatically. You can extend the internal list using the assetsInclude option.

Repository with Vite example, with dist folder code vite-test-static

Describe the proposed solution

Is this missing of functionality or feature consciously not added?

If missing of functionality — it would be great to add a feature from Vite that allows working with static resources as in native HTML, without JavaScript.

If feature consciously not added — wonder why, especially considering that Svelte strives to adhere to web standards as much as possible. It would be good to add information to the Svelte documentation about working with static resources and why there is no possibility to work with them as in native HTML.

Alternatives considered

Using JS for add static resources:

<script>
  import svelteLogo from './assets/svelte.svg'
</script>

<img src={svelteLogo} class="logo svelte" alt="Svelte Logo" />

This only way to added hashing in the name and use assetsInlineLimit, static folder not did this.

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions