Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit a9252d5

Browse files
committedOct 23, 2016
Use shorthand method syntax; outsource final page rendering to Hogan;
get the umd module to resolve 'react' to 'React'; provide custom code formatters to Pattern Lab's file writer
1 parent e17c591 commit a9252d5

File tree

3 files changed

+65
-43
lines changed

3 files changed

+65
-43
lines changed
 

‎lib/engine_react.js

+43-43
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,28 @@
1919

2020
"use strict";
2121

22+
const fs = require('fs');
23+
const path = require('path');
2224
const React = require('react');
2325
const ReactDOMServer = require('react-dom/server');
2426
const Babel = require('babel-core');
27+
const Hogan = require('hogan.js');
28+
const beautify = require('js-beautify');
29+
const cheerio = require('cheerio');
30+
31+
const outputTemplate = Hogan.compile(
32+
fs.readFileSync(
33+
path.join(__dirname, './outputTemplate.mustache'),
34+
'utf8'
35+
)
36+
);
2537

2638
var engine_react = {
2739
engine: React,
2840
engineName: 'react',
2941
engineFileExtension: '.jsx',
3042

31-
// partial expansion is only necessary for React templates that have
32-
// style modifiers or pattern parameters (I think)
43+
// hell no
3344
expandPartials: false,
3445

3546
// regexes, stored here so they're only compiled once
@@ -40,7 +51,7 @@ var engine_react = {
4051
findPartialRE: null,
4152

4253
// render it
43-
renderPattern: function renderPattern(pattern, data, partials) {
54+
renderPattern(pattern, data, partials) {
4455
try {
4556
/* eslint-disable no-eval */
4657
const componentString = pattern.template || pattern.extendedTemplate;
@@ -50,40 +61,19 @@ var engine_react = {
5061
});
5162
const runtimeComponent = Babel.transform(componentString, {
5263
presets: [ require('babel-preset-react') ],
53-
plugins: [ require('babel-plugin-transform-es2015-modules-umd') ]
64+
plugins: [[require('babel-plugin-transform-es2015-modules-umd'), {
65+
globals: {
66+
"react": "React"
67+
}
68+
}]]
5469
});
5570
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-
66-
6771

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>`;
72+
return outputTemplate.render({
73+
json: JSON.stringify(data),
74+
htmlOutput: ReactDOMServer.renderToStaticMarkup(Component(data)),
75+
runtimeCode: runtimeComponent.code
76+
});
8777
}
8878
catch (e) {
8979
console.log("Error rendering React pattern.", e);
@@ -98,7 +88,7 @@ ReactDOM.render(React.createElement(component, JSON.parse(patternJSON)), documen
9888
* @param {object} regex A JavaScript RegExp object.
9989
* @returns {array|null} An array if a match is found, null if not.
10090
*/
101-
patternMatcher: function patternMatcher(pattern, regex) {
91+
patternMatcher(pattern, regex) {
10292
var matches;
10393
if (typeof pattern === 'string') {
10494
matches = pattern.match(regex);
@@ -109,32 +99,42 @@ ReactDOM.render(React.createElement(component, JSON.parse(patternJSON)), documen
10999
},
110100

111101
// find and return any {{> template-name }} within pattern
112-
findPartials: function findPartials(pattern) {
102+
findPartials(pattern) {
113103
return [];
114104
},
115-
findPartialsWithStyleModifiers: function (pattern) {
105+
findPartialsWithStyleModifiers(pattern) {
116106
return [];
117107
},
118108

119109
// returns any patterns that match {{> value(foo:"bar") }} or {{>
120110
// value:mod(foo:"bar") }} within the pattern
121-
findPartialsWithPatternParameters: function (pattern) {
111+
findPartialsWithPatternParameters(pattern) {
122112
return [];
123113
},
124-
findListItems: function (pattern) {
114+
findListItems(pattern) {
125115
return [];
126116
},
127117

128118
// given a pattern, and a partial string, tease out the "pattern key" and
129119
// return it.
130-
findPartial_new: function (partialString) {
120+
findPartial(partialString) {
131121
return [];
132122
},
133123

134-
// GTP: the old implementation works better. We might not need
135-
// this.findPartialRE anymore if it works in all cases!
136-
findPartial: function (partialString) {
124+
rawTemplateCodeFormatter(unformattedString) {
125+
console.log('rawTemplateCodeFormatter()');
126+
return beautify(unformattedString, {e4x: true, indent_size: 2});
127+
},
128+
129+
renderedCodeFormatter(unformattedString) {
130+
console.log('renderedCodeFormatter()');
131+
return unformattedString;
132+
},
137133

134+
markupOnlyCodeFormatter(unformattedString) {
135+
const $ = cheerio.load(unformattedString);
136+
console.log('markupOnlyCodeFormatter()');
137+
return beautify.html($('#reactContainer').html(), {indent_size: 2});
138138
}
139139
};
140140

‎lib/outputTemplate.mustache

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<div id="reactContainer">
2+
{{{htmlOutput}}}
3+
</div>
4+
5+
<!-- pattern JSON (React props) -->
6+
<script id="patternJSON" type="application/json">
7+
{{{json}}}
8+
</script>
9+
10+
<!-- runtime React output -->
11+
<script>
12+
{{{runtimeCode}}};
13+
14+
<!-- runtime rendering -->
15+
var component = unknown.default;
16+
var patternJSON = document.getElementById('patternJSON').textContent;
17+
ReactDOM.render(React.createElement(component, JSON.parse(patternJSON)), document.getElementById('reactContainer'));
18+
</script>

‎package.json

+4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
"main": "lib/engine_react.js",
66
"dependencies": {
77
"babel-core": "^6.17.0",
8+
"babel-plugin-module-alias": "^1.6.0",
89
"babel-plugin-transform-es2015-modules-commonjs": "^6.16.0",
910
"babel-plugin-transform-es2015-modules-umd": "^6.12.0",
1011
"babel-preset-react": "^6.16.0",
12+
"cheerio": "^0.22.0",
13+
"hogan": "^1.0.2",
14+
"js-beautify": "^1.6.4",
1115
"react": "^15.3.2",
1216
"react-dom": "^15.3.2"
1317
},

0 commit comments

Comments
 (0)
This repository has been archived.