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(scan): handle html script tag attributes that contain ">" #13101

Merged
merged 1 commit into from May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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