File tree Expand file tree Collapse file tree 3 files changed +46
-2
lines changed
packages/vite-plugin-svelte Expand file tree Collapse file tree 3 files changed +46
-2
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @sveltejs/vite-plugin-svelte ' : patch
3
+ ---
4
+
5
+ fix(compile): correctly determine script lang in files where a comment precedes the script tag
Original file line number Diff line number Diff line change @@ -45,5 +45,36 @@ describe('createCompileSvelte', () => {
45
45
) ;
46
46
expect ( output . compiled . js . code ) . not . toContain ( '/* @__PURE__ */\n' ) ;
47
47
} ) ;
48
+
49
+ it ( 'detects script lang' , async ( ) => {
50
+ const code = `<!-- this file uses typescript -->
51
+ <!--
52
+ <script lang="foo">
53
+ </script>-->
54
+ <script lang="ts" generics="T">
55
+ const x = 1;
56
+ console.log('something',/* @__PURE__ */ new Date());
57
+ console.log('something else');
58
+ </script>
59
+ <div>{x}</div>` ;
60
+
61
+ const compileSvelte = createCompileSvelte ( options ) ;
62
+ const output = await compileSvelte (
63
+ {
64
+ cssId : 'svelte-xxxxx' ,
65
+ query : { } ,
66
+ raw : false ,
67
+ ssr : false ,
68
+ timestamp : Date . now ( ) ,
69
+ id : 'id' ,
70
+ filename : '/some/File.svelte' ,
71
+ normalizedFilename : 'some/File.svelte'
72
+ } ,
73
+ code ,
74
+ { }
75
+ ) ;
76
+
77
+ expect ( output . lang ) . toBe ( 'ts' ) ;
78
+ } ) ;
48
79
} ) ;
49
80
} ) ;
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ import { isSvelte5 } from './svelte-version.js';
16
16
// which is closer to the other regexes in at least not falling into commented script
17
17
// but ideally would be shared exactly with svelte and other tools that use it
18
18
const scriptLangRE =
19
- / < ! - - [ ^ ] * ?- - > | < s c r i p t (?: [ ^ > ] * | (?: [ ^ = > ' " / ] + = (?: " [ ^ " ] * " | ' [ ^ ' ] * ' | [ ^ > \s ] + ) \s + ) * ) l a n g = [ " ' ] ? ( [ ^ " ' > ] + ) [ " ' ] ? [ ^ > ] * > / ;
19
+ / < ! - - [ ^ ] * ?- - > | < s c r i p t (?: [ ^ > ] * | (?: [ ^ = > ' " / ] + = (?: " [ ^ " ] * " | ' [ ^ ' ] * ' | [ ^ > \s ] + ) \s + ) * ) l a n g = [ " ' ] ? ( [ ^ " ' > ] + ) [ " ' ] ? [ ^ > ] * > / g ;
20
20
21
21
/**
22
22
* @param {Function } [makeHot]
@@ -184,10 +184,18 @@ export const _createCompileSvelte = (makeHot) => {
184
184
}
185
185
}
186
186
187
+ let lang = 'js' ;
188
+ for ( const match of code . matchAll ( scriptLangRE ) ) {
189
+ if ( match [ 1 ] ) {
190
+ lang = match [ 1 ] ;
191
+ break ;
192
+ }
193
+ }
194
+
187
195
return {
188
196
filename,
189
197
normalizedFilename,
190
- lang : code . match ( scriptLangRE ) ?. [ 1 ] || 'js' ,
198
+ lang,
191
199
// @ts -ignore
192
200
compiled,
193
201
ssr,
You can’t perform that action at this time.
0 commit comments