Skip to content

Commit 7409698

Browse files
Merge branch 'typescript'
2 parents 2b35e16 + 2b9c9fc commit 7409698

18 files changed

+997
-439
lines changed

.babelrc.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ const output = process.env.BABEL_OUTPUT;
44
const requirePolyfills = process.env.INCLUDE_POLYFILLS;
55
const modules = output == null ? false : output;
66
const options = {
7-
presets: [['@babel/env', {loose: true, modules}], '@babel/react'],
8-
plugins: [
9-
'@babel/plugin-transform-react-jsx',
7+
presets: [
8+
['@babel/env', {loose: true, modules}],
9+
'@babel/react',
1010
[
11-
'transform-react-remove-prop-types',
11+
'@babel/preset-typescript',
1212
{
13-
mode: 'remove',
14-
ignoreFilenames: ['node_modules'],
13+
isTSX: true,
14+
allExtensions: true,
1515
},
1616
],
1717
],
18+
plugins: ['@babel/plugin-transform-react-jsx'],
1819
env: {
1920
test: {
2021
// extra configuration for process.env.NODE_ENV === 'test'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ example/node_modules
88

99
# testing
1010
/coverage
11+
/sandbox
1112

1213
# production
1314
/build

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rollup*
1313

1414
#development
1515
/build
16+
/sandbox
1617

1718
#demo
1819
/example

README.md

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# string-to-react-component
1+
# string-to-react-component
22

33
Create React component from string
44

@@ -22,16 +22,19 @@ Create React component from string
2222

2323
## Installation
2424

25-
First You need to load `@babel/standalone` in the browser :
26-
2725
```js
28-
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
26+
# with npm
27+
$ npm install string-to-react-component @babel/standalone --save
28+
29+
# with yarn
30+
yarn add string-to-react-component @babel/standalone
2931
```
3032

31-
Then install `string-to-react-component` package
33+
### CDN Links
3234

3335
```js
34-
$ npm install string-to-react-component --save
36+
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
37+
<script src="https://unpkg.com/string-to-react-component@3.1.0/dist/stringToReactComponent.umd.min.js"></script>
3538
```
3639

3740
## Basic Example
@@ -61,18 +64,18 @@ function App() {
6164

6265
- The given code inside the string should be a function.
6366

64-
- The code inside the string is executed in the global scope, so imported objects from `react` package including `useState`, `useEffect`, ... are not accessible inside it and you should get them from `React` global variable :
67+
- The code inside the string is executed in the global scope, so imported objects from `react` package including `useState`, `useEffect`, ... are not accessible inside it and you can get them from `React` global variable or pass them as props to the component :
6568

6669
```js
6770
import {useState} from 'react';
6871
import StringToReactComponent from 'string-to-react-component';
6972
function App() {
7073
return (
71-
<StringToReactComponent>
72-
{`()=>{
74+
<StringToReactComponent data={{useState}}>
75+
{`(props)=>{
7376
console.log(typeof useState); // undefined
7477
console.log(typeof React.useState); // function
75-
78+
console.log(typeof props.useState); // function
7679
...
7780
7881
}`}
@@ -116,38 +119,38 @@ function App() {
116119
- not required
117120
- See the full option list [here](https://babeljs.io/docs/en/options)
118121
- examples :
119-
* using source map :
120-
```js
121-
<StringToReactComponent babelOptions={{filename: 'counter.js', sourceMaps: 'inline'}}>
122-
{`(props)=>{
123-
const {useState}=React;
124-
const [counter,setCounter]=useState(0);
125-
const increase=()=>{
126-
setCounter(counter+1);
127-
};
128-
return (<>
129-
<button onClick={increase}>+</button>
130-
<span>{'counter : '+ counter}</span>
131-
</>);
132-
}`}
133-
</StringToReactComponent>
134-
```
135-
* using typescript :
136-
```js
137-
<StringToReactComponent babelOptions={{ filename: 'counter.ts',presets: [["typescript", { allExtensions: true, isTSX: true }]] }}>
138-
{`()=>{
139-
const [counter,setCounter]=React.useState<number>(0);
140-
const increase=()=>{
141-
setCounter(counter+1);
142-
};
143-
return (<>
144-
<button onClick={increase}>+</button>
145-
<span>{'counter : '+ counter}</span>
146-
</>);
147-
}`}
148-
</StringToReactComponent>
149-
```
150-
122+
- using source map :
123+
```js
124+
<StringToReactComponent babelOptions={{filename: 'counter.js', sourceMaps: 'inline'}}>
125+
{`(props)=>{
126+
const {useState}=React;
127+
const [counter,setCounter]=useState(0);
128+
const increase=()=>{
129+
setCounter(counter+1);
130+
};
131+
return (<>
132+
<button onClick={increase}>+</button>
133+
<span>{'counter : '+ counter}</span>
134+
</>);
135+
}`}
136+
</StringToReactComponent>
137+
```
138+
- using typescript :
139+
```js
140+
<StringToReactComponent
141+
babelOptions={{filename: 'counter.ts', presets: [['typescript', {allExtensions: true, isTSX: true}]]}}>
142+
{`()=>{
143+
const [counter,setCounter]=React.useState<number>(0);
144+
const increase=()=>{
145+
setCounter(counter+1);
146+
};
147+
return (<>
148+
<button onClick={increase}>+</button>
149+
<span>{'counter : '+ counter}</span>
150+
</>);
151+
}`}
152+
</StringToReactComponent>
153+
```
151154

152155
## Caveats
153156

index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { FC, PropsWithChildren } from 'react';
2+
import type { StringToReactComponentProps } from './src/index.d';
3+
export { StringToReactComponentProps } from './src/index.d';
4+
declare const StringToReactComponent: FC<PropsWithChildren<StringToReactComponentProps>>;
5+
export default StringToReactComponent;

0 commit comments

Comments
 (0)