From 9deeec925e2db08e6e3bec540aabcda6cb9d19f5 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Sun, 3 Sep 2023 23:41:25 -0500 Subject: [PATCH 1/9] refactor: Ensure HTML is stripped of generated blank lines --- packages/vite/src/node/plugins/html.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 916abdf9c69f15..c1633c5b863266 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -588,7 +588,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { } } - processedHtml.set(id, s.toString()) + processedHtml.set(id, s.toString().replace(/(^\s*\n\s*|\s*\n\s*$)/gm, '')) // inject module preload polyfill only when configured and needed const { modulePreload } = config.build From 929074f402447d2b45e622f1a88435df76bd6276 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Sun, 3 Sep 2023 23:52:08 -0500 Subject: [PATCH 2/9] style: Fix formatting --- packages/vite/src/node/plugins/html.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index c1633c5b863266..0a67fbda1af3e1 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -588,7 +588,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { } } - processedHtml.set(id, s.toString().replace(/(^\s*\n\s*|\s*\n\s*$)/gm, '')) + processedHtml.set( + id, + s.toString().replace(/(^\s*\n\s*|\s*\n\s*$)/gm, ''), + ) // inject module preload polyfill only when configured and needed const { modulePreload } = config.build From c1fad948598075a27f6e193665195c6bc747ec42 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Mon, 4 Sep 2023 03:33:53 -0500 Subject: [PATCH 3/9] revert: Undo 9deeec9 --- packages/vite/src/node/plugins/html.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 0a67fbda1af3e1..916abdf9c69f15 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -588,10 +588,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { } } - processedHtml.set( - id, - s.toString().replace(/(^\s*\n\s*|\s*\n\s*$)/gm, ''), - ) + processedHtml.set(id, s.toString()) // inject module preload polyfill only when configured and needed const { modulePreload } = config.build From 30995745de25fc328df3ec86d71779c33e40a138 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Mon, 4 Sep 2023 03:35:07 -0500 Subject: [PATCH 4/9] refactor: Better method of ensuring generated HTML doesn't leave blank lines --- packages/vite/src/node/plugins/html.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 916abdf9c69f15..b9c46fe02af15d 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -323,6 +323,20 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { config, publicToRelative, ) + const nodeStartWithLeadingWhitespace = ( + node: DefaultTreeAdapterMap, + ) => { + const lineStartOffset = + node.sourceCodeLocation!.startOffset - + node.sourceCodeLocation!.startCol + const line = s.slice( + lineStartOffset, + node.sourceCodeLocation!.startOffset, + ) + return /\S/.test(line) + ? node.sourceCodeLocation!.startOffset + : lineStartOffset + } // pre-transform html = await applyHtmlTransforms(html, preHooks, { @@ -444,7 +458,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { const importExpression = `\nimport ${JSON.stringify(url)}` styleUrls.push({ url, - start: node.sourceCodeLocation!.startOffset, + start: nodeStartWithLeadingWhitespace(node), end: node.sourceCodeLocation!.endOffset, }) js += importExpression @@ -516,7 +530,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { // remove the script tag from the html. we are going to inject new // ones in the end. s.remove( - node.sourceCodeLocation!.startOffset, + nodeStartWithLeadingWhitespace(node), node.sourceCodeLocation!.endOffset, ) } From 1404bd231fdd1c4582757ab54a287f275bd7fb2c Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Mon, 4 Sep 2023 03:43:06 -0500 Subject: [PATCH 5/9] fix: Correct type signature? --- packages/vite/src/node/plugins/html.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index b9c46fe02af15d..78b484b7ac55df 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -324,7 +324,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { publicToRelative, ) const nodeStartWithLeadingWhitespace = ( - node: DefaultTreeAdapterMap, + node: DefaultTreeAdapterMap['node'], ) => { const lineStartOffset = node.sourceCodeLocation!.startOffset - From e756d5749931a9d14f0ff5a37e266686416a17bf Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Mon, 4 Sep 2023 05:22:18 -0500 Subject: [PATCH 6/9] fix: Account for HTML that starts with to be removed node --- packages/vite/src/node/plugins/html.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 78b484b7ac55df..08e61b95a780a1 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -326,6 +326,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { const nodeStartWithLeadingWhitespace = ( node: DefaultTreeAdapterMap['node'], ) => { + if (node.sourceCodeLocation!.startOffset == 0) + return node.sourceCodeLocation!.startOffset const lineStartOffset = node.sourceCodeLocation!.startOffset - node.sourceCodeLocation!.startCol From 645fbbf49da363b39da7820158d837b51f5665c2 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Wed, 6 Sep 2023 17:33:04 -0500 Subject: [PATCH 7/9] chore: Correct equality check & add comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 / green --- packages/vite/src/node/plugins/html.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 08e61b95a780a1..79719a5440e5bd 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -326,8 +326,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { const nodeStartWithLeadingWhitespace = ( node: DefaultTreeAdapterMap['node'], ) => { - if (node.sourceCodeLocation!.startOffset == 0) + if (node.sourceCodeLocation!.startOffset === 0) return node.sourceCodeLocation!.startOffset + + // Gets the offset for the start of the line that the node is on const lineStartOffset = node.sourceCodeLocation!.startOffset - node.sourceCodeLocation!.startCol From 9754e25d0bc4b5b2296c9e9951e45c14c6547531 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Sun, 10 Sep 2023 03:44:28 -0500 Subject: [PATCH 8/9] chore: Update comment --- packages/vite/src/node/plugins/html.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 79719a5440e5bd..6f770d02a6ac2e 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -329,7 +329,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { if (node.sourceCodeLocation!.startOffset === 0) return node.sourceCodeLocation!.startOffset - // Gets the offset for the start of the line that the node is on + // Gets the offset for the start of the line including the + // newline trailing the previous node const lineStartOffset = node.sourceCodeLocation!.startOffset - node.sourceCodeLocation!.startCol From 76327fb517955e4486e6e533d9b0c1d0567af9e9 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Mon, 9 Oct 2023 00:33:17 -0500 Subject: [PATCH 9/9] refactor: Update content detection, add clarifying comments --- packages/vite/src/node/plugins/html.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 6f770d02a6ac2e..b879d4066874a6 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -323,6 +323,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { config, publicToRelative, ) + // Determines true start position for the node, either the < character + // position, or the newline at the end of the previous line's node. const nodeStartWithLeadingWhitespace = ( node: DefaultTreeAdapterMap['node'], ) => { @@ -338,7 +340,19 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { lineStartOffset, node.sourceCodeLocation!.startOffset, ) - return /\S/.test(line) + + // + // + // + // Here we want to target the newline at the end of the previous line + // as the start position for our target. + // + // + // + // + // However, if there is content between our target node start and the + // previous newline, we cannot strip it out without risking content deletion. + return line.trim() ? node.sourceCodeLocation!.startOffset : lineStartOffset }