Skip to content

Commit

Permalink
fix: package import & exports + typescript references
Browse files Browse the repository at this point in the history
  • Loading branch information
bfanger committed Jul 28, 2023
1 parent 44ea445 commit f0ce92c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ You're also able to use libraries from the react ecosystem, react-youtube for ex
The snippet above would be generate:

```svelte
<!-- generated by svelte-preprocess-react -->
<!-- Generated by svelte-preprocess-react -->
<script>
import sveltify from "svelte-preprocess-react/sveltify";
import { sveltify } from "svelte-preprocess-react";
import { createPortal } from "react-dom";
import ReactDOM from "react-dom/client";
import { renderToString } from "react-dom/server";
import YouTube from "react-youtube";
const React$YouTube = sveltify(
const ReactYouTube = sveltify(
YouTube,
createPortal,
ReactDOM,
renderToString,
);
</script>
<React$YouTube videoId="AdNJ3fydeao" />
<ReactYouTube videoId="AdNJ3fydeao" />
```

## Setup
Expand All @@ -64,6 +64,8 @@ The snippet above would be generate:
npm install svelte-preprocess-react
```

Add `preprocessReact` to your svelte.config.js:

```js
// svelte.config.js
import preprocessReact from "svelte-preprocess-react/preprocessReact";
Expand Down
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"files": [
"./dist"
],
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand All @@ -111,10 +112,19 @@
"types": "./dist/react-router/index.d.ts",
"svelte": "./dist/react-router/index.js",
"default": "./dist/react-router/index.js"
},
"./react-router/types": {
"types": "./dist/react-router/types.d.ts",
"default": "./dist/react-router/types.js"
}
},
"typesVersions": {
">4.0": {
"index.d.ts": [
"./dist/index.d.ts"
],
"preprocessReact": [
"./dist/preprocessReact.d.ts"
],
"react-router": [
"./dist/react-router/index.d.ts"
]
}
}
}
2 changes: 1 addition & 1 deletion src/lib/preprocessReact.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function transform(content, options) {
/** @type {string} */
let portal;
const imports = [
`import ${prefix}sveltify from "svelte-preprocess-react/sveltify"`,
`import { sveltify as ${prefix}sveltify } from "svelte-preprocess-react"`,
];
if (options.react >= 18) {
imports.push(
Expand Down
20 changes: 10 additions & 10 deletions src/tests/__snapshots__/preprocess.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`svelte-preprocess-react > should convert text content to react children prop 1`] = `
"<script lang=\\"ts\\">import React$$sveltify from \\"svelte-preprocess-react/sveltify\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\"; import Alert from \\"../../demo/react-components/Alert\\";
"<script lang=\\"ts\\">import { sveltify as React$$sveltify } from \\"svelte-preprocess-react\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\"; import Alert from \\"../../demo/react-components/Alert\\";
const count = 0;
const React$Alert = React$$sveltify(Alert, React$$createPortal, React$$ReactDOM, React$$renderToString);</script>
Expand All @@ -14,7 +14,7 @@ const React$Alert = React$$sveltify(Alert, React$$createPortal, React$$ReactDOM,
`;

exports[`svelte-preprocess-react > should import 'react-dom/server' when ssr is enabled 1`] = `
"<script>import React$$sveltify from \\"svelte-preprocess-react/sveltify\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\";
"<script>import { sveltify as React$$sveltify } from \\"svelte-preprocess-react\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\";
// @ts-nocheck
import Clicker from \\"./Clicker\\";
Expand All @@ -32,7 +32,7 @@ const React$Clicker = React$$sveltify(Clicker, React$$createPortal, React$$React

exports[`svelte-preprocess-react > should inject a script tag 1`] = `
"<script>
import React$$sveltify from \\"svelte-preprocess-react/sveltify\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\"
import { sveltify as React$$sveltify } from \\"svelte-preprocess-react\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\"
const React$Counter = React$$sveltify(Counter, React$$createPortal, React$$ReactDOM, React$$renderToString);
</script>
Expand All @@ -43,7 +43,7 @@ const React$Counter = React$$sveltify(Counter, React$$createPortal, React$$React
`;

exports[`svelte-preprocess-react > should not import 'react-dom/server' when ssr is disabled 1`] = `
"<script>import React$$sveltify from \\"svelte-preprocess-react/sveltify\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\";
"<script>import { sveltify as React$$sveltify } from \\"svelte-preprocess-react\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\";
// @ts-nocheck
import Clicker from \\"./Clicker\\";
Expand All @@ -60,7 +60,7 @@ const React$Clicker = React$$sveltify(Clicker, React$$createPortal, React$$React
`;

exports[`svelte-preprocess-react > should portal slotted content as children 1`] = `
"<script>import React$$sveltify from \\"svelte-preprocess-react/sveltify\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\";
"<script>import { sveltify as React$$sveltify } from \\"svelte-preprocess-react\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\";
import Alert from \\"../../demo/react-components/Alert\\";
if (!Alert) {
Expand All @@ -73,7 +73,7 @@ const React$Alert = React$$sveltify(Alert, React$$createPortal, React$$ReactDOM,
`;

exports[`svelte-preprocess-react > should process <react:Context.Provider> tags 1`] = `
"<script lang=\\"ts\\">import React$$sveltify from \\"svelte-preprocess-react/sveltify\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\"; import { used } from \\"svelte-preprocess-react\\";
"<script lang=\\"ts\\">import { sveltify as React$$sveltify } from \\"svelte-preprocess-react\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\"; import { used } from \\"svelte-preprocess-react\\";
export let Context;
used(Context);
const React$Context$Provider = React$$sveltify(Context.Provider, React$$createPortal, React$$ReactDOM, React$$renderToString);</script>
Expand All @@ -87,7 +87,7 @@ const React$Context$Provider = React$$sveltify(Context.Provider, React$$createPo
`;

exports[`svelte-preprocess-react > should process <react:component> tags 1`] = `
"<script>import React$$sveltify from \\"svelte-preprocess-react/sveltify\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\";
"<script>import { sveltify as React$$sveltify } from \\"svelte-preprocess-react\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\";
// @ts-nocheck
import Clicker from \\"./Clicker\\";
Expand All @@ -104,7 +104,7 @@ const React$Clicker = React$$sveltify(Clicker, React$$createPortal, React$$React
`;

exports[`svelte-preprocess-react > should process <react:component> tags 2`] = `
"<script>import React$$sveltify from \\"svelte-preprocess-react/sveltify\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\";
"<script>import { sveltify as React$$sveltify } from \\"svelte-preprocess-react\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\";
// @ts-nocheck
import Clicker from \\"./Clicker\\";
Expand Down Expand Up @@ -134,7 +134,7 @@ const React$Clicker = React$$sveltify(Clicker, React$$createPortal, React$$React

exports[`svelte-preprocess-react > should process <react:element> (lowercase) tags 1`] = `
"<script>
import React$$sveltify from \\"svelte-preprocess-react/sveltify\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\"
import { sveltify as React$$sveltify } from \\"svelte-preprocess-react\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\"
const React$button = React$$sveltify(\\"button\\", React$$createPortal, React$$ReactDOM, React$$renderToString);
</script>
Expand All @@ -147,7 +147,7 @@ const React$button = React$$sveltify(\\"button\\", React$$createPortal, React$$R
`;

exports[`svelte-preprocess-react > should process {:else} {:then} and {:catch} sections 1`] = `
"<script lang=\\"ts\\">import React$$sveltify from \\"svelte-preprocess-react/sveltify\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\"; const number = 1;
"<script lang=\\"ts\\">import { sveltify as React$$sveltify } from \\"svelte-preprocess-react\\"; import React$$ReactDOM from \\"react-dom/client\\"; import { createPortal as React$$createPortal} from \\"react-dom\\"; import { renderToString as React$$renderToString } from \\"react-dom/server\\"; const number = 1;
const Component = () => null;
const React$Component = React$$sveltify(Component, React$$createPortal, React$$ReactDOM, React$$renderToString);</script>
Expand Down

0 comments on commit f0ce92c

Please sign in to comment.