@@ -284,7 +284,9 @@ export const commentRE = /<!--.*?-->/gs
284
284
const srcRE = / \b s r c \s * = \s * (?: " ( [ ^ " ] + ) " | ' ( [ ^ ' ] + ) ' | ( [ ^ \s ' " > ] + ) ) / i
285
285
const typeRE = / \b t y p e \s * = \s * (?: " ( [ ^ " ] + ) " | ' ( [ ^ ' ] + ) ' | ( [ ^ \s ' " > ] + ) ) / i
286
286
const langRE = / \b l a n g \s * = \s * (?: " ( [ ^ " ] + ) " | ' ( [ ^ ' ] + ) ' | ( [ ^ \s ' " > ] + ) ) / i
287
- const contextRE = / \b c o n t e x t \s * = \s * (?: " ( [ ^ " ] + ) " | ' ( [ ^ ' ] + ) ' | ( [ ^ \s ' " > ] + ) ) / i
287
+ const svelteScriptModuleRE =
288
+ / \b c o n t e x t \s * = \s * (?: " ( [ ^ " ] + ) " | ' ( [ ^ ' ] + ) ' | ( [ ^ \s ' " > ] + ) ) / i
289
+ const svelteModuleRE = / \s m o d u l e \b / i
288
290
289
291
function esbuildScanPlugin (
290
292
config : ResolvedConfig ,
@@ -480,17 +482,28 @@ function esbuildScanPlugin(
480
482
481
483
const virtualModulePath = JSON . stringify ( virtualModulePrefix + key )
482
484
483
- const contextMatch = contextRE . exec ( openTag )
484
- const context =
485
- contextMatch &&
486
- ( contextMatch [ 1 ] || contextMatch [ 2 ] || contextMatch [ 3 ] )
485
+ let addedImport = false
487
486
488
- // Especially for Svelte files, exports in <script context="module"> means module exports,
487
+ // For Svelte files, exports in <script context="module"> or <script module > means module exports,
489
488
// exports in <script> means component props. To avoid having two same export name from the
490
489
// star exports, we need to ignore exports in <script>
491
- if ( p . endsWith ( '.svelte' ) && context !== 'module' ) {
492
- js += `import ${ virtualModulePath } \n`
493
- } else {
490
+ if ( p . endsWith ( '.svelte' ) ) {
491
+ let isModule = svelteModuleRE . test ( openTag ) // test for svelte5 <script module> syntax
492
+ if ( ! isModule ) {
493
+ // fallback, test for svelte4 <script context="module"> syntax
494
+ const contextMatch = svelteScriptModuleRE . exec ( openTag )
495
+ const context =
496
+ contextMatch &&
497
+ ( contextMatch [ 1 ] || contextMatch [ 2 ] || contextMatch [ 3 ] )
498
+ isModule = context === 'module'
499
+ }
500
+ if ( ! isModule ) {
501
+ addedImport = true
502
+ js += `import ${ virtualModulePath } \n`
503
+ }
504
+ }
505
+
506
+ if ( ! addedImport ) {
494
507
js += `export * from ${ virtualModulePath } \n`
495
508
}
496
509
}
0 commit comments