Skip to content

Commit a492253

Browse files
authored
fix(html): error while removing vite-ignore attribute for inline script (#19062)
1 parent e1b520c commit a492253

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

packages/vite/src/node/plugins/html.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,13 @@ export async function traverseHtml(
199199

200200
export function getScriptInfo(node: DefaultTreeAdapterMap['element']): {
201201
src: Token.Attribute | undefined
202-
sourceCodeLocation: Token.Location | undefined
202+
srcSourceCodeLocation: Token.Location | undefined
203203
isModule: boolean
204204
isAsync: boolean
205205
isIgnored: boolean
206206
} {
207207
let src: Token.Attribute | undefined
208-
let sourceCodeLocation: Token.Location | undefined
208+
let srcSourceCodeLocation: Token.Location | undefined
209209
let isModule = false
210210
let isAsync = false
211211
let isIgnored = false
@@ -214,7 +214,7 @@ export function getScriptInfo(node: DefaultTreeAdapterMap['element']): {
214214
if (p.name === 'src') {
215215
if (!src) {
216216
src = p
217-
sourceCodeLocation = node.sourceCodeLocation?.attrs!['src']
217+
srcSourceCodeLocation = node.sourceCodeLocation?.attrs!['src']
218218
}
219219
} else if (p.name === 'type' && p.value && p.value === 'module') {
220220
isModule = true
@@ -224,7 +224,7 @@ export function getScriptInfo(node: DefaultTreeAdapterMap['element']): {
224224
isIgnored = true
225225
}
226226
}
227-
return { src, sourceCodeLocation, isModule, isAsync, isIgnored }
227+
return { src, srcSourceCodeLocation, isModule, isAsync, isIgnored }
228228
}
229229

230230
const attrValueStartRE = /=\s*(.)/
@@ -447,7 +447,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
447447

448448
// script tags
449449
if (node.nodeName === 'script') {
450-
const { src, sourceCodeLocation, isModule, isAsync, isIgnored } =
450+
const { src, srcSourceCodeLocation, isModule, isAsync, isIgnored } =
451451
getScriptInfo(node)
452452

453453
if (isIgnored) {
@@ -459,7 +459,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
459459
// referencing public dir url, prefix with base
460460
overwriteAttrValue(
461461
s,
462-
sourceCodeLocation!,
462+
srcSourceCodeLocation!,
463463
partialEncodeURIPath(toOutputPublicFilePath(url)),
464464
)
465465
}

packages/vite/src/node/server/middlewares/indexHtml.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ const devHtmlHook: IndexHtmlTransformHook = async (
264264

265265
// script tags
266266
if (node.nodeName === 'script') {
267-
const { src, sourceCodeLocation, isModule, isIgnored } =
267+
const { src, srcSourceCodeLocation, isModule, isIgnored } =
268268
getScriptInfo(node)
269269

270270
if (isIgnored) {
271-
removeViteIgnoreAttr(s, sourceCodeLocation!)
271+
removeViteIgnoreAttr(s, node.sourceCodeLocation!)
272272
} else if (src) {
273273
const processedUrl = processNodeUrl(
274274
src.value,
@@ -280,7 +280,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
280280
!isModule,
281281
)
282282
if (processedUrl !== src.value) {
283-
overwriteAttrValue(s, sourceCodeLocation!, processedUrl)
283+
overwriteAttrValue(s, srcSourceCodeLocation!, processedUrl)
284284
}
285285
} else if (isModule && node.childNodes.length) {
286286
addInlineModule(node, 'js')

playground/html/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ <h1>Hello</h1>
1212

1313
<div>External path: <span class="external-path"></span></div>
1414
<script type="module" src="/external-path.js" vite-ignore></script>
15+
<script type="module" vite-ignore></script>
1516
<link rel="stylesheet" href="/external-path.css" vite-ignore />
1617
<div>
1718
External path by rollupOptions.external (build only):

0 commit comments

Comments
 (0)