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

feat: build.assetsInlineLimit callback #15366

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/config/build-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ Specify the directory to nest generated assets under (relative to `build.outDir`

## build.assetsInlineLimit

- **Type:** `number`
- **Type:** `number` | `((filePath: string, content: Buffer) => boolean | undefined)`
- **Default:** `4096` (4 KiB)

Imported or referenced assets that are smaller than this threshold will be inlined as base64 URLs to avoid extra http requests. Set to `0` to disable inlining altogether.

If a callback is passed, a boolean can be returned to opt-in or opt-out. If nothing is returned the default logic applies.

Git LFS placeholders are automatically excluded from inlining because they do not contain the content of the file they represent.

::: tip Note
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ export interface BuildOptions {
* base64 strings. Default limit is `4096` (4 KiB). Set to `0` to disable.
* @default 4096
*/
assetsInlineLimit?: number
assetsInlineLimit?:
| number
| ((filePath: string, content: Buffer) => boolean | undefined)
/**
* Whether to code-split CSS. When enabled, CSS in async chunks will be
* inlined as strings in the chunk and inserted via dynamically created
Expand Down
27 changes: 17 additions & 10 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,25 @@ async function fileToBuiltUrl(
const file = cleanUrl(id)
const content = await fsp.readFile(file)

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))
}
const finalShouldInline = (() => {
if (config.build.lib) return true
let limit: number
if (typeof config.build.assetsInlineLimit === 'function') {
const userShouldInline = config.build.assetsInlineLimit(file, content)
if (userShouldInline != null) return userShouldInline
limit = 4096
ArnaudBarre marked this conversation as resolved.
Show resolved Hide resolved
} else {
limit = Number(config.build.assetsInlineLimit)
}
if (shouldInline != null) return shouldInline
bluwy marked this conversation as resolved.
Show resolved Hide resolved
if (file.endsWith('.html')) return false
// Don't inline SVG with fragments, as they are meant to be reused
if (file.endsWith('.svg') && id.includes('#')) return false
return content.length < limit && !isGitLfsPlaceholder(content)
})()

let url: string
if (shouldInline) {
if (finalShouldInline) {
if (config.build.lib && isGitLfsPlaceholder(content)) {
config.logger.warn(
colors.yellow(`Inlined file ${id} was not downloaded via Git LFS`),
Expand Down
3 changes: 2 additions & 1 deletion playground/worker/vite.config-es.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default defineConfig({
},
build: {
outDir: 'dist/es',
assetsInlineLimit: 100, // keep SVG as assets URL
assetsInlineLimit: (filePath) =>
filePath.endsWith('.svg') ? false : undefined,
rollupOptions: {
output: {
assetFileNames: 'assets/[name].[ext]',
Expand Down
3 changes: 2 additions & 1 deletion playground/worker/vite.config-iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export default defineConfig({
},
build: {
outDir: 'dist/iife',
assetsInlineLimit: 100, // keep SVG as assets URL
assetsInlineLimit: (filePath) =>
filePath.endsWith('.svg') ? false : undefined,
manifest: true,
rollupOptions: {
output: {
Expand Down
3 changes: 2 additions & 1 deletion playground/worker/vite.config-relative-base-iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default defineConfig(({ isPreview }) => ({
},
build: {
outDir: 'dist/relative-base-iife',
assetsInlineLimit: 100, // keep SVG as assets URL
assetsInlineLimit: (filePath) =>
filePath.endsWith('.svg') ? false : undefined,
rollupOptions: {
output: {
assetFileNames: 'other-assets/[name]-[hash].[ext]',
Expand Down
3 changes: 2 additions & 1 deletion playground/worker/vite.config-relative-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default defineConfig(({ isPreview }) => ({
},
build: {
outDir: 'dist/relative-base',
assetsInlineLimit: 100, // keep SVG as assets URL
assetsInlineLimit: (filePath) =>
filePath.endsWith('.svg') ? false : undefined,
rollupOptions: {
output: {
assetFileNames: 'other-assets/[name]-[hash].[ext]',
Expand Down
3 changes: 2 additions & 1 deletion playground/worker/worker-sourcemap-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export default (sourcemap) => {
},
build: {
outDir: `dist/iife-${typeName}/`,
assetsInlineLimit: 100, // keep SVG as assets URL
assetsInlineLimit: (filePath) =>
filePath.endsWith('.svg') ? false : undefined,
sourcemap: sourcemap,
rollupOptions: {
output: {
Expand Down
Loading