Skip to content

Commit

Permalink
feat: allow boolean attr values in html transform tag descriptors (#1381
Browse files Browse the repository at this point in the history
)
  • Loading branch information
stafyniaksacha committed Jan 5, 2021
1 parent 724aa8a commit 0fad96e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/guide/api-plugin.md
Expand Up @@ -263,7 +263,7 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo

interface HtmlTagDescriptor {
tag: string
attrs?: Record<string, string>
attrs?: Record<string, string | boolean>
children?: string | HtmlTagDescriptor[]
/**
* default: 'head-prepend'
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/plugins/html.ts
Expand Up @@ -303,7 +303,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {

export interface HtmlTagDescriptor {
tag: string
attrs?: Record<string, string>
attrs?: Record<string, string | boolean>
children?: string | HtmlTagDescriptor[]
/**
* default: 'head-prepend'
Expand Down Expand Up @@ -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
}

0 comments on commit 0fad96e

Please sign in to comment.