Skip to content

Commit e95d863

Browse files
RisaIdominikg
andauthored
fix: correctly detect script language during compilation (#844)
* fix: correctly detect script language during compilation * fix: correctly handle commented script blocks in compile.js * chore: add changeset --------- Co-authored-by: Richard Ivanek <[email protected]> Co-authored-by: dominikg <[email protected]>
1 parent c36c3ae commit e95d863

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

.changeset/rare-buttons-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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

packages/vite-plugin-svelte/__tests__/compile.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

packages/vite-plugin-svelte/src/utils/compile.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff 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
1818
const scriptLangRE =
19-
/<!--[^]*?-->|<script (?:[^>]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=["']?([^"' >]+)["']?[^>]*>/;
19+
/<!--[^]*?-->|<script (?:[^>]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=["']?([^"' >]+)["']?[^>]*>/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,

0 commit comments

Comments
 (0)