Skip to content

Commit

Permalink
fix: skip optimizer run on non-JS script tags (#4565)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Aug 14, 2021
1 parent b010678 commit 270bd3a
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const scriptModuleRE =
export const scriptRE = /(<script\b(?:\s[^>]*>|>))(.*?)<\/script>/gims
export const commentRE = /<!--(.|[\r\n])*?-->/
const srcRE = /\bsrc\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im
const typeRE = /\btype\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im
const langRE = /\blang\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im

function esbuildScanPlugin(
Expand Down Expand Up @@ -207,9 +208,23 @@ function esbuildScanPlugin(
while ((match = regex.exec(raw))) {
const [, openTag, content] = match
const srcMatch = openTag.match(srcRE)
const typeMatch = openTag.match(typeRE)
const langMatch = openTag.match(langRE)
const type =
typeMatch && (typeMatch[1] || typeMatch[2] || typeMatch[3])
const lang =
langMatch && (langMatch[1] || langMatch[2] || langMatch[3])
// skip type="application/ld+json" and other non-JS types
if (
type &&
!(
type.includes('javascript') ||
type.includes('ecmascript') ||
type === 'module'
)
) {
continue
}
if (lang === 'ts' || lang === 'tsx' || lang === 'jsx') {
loader = lang
}
Expand Down

0 comments on commit 270bd3a

Please sign in to comment.