From d90ba40474aa1da8e722e1660ba739621238e5ac Mon Sep 17 00:00:00 2001 From: patak <583075+patak-dev@users.noreply.github.com> Date: Wed, 11 Sep 2024 10:04:20 +0200 Subject: [PATCH] fix: backport #18063, allow scanning exports from `script module` in svelte (#18077) Co-authored-by: Paolo Ricciuti Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- packages/vite/src/node/optimizer/scan.ts | 31 +++++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index c47f38e18a8b32..33a0e83626a1bd 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -284,7 +284,9 @@ export const commentRE = //gs const srcRE = /\bsrc\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i const typeRE = /\btype\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i const langRE = /\blang\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i -const contextRE = /\bcontext\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i +const svelteScriptModuleRE = + /\bcontext\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i +const svelteModuleRE = /\smodule\b/i function esbuildScanPlugin( config: ResolvedConfig, @@ -480,17 +482,28 @@ function esbuildScanPlugin( const virtualModulePath = JSON.stringify(virtualModulePrefix + key) - const contextMatch = contextRE.exec(openTag) - const context = - contextMatch && - (contextMatch[1] || contextMatch[2] || contextMatch[3]) + let addedImport = false - // Especially for Svelte files, exports in