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

Compiler option for bundler to provide optimization info about asset import #13298

Open
benmccann opened this issue Sep 17, 2024 · 0 comments
Open
Labels

Comments

@benmccann
Copy link
Member

benmccann commented Sep 17, 2024

Describe the problem

Developers and the bundler know that img is an asset in import img from './myimg.png';. However, the Svelte compiler does not know this, which limits the ability to inline it into the template and otherwise optimize it. The fact that it's an asset means it has a few key properties: it is a string, it is always defined, it is immutable, and it is the same on server and client so does not affect hydration.

Describe the proposed solution

Four options:

  • Harcode knowledge that .png, .jpg, .svg, etc. file extension represents an asset. This is probably a pretty safe assumption an would minimize API surface area. It's not the ideal long-term solution, but would be a quick win in terms of reducing the output file size
  • A compiler option such as isAsset. The bundler can provide a function which Svelte can call to determine whether an import URL is an asset. This is still relatively easy to implement, but introduces an API that may eventually be obsoleted by some more general API. I think this is okay as we could deprecate it when that time comes and as it's just an optimization is easy to ignore any value passed in that manner
  • Some more general API. This is impossible to do without an API because only the bundler knows if the same value will be returned both on the client side and server side (e.g. aliases or exports conditions). This cannot be implemented purely in Svelte (perf: inline default imports into template #13242) or purely in Vite (Write asset string directly into source file instead of an imported file vitejs/vite#18119) and requires the two to communicate. This could be something like:
bundler: {
  resolveId,
  load
}
  • vite-plugin-svelte could do some preprocessing to turn imports into constants so that they can be inlined in the templates

Importance

nice to have

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

No branches or pull requests

1 participant