Skip to content

Commit 7ca93df

Browse files
committed
doc: Improved error messages
1 parent e10c463 commit 7ca93df

File tree

6 files changed

+45
-30
lines changed

6 files changed

+45
-30
lines changed

README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,23 @@ Supports:
1313

1414
This project was featured at the [Svelte London - November 2022 Meetup](https://www.youtube.com/live/DXQl1G54DJY?feature=share&t=2569)
1515

16-
# "Embrace, extend and extinguish"
16+
> "Embrace, extend and extinguish"
1717
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.
2019

2120
## Using React inside Svelte components
2221

23-
> Embrace
24-
2522
Inside the Svelte template prepend the name of the component with `react.` prefix.
2623

2724
Instead of `<Button>`, you'd write `<react.Button>`
2825

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:
3027

3128
```svelte
3229
<script>
3330
import YouTube from "react-youtube";
3431
35-
const react = sveltify({ YouTube }); // Optional, but improves tooling
32+
const react = sveltify({ YouTube }); // Optional step, but adds type-safety
3633
</script>
3734
3835
<react.YouTube videoId="AdNJ3fydeao" />
@@ -58,7 +55,7 @@ The snippet above would be generate:
5855
<react.YouTube videoId="AdNJ3fydeao" />
5956
```
6057

61-
## Setup
58+
## Setup / Installation
6259

6360
```sh
6461
npm install svelte-preprocess-react
@@ -79,7 +76,7 @@ When using other processors like [@sveltejs/vite-plugin-svelte](https://github.c
7976

8077
```js
8178
// svelte.config.js
82-
import { vitePreprocess } from "@sveltejs/kit/vite";
79+
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";
8380
import preprocessReact from "svelte-preprocess-react/preprocessReact";
8481

8582
export default {
@@ -89,8 +86,6 @@ export default {
8986

9087
## Using Svelte inside React components
9188

92-
> Extend
93-
9489
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.
9590

9691
```jsx
@@ -106,15 +101,17 @@ function MyComponent() {
106101

107102
## Using multiple frameworks is a bad idea
108103

109-
> Extinguish
110-
111104
Using multiple frontend frameworks adds overhead, both in User and Developer experience.
112105

113106
- Increased download size
114107
- Slower (each framework boundary adds overhead)
115108
- Context switching, keeping the intricacies of both Svelte and React in your head slows down development
116109

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.
118115

119116
# More info
120117

docs/migration-to-2.0.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# Migration to 2.0
22

3-
Change `<react:MyComponent>` into `<react.MyComponent>` (`:` to `.`).
3+
In the tag change the `:` double colon into a `.` dot, instead of:
44

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:
614

715
```ts
816
const react = sveltify({ MyComponent });
917
```
1018

11-
in the `<script lang="ts">` section for type safety.
19+
## ESLint
1220

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.
1422

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.
1624

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.
1827

1928
## Why the change?
2029

2130
In Svelte 5 the compiler gives a warning when using `<react:MyComponent />` syntax:
2231

2332
> Self-closing HTML tags for non-void elements are ambiguous — use `<react:MyComponent ...></react:MyComponent>` rather than `<react:MyComponent ... />`(element_invalid_self_closing_tag)
2433
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.
2835

36+
Secondly, a huge benefit of the new rune-inspired syntax is that it's compatible with existing tooling.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "git",
1111
"url": "https://github.com/bfanger/svelte-preprocess-react.git"
1212
},
13-
"version": "2.0.0-next.8",
13+
"version": "2.0.0",
1414
"license": "MIT",
1515
"type": "module",
1616
"scripts": {

src/lib/preprocessReact.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function transform(content, options) {
104104
modern: false,
105105
});
106106
const s = new MagicString(content, { filename: options.filename });
107-
const components = replaceReactTags(ast.html, s);
107+
const components = replaceReactTags(ast.html, s, options.filename);
108108
const aliases = Object.entries(components);
109109

110110
let depsInjected = false;
@@ -197,16 +197,26 @@ function transform(content, options) {
197197
*
198198
* @param {any} node
199199
* @param {MagicString} content
200+
* @param {string | undefined} filename
200201
* @param {Record<string, { dispatcher: boolean }>} components
201202
*/
202-
function replaceReactTags(node, content, components = {}) {
203+
function replaceReactTags(node, content, filename, components = {}) {
203204
if (
204205
(node.type === "Element" && node.name.startsWith("react:")) ||
205206
(node.type === "InlineComponent" && node.name.startsWith("react."))
206207
) {
207208
let legacy = node.name.startsWith("react:");
208209
if (legacy) {
209-
console.warn("'<react:*' syntax is deprecated, use '<react.*'");
210+
let location = "";
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`,
219+
);
210220
content.overwrite(node.start + 6, node.start + 7, ".");
211221
const tagEnd = node.end - node.name.length - 3;
212222
if (content.slice(tagEnd, tagEnd + 8) === `</react:`) {
@@ -281,7 +291,7 @@ function replaceReactTags(node, content, components = {}) {
281291
* @param {any} child
282292
*/
283293
function processChild(child) {
284-
replaceReactTags(child, content, components);
294+
replaceReactTags(child, content, filename, components);
285295
}
286296
// traverse children & branching blocks
287297
node.children?.forEach(processChild);

src/lib/used.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* @deprecated
5-
* Stop using used() to silence linting & typescript errors, and add:
5+
* Stop using used() to silence linting & typescript errors, instead:
66
* ```ts
77
* const react = sveltity({ MyComponent });
88
* ```

src/routes/preprocessor/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<react.Counter initial={25} onCount={console.info} />
1111

12-
<react.Alert>A simple alert</react.Alert>
12+
<react:Alert>A simple alert</react:Alert>
1313

1414
<react.Alert>
1515
"Multiline content". {10 ** 4} Lorem ipsum dolor sit amet consectetur adipisicing

0 commit comments

Comments
 (0)