Skip to content

Commit 873d982

Browse files
fix updating babel options issue
1 parent 385b0a8 commit 873d982

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/ctx.tsx

+3-8
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,10 @@ class Ctx implements IStringToReactApi {
4141
}
4242
}
4343
}
44-
getModule(code: string): Promise<any> {
44+
getModule(code: string, babelOptions: TransformOptions): Promise<any> {
45+
this._checkBabelOptions(babelOptions);
4546
code = `import React from "react";\nexport default ${code}`;
46-
const op: TransformOptions = {
47-
presets: ['react', ['env', {modules: false}]],
48-
filename: 'counter.js',
49-
sourceMaps: 'inline',
50-
sourceType: 'module',
51-
};
52-
const resultObj = this._getBabel().transform(code, op);
47+
const resultObj = this._getBabel().transform(code, babelOptions);
5348
// 1. Define your module code as a string
5449
code = resultObj.code || '';
5550
code = code

src/strintToReact.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,26 @@ function StringToReactComponent(
88
let ref = useRef<any>(null);
99
ref.current = ref.current || getCtx(react, Babel);
1010
const api = ref.current as IStringToReactApi;
11-
//const babelOptions = props.babelOptions || {};
12-
//const GeneratedComponent = api.updateTemplate(props.children || '', babelOptions).getComponent();
11+
const babelOptions = props.babelOptions || {};
12+
const stringCode = props.children || '() => null';
1313
const data = props.data || {};
14+
//const GeneratedComponent = api.updateTemplate(props.children || '', babelOptions).getComponent();
15+
1416
//
1517
const ModuleComponent = useCallback(
1618
(data: any) => {
17-
const ref = useRef<{Com: FC<any>}>({Com: (props) => <p>hhhhhhhhhhhhhhhhhh</p>});
19+
const ref = useRef<{Com: FC<any>}>({Com: () => <p>Loading...</p>});
1820
const [, rerender] = useState<object>({});
1921
useEffect(() => {
20-
api.getModule(props.children).then((Mod: FC) => {
22+
api.getModule(stringCode, babelOptions).then((Mod: FC) => {
2123
ref.current.Com = Mod;
2224
rerender({});
2325
});
2426
}, []);
2527
const Com = ref.current.Com;
2628
return <Com {...data} />;
2729
},
28-
[props.children],
30+
[stringCode, babelOptions],
2931
);
3032
return <ModuleComponent {...data} />;
3133
}

0 commit comments

Comments
 (0)