diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index 5571d031245f6d..3c592cec499de5 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -246,9 +246,8 @@ function globEntries(pattern: string | string[], config: ResolvedConfig) { }) } -const scriptModuleRE = - /(]+type\s*=\s*(?:"module"|'module')[^>]*>)(.*?)<\/script>/gis -export const scriptRE = /(]*>|>))(.*?)<\/script>/gis +export const scriptRE = + /(=\s]+))?)*\s*>)(.*?)<\/script>/gis export const commentRE = //gs const srcRE = /\bsrc\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i const typeRE = /\btype\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i @@ -378,12 +377,11 @@ 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 = @@ -391,6 +389,10 @@ function esbuildScanPlugin( 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 && diff --git a/playground/optimize-deps/generics.vue b/playground/optimize-deps/generics.vue new file mode 100644 index 00000000000000..13e17ce1c12d6a --- /dev/null +++ b/playground/optimize-deps/generics.vue @@ -0,0 +1,22 @@ + + + + + + + diff --git a/playground/optimize-deps/index.html b/playground/optimize-deps/index.html index 8c5719075650ce..a07e6a17798154 100644 --- a/playground/optimize-deps/index.html +++ b/playground/optimize-deps/index.html @@ -165,6 +165,7 @@

Pre bundle css require

) 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' diff --git a/playground/optimize-deps/vite.config.js b/playground/optimize-deps/vite.config.js index 7eb4c5b10c3101..04ba79b5a2e38d 100644 --- a/playground/optimize-deps/vite.config.js +++ b/playground/optimize-deps/vite.config.js @@ -121,6 +121,11 @@ export default defineComponent({ `.trim(), } } + + // fallback to empty module for other vue files + if (id.endsWith('.vue')) { + return { code: `export default {}` } + } }, } }