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', () => {
4545 ) ;
4646 expect ( output . compiled . js . code ) . not . toContain ( '/* @__PURE__ */\n' ) ;
4747 } ) ;
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+ } ) ;
4879 } ) ;
4980} ) ;
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ import { isSvelte5 } from './svelte-version.js';
1616// which is closer to the other regexes in at least not falling into commented script
1717// but ideally would be shared exactly with svelte and other tools that use it
1818const 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 ;
2020
2121/**
2222 * @param {Function } [makeHot]
@@ -184,10 +184,18 @@ export const _createCompileSvelte = (makeHot) => {
184184 }
185185 }
186186
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+
187195 return {
188196 filename,
189197 normalizedFilename,
190- lang : code . match ( scriptLangRE ) ?. [ 1 ] || 'js' ,
198+ lang,
191199 // @ts -ignore
192200 compiled,
193201 ssr,
You can’t perform that action at this time.
0 commit comments