Skip to content

Commit

Permalink
fix(scan): handle html script tag attributes that contain ">" (#13101)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 5, 2023
1 parent 91d7b67 commit 8a37de6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/vite/src/node/optimizer/scan.ts
Expand Up @@ -246,9 +246,8 @@ function globEntries(pattern: string | string[], config: ResolvedConfig) {
})
}

const scriptModuleRE =
/(<script\b[^>]+type\s*=\s*(?:"module"|'module')[^>]*>)(.*?)<\/script>/gis
export const scriptRE = /(<script(?:\s[^>]*>|>))(.*?)<\/script>/gis
export const scriptRE =
/(<script(?:\s+[a-z_:][-\w:]*(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^"'<>=\s]+))?)*\s*>)(.*?)<\/script>/gis
export const commentRE = /<!--.*?-->/gs
const srcRE = /\bsrc\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i
const typeRE = /\btype\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i
Expand Down Expand Up @@ -378,19 +377,22 @@ function esbuildScanPlugin(
// Avoid matching the content of the comment
raw = raw.replace(commentRE, '<!---->')
const isHtml = path.endsWith('.html')
const regex = isHtml ? scriptModuleRE : scriptRE
regex.lastIndex = 0
scriptRE.lastIndex = 0
let js = ''
let scriptId = 0
let match: RegExpExecArray | null
while ((match = regex.exec(raw))) {
while ((match = scriptRE.exec(raw))) {
const [, openTag, content] = match
const typeMatch = openTag.match(typeRE)
const type =
typeMatch && (typeMatch[1] || typeMatch[2] || typeMatch[3])
const langMatch = openTag.match(langRE)
const lang =
langMatch && (langMatch[1] || langMatch[2] || langMatch[3])
// skip non type module script
if (isHtml && type !== 'module') {
continue
}
// skip type="application/ld+json" and other non-JS types
if (
type &&
Expand Down
22 changes: 22 additions & 0 deletions playground/optimize-deps/generics.vue
@@ -0,0 +1,22 @@
<!--
https://github.com/vuejs/core/issues/8171
https://github.com/vitejs/vite-plugin-vue/issues/162
generic attribute includes angle brackets which breaks scanning
This file only verifies that the scanner can work with such usage and nothing
else.
-->

<script lang="ts">
export class Item<TValue> {
value: TValue
}
</script>

<script setup lang="ts" generic="TItem extends Item<TValue>, TValue">
defineProps<{
items: TItem[]
modelValue: TItem[]
}>()
</script>

<template>{{ items }}</template>
1 change: 1 addition & 0 deletions playground/optimize-deps/index.html
Expand Up @@ -165,6 +165,7 @@ <h2>Pre bundle css require</h2>
)

import './index.astro'
import './generics.vue'

// All these imports should end up resolved to the same URL (same ?v= injected on them)
import { add as addFromDirectAbsolutePath } from '/node_modules/@vitejs/test-dep-non-optimized/index.js'
Expand Down
5 changes: 5 additions & 0 deletions playground/optimize-deps/vite.config.js
Expand Up @@ -121,6 +121,11 @@ export default defineComponent({
`.trim(),
}
}

// fallback to empty module for other vue files
if (id.endsWith('.vue')) {
return { code: `export default {}` }
}
},
}
}
Expand Down

0 comments on commit 8a37de6

Please sign in to comment.