-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.js
90 lines (64 loc) · 2.71 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
'use strict';
const Hoek = require('@hapi/hoek');
const Path = require('path');
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const DEFAULTS = {
doctype: '<!DOCTYPE html>',
renderMethod: 'renderToStaticMarkup',
removeCache: process.env.NODE_ENV !== 'production',
removeCacheRegExp: undefined,
layout: undefined,
layoutPath: undefined,
layoutRenderMethod: 'renderToStaticMarkup'
};
const compile = function compile(template, compileOpts) {
compileOpts = Hoek.applyToDefaults(DEFAULTS, compileOpts);
return function runtime(context, renderOpts) {
renderOpts = Hoek.applyToDefaults(compileOpts, renderOpts);
let View = require(renderOpts.filename);
// support for es6 default export semantics
View = View.default || View;
const createViewElement = (props = null, children = null) => React.createElement(View, props, children);
let output = renderOpts.doctype;
let layoutPath;
if (renderOpts.layout) {
layoutPath = Path.join(renderOpts.layoutPath, renderOpts.layout);
let Layout = require(layoutPath);
// support for es6 default export semantics
Layout = Layout.default || Layout;
const createLayoutElement = (props = null, children = null) => React.createElement(Layout, props, children);
const viewOutput = ReactDOMServer[renderOpts.renderMethod](createViewElement(context));
output += ReactDOMServer[renderOpts.layoutRenderMethod](createLayoutElement(context, viewOutput));
}
else {
output += ReactDOMServer[renderOpts.renderMethod](createViewElement(context));
}
/*
* Transpilers tend to take a while to start up. Here we delete the
* view and layout modules (and any modules matching the
* `removeCacheRegExp` pattern) from the require cache so we don't need
* to restart the app to see view changes.
*/
if (renderOpts.removeCache) {
if (renderOpts.layout) {
const layoutKey = require.resolve(layoutPath);
delete require.cache[layoutKey];
}
const viewKey = require.resolve(renderOpts.filename);
delete require.cache[viewKey];
if (renderOpts.removeCacheRegExp) {
const regexp = new RegExp(renderOpts.removeCacheRegExp);
Object.keys(require.cache).forEach((cacheKey) => {
if (regexp.test(cacheKey)) {
delete require.cache[cacheKey];
}
});
}
}
return output;
};
};
module.exports = {
compile
};