Skip to content

Commit

Permalink
fix(html): error while removing vite-ignore attribute for inline sc…
Browse files Browse the repository at this point in the history
…ript (#19062)
  • Loading branch information
btea authored Dec 31, 2024
1 parent e1b520c commit a492253
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
12 changes: 6 additions & 6 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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*(.)/
Expand Down Expand Up @@ -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) {
Expand All @@ -459,7 +459,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
// referencing public dir url, prefix with base
overwriteAttrValue(
s,
sourceCodeLocation!,
srcSourceCodeLocation!,
partialEncodeURIPath(toOutputPublicFilePath(url)),
)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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')
Expand Down
1 change: 1 addition & 0 deletions playground/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1>Hello</h1>

<div>External path: <span class="external-path"></span></div>
<script type="module" src="/external-path.js" vite-ignore></script>
<script type="module" vite-ignore></script>
<link rel="stylesheet" href="/external-path.css" vite-ignore />
<div>
External path by rollupOptions.external (build only):
Expand Down

0 comments on commit a492253

Please sign in to comment.