1 | 'use strict';
|
2 |
|
3 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
4 |
|
5 | var React = require('react');
|
6 |
|
7 | var _require = require('react-router'),
|
8 | StaticRouter = _require.StaticRouter;
|
9 |
|
10 | var _require2 = require('react-dom/server'),
|
11 | renderToString = _require2.renderToString,
|
12 | renderToStaticMarkup = _require2.renderToStaticMarkup;
|
13 |
|
14 | var _require3 = require('react-router-config'),
|
15 | renderRoutes = _require3.renderRoutes,
|
16 | matchRoutes = _require3.matchRoutes;
|
17 |
|
18 | function isFunction(x) {
|
19 | return Object.prototype.toString.call(x) == '[object Function]';
|
20 | }
|
21 |
|
22 | function isString(input) {
|
23 | return typeof input === "string" ? true : false;
|
24 | }
|
25 |
|
26 | function isObject(value) {
|
27 | if (Object.prototype.toString.call(value) !== '[object Object]') {
|
28 | return false;
|
29 | } else {
|
30 | var prototype = Object.getPrototypeOf(value);
|
31 | return prototype === null || prototype === Object.prototype;
|
32 | }
|
33 | }
|
34 |
|
35 | function isUndefined(x) {
|
36 | return !!(x === undefined);
|
37 | }
|
38 |
|
39 | function isNull(x) {
|
40 | return !!(x === null);
|
41 | }
|
42 |
|
43 | function isBoolean(value) {
|
44 | return !!(typeof value === 'boolean');
|
45 | }
|
46 |
|
47 | function isArray(o) {
|
48 | return !!o && (typeof o === 'undefined' ? 'undefined' : _typeof(o)) === "object" && o.length !== undefined;
|
49 | }
|
50 |
|
51 | function isReactComponent(x) {
|
52 | return !!(x && objectHasValues(x) && '$$typeof' in x && _typeof(x['$$typeof']) === 'symbol' && x['$$typeof'].toString() === 'Symbol(react.element)');
|
53 | }
|
54 |
|
55 | function objectHasValues(obj) {
|
56 | if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === "object") {
|
57 | if (Object.getOwnPropertyNames(obj).length > 0) {
|
58 | return true;
|
59 | } else {
|
60 | return false;
|
61 | }
|
62 | } else if (typeof obj === 'undefined') {
|
63 | return false;
|
64 | }
|
65 | }
|
66 |
|
67 | function arrayHasValues(array) {
|
68 | if (array) {
|
69 | if (isArray(array)) {
|
70 | if (array.length) {
|
71 | return true;
|
72 | } else {
|
73 | return false;
|
74 | }
|
75 | } else {
|
76 | return false;
|
77 | }
|
78 | } else {
|
79 | return false;
|
80 | }
|
81 | }
|
82 |
|
83 | function avoidXSS(props) {
|
84 | return JSON.stringify(props).replace(/<\/script/g, '<\\/script').replace(/<!--/g, '<\\!--');
|
85 | }
|
86 |
|
87 | function resolveComponent(path) {
|
88 | var result = void 0;
|
89 | try {
|
90 | if (process.env.NODE_ENV !== "production") {
|
91 | delete require.cache[require.resolve(path)];
|
92 | }
|
93 | result = require(path).default;
|
94 | } catch (err) {
|
95 | result = false;
|
96 | } finally {
|
97 | return result;
|
98 | }
|
99 | }
|
100 |
|
101 | function renderComponent(location, component, props) {
|
102 | var Component = void 0;
|
103 |
|
104 | if (!component) {
|
105 | Component = React.createElement('div', null, null);
|
106 | if (process.env.NODE_ENV !== 'production') {
|
107 | console.info('The component you\'re trying to render seems to not exists.');
|
108 | }
|
109 | } else {
|
110 | Component = React.createElement(component, JSON.parse(avoidXSS(props.props)));
|
111 | }
|
112 |
|
113 | var context = {};
|
114 | var content = React.createElement(StaticRouter, { location: location, context: context }, Component);
|
115 |
|
116 | if (process.env.NODE_ENV === 'production') {
|
117 | return { html: renderToStaticMarkup(content), context: context };
|
118 | } else {
|
119 | return { html: renderToString(content), context: context };
|
120 | }
|
121 | }
|
122 |
|
123 | function getComponentByPathname(routes, path) {
|
124 | var route_ = null;
|
125 |
|
126 | function get(_path, _routes) {
|
127 | _routes.some(function (route) {
|
128 | if ('path' in route && route.path === _path) {
|
129 | route_ = route;
|
130 | return true;
|
131 | } else {
|
132 | if ('routes' in route) {
|
133 | get(_path, route.routes);
|
134 | }
|
135 | return false;
|
136 | }
|
137 | });
|
138 | }
|
139 |
|
140 | get(path, routes);
|
141 | return route_;
|
142 | }
|
143 |
|
144 | function getComponentFromRoutes(routes, url, props) {
|
145 | var extract = isBoolean(arguments[arguments.length - 1]) ? arguments[arguments.length - 1] : false;
|
146 | var output = { url: url, props: props, extract: extract };
|
147 |
|
148 | if (arrayHasValues(routes)) {
|
149 | var branch = matchRoutes(routes, url);
|
150 | if (arrayHasValues(branch)) {
|
151 | if (extract) {
|
152 | if (objectHasValues(branch[0])) {
|
153 | if (objectHasValues(branch[0].route)) {
|
154 | if (objectHasValues(branch[0].route.component)) {
|
155 | if (isFunction(branch[0].route.component.default)) {
|
156 |
|
157 | output.Component = branch[0].route.component.default;
|
158 |
|
159 |
|
160 | if (objectHasValues(branch[1]) && objectHasValues(branch[1].match) && !branch[1].match.isExact) {
|
161 | var found = getComponentByPathname(routes, url);
|
162 | if (found && objectHasValues(found) && objectHasValues(found.component) && isFunction(found.component.default)) {
|
163 | output.Component = found.component.default;
|
164 | }
|
165 | }
|
166 | }
|
167 | } else if (isFunction(branch[0].route.component)) {
|
168 | output.Component = branch[0].route.component;
|
169 |
|
170 |
|
171 | if (objectHasValues(branch[1]) && objectHasValues(branch[1].match) && !branch[1].match.isExact) {
|
172 | var _found = getComponentByPathname(routes, url);
|
173 | if (_found && objectHasValues(_found) && objectHasValues(_found.component) && isFunction(_found.component.default)) {
|
174 | output.Component = _found.component.default;
|
175 | }
|
176 | }
|
177 | }
|
178 | }
|
179 | }
|
180 | } else {
|
181 | if (branch[0].route.component.default) {
|
182 | output.Component = branch[0].route.component.default;
|
183 | } else {
|
184 | output.Component = branch[0].route.component;
|
185 | }
|
186 | }
|
187 | } else {
|
188 | if (isArray(routes) && arrayHasValues(routes) && routes.length === 1) {
|
189 | if (isObject(routes[0]) && objectHasValues(routes[0])) {
|
190 | if (isFunction(routes[0].component)) {
|
191 | output.Component = routes[0].component;
|
192 | }
|
193 | }
|
194 | }
|
195 | }
|
196 | }
|
197 |
|
198 | if (isBrowser()) {
|
199 | output.element = React.createElement(output.Component, JSON.parse(avoidXSS(props || {})));
|
200 | }
|
201 |
|
202 | return output;
|
203 | }
|
204 |
|
205 | function isBrowser() {
|
206 | return typeof window !== 'undefined';
|
207 | }
|
208 |
|
209 | module.exports.isFunction = isFunction;
|
210 | module.exports.isString = isString;
|
211 | module.exports.isObject = isObject;
|
212 | module.exports.isUndefined = isUndefined;
|
213 | module.exports.isNull = isNull;
|
214 | module.exports.isBoolean = isBoolean;
|
215 | module.exports.isArray = isArray;
|
216 | module.exports.isReactComponent = isReactComponent;
|
217 | module.exports.arrayHasValues = arrayHasValues;
|
218 | module.exports.objectHasValues = objectHasValues;
|
219 | module.exports.avoidXSS = avoidXSS;
|
220 | module.exports.resolveComponent = resolveComponent;
|
221 | module.exports.renderComponent = renderComponent;
|
222 | module.exports.getComponentByPathname = getComponentByPathname;
|
223 | module.exports.getComponentFromRoutes = getComponentFromRoutes;
|
224 | module.exports.isBrowser = isBrowser;
|