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

Config callback to define if asset should be inlined or not #15343

Closed
4 tasks done
freakzlike opened this issue Dec 13, 2023 · 1 comment
Closed
4 tasks done

Config callback to define if asset should be inlined or not #15343

freakzlike opened this issue Dec 13, 2023 · 1 comment

Comments

@freakzlike
Copy link

Description

As a developer using Vite I want to specify whether an asset should be inlined or not. In my use case I have a internal icon library with SVG files with fragments. This library is build in library mode with Vite.

I think there might be more use cases where specifc files/file types should be inlined or not.

I can submit a PR if you want.

Suggested solution

In module vite/src/node/plugins/asset.ts we could add a config callback, so that the developer can specify per file if this should be inlined or not.

Example:
vite.config.ts

export default defineConfig({
  build: {
    assetsInline: (file, id, content) => file.endsWith('.png')
  },
})

vite/src/node/plugins/asset.ts

if (shouldInline == null && config.build.assetsInline) {
  shouldInline = config.build.assetsInline(file, id content)
}

if (shouldInline == null) {
shouldInline =
!!config.build.lib ||
// Don't inline SVG with fragments, as they are meant to be reused
(!(file.endsWith('.svg') && id.includes('#')) &&
!file.endsWith('.html') &&
content.length < Number(config.build.assetsInlineLimit) &&
!isGitLfsPlaceholder(content))
}

Add some later stage, this could be merged into one config together with assetsInclude

Alternative

No response

Additional context

No response

Validations

@bluwy
Copy link
Member

bluwy commented Dec 22, 2023

I think this is a duplicate of #2173, we keep assets inlining config feature requests there.

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Dec 22, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jan 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants