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 | var _require4 = require('react-router-dom'),
|
19 | BrowserRouter = _require4.BrowserRouter,
|
20 | HashRouter = _require4.HashRouter;
|
21 |
|
22 | var _require5 = require('react-dom'),
|
23 | render = _require5.render;
|
24 |
|
25 | function isFunction(x) {
|
26 | return Object.prototype.toString.call(x) == '[object Function]';
|
27 | }
|
28 |
|
29 | function isString(input) {
|
30 | return typeof input === "string" ? true : false;
|
31 | }
|
32 |
|
33 | function isObject(value) {
|
34 | if (Object.prototype.toString.call(value) !== '[object Object]') {
|
35 | return false;
|
36 | } else {
|
37 | var prototype = Object.getPrototypeOf(value);
|
38 | return prototype === null || prototype === Object.prototype;
|
39 | }
|
40 | }
|
41 |
|
42 | function isUndefined(x) {
|
43 | return !!(x === undefined);
|
44 | }
|
45 |
|
46 | function isNull(x) {
|
47 | return !!(x === null);
|
48 | }
|
49 |
|
50 | function isBoolean(value) {
|
51 | return !!(typeof value === 'boolean');
|
52 | }
|
53 |
|
54 | function isArray(o) {
|
55 | return !!o && (typeof o === 'undefined' ? 'undefined' : _typeof(o)) === "object" && o.length !== undefined;
|
56 | }
|
57 |
|
58 | function isReactComponent(x) {
|
59 | return !!(x && objectHasValues(x) && '$$typeof' in x && _typeof(x['$$typeof']) === 'symbol' && x['$$typeof'].toString() === 'Symbol(react.element)');
|
60 | }
|
61 |
|
62 | function objectHasValues(obj) {
|
63 | if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === "object") {
|
64 | if (Object.getOwnPropertyNames(obj).length > 0) {
|
65 | return true;
|
66 | } else {
|
67 | return false;
|
68 | }
|
69 | } else if (typeof obj === 'undefined') {
|
70 | return false;
|
71 | }
|
72 | }
|
73 |
|
74 | function arrayHasValues(array) {
|
75 | if (array) {
|
76 | if (isArray(array)) {
|
77 | if (array.length) {
|
78 | return true;
|
79 | } else {
|
80 | return false;
|
81 | }
|
82 | } else {
|
83 | return false;
|
84 | }
|
85 | } else {
|
86 | return false;
|
87 | }
|
88 | }
|
89 |
|
90 | function avoidXSS(props) {
|
91 | return JSON.stringify(props).replace(/<\/script/g, '<\\/script').replace(/<!--/g, '<\\!--');
|
92 | }
|
93 |
|
94 | function resolveComponent(path) {
|
95 | var result = void 0;
|
96 | try {
|
97 | if (process.env.NODE_ENV !== "production") {
|
98 | delete require.cache[require.resolve(path)];
|
99 | }
|
100 | result = require(path).default;
|
101 | } catch (err) {
|
102 | result = false;
|
103 | } finally {
|
104 | return result;
|
105 | }
|
106 | }
|
107 |
|
108 | function renderComponent(location, component, props) {
|
109 | var Component = void 0;
|
110 |
|
111 | if (!component) {
|
112 | Component = React.createElement('div', null, null);
|
113 | if (process.env.NODE_ENV !== 'production') {
|
114 | console.info('The component you\'re trying to render seems to not exists.');
|
115 | }
|
116 | } else {
|
117 | Component = React.createElement(component, JSON.parse(avoidXSS(props.props)));
|
118 | }
|
119 |
|
120 | var context = {};
|
121 | var content = React.createElement(StaticRouter, { location: location, context: context }, Component);
|
122 |
|
123 | if (process.env.NODE_ENV === 'production') {
|
124 | return { html: renderToStaticMarkup(content), context: context };
|
125 | } else {
|
126 | return { html: renderToString(content), context: context };
|
127 | }
|
128 | }
|
129 |
|
130 | function getComponentByPathname(routes, path) {
|
131 | var route_ = null;
|
132 |
|
133 | function get(_path, _routes) {
|
134 | _routes.some(function (route) {
|
135 | if ('path' in route && route.path === _path) {
|
136 | route_ = route;
|
137 | return true;
|
138 | } else {
|
139 | if ('routes' in route) {
|
140 | get(_path, route.routes);
|
141 | }
|
142 | return false;
|
143 | }
|
144 | });
|
145 | }
|
146 |
|
147 | get(path, routes);
|
148 | return route_;
|
149 | }
|
150 |
|
151 | function getComponentFromRoutes(routes, url, props) {
|
152 | var extract = isBoolean(arguments[arguments.length - 1]) ? arguments[arguments.length - 1] : false;
|
153 | var output = { url: url, props: props, extract: extract };
|
154 |
|
155 | if (arrayHasValues(routes)) {
|
156 | var branch = matchRoutes(routes, url);
|
157 | if (arrayHasValues(branch)) {
|
158 | if (extract) {
|
159 | if (objectHasValues(branch[0])) {
|
160 | if (objectHasValues(branch[0].route)) {
|
161 | if (objectHasValues(branch[0].route.component)) {
|
162 | if (isFunction(branch[0].route.component.default)) {
|
163 |
|
164 | output.Component = branch[0].route.component.default;
|
165 |
|
166 |
|
167 | if (objectHasValues(branch[1]) && objectHasValues(branch[1].match) && !branch[1].match.isExact) {
|
168 | var found = getComponentByPathname(routes, url);
|
169 | if (found && objectHasValues(found) && objectHasValues(found.component) && isFunction(found.component.default)) {
|
170 | output.Component = found.component.default;
|
171 | }
|
172 | }
|
173 | }
|
174 | } else if (isFunction(branch[0].route.component)) {
|
175 | output.Component = branch[0].route.component;
|
176 |
|
177 |
|
178 | if (objectHasValues(branch[1]) && objectHasValues(branch[1].match) && !branch[1].match.isExact) {
|
179 | var _found = getComponentByPathname(routes, url);
|
180 | if (_found && objectHasValues(_found) && objectHasValues(_found.component) && isFunction(_found.component.default)) {
|
181 | output.Component = _found.component.default;
|
182 | }
|
183 | }
|
184 | }
|
185 | }
|
186 | }
|
187 | } else {
|
188 | if (branch[0].route.component.default) {
|
189 | output.Component = branch[0].route.component.default;
|
190 | } else {
|
191 | output.Component = branch[0].route.component;
|
192 | }
|
193 | }
|
194 | } else {
|
195 | if (isArray(routes) && arrayHasValues(routes) && routes.length === 1) {
|
196 | if (isObject(routes[0]) && objectHasValues(routes[0])) {
|
197 | if (isFunction(routes[0].component)) {
|
198 | output.Component = routes[0].component;
|
199 | }
|
200 | }
|
201 | }
|
202 | }
|
203 | }
|
204 |
|
205 | if (isBrowser()) {
|
206 | var Comp = output.Component;
|
207 | output.element = React.createElement(Comp, avoidXSS(props || {}));
|
208 |
|
209 | }
|
210 |
|
211 | return output;
|
212 | }
|
213 |
|
214 | function isBrowser() {
|
215 | return typeof window !== 'undefined';
|
216 | }
|
217 |
|
218 | function syncRouter(arrayRoutes, defaultComponent) {
|
219 | var properties = {};
|
220 | var component = null;
|
221 |
|
222 | var router = window.__INITIAL_STATE__ && window.__INITIAL_STATE__.reactRouter;
|
223 |
|
224 | if (router) {
|
225 | var _window$__INITIAL_STA = window.__INITIAL_STATE__,
|
226 | url = _window$__INITIAL_STA.url,
|
227 | props = _window$__INITIAL_STA.props,
|
228 | extract = _window$__INITIAL_STA.extract;
|
229 |
|
230 |
|
231 |
|
232 | if (isObject(props)) {
|
233 | properties = props;
|
234 | }
|
235 |
|
236 |
|
237 | if (arrayRoutes && isArray(arrayRoutes) && arrayHasValues(arrayRoutes) && isString(url)) {
|
238 | var _getComponentFromRout = getComponentFromRoutes(arrayRoutes, url, properties, extract),
|
239 | Component = _getComponentFromRout.Component;
|
240 |
|
241 | component = Component;
|
242 | }
|
243 | }
|
244 |
|
245 | var output = {
|
246 | Router: window.location.origin.startsWith("file://") ? HashRouter : BrowserRouter,
|
247 | Component: component || defaultComponent || null,
|
248 | props: properties,
|
249 | refresh: !('pushState' in window.history),
|
250 | render: render,
|
251 | found: !!component
|
252 | };
|
253 |
|
254 | removeInitialState();
|
255 | disableLogHMR();
|
256 |
|
257 | if (isFunction(arguments[arguments.length - 1])) {
|
258 | return arguments[arguments.length - 1](output);
|
259 | } else {
|
260 | return output;
|
261 | }
|
262 | }
|
263 |
|
264 | function removeInitialState() {
|
265 | if (isBrowser()) {
|
266 | var parent = window.document.querySelector('head');
|
267 | var child = window.document.getElementById('__initial_state__');
|
268 | if (child) {
|
269 | parent.removeChild(child);
|
270 | }
|
271 | window.__INITIAL_STATE__ = null;
|
272 | }
|
273 | }
|
274 |
|
275 | function disableLogHMR() {
|
276 | if (isBrowser()) {
|
277 | if (process.env.NODE_ENV === 'development') {
|
278 |
|
279 |
|
280 |
|
281 |
|
282 | (function (global) {
|
283 | var console_log = global.console.log;
|
284 | global.console.log = function () {
|
285 | if (!(arguments.length == 1 && typeof arguments[0] === 'string' && arguments[0].match(/^\[(HMR|WDS)\]/))) {
|
286 | console_log.apply(global.console, arguments);
|
287 | }
|
288 | };
|
289 |
|
290 | })(window);
|
291 | }
|
292 | }
|
293 | }
|
294 |
|
295 | module.exports.isFunction = isFunction;
|
296 | module.exports.isString = isString;
|
297 | module.exports.isObject = isObject;
|
298 | module.exports.isUndefined = isUndefined;
|
299 | module.exports.isNull = isNull;
|
300 | module.exports.isBoolean = isBoolean;
|
301 | module.exports.isArray = isArray;
|
302 | module.exports.isReactComponent = isReactComponent;
|
303 | module.exports.arrayHasValues = arrayHasValues;
|
304 | module.exports.objectHasValues = objectHasValues;
|
305 | module.exports.avoidXSS = avoidXSS;
|
306 | module.exports.resolveComponent = resolveComponent;
|
307 | module.exports.renderComponent = renderComponent;
|
308 | module.exports.getComponentByPathname = getComponentByPathname;
|
309 | module.exports.getComponentFromRoutes = getComponentFromRoutes;
|
310 | module.exports.isBrowser = isBrowser;
|
311 | module.exports.syncRouter = syncRouter;
|
312 | module.exports.removeInitialState = removeInitialState;
|