-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
199 lines (164 loc) · 7.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var React = require('react');
var assign = require('object-assign');
var fs = require("fs");
var path = require("path");
var cheerio = require("cheerio");
var Promise = require("bluebird");
var __ = require("lodash");
var DEFAULT_ENGINE_OPTIONS = {
useBabel: true
};
var DEFAULT_PROVIDER_OPTIONS = {
propsProvider: function(componentDomId, componentFilename, componentOptions) {
return Promise.resolve({});
},
prependMarkupProvider: function(componentDomId, componentFilename, componentOptions) {
return Promise.resolve("");
},
appendMarkupProvider: function(componentDomId, componentFilename, componentOptions) {
return Promise.resolve("");
}
};
//Because the providerService may caculate servarl datas that renderFile may use.
//So I will keep the memoriedData here to prevent twice caculation.
var _memoriedData = {};
function _loadHTMLContent(filename) {
//Load baseHTML content
var baseHTML = fs.readFileSync(filename, {
encoding: 'utf-8'
});
var contentQuery = cheerio.load(baseHTML);
return contentQuery;
}
function _parseReactComponentConfigs(contentQuery, rawReactComponentConfigsOnHTML) {
//Set up reactComponent Configs
var reactComponentConfigs = rawReactComponentConfigsOnHTML.map(function(index) {
var reactComponentConfig;
try {
reactComponentConfig = JSON.parse(contentQuery(this).html());
if (typeof reactComponentConfig.filename === "undefined") {
throw new Error("Must be a filename in application/x-react-component.");
}
if (typeof reactComponentConfig.domid === "undefined") {
throw new Error("Must be a domid in application/x-react-component.");
}
} catch (e) {
throw new Error("There is an error in application/x-react-component. " + e.toString());
}
return reactComponentConfig;
});
return reactComponentConfigs;
}
function createEngine(engineOptions) {
var moduleDetectRegEx,
reactComponentFolder,
babelRegistered = false;
engineOptions = assign({}, DEFAULT_ENGINE_OPTIONS, engineOptions || {});
function renderFile(filename, options, cb) {
if (engineOptions.useBabel && !babelRegistered) {
// Passing a RegExp to Babel results in an issue on Windows so we'll just
// pass the view path.
require('babel-core/register')({
only: options.settings.views
});
babelRegistered = true;
}
//Check the reactComponent Folder settings
if (typeof options.settings.reactComponentFolder !== "undefined") {
reactComponentFolder = options.settings.reactComponentFolder;
moduleDetectRegEx = new RegExp('^' + options.settings.reactComponentFolder);
} else {
throw new Error('You should set a reactComponentFolder for using express-partial-react-views');
}
//If user used providerService the following values can be done by providerService, so read it from _memoriedData
var contentQuery = _memoriedData._contentQuery || _loadHTMLContent(filename);
var rawReactComponentConfigsOnHTML = _memoriedData._rawReactComponentConfigsOnHTML || contentQuery("[type='application/x-react-component']");
var reactComponentConfigs = _memoriedData._reactComponentConfigs || _parseReactComponentConfigs(contentQuery, rawReactComponentConfigsOnHTML);
//Replace configs with Server side rendering HTML
rawReactComponentConfigsOnHTML.map(function(index) {
var componentFilename = reactComponentConfigs[index].filename;
var componentDomId = reactComponentConfigs[index].domid
var componentOptions = reactComponentConfigs[index].options;
var componentPath = path.join(reactComponentFolder, componentFilename);
//setup props
var componentProps = __.get(options, '[' + index + '][0]') || {}; //Accrding to the Promise.all order [0] is componentProps
//setip prependMarkup
var prependMarkup = __.get(options, '[' + index + '][1]') || ""; //Accrding to the Promise.all order [1] is prependMarkup
//setup appendMarkup
var appendMarkup = __.get(options, '[' + index + '][2]') || ""; //Accrding to the Promise.all order [1] is appendMarkup
try {
var markup = "";
var component = require(componentPath);
// Transpiled ES6 may export components as { default: Component }
component = component.default || component;
markup += React.renderToStaticMarkup(
React.createElement(component, componentProps)
);
//replace with renderred React Components
contentQuery(this).replaceWith(prependMarkup + "<div id='" + componentDomId + "'>" + markup + "</div>" + appendMarkup);
} catch (e) {
return cb(componentPath + " rendering fails. " + e.toString());
}
});
if (options.settings.env === 'development') {
// Remove all files from the module cache that are in the view folder.
Object.keys(require.cache).forEach(function(module) {
if (moduleDetectRegEx.test(require.cache[module].filename)) {
delete require.cache[module];
}
});
}
cb(null, contentQuery.html({
decodeEntities: false
}));
}
return renderFile;
}
function providerService(app, filename, providerOptions) {
var propsProvider,
appendMarkupProvider,
prependMarkupProvider;
//Get filename meta information
var viewPath = app.get("views");
var viewExtrention = app.get("view engine");
var reactComponentFolder = app.get("reactComponentFolder");
if (typeof viewPath === "undefined") {
throw new Error("Please set the app.set('views')");
}
if (typeof viewExtrention === "undefined") {
throw new Error("Please set the app.set('view engine')");
}
if (typeof reactComponentFolder === "undefined") {
throw new Error("Please set the app.set('reactComponentFolder')");
}
//Set up React Configs
filename = path.join(viewPath, filename) + "." + viewExtrention;
var contentQuery = _loadHTMLContent(filename);
var rawReactComponentConfigsOnHTML = contentQuery("[type='application/x-react-component']");
var reactComponentConfigs = _parseReactComponentConfigs(contentQuery, rawReactComponentConfigsOnHTML);
//memory in _caculated data for renderFile to use.
_memoriedData._contentQuery = contentQuery;
_memoriedData._rawReactComponentConfigsOnHTML = rawReactComponentConfigsOnHTML;
_memoriedData._reactComponentConfigs = reactComponentConfigs;
//Set up Providers
providerOptions = assign({}, DEFAULT_PROVIDER_OPTIONS, providerOptions || {});
propsProvider = providerOptions.propsProvider;
prependMarkupProvider = providerOptions.prependMarkupProvider;
appendMarkupProvider = providerOptions.appendMarkupProvider;
var providedDataPromises = [];
reactComponentConfigs.map(function(index) {
var componentDomId = reactComponentConfigs[index].domid;
var componentFilename = reactComponentConfigs[index].filename;
var componentOptions = reactComponentConfigs[index].options;
providedDataPromises.push(Promise.all(
[
propsProvider(componentDomId, componentFilename, componentOptions),
prependMarkupProvider(componentDomId, componentFilename, componentOptions),
appendMarkupProvider(componentDomId, componentFilename, componentOptions)
]
));
});
return Promise.all(providedDataPromises);
}
exports.createEngine = createEngine;
exports.providerService = providerService;