From f557b4240a40558d8505085d3bdbee9c3a1aa705 Mon Sep 17 00:00:00 2001 From: Menci Date: Mon, 14 Feb 2022 04:37:40 +0800 Subject: [PATCH] fix(plugins/html): fix tags injected by previous plugins not visible to next --- packages/vite/src/node/plugins/html.ts | 34 ++++++++++++-------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index e7ca5e63961253..06c5e10d09551a 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -698,11 +698,6 @@ export async function applyHtmlTransforms( hooks: IndexHtmlTransformHook[], ctx: IndexHtmlTransformContext ): Promise { - const headTags: HtmlTagDescriptor[] = [] - const headPrependTags: HtmlTagDescriptor[] = [] - const bodyTags: HtmlTagDescriptor[] = [] - const bodyPrependTags: HtmlTagDescriptor[] = [] - for (const hook of hooks) { const res = await hook(html, ctx) if (!res) { @@ -718,6 +713,12 @@ export async function applyHtmlTransforms( html = res.html || html tags = res.tags } + + const headTags: HtmlTagDescriptor[] = [] + const headPrependTags: HtmlTagDescriptor[] = [] + const bodyTags: HtmlTagDescriptor[] = [] + const bodyPrependTags: HtmlTagDescriptor[] = [] + for (const tag of tags) { if (tag.injectTo === 'body') { bodyTags.push(tag) @@ -729,21 +730,12 @@ export async function applyHtmlTransforms( headPrependTags.push(tag) } } - } - } - // inject tags - if (headPrependTags.length) { - html = injectToHead(html, headPrependTags, true) - } - if (headTags.length) { - html = injectToHead(html, headTags) - } - if (bodyPrependTags.length) { - html = injectToBody(html, bodyPrependTags, true) - } - if (bodyTags.length) { - html = injectToBody(html, bodyTags) + html = injectToHead(html, headPrependTags, true) + html = injectToHead(html, headTags) + html = injectToBody(html, bodyPrependTags, true) + html = injectToBody(html, bodyTags) + } } return html @@ -777,6 +769,8 @@ function injectToHead( tags: HtmlTagDescriptor[], prepend = false ) { + if (tags.length === 0) return html + if (prepend) { // inject as the first element of head if (headPrependInjectRE.test(html)) { @@ -811,6 +805,8 @@ function injectToBody( tags: HtmlTagDescriptor[], prepend = false ) { + if (tags.length === 0) return html + if (prepend) { // inject after body open if (bodyPrependInjectRE.test(html)) {