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(html,asset): improve asset resolution #7643

Closed
wants to merge 2 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
23 changes: 23 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,29 @@ export default defineConfig({
If you specify `build.lib`, `build.assetsInlineLimit` will be ignored and assets will always be inlined, regardless of file size.
:::

### build.htmlAssetSources

- **Type:** `{ tag: string, attributes: string[], type: 'src' | 'srcset', filter?: (attribute: string, value: string, node: ElementNode) => boolean }`

Specify which HTML tags and attributes should be resolved as assets.

::: details Details: HTML tags and attributes that are resolved automatically

- the `src` attribute of the `audio` tag
- the `src` attribute of the `embed` tag
- the `src` and `srcset` attribute of the `img` tag
- the `src` attribute of the `input` tag
- the `data` attribute of the `object` tag
- the `src` and `srcset` attribute of the `source` tag
- the `src` attribute of the `track` tag
- the `src` and `poster` attribute of the `video` tag
- the `href` and `xlink:href` attribute of the `image` tag
- the `href` and `xlink:href` attribute of the `use` tag
- the `href` attribute of the `link` tag when the `rel` attribute contains `stylesheet`, `icon`, `shortcut icon`, `mask-icon`, `apple-touch-icon`, `apple-touch-icon-precomposed`, `apple-touch-startup-image`, `manifest`, `prefetch`, `preload` or when the `itemprop` attribute is `image`, `logo`, `screenshot`, `thumbnailurl`, `contenturl`, `downloadurl`, `duringmedia`, `embedurl`, `installurl`, `layoutimage`
- the `imagesrcset` attribute of the `link` tag when the `rel` attribute contains `stylesheet`, `icon`, `shortcut icon`, `mask-icon`, `apple-touch-icon`, `apple-touch-icon-precomposed`, `apple-touch-startup-image`, `manifest`, `prefetch`, `preload`
- the `content` attribute of the `meta` tag when the `name` attribute is `msapplication-tileimage`, `msapplication-square70x70logo`, `msapplication-square150x150logo`, `msapplication-wide310x150logo`, `msapplication-square310x310logo`, `msapplication-config`, `twitter:image` or when the `property` attribute is `og:image`, `og:image:url`, `og:image:secure_url`, `og:audio`, `og:audio:secure_url`, `og:video`, `og:video:secure_url`, `vk:image` or when the `itemprop` attribute is `image`, `logo`, `screenshot`, `thumbnailurl`, `contenturl`, `downloadurl`, `duringmedia`, `embedurl`, `installurl`, `layoutimage`
:::

### build.cssCodeSplit

- **Type:** `boolean`
Expand Down
11 changes: 10 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import { loadFallbackPlugin } from './plugins/loadFallback'
import type { PackageData } from './packages'
import { watchPackageDataPlugin } from './packages'
import { ensureWatchPlugin } from './plugins/ensureWatch'
import type { HtmlAssetSource } from './plugins/html'
import { defaultHtmlAssetSources } from './plugins/html'

export interface BuildOptions {
/**
Expand Down Expand Up @@ -83,6 +85,12 @@ export interface BuildOptions {
* @default 4096
*/
assetsInlineLimit?: number
/**
* Html tags and their corresponding attributes
* that should be resolved as assets.
* @default {@link defaultHtmlAssetSources#}
*/
htmlAssetSources?: HtmlAssetSource[]
/**
* 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 @@ -237,7 +245,8 @@ export function resolveBuildOptions(raw?: BuildOptions): ResolvedBuildOptions {
warnOnError: true,
exclude: [/node_modules/],
...raw?.dynamicImportVarsOptions
}
},
htmlAssetSources: raw?.htmlAssetSources || defaultHtmlAssetSources
}

// handle special build targets
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type { Manifest, ManifestChunk } from './plugins/manifest'
export type { ResolveOptions, InternalResolveOptions } from './plugins/resolve'
export type { SplitVendorChunkCache } from './plugins/splitVendorChunk'
import type { ChunkMetadata } from './plugins/metadata'
export type { HtmlAssetSource } from './plugins/html'

export type {
WebSocketServer,
Expand Down
Loading