You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-14Lines changed: 11 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,26 +13,23 @@ Supports:
13
13
14
14
This project was featured at the [Svelte London - November 2022 Meetup](https://www.youtube.com/live/DXQl1G54DJY?feature=share&t=2569)
15
15
16
-
#"Embrace, extend and extinguish"
16
+
>"Embrace, extend and extinguish"
17
17
18
-
This preprocessor is intended as temporary solution when migrating an existing large React codebase or when a third-party hasn't yet provided a Svelte adapter.
19
-
After you've gradually converted all components to Svelte you can remove this preprocessor from your setup.
18
+
This preprocessor is intended as solution using third-party React components or for migrating an existing React codebase.
20
19
21
20
## Using React inside Svelte components
22
21
23
-
> Embrace
24
-
25
22
Inside the Svelte template prepend the name of the component with `react.` prefix.
26
23
27
24
Instead of `<Button>`, you'd write `<react.Button>`
28
25
29
-
You're also able to use libraries from the react ecosystem, react-youtube for example:
26
+
Use libraries from the React's ecosystem, react-youtube for example:
Once you've converted a React component to Svelte, you'd want delete that React component, but some if other React components depended on that component you can use `reactify` to use the new Svelte component as a React component.
95
90
96
91
```jsx
@@ -106,15 +101,17 @@ function MyComponent() {
106
101
107
102
## Using multiple frameworks is a bad idea
108
103
109
-
> Extinguish
110
-
111
104
Using multiple frontend frameworks adds overhead, both in User and Developer experience.
112
105
113
106
- Increased download size
114
107
- Slower (each framework boundary adds overhead)
115
108
- Context switching, keeping the intricacies of both Svelte and React in your head slows down development
116
109
117
-
svelte-preprocess-react is a migration tool, it can be used to migrate _from_ or _to_ React, it's not a long term solution.
110
+
When using third-party React components, keep an eye out for Svelte alternatives, or publish your own.
111
+
112
+
When used as migration tool (can be used to migrate _from_ or _to_ React),
113
+
the goal should be to stop writing new React components, and to convert existing React components to Svelte components.
114
+
Once all components are converted this preprocessor should be uninstalled.
Change `<react:MyComponent>`into `<react.MyComponent>` (`:` to `.`).
3
+
In the tag change the `:` double colon into a `.` dot, instead of:
4
4
5
-
When using Typescript, add:
5
+
`<react:MyComponent>` ❌ old
6
+
7
+
write:
8
+
9
+
`<react.MyComponent>` ✅ new
10
+
11
+
## Type safety
12
+
13
+
When using Typescript, in the `<script lang="ts">` section add:
6
14
7
15
```ts
8
16
const react =sveltify({ MyComponent });
9
17
```
10
18
11
-
in the `<script lang="ts">` section for type safety.
19
+
## ESLint
12
20
13
-
When getting ESLint no-undef errors:
21
+
The preprocessor will autoimport sveltify and can also generate the react object based on usage of `<react.* />` tags.
14
22
15
-
in `eslint.config.js` add `sveltify: true` to your globals or add a`import { sveltify } from 'svelte-preprocess-react'`.
23
+
So both `const react = sveltify({ MyComponent })` and the`import { sveltify } from "svelte-preprocess-react"` are optional, but that confuses ESLint.
16
24
17
-
Also add `react: true` if you don't want
25
+
To avoid the `no-undef` errors in your `eslint.config.js` add `sveltify: true, react: true` to your `globals`.
26
+
When using Typescript it's recommended to only add `sveltify: true`, then the eslint warnign acts as a reminder to add a `const react = sveltify({..})` for type-safety.
18
27
19
28
## Why the change?
20
29
21
30
In Svelte 5 the compiler gives a warning when using `<react:MyComponent />` syntax:
22
31
23
32
> Self-closing HTML tags for non-void elements are ambiguous — use `<react:MyComponent ...></react:MyComponent>` rather than `<react:MyComponent ... />`(element_invalid_self_closing_tag)
24
33
25
-
Easily solved, but it a less elegant syntax.
26
-
27
-
Secondly, the new syntax allows for IDE support.
34
+
Easily solved by adding a closing tag, but it's a less elegant syntax.
28
35
36
+
Secondly, a huge benefit of the new rune-inspired syntax is that it's compatible with existing tooling.
console.warn("'<react:*' syntax is deprecated, use '<react.*'");
210
+
letlocation="";
211
+
if(filename){
212
+
location+=` in ${filename}`;
213
+
}
214
+
if(node.start){
215
+
location+=` on line ${content.original.substring(0,node.start).split("\n").length}`;
216
+
}
217
+
console.warn(
218
+
`'<${node.name}' syntax is deprecated, use '<react.${node.name.substring(6)}'${location}.\nhttps://github.com/bfanger/svelte-preprocess-react/blob/main/docs/migration-to-2.0.md\n`,
0 commit comments