@@ -44,20 +44,48 @@ var engine_react = {
44
44
try {
45
45
/* eslint-disable no-eval */
46
46
const componentString = pattern . template || pattern . extendedTemplate ;
47
- const transformedComponent = Babel . transform ( componentString , {
48
- presets : [
49
- require ( 'babel-preset-react' )
50
- ] ,
51
- plugins : [
52
- require ( 'babel-plugin-transform-es2015-modules-commonjs' )
53
- ]
47
+ const nodeComponent = Babel . transform ( componentString , {
48
+ presets : [ require ( 'babel-preset-react' ) ] ,
49
+ plugins : [ require ( 'babel-plugin-transform-es2015-modules-commonjs' ) ]
54
50
} ) ;
55
- const Component = React . createFactory (
56
- eval ( transformedComponent . code )
57
- ) ;
51
+ const runtimeComponent = Babel . transform ( componentString , {
52
+ presets : [ require ( 'babel-preset-react' ) ] ,
53
+ plugins : [ require ( 'babel-plugin-transform-es2015-modules-umd' ) ]
54
+ } ) ;
55
+ const Component = React . createFactory ( eval ( nodeComponent . code ) ) ;
56
+ const output = ReactDOMServer . renderToStaticMarkup ( Component ( data ) ) ;
57
+
58
+ return `<div id="reactContainer">
59
+
60
+ <!-- pattern HTML -->
61
+ ${ output }
62
+
63
+ </div>
64
+
65
+
58
66
59
- return ReactDOMServer . renderToStaticMarkup ( Component ( data ) ) ;
60
- } catch ( e ) {
67
+
68
+ <!-- pattern JSON (React props) -->
69
+ <script id="patternJSON" type="application/json">
70
+ ${ JSON . stringify ( data ) }
71
+ </script>
72
+
73
+ <!-- dependencies -->
74
+ <script>
75
+ var react = React;
76
+ </script>
77
+
78
+ <!-- runtime React output -->
79
+ <script>
80
+ ${ runtimeComponent . code } ;
81
+
82
+ <!-- runtime rendering -->
83
+ var component = unknown.default;
84
+ var patternJSON = document.getElementById('patternJSON').textContent;
85
+ ReactDOM.render(React.createElement(component, JSON.parse(patternJSON)), document.getElementById('reactContainer'));
86
+ </script>` ;
87
+ }
88
+ catch ( e ) {
61
89
console . log ( "Error rendering React pattern." , e ) ;
62
90
return "" ;
63
91
}
0 commit comments