Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(html): error while removing vite-ignore attribute for inline script #19062

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved

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
Loading