From a4922537a8d705da7769d30626a0d846511fc124 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Tue, 31 Dec 2024 12:55:28 +0800 Subject: [PATCH] fix(html): error while removing `vite-ignore` attribute for inline script (#19062) --- packages/vite/src/node/plugins/html.ts | 12 ++++++------ .../vite/src/node/server/middlewares/indexHtml.ts | 6 +++--- playground/html/index.html | 1 + 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 2e9b16668f36d6..cb9bf87192258d 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -199,13 +199,13 @@ export async function traverseHtml( export function getScriptInfo(node: DefaultTreeAdapterMap['element']): { src: Token.Attribute | undefined - sourceCodeLocation: Token.Location | undefined + srcSourceCodeLocation: Token.Location | undefined isModule: boolean isAsync: boolean isIgnored: boolean } { let src: Token.Attribute | undefined - let sourceCodeLocation: Token.Location | undefined + let srcSourceCodeLocation: Token.Location | undefined let isModule = false let isAsync = false let isIgnored = false @@ -214,7 +214,7 @@ export function getScriptInfo(node: DefaultTreeAdapterMap['element']): { if (p.name === 'src') { if (!src) { src = p - sourceCodeLocation = node.sourceCodeLocation?.attrs!['src'] + srcSourceCodeLocation = node.sourceCodeLocation?.attrs!['src'] } } else if (p.name === 'type' && p.value && p.value === 'module') { isModule = true @@ -224,7 +224,7 @@ export function getScriptInfo(node: DefaultTreeAdapterMap['element']): { isIgnored = true } } - return { src, sourceCodeLocation, isModule, isAsync, isIgnored } + return { src, srcSourceCodeLocation, isModule, isAsync, isIgnored } } const attrValueStartRE = /=\s*(.)/ @@ -447,7 +447,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { // script tags if (node.nodeName === 'script') { - const { src, sourceCodeLocation, isModule, isAsync, isIgnored } = + const { src, srcSourceCodeLocation, isModule, isAsync, isIgnored } = getScriptInfo(node) if (isIgnored) { @@ -459,7 +459,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { // referencing public dir url, prefix with base overwriteAttrValue( s, - sourceCodeLocation!, + srcSourceCodeLocation!, partialEncodeURIPath(toOutputPublicFilePath(url)), ) } diff --git a/packages/vite/src/node/server/middlewares/indexHtml.ts b/packages/vite/src/node/server/middlewares/indexHtml.ts index 228dce2ae7befa..4ab89bcc6f5ed2 100644 --- a/packages/vite/src/node/server/middlewares/indexHtml.ts +++ b/packages/vite/src/node/server/middlewares/indexHtml.ts @@ -264,11 +264,11 @@ const devHtmlHook: IndexHtmlTransformHook = async ( // script tags if (node.nodeName === 'script') { - const { src, sourceCodeLocation, isModule, isIgnored } = + const { src, srcSourceCodeLocation, isModule, isIgnored } = getScriptInfo(node) if (isIgnored) { - removeViteIgnoreAttr(s, sourceCodeLocation!) + removeViteIgnoreAttr(s, node.sourceCodeLocation!) } else if (src) { const processedUrl = processNodeUrl( src.value, @@ -280,7 +280,7 @@ const devHtmlHook: IndexHtmlTransformHook = async ( !isModule, ) if (processedUrl !== src.value) { - overwriteAttrValue(s, sourceCodeLocation!, processedUrl) + overwriteAttrValue(s, srcSourceCodeLocation!, processedUrl) } } else if (isModule && node.childNodes.length) { addInlineModule(node, 'js') diff --git a/playground/html/index.html b/playground/html/index.html index 431b02b6581541..37554e86e7cd76 100644 --- a/playground/html/index.html +++ b/playground/html/index.html @@ -12,6 +12,7 @@