Skip to content

Commit

Permalink
fix: don't strip lang="ts" tag in Svelte 5 (#12080)
Browse files Browse the repository at this point in the history
* fix: don't strip `lang="ts"` tag in Svelte 5

closes sveltejs/svelte#10766

* rename parameters

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
  • Loading branch information
dummdidumm and Rich-Harris committed Apr 4, 2024
1 parent 5ecf75c commit a84d9b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-pens-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/package": patch
---

fix: don't strip `lang="ts"` tag in Svelte 5
12 changes: 10 additions & 2 deletions packages/package/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import { VERSION } from 'svelte/compiler';
import { posixify, mkdirp, walk } from './filesystem.js';

const is_svelte_5_plus = Number(VERSION.split('.')[0]) >= 5;

/**
* Resolves aliases
*
Expand Down Expand Up @@ -45,8 +48,13 @@ export function strip_lang_tags(content) {
return content
.replace(
/(<!--[^]*?-->)|(<script[^>]*?)\s(?:type|lang)=(["'])(.*?)\3/g,
// things like application/ld+json should be kept as-is. Preprocessed languages are "ts" etc
(match, s1, s2, _, s4) => (s4?.startsWith('application/') ? match : (s1 ?? '') + (s2 ?? ''))
// Things like application/ld+json should be kept as-is. Preprocessed languages are "ts" etc.
// Svelte 5 deals with TypeScript natively, and in the template, too, therefore keep it in.
// Not removing it would mean Svelte parses without its TS plugin and then runs into errors.
(match, comment, tag_open, _, type) =>
type?.startsWith('application/') || (is_svelte_5_plus && type === 'ts')
? match
: (comment ?? '') + (tag_open ?? '')
)
.replace(/(<!--[^]*?-->)|(<style[^>]*?)\s(?:type|lang)=(["']).*?\3/g, '$1$2');
}
Expand Down

0 comments on commit a84d9b3

Please sign in to comment.