diff --git a/docs/guide/api-plugin.md b/docs/guide/api-plugin.md index 2dd1e0ede9bdf0..1b816248deadc0 100644 --- a/docs/guide/api-plugin.md +++ b/docs/guide/api-plugin.md @@ -263,7 +263,7 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo interface HtmlTagDescriptor { tag: string - attrs?: Record + attrs?: Record children?: string | HtmlTagDescriptor[] /** * default: 'head-prepend' diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 4e7ea502d6a1a1..129ace2048859d 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -303,7 +303,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { export interface HtmlTagDescriptor { tag: string - attrs?: Record + attrs?: Record children?: string | HtmlTagDescriptor[] /** * default: 'head-prepend' @@ -484,7 +484,11 @@ function serializeTags(tags: HtmlTagDescriptor['children']): string { function serializeAttrs(attrs: HtmlTagDescriptor['attrs']): string { let res = '' for (const key in attrs) { - res += ` ${key}=${JSON.stringify(attrs[key])}` + if (typeof attrs[key] === 'boolean' && attrs[key]) { + res += ` ${key}` + } else { + res += ` ${key}=${JSON.stringify(attrs[key])}` + } } return res }