-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
72 lines (59 loc) · 2.24 KB
/
index.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
var React = require('react');
var ReactDOMServer = require('react-dom/server');
var defaults = require('lodash.defaults');
var path = require('path');
module.exports = function engineFactory (engineOptions) {
engineOptions = defaults(engineOptions || {}, {
extension: '.jsx',
babel: {},
docType: '<!DOCTYPE html>',
staticMarkup: false
});
if (engineOptions.babel) {
require('babel-core/register')(engineOptions.babel);
}
function requireComponent(_path_){
return require(_path_) ? require(_path_).default ? require(_path_).default : require(_path_) : require(_path_);
}
return function renderComponent (filename, options, callback) {
options = options || {};
try {
var markup = engineOptions.docType;
/**
Depending on whether one is using ES6 import/export style modules,
or Node's require/exports style Modules, we need to specify how
we should reference the component from the required file
*/
var ReactComponent = requireComponent(filename);
var instance = React.createElement( ReactComponent, options);
var componentMarkup;
if (engineOptions.staticMarkup) {
componentMarkup = ReactDOMServer.renderToStaticMarkup(instance);
} else {
componentMarkup = ReactDOMServer.renderToString(instance);
}
if (engineOptions.wrapper) {
var Wrapper = requireComponent(path.join(this.root, engineOptions.wrapper));
var wrapperInstance = React.createElement(Wrapper, {
body: componentMarkup,
props: options
});
markup += ReactDOMServer.renderToStaticMarkup(wrapperInstance);
} else {
markup += componentMarkup;
}
if (options.settings.env === 'development') {
Object.keys(require.cache).forEach(function(module) {
if (new RegExp('\\' + engineOptions.extension + '$')
.test(require.cache[module].filename)) {
delete require.cache[module];
}
});
}
callback(null, markup);
} catch (error) {
callback(error);
}
};
};