Skip to content

Commit 505e8ca

Browse files
authored
fix: blank CSS contents while migrating (#13403)
Blank CSS, could contain SCSS or similar that needs a preprocessor. Since we don't care about CSS in this migration, we'll just ignore it.
1 parent 9627426 commit 505e8ca

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

.changeset/early-needles-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: blank CSS contents while migrating

packages/svelte/src/compiler/migrate/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import { extract_identifiers } from '../utils/ast.js';
1313
import { migrate_svelte_ignore } from '../utils/extract_svelte_ignore.js';
1414
import { validate_component_options } from '../validate-options.js';
1515

16+
const regex_style_tags = /(<style[^>]+>)([\S\s]*?)(<\/style>)/g;
17+
const style_placeholder = '/*$$__STYLE_CONTENT__$$*/';
18+
1619
/**
1720
* Does a best-effort migration of Svelte code towards using runes, event attributes and render tags.
1821
* May throw an error if the code is too complex to migrate automatically.
@@ -22,6 +25,15 @@ import { validate_component_options } from '../validate-options.js';
2225
*/
2326
export function migrate(source) {
2427
try {
28+
// Blank CSS, could contain SCSS or similar that needs a preprocessor.
29+
// Since we don't care about CSS in this migration, we'll just ignore it.
30+
/** @type {Array<[number, string]>} */
31+
const style_contents = [];
32+
source = source.replace(regex_style_tags, (_, start, content, end, idx) => {
33+
style_contents.push([idx + start.length, content]);
34+
return start + style_placeholder + end;
35+
});
36+
2537
reset_warning_filter(() => false);
2638
reset(source, { filename: 'migrate.svelte' });
2739

@@ -40,6 +52,10 @@ export function migrate(source) {
4052
const analysis = analyze_component(parsed, source, combined_options);
4153
const indent = guess_indent(source);
4254

55+
for (const content of style_contents) {
56+
str.overwrite(content[0], content[0] + style_placeholder.length, content[1]);
57+
}
58+
4359
/** @type {State} */
4460
let state = {
4561
scope: analysis.instance.scope,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
export let name;
3+
</script>
4+
5+
<div>{name}</div>
6+
7+
<style lang="scss">
8+
$font-stack: Helvetica, sans-serif;
9+
$primary-color: #333;
10+
11+
body {
12+
font: 100% $font-stack;
13+
color: $primary-color;
14+
}
15+
</style>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script>
2+
/** @type {{name: any}} */
3+
let { name } = $props();
4+
</script>
5+
6+
<div>{name}</div>
7+
8+
<style lang="scss">
9+
$font-stack: Helvetica, sans-serif;
10+
$primary-color: #333;
11+
12+
body {
13+
font: 100% $font-stack;
14+
color: $primary-color;
15+
}
16+
</style>

0 commit comments

Comments
 (0)