Skip to content

Commit 8a37de6

Browse files
authored
fix(scan): handle html script tag attributes that contain ">" (#13101)
1 parent 91d7b67 commit 8a37de6

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

packages/vite/src/node/optimizer/scan.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,8 @@ function globEntries(pattern: string | string[], config: ResolvedConfig) {
246246
})
247247
}
248248

249-
const scriptModuleRE =
250-
/(<script\b[^>]+type\s*=\s*(?:"module"|'module')[^>]*>)(.*?)<\/script>/gis
251-
export const scriptRE = /(<script(?:\s[^>]*>|>))(.*?)<\/script>/gis
249+
export const scriptRE =
250+
/(<script(?:\s+[a-z_:][-\w:]*(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^"'<>=\s]+))?)*\s*>)(.*?)<\/script>/gis
252251
export const commentRE = /<!--.*?-->/gs
253252
const srcRE = /\bsrc\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i
254253
const typeRE = /\btype\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i
@@ -378,19 +377,22 @@ function esbuildScanPlugin(
378377
// Avoid matching the content of the comment
379378
raw = raw.replace(commentRE, '<!---->')
380379
const isHtml = path.endsWith('.html')
381-
const regex = isHtml ? scriptModuleRE : scriptRE
382-
regex.lastIndex = 0
380+
scriptRE.lastIndex = 0
383381
let js = ''
384382
let scriptId = 0
385383
let match: RegExpExecArray | null
386-
while ((match = regex.exec(raw))) {
384+
while ((match = scriptRE.exec(raw))) {
387385
const [, openTag, content] = match
388386
const typeMatch = openTag.match(typeRE)
389387
const type =
390388
typeMatch && (typeMatch[1] || typeMatch[2] || typeMatch[3])
391389
const langMatch = openTag.match(langRE)
392390
const lang =
393391
langMatch && (langMatch[1] || langMatch[2] || langMatch[3])
392+
// skip non type module script
393+
if (isHtml && type !== 'module') {
394+
continue
395+
}
394396
// skip type="application/ld+json" and other non-JS types
395397
if (
396398
type &&

playground/optimize-deps/generics.vue

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!--
2+
https://github.com/vuejs/core/issues/8171
3+
https://github.com/vitejs/vite-plugin-vue/issues/162
4+
generic attribute includes angle brackets which breaks scanning
5+
This file only verifies that the scanner can work with such usage and nothing
6+
else.
7+
-->
8+
9+
<script lang="ts">
10+
export class Item<TValue> {
11+
value: TValue
12+
}
13+
</script>
14+
15+
<script setup lang="ts" generic="TItem extends Item<TValue>, TValue">
16+
defineProps<{
17+
items: TItem[]
18+
modelValue: TItem[]
19+
}>()
20+
</script>
21+
22+
<template>{{ items }}</template>

playground/optimize-deps/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ <h2>Pre bundle css require</h2>
165165
)
166166

167167
import './index.astro'
168+
import './generics.vue'
168169

169170
// All these imports should end up resolved to the same URL (same ?v= injected on them)
170171
import { add as addFromDirectAbsolutePath } from '/node_modules/@vitejs/test-dep-non-optimized/index.js'

playground/optimize-deps/vite.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ export default defineComponent({
121121
`.trim(),
122122
}
123123
}
124+
125+
// fallback to empty module for other vue files
126+
if (id.endsWith('.vue')) {
127+
return { code: `export default {}` }
128+
}
124129
},
125130
}
126131
}

0 commit comments

Comments
 (0)