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 @@

Hello

External path:
+
External path by rollupOptions.external (build only):