Skip to content

Commit a84d9b3

Browse files
fix: don't strip lang="ts" tag in Svelte 5 (#12080)
* fix: don't strip `lang="ts"` tag in Svelte 5 closes sveltejs/svelte#10766 * rename parameters --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 5ecf75c commit a84d9b3

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

.changeset/loud-pens-mate.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/package": patch
3+
---
4+
5+
fix: don't strip `lang="ts"` tag in Svelte 5

packages/package/src/utils.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import * as fs from 'node:fs';
22
import * as path from 'node:path';
3+
import { VERSION } from 'svelte/compiler';
34
import { posixify, mkdirp, walk } from './filesystem.js';
45

6+
const is_svelte_5_plus = Number(VERSION.split('.')[0]) >= 5;
7+
58
/**
69
* Resolves aliases
710
*
@@ -45,8 +48,13 @@ export function strip_lang_tags(content) {
4548
return content
4649
.replace(
4750
/(<!--[^]*?-->)|(<script[^>]*?)\s(?:type|lang)=(["'])(.*?)\3/g,
48-
// things like application/ld+json should be kept as-is. Preprocessed languages are "ts" etc
49-
(match, s1, s2, _, s4) => (s4?.startsWith('application/') ? match : (s1 ?? '') + (s2 ?? ''))
51+
// Things like application/ld+json should be kept as-is. Preprocessed languages are "ts" etc.
52+
// Svelte 5 deals with TypeScript natively, and in the template, too, therefore keep it in.
53+
// Not removing it would mean Svelte parses without its TS plugin and then runs into errors.
54+
(match, comment, tag_open, _, type) =>
55+
type?.startsWith('application/') || (is_svelte_5_plus && type === 'ts')
56+
? match
57+
: (comment ?? '') + (tag_open ?? '')
5058
)
5159
.replace(/(<!--[^]*?-->)|(<style[^>]*?)\s(?:type|lang)=(["']).*?\3/g, '$1$2');
5260
}

0 commit comments

Comments
 (0)