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: add assets inline exclude #6892

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,14 @@ export default defineConfig({
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.

::: tip Note
If you specify `build.lib`, `build.assetsInlineLimit` will be ignored and assets will always be inlined, regardless of file size.
If you specify `build.lib` or `build.assetsInlineExclude` , `build.assetsInlineLimit`will be ignored and assets will always be inlined, regardless of file size.
:::

### build.assetsInlineExclude

- **Type:** `string | RegExp | (string | RegExp)[]`
- **Default:** `[]`

### build.cssCodeSplit

- **Type:** `boolean`
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ export interface BuildOptions {
* @default 4096
*/
assetsInlineLimit?: number
/**
* Exclude paths that are less than the limit but do not want to be included
* @default []
*/
assetsInlineExclude?: string | RegExp | (string | RegExp)[]
/**
* 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 Expand Up @@ -243,6 +248,7 @@ export function resolveBuildOptions(raw?: BuildOptions): ResolvedBuildOptions {
outDir: 'dist',
assetsDir: 'assets',
assetsInlineLimit: 4096,
assetsInlineExclude: [],
cssCodeSplit: !raw?.lib,
cssTarget: false,
sourcemap: false,
Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { OutputOptions, PluginContext, RenderedChunk } from 'rollup'
import MagicString from 'magic-string'
import { createHash } from 'crypto'
import { normalizePath } from '../utils'
import { createFilter } from '@rollup/pluginutils'

export const assetUrlRE = /__VITE_ASSET__([a-z\d]{8})__(?:\$_(.*?)__)?/g

Expand Down Expand Up @@ -287,11 +288,15 @@ async function fileToBuiltUrl(

const file = cleanUrl(id)
const content = await fsp.readFile(file)
const assetsFilter = config.build.assetsInlineExclude
? createFilter(config.build.assetsInlineExclude)
: () => false

let url: string
if (
config.build.lib ||
(!file.endsWith('.svg') &&
!assetsFilter(file) &&
content.length < Number(config.build.assetsInlineLimit))
) {
// base64 inlined as a string
Expand Down