From 673f5295b7acf8103d7547fdcdfb6bc92596e74e Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Wed, 4 Oct 2023 21:10:54 +0900 Subject: [PATCH] fix: esbuild glob resolve error --- packages/vite/src/node/optimizer/scan.ts | 220 ++++++++++++----------- 1 file changed, 116 insertions(+), 104 deletions(-) diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index b27f8e0e8962fd..bb939a7567a09a 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -7,6 +7,7 @@ import type { BuildContext, BuildOptions, Loader, + OnLoadArgs, OnLoadResult, Plugin, } from 'esbuild' @@ -380,116 +381,127 @@ function esbuildScanPlugin( } }) - // extract scripts inside HTML-like files and treat it as a js module - build.onLoad( - { filter: htmlTypesRE, namespace: 'html' }, - async ({ path }) => { - let raw = await fsp.readFile(path, 'utf-8') - // Avoid matching the content of the comment - raw = raw.replace(commentRE, '') - const isHtml = path.endsWith('.html') - scriptRE.lastIndex = 0 - let js = '' - let scriptId = 0 - let match: RegExpExecArray | null - 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 && - !( - type.includes('javascript') || - type.includes('ecmascript') || - type === 'module' - ) - ) { - continue - } - let loader: Loader = 'js' - if (lang === 'ts' || lang === 'tsx' || lang === 'jsx') { - loader = lang - } else if (path.endsWith('.astro')) { - loader = 'ts' - } - const srcMatch = openTag.match(srcRE) - if (srcMatch) { - const src = srcMatch[1] || srcMatch[2] || srcMatch[3] - js += `import ${JSON.stringify(src)}\n` - } else if (content.trim()) { - // The reason why virtual modules are needed: - // 1. There can be module scripts (`