UNPKG

14.2 kBJavaScriptView Raw
1(function webpackUniversalModuleDefinition(root, factory) {
2 if(typeof exports === 'object' && typeof module === 'object')
3 module.exports = factory(require("@r/middleware"), require("redux"), require("./navigationMiddleware.js"), require("./reducer.js"), require("koa"), require("koa-router"), require("koa-bodyparser"), require("koa-static"), require("lodash/object"), require("lodash/lang"), require("./actions.js"), require("./pageUtils.js"), require("./router.js"));
4 else if(typeof define === 'function' && define.amd)
5 define(["@r/middleware", "redux", "./navigationMiddleware.js", "./reducer.js", "koa", "koa-router", "koa-bodyparser", "koa-static", "lodash/object", "lodash/lang", "./actions.js", "./pageUtils.js", "./router.js"], factory);
6 else if(typeof exports === 'object')
7 exports["Server.js"] = factory(require("@r/middleware"), require("redux"), require("./navigationMiddleware.js"), require("./reducer.js"), require("koa"), require("koa-router"), require("koa-bodyparser"), require("koa-static"), require("lodash/object"), require("lodash/lang"), require("./actions.js"), require("./pageUtils.js"), require("./router.js"));
8 else
9 root["Server.js"] = factory(root["@r/middleware"], root["redux"], root["./navigationMiddleware.js"], root["./reducer.js"], root["koa"], root["koa-router"], root["koa-bodyparser"], root["koa-static"], root["lodash/object"], root["lodash/lang"], root["./actions.js"], root["./pageUtils.js"], root["./router.js"]);
10})(this, function(__WEBPACK_EXTERNAL_MODULE_1__, __WEBPACK_EXTERNAL_MODULE_4__, __WEBPACK_EXTERNAL_MODULE_6__, __WEBPACK_EXTERNAL_MODULE_7__, __WEBPACK_EXTERNAL_MODULE_8__, __WEBPACK_EXTERNAL_MODULE_9__, __WEBPACK_EXTERNAL_MODULE_10__, __WEBPACK_EXTERNAL_MODULE_11__, __WEBPACK_EXTERNAL_MODULE_12__, __WEBPACK_EXTERNAL_MODULE_13__, __WEBPACK_EXTERNAL_MODULE_14__, __WEBPACK_EXTERNAL_MODULE_15__, __WEBPACK_EXTERNAL_MODULE_16__) {
11return /******/ (function(modules) { // webpackBootstrap
12/******/ // The module cache
13/******/ var installedModules = {};
14
15/******/ // The require function
16/******/ function __webpack_require__(moduleId) {
17
18/******/ // Check if module is in cache
19/******/ if(installedModules[moduleId])
20/******/ return installedModules[moduleId].exports;
21
22/******/ // Create a new module (and put it into the cache)
23/******/ var module = installedModules[moduleId] = {
24/******/ exports: {},
25/******/ id: moduleId,
26/******/ loaded: false
27/******/ };
28
29/******/ // Execute the module function
30/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
31
32/******/ // Flag the module as loaded
33/******/ module.loaded = true;
34
35/******/ // Return the exports of the module
36/******/ return module.exports;
37/******/ }
38
39
40/******/ // expose the modules object (__webpack_modules__)
41/******/ __webpack_require__.m = modules;
42
43/******/ // expose the module cache
44/******/ __webpack_require__.c = installedModules;
45
46/******/ // __webpack_public_path__
47/******/ __webpack_require__.p = "";
48
49/******/ // Load entry module and return exports
50/******/ return __webpack_require__(0);
51/******/ })
52/************************************************************************/
53/******/ ([
54/* 0 */
55/***/ function(module, exports, __webpack_require__) {
56
57 'use strict';
58
59 Object.defineProperty(exports, "__esModule", {
60 value: true
61 });
62
63 var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
64
65 var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
66
67 var _middleware = __webpack_require__(1);
68
69 var _koa = __webpack_require__(8);
70
71 var _koa2 = _interopRequireDefault(_koa);
72
73 var _koaRouter = __webpack_require__(9);
74
75 var _koaRouter2 = _interopRequireDefault(_koaRouter);
76
77 var _koaBodyparser = __webpack_require__(10);
78
79 var _koaBodyparser2 = _interopRequireDefault(_koaBodyparser);
80
81 var _koaStatic = __webpack_require__(11);
82
83 var _koaStatic2 = _interopRequireDefault(_koaStatic);
84
85 var _redux = __webpack_require__(4);
86
87 var _object = __webpack_require__(12);
88
89 var _lang = __webpack_require__(13);
90
91 var _navigationMiddleware = __webpack_require__(6);
92
93 var _navigationMiddleware2 = _interopRequireDefault(_navigationMiddleware);
94
95 var _reducer = __webpack_require__(7);
96
97 var _reducer2 = _interopRequireDefault(_reducer);
98
99 var _actions = __webpack_require__(14);
100
101 var _actions2 = _interopRequireDefault(_actions);
102
103 var _pageUtils = __webpack_require__(15);
104
105 var _router = __webpack_require__(16);
106
107 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
108
109 function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
110
111 function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }
112
113 exports.default = function (config) {
114 var _config$port = config.port;
115 var port = _config$port === undefined ? 8888 : _config$port;
116 var _config$preRouteServe = config.preRouteServerMiddleware;
117 var preRouteServerMiddleware = _config$preRouteServe === undefined ? [] : _config$preRouteServe;
118 var _config$postRouteServ = config.postRouteServerMiddleware;
119 var postRouteServerMiddleware = _config$postRouteServ === undefined ? [] : _config$postRouteServ;
120 var _config$reduxMiddlewa = config.reduxMiddleware;
121 var reduxMiddleware = _config$reduxMiddlewa === undefined ? [] : _config$reduxMiddlewa;
122 var _config$reducers = config.reducers;
123 var reducers = _config$reducers === undefined ? {} : _config$reducers;
124 var _config$routes = config.routes;
125 var routes = _config$routes === undefined ? [] : _config$routes;
126 var _config$getServerRout = config.getServerRouter;
127 var getServerRouter = _config$getServerRout === undefined ? function () {} : _config$getServerRout;
128 var _config$template = config.template;
129 var template = _config$template === undefined ? function () {} : _config$template;
130 var _config$dispatchBefor = config.dispatchBeforeNavigation;
131 var dispatchBeforeNavigation = _config$dispatchBefor === undefined ? _asyncToGenerator(regeneratorRuntime.mark(function _callee3() {
132 return regeneratorRuntime.wrap(function _callee3$(_context3) {
133 while (1) {
134 switch (_context3.prev = _context3.next) {
135 case 0:
136 case 'end':
137 return _context3.stop();
138 }
139 }
140 }, _callee3, undefined);
141 })) : _config$dispatchBefor;
142
143
144 var server = new _koa2.default();
145 var bodyparser = (0, _koaBodyparser2.default)();
146 var router = new _koaRouter2.default();
147
148 var handleRoute = function () {
149 var ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(ctx, next) {
150 var nav, well, thunk, r, store, state, currentUrl, currentQuery, newUrl;
151 return regeneratorRuntime.wrap(function _callee2$(_context2) {
152 while (1) {
153 switch (_context2.prev = _context2.next) {
154 case 0:
155 nav = _navigationMiddleware2.default.create(routes);
156 well = _middleware.PromiseWell.create();
157 thunk = _middleware.Thunker.create();
158 r = (0, _redux.combineReducers)(_extends({}, reducers, { platform: _reducer2.default }));
159 store = (0, _redux.createStore)(r, {}, _redux.applyMiddleware.apply(undefined, _toConsumableArray(reduxMiddleware).concat([nav, thunk, well.middleware])));
160
161
162 store.dispatch(function () {
163 var ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(dispatch, getState, utils) {
164 return regeneratorRuntime.wrap(function _callee$(_context) {
165 while (1) {
166 switch (_context.prev = _context.next) {
167 case 0:
168 _context.next = 2;
169 return dispatchBeforeNavigation(ctx, dispatch, getState, utils);
170
171 case 2:
172 case 'end':
173 return _context.stop();
174 }
175 }
176 }, _callee, undefined);
177 }));
178
179 return function (_x3, _x4, _x5) {
180 return ref.apply(this, arguments);
181 };
182 }());
183
184 store.dispatch(_actions2.default.navigateToUrl(ctx.request.method.toLowerCase(), ctx.path, {
185 queryParams: ctx.request.query,
186 bodyParams: ctx.request.body
187 }));
188
189 _context2.next = 9;
190 return well.onComplete();
191
192 case 9:
193 state = store.getState();
194
195 // check for redirects
196
197 currentUrl = state.platform.currentPage.url;
198 currentQuery = state.platform.currentPage.queryParams;
199
200
201 if (!(0, _lang.isEqual)(currentUrl, ctx.path) || !(0, _lang.isEqual)(currentQuery, ctx.request.query)) {
202 if (currentUrl) {
203 newUrl = currentUrl;
204
205 if (!(0, _lang.isEmpty)(currentQuery)) newUrl += (0, _pageUtils.createQuery)(currentQuery);
206 ctx.redirect(newUrl);
207 } else {
208 ctx.redirect('/');
209 }
210 } else {
211 ctx.body = template(state, store);
212 }
213
214 case 13:
215 case 'end':
216 return _context2.stop();
217 }
218 }
219 }, _callee2, undefined);
220 }));
221
222 return function handleRoute(_x, _x2) {
223 return ref.apply(this, arguments);
224 };
225 }();
226
227 var _iteratorNormalCompletion = true;
228 var _didIteratorError = false;
229 var _iteratorError = undefined;
230
231 try {
232 for (var _iterator = routes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
233 var route = _step.value;
234
235 var _route = _slicedToArray(route, 2);
236
237 var path = _route[0];
238 var handler = _route[1];
239 var _iteratorNormalCompletion2 = true;
240 var _didIteratorError2 = false;
241 var _iteratorError2 = undefined;
242
243 try {
244 for (var _iterator2 = (0, _object.values)(_router.METHODS)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
245 var method = _step2.value;
246
247 if (handler.prototype[method]) {
248 router[method](path, handleRoute);
249 }
250 }
251 } catch (err) {
252 _didIteratorError2 = true;
253 _iteratorError2 = err;
254 } finally {
255 try {
256 if (!_iteratorNormalCompletion2 && _iterator2.return) {
257 _iterator2.return();
258 }
259 } finally {
260 if (_didIteratorError2) {
261 throw _iteratorError2;
262 }
263 }
264 }
265 }
266 } catch (err) {
267 _didIteratorError = true;
268 _iteratorError = err;
269 } finally {
270 try {
271 if (!_iteratorNormalCompletion && _iterator.return) {
272 _iterator.return();
273 }
274 } finally {
275 if (_didIteratorError) {
276 throw _iteratorError;
277 }
278 }
279 }
280
281 getServerRouter(router);
282 preRouteServerMiddleware.forEach(function (m) {
283 return server.use(m);
284 });
285 server.use(bodyparser);
286 server.use(router.routes());
287 server.use(router.allowedMethods());
288 postRouteServerMiddleware.forEach(function (m) {
289 return server.use(m);
290 });
291
292 return function () {
293 server.listen(port, function () {
294 console.log('App launching on port ' + port);
295 });
296 };
297 };
298
299/***/ },
300/* 1 */
301/***/ function(module, exports) {
302
303 module.exports = require("@r/middleware");
304
305/***/ },
306/* 2 */,
307/* 3 */,
308/* 4 */
309/***/ function(module, exports) {
310
311 module.exports = require("redux");
312
313/***/ },
314/* 5 */,
315/* 6 */
316/***/ function(module, exports) {
317
318 module.exports = __WEBPACK_EXTERNAL_MODULE_6__;
319
320/***/ },
321/* 7 */
322/***/ function(module, exports) {
323
324 module.exports = __WEBPACK_EXTERNAL_MODULE_7__;
325
326/***/ },
327/* 8 */
328/***/ function(module, exports) {
329
330 module.exports = require("koa");
331
332/***/ },
333/* 9 */
334/***/ function(module, exports) {
335
336 module.exports = require("koa-router");
337
338/***/ },
339/* 10 */
340/***/ function(module, exports) {
341
342 module.exports = require("koa-bodyparser");
343
344/***/ },
345/* 11 */
346/***/ function(module, exports) {
347
348 module.exports = require("koa-static");
349
350/***/ },
351/* 12 */
352/***/ function(module, exports) {
353
354 module.exports = require("lodash/object");
355
356/***/ },
357/* 13 */
358/***/ function(module, exports) {
359
360 module.exports = require("lodash/lang");
361
362/***/ },
363/* 14 */
364/***/ function(module, exports) {
365
366 module.exports = __WEBPACK_EXTERNAL_MODULE_14__;
367
368/***/ },
369/* 15 */
370/***/ function(module, exports) {
371
372 module.exports = __WEBPACK_EXTERNAL_MODULE_15__;
373
374/***/ },
375/* 16 */
376/***/ function(module, exports) {
377
378 module.exports = __WEBPACK_EXTERNAL_MODULE_16__;
379
380/***/ }
381/******/ ])
382});
383;
\No newline at end of file