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

Commit a38c867

Browse files
committed
Merge branch 'dev'
2 parents d68b361 + fa00871 commit a38c867

File tree

6 files changed

+329
-2
lines changed

6 files changed

+329
-2
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
tab_width = 2
8+
end_of_line = lf
9+
charset = utf-8
10+
trim_trailing_whitespace = true
11+
insert_final_newline = true

.eslintrc

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"builtin": true
5+
},
6+
"parserOptions": {
7+
"ecmaVersion": 6,
8+
"sourceType": "module",
9+
"ecmaFeatures": {
10+
"jsx": true
11+
}
12+
},
13+
"globals": {},
14+
"rules": {
15+
"block-scoped-var": 0,
16+
"camelcase": 0,
17+
"comma-spacing": [1, {"before": false, "after": true}],
18+
"consistent-return": 2,
19+
"curly": [2, "all"],
20+
"dot-notation": [1, { "allowKeywords": true }],
21+
"eqeqeq": [2, "allow-null"],
22+
"global-strict": [0, "never"],
23+
"guard-for-in": 2,
24+
"indent": [1, 2, {"SwitchCase": 1, "VariableDeclarator": 1}],
25+
"lines-around-comment": [1, {
26+
"beforeBlockComment": true,
27+
"beforeLineComment": true,
28+
"allowBlockStart": true,
29+
"allowObjectStart": true,
30+
"allowArrayStart": true
31+
}],
32+
"key-spacing": 0,
33+
"keyword-spacing": 1,
34+
"new-cap": 0,
35+
"no-alert": 2,
36+
"no-bitwise": 2,
37+
"no-caller": 2,
38+
"no-cond-assign": [2, "except-parens"],
39+
"no-debugger": 2,
40+
"no-dupe-args": 2,
41+
"no-dupe-keys": 2,
42+
"no-empty": 2,
43+
"no-eval": 2,
44+
"no-extend-native": 2,
45+
"no-extra-bind": 2,
46+
"no-extra-parens": 0,
47+
"no-extra-semi": 2,
48+
"no-func-assign": 2,
49+
"no-implied-eval": 2,
50+
"no-invalid-regexp": 2,
51+
"no-irregular-whitespace": 1,
52+
"no-iterator": 2,
53+
"no-loop-func": 2,
54+
"no-mixed-requires": 0,
55+
"no-multi-str": 2,
56+
"no-multi-spaces": 1,
57+
"no-native-reassign": 2,
58+
"no-new": 2,
59+
"no-param-reassign": 1,
60+
"no-proto": 2,
61+
"no-redeclare": 0,
62+
"no-script-url": 2,
63+
"no-self-assign": 2,
64+
"no-self-compare": 2,
65+
"no-sequences": 2,
66+
"no-shadow": 2,
67+
"no-undef": 2,
68+
"no-underscore-dangle": 0,
69+
"no-unreachable": 1,
70+
"no-unused-vars": 1,
71+
"no-use-before-define": 1,
72+
"no-useless-call": 2,
73+
"no-useless-concat": 2,
74+
"no-with": 2,
75+
"quotes": [0, "single"],
76+
"radix": 2,
77+
"semi": [0, "never"],
78+
"strict": 0,
79+
"space-before-blocks": 1,
80+
"space-before-function-paren": [1, {
81+
"anonymous": "always",
82+
"named": "never"
83+
}],
84+
"space-in-parens": [1, "never"],
85+
"space-infix-ops": 1,
86+
"valid-typeof": 2,
87+
"vars-on-top": 0,
88+
"wrap-iife": [2, "inside"]
89+
}
90+
}

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
# The React engine for Patternlab/Node
1+
# patternengine-node-react
2+
The (**very preliminary almost not-working-at-all**) React engine for Patternlab/Node
23

34
## Status
4-
This engine is experimental and at present is still non-functional -- development is taking place in the `dev` branch.
5+
You can author standalone React components that include only the main React module, which I know isn't much yet. We're still working out how React components will resolve and load the modules they depend on, including other patterns. We believe this is tricky, but doable.
6+
7+
## Supported features
8+
Kind of nothing works yet. **Very early in development.**
9+
10+
- [ ] [Includes](http://patternlab.io/docs/pattern-including.html)
11+
- [ ] Lineage
12+
- [ ] [Hidden Patterns](http://patternlab.io/docs/pattern-hiding.html)
13+
- [ ] [Pseudo-Patterns](http://patternlab.io/docs/pattern-pseudo-patterns.html)
14+
- [ ] [Pattern States](http://patternlab.io/docs/pattern-states.html)
15+
- [ ] [Pattern Parameters](http://patternlab.io/docs/pattern-parameters.html)
16+
- [ ] [Style Modifiers](http://patternlab.io/docs/pattern-stylemodifier.html)
17+
18+
## Usage
19+
20+
* `*.jsx` files are detected as patterns.
21+
* Standard pattern JSON is passed into React components as props.
22+
* We currently assume the React include (and others, once we figure that out) are written using es2015 module syntax.
23+
* The Babel transforms are currently hard-coded into the engine, but we hope to make this configurable in the future.

lib/engine_react.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* react pattern engine for patternlab-node - v0.1.0 - 2016
3+
*
4+
* Geoffrey Pursell, Brian Muenzenmeyer, and the web community.
5+
* Licensed under the MIT license.
6+
*
7+
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
8+
*
9+
*/
10+
11+
/*
12+
* ENGINE SUPPORT LEVEL:
13+
*
14+
* Full + extensions. Partial calls and lineage hunting are supported. Style
15+
* modifiers and pattern parameters are used to extend the core feature set of
16+
* React templates.
17+
*
18+
*/
19+
20+
"use strict";
21+
22+
const fs = require('fs');
23+
const path = require('path');
24+
const React = require('react');
25+
const ReactDOMServer = require('react-dom/server');
26+
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+
);
37+
38+
var engine_react = {
39+
engine: React,
40+
engineName: 'react',
41+
engineFileExtension: '.jsx',
42+
43+
// hell no
44+
expandPartials: false,
45+
46+
// regexes, stored here so they're only compiled once
47+
findPartialsRE: null,
48+
findPartialsWithStyleModifiersRE: null,
49+
findPartialsWithPatternParametersRE: null,
50+
findListItemsRE: null,
51+
findPartialRE: null,
52+
53+
// render it
54+
renderPattern(pattern, data, partials) {
55+
try {
56+
/* eslint-disable no-eval */
57+
const componentString = pattern.template || pattern.extendedTemplate;
58+
const nodeComponent = Babel.transform(componentString, {
59+
presets: [ require('babel-preset-react') ],
60+
plugins: [ require('babel-plugin-transform-es2015-modules-commonjs') ]
61+
});
62+
const runtimeComponent = Babel.transform(componentString, {
63+
presets: [ require('babel-preset-react') ],
64+
plugins: [[require('babel-plugin-transform-es2015-modules-umd'), {
65+
globals: {
66+
"react": "React"
67+
}
68+
}]]
69+
});
70+
const Component = React.createFactory(eval(nodeComponent.code));
71+
72+
return outputTemplate.render({
73+
patternPartial: pattern.patternPartial,
74+
json: JSON.stringify(data),
75+
htmlOutput: ReactDOMServer.renderToStaticMarkup(Component(data)),
76+
runtimeCode: runtimeComponent.code
77+
});
78+
}
79+
catch (e) {
80+
console.log("Error rendering React pattern.", e);
81+
return "";
82+
}
83+
},
84+
85+
/**
86+
* Find regex matches within both pattern strings and pattern objects.
87+
*
88+
* @param {string|object} pattern Either a string or a pattern object.
89+
* @param {object} regex A JavaScript RegExp object.
90+
* @returns {array|null} An array if a match is found, null if not.
91+
*/
92+
patternMatcher(pattern, regex) {
93+
var matches;
94+
if (typeof pattern === 'string') {
95+
matches = pattern.match(regex);
96+
} else if (typeof pattern === 'object' && typeof pattern.template === 'string') {
97+
matches = pattern.template.match(regex);
98+
}
99+
return matches;
100+
},
101+
102+
// find and return any {{> template-name }} within pattern
103+
findPartials(pattern) {
104+
return [];
105+
},
106+
findPartialsWithStyleModifiers(pattern) {
107+
return [];
108+
},
109+
110+
// returns any patterns that match {{> value(foo:"bar") }} or {{>
111+
// value:mod(foo:"bar") }} within the pattern
112+
findPartialsWithPatternParameters(pattern) {
113+
return [];
114+
},
115+
findListItems(pattern) {
116+
return [];
117+
},
118+
119+
// given a pattern, and a partial string, tease out the "pattern key" and
120+
// return it.
121+
findPartial(partialString) {
122+
return [];
123+
},
124+
125+
rawTemplateCodeFormatter(unformattedString) {
126+
return beautify(unformattedString, {e4x: true, indent_size: 2});
127+
},
128+
129+
renderedCodeFormatter(unformattedString) {
130+
return unformattedString;
131+
},
132+
133+
markupOnlyCodeFormatter(unformattedString, pattern) {
134+
const $ = cheerio.load(unformattedString);
135+
return beautify.html($('.reactPatternContainer').html(), {indent_size: 2});
136+
},
137+
138+
/**
139+
* Add custom output files to the pattern output
140+
* @param {object} patternlab - the global state object
141+
* @returns {(object|object[])} - an object or array of objects,
142+
* each with two properties: path, and content
143+
*/
144+
addOutputFiles(paths, patternlab) {
145+
return [];
146+
}
147+
};
148+
149+
module.exports = engine_react;

lib/outputTemplate.mustache

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<div class="reactPatternContainer" id="reactPatternContainer--{{{patternPartial}}}">
2+
{{{htmlOutput}}}
3+
</div>
4+
5+
<!-- pattern JSON (React props) -->
6+
<script data-type="patternJSON" id="patternJSON--{{{patternPartial}}}" 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--{{{patternPartial}}}').textContent;
17+
ReactDOM.render(React.createElement(component, JSON.parse(patternJSON)), document.getElementById('reactPatternContainer--{{{patternPartial}}}'));
18+
</script>

package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "patternengine-node-react",
3+
"description": "The React engine for Pattern Lab / Node",
4+
"version": "0.1.0",
5+
"main": "lib/engine_react.js",
6+
"dependencies": {
7+
"babel-core": "^6.17.0",
8+
"babel-plugin-module-alias": "^1.6.0",
9+
"babel-plugin-transform-es2015-modules-commonjs": "^6.16.0",
10+
"babel-plugin-transform-es2015-modules-umd": "^6.12.0",
11+
"babel-preset-react": "^6.16.0",
12+
"cheerio": "^0.22.0",
13+
"hogan": "^1.0.2",
14+
"js-beautify": "^1.6.4",
15+
"react": "^15.3.2",
16+
"react-dom": "^15.3.2"
17+
},
18+
"devDependencies": {},
19+
"keywords": [
20+
"Pattern Lab",
21+
"Atomic Web Design",
22+
"Node",
23+
"Gulp",
24+
"Javascript",
25+
"React"
26+
],
27+
"repository": {
28+
"type": "git",
29+
"url": "https://github.com/pattern-lab/patternengine-node-react.git"
30+
},
31+
"bugs": "https://github.com/pattern-lab/patternlab-node/issues",
32+
"author": "Brian Muenzenmeyer & Geoffrey Pursell",
33+
"license": "MIT",
34+
"scripts": {
35+
"test": "grunt travis --verbose"
36+
},
37+
"engines": {
38+
"node": ">=4.4"
39+
}
40+
}

0 commit comments

Comments
 (0)