-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (43 loc) · 1.62 KB
/
Copy pathindex.js
File metadata and controls
50 lines (43 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import React from 'react';
import {transform} from '@babel/core';
import {renderToString} from 'react-dom/server';
import '@babel/preset-react';
// import '@babel/plugin-transform-react-jsx';
// import {parse} from '@babel/parser';
// import traverse from '@babel/traverse';
// import generate from '@babel/generator';
// import t from '@babel/types';
// const knownIdentifiers = [
// 'React', 'createElement',
// 'style', 'href', 'textDecoration', 'fontSize',
// ];
const replaceVars = function(code) {
return code;
// // parse the code -> ast
// const ast = parse(code);
// // transform the ast
// traverse(ast, {
// enter(path) {
// // if (path.node.type === 'Identifier') console.log(path.node.name, 'id', t.isIdentifier(path.node), 'jsx', t.isJSXIdentifier(path.node), 'node', t.jsxIdentifier(path.node.name));
// if (path.node.type === 'Identifier' && !knownIdentifiers.includes(path.node.name)) {
// Logger.debug('Replacing', path.node.name);
// path.node.name = 'data.' + path.node.name;
// }
// },
// });
// // generate code <- ast
// const output = generate(ast, code);
// return output.code;
};
export const getCodeFromTemplate = function(template) {
const {code} = transform(template, {
presets: ['@babel/preset-react'],
// plugins: ['@babel/plugin-transform-react-jsx'],
});
// we do not use (data) before replaceVars as it too will be replaced;
return '(props) => ' + replaceVars(code);
};
export const render = function({template, params}) {
const c = new Function('React', `return ${getCodeFromTemplate(template)}`)(React); // eslint-disable-line no-new-func
return renderToString(c(params));
};