UNPKG

10.9 kBJavaScriptView Raw
1"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var React=_interopDefault(require("react")),ReactDOM=_interopDefault(require("react-dom")),createStore=_interopDefault(require("picostate")),esm=require("flatted/esm"),matchit=require("matchit"),react=require("@picostate/react"),ReactDOMServer=require("react-dom/server"),ReactDOMServer__default=_interopDefault(ReactDOMServer),cx=_interopDefault(require("nanoclass")),plugins$2=_interopDefault(require("@/rola.plugins.js"));function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r<t.length;r++){var o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}function _createClass(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),e}function _extends(){return(_extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&(e[o]=r[o])}return e}).apply(this,arguments)}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&_setPrototypeOf(e,t)}function _getPrototypeOf(e){return(_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function _setPrototypeOf(e,t){return(_setPrototypeOf=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function _objectWithoutPropertiesLoose(e,t){if(null==e)return{};var r,o,n={},a=Object.keys(e);for(o=0;o<a.length;o++)r=a[o],t.indexOf(r)>=0||(n[r]=e[r]);return n}function _objectWithoutProperties(e,t){if(null==e)return{};var r,o,n=_objectWithoutPropertiesLoose(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(o=0;o<a.length;o++)r=a[o],t.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(e,r)&&(n[r]=e[r])}return n}function _assertThisInitialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function _possibleConstructorReturn(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?_assertThisInitialized(e):t}function _slicedToArray(e,t){return _arrayWithHoles(e)||_iterableToArrayLimit(e,t)||_nonIterableRest()}function _arrayWithHoles(e){if(Array.isArray(e))return e}function _iterableToArrayLimit(e,t){var r=[],o=!0,n=!1,a=void 0;try{for(var i,c=e[Symbol.iterator]();!(o=(i=c.next()).done)&&(r.push(i.value),!t||r.length!==t);o=!0);}catch(e){n=!0,a=e}finally{try{o||null==c.return||c.return()}finally{if(n)throw a}}return r}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function matcher(e){var t=e.map(function(e){return matchit.parse(e[0])}),r=e.reduce(function(e,t){return e[t[0]]=t[1],e},{});return function(e){var o=matchit.match(e,t);return o.length?[r[o[0].old],matchit.exec(e,o)]:[]}}var store=createStore({});function historyUpdater(e,t){var r="function"==typeof e?e(store.state):e;r=r.replace(window.location.origin,""),store.hydrate({location:r})(function(){t(r)})}var history={get state(){return store.state},replaceState:function(e){historyUpdater(e,function(e){window.history.replaceState({},"",e)})},pushState:function(e,t){historyUpdater(e,function(e){!t&&window.history.pushState({},"",e)})}};function withHistory(e){return function(t){return React.createElement(e,_extends({history:history},t))}}var store$1=createStore({}),withState=react.connect,Router=function(e){function t(e){var r;return _classCallCheck(this,t),(r=_possibleConstructorReturn(this,_getPrototypeOf(t).call(this,e))).state={children:e.children.pop?e.children[0]:e.children},r.currentLocation=e.location,store.listen(function(t){var o=t.location,n=_slicedToArray(e.router(o),2),a=n[0],i=n[1];if(o!==r.currentLocation)return a.redirect?history.pushState(a.redirect.to):void e.resolve({location:o,params:i,route:a},function(e){r.currentLocation=o,r.setState({children:e})})}),r}return _inherits(t,React.Component),_createClass(t,[{key:"componentDidMount",value:function(){window.addEventListener("popstate",function(e){e.target.window&&history.pushState(e.target.location.href,!0)})}},{key:"render",value:function(){var e=this.state.children;return"function"==typeof e?React.createElement(e,store$1.state):e}}]),t}();function Hypr(e){var t=e.store,r=e.router,o=e.location,n=e.resolve,a=e.children;return React.createElement(react.Provider,{store:t},React.createElement(Router,{router:r,location:o,resolve:n},a))}var plugins=[];try{plugins=require("@/rola.plugins.js").default}catch(e){}var createClientRoot=require("@rola/util/createClientRoot.js");function client(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},o=window.location.href.replace(window.location.origin,""),n=matcher(e.map(function(e){return[e.pathname,_objectWithoutProperties(e,["pathname"])]})),a=_slicedToArray(n(o),2),i=a[0],c=a[1],s=esm.parse(JSON.stringify(window.__rola));store$1.hydrate(Object.assign({},s.state,t,{router:{location:o,params:c}}));var u=i.view,l={state:store$1.state,pathname:window.location.pathname},p=createClientRoot({root:u,context:Object.assign({},l),plugins:plugins});return function(e){ReactDOM.hydrate(React.createElement(Hypr,{store:store$1,router:n,location:o,resolve:function(e,t){var o=e.location,n=e.params,a=e.route;store$1.hydrate({router:{location:o,params:n}});var i=a.load,c=void 0===i?function(){}:i;a.view;Promise.resolve(c(store$1.state)).then(function(e){var o=e.redirect,n=e.state;if(o)return history.pushState(o.to);var i=n.meta||{};store$1.hydrate(n),Promise.resolve(r.resolve?r.resolve({state:store$1.state,pathname:a.pathname}):null).then(function(){document.title=i.title||document.title;var e=a.view,r={state:store$1.state,pathname:a.pathname},o=createClientRoot({root:e,context:Object.assign({},r),plugins:plugins});t(React.createElement(o,r))}).catch(function(e){console.error("options.resolve failed"),console.error(e)})}).catch(function(e){console.error("route.load failed"),console.error(e)})}},React.createElement(p,l)),e)}}var plugins$1=[];try{plugins$1=require("@/rola.plugins.js").default}catch(e){}var doc=require("@rola/util/document.js"),createDocument=require("@rola/util/createDocument.js"),createServerRoot=require("@rola/util/createServerRoot.js"),postServerRender=require("@rola/util/postServerRender.js"),preServerRender=require("@rola/util/preServerRender.js");function redir(e,t,r){var o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:302;e.writeHead(o,{Location:t,Referer:r}),e.end()}function server(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=matcher(e.map(function(e){return[e.pathname,_objectWithoutProperties(e,["pathname"])]}));return function(e,o,n,a){var i=createStore({}),c=_slicedToArray(r(e.url),2),s=c[0],u=c[1];if(!s)return n();var l=s.state,p=void 0===l?{}:l,d=s.load,f=void 0===d?function(){}:d,h=s.view,v=s.redirect;return v?redir(o,v):(i.hydrate(Object.assign(t,p,{router:{location:e.url,params:u}})),Promise.resolve(f(i.state,e)).then(function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=t.redirect,c=t.cache,u=t.status,l=t.pathname,p=t.state,d=void 0===p?{}:p;if(n)return redir(o,n);o.statusCode=u||200,o.setHeader("Content-Type","text/html"),o.setHeader("Cache-Control","string"==typeof c?c:!1===c?"no-cache, no-store, must-revalidate":"public, max-age=".concat(c||86400)),i.hydrate(d);var f={state:i.state,pathname:s.pathname||l},v=createServerRoot({root:h,context:Object.assign({},f),plugins:plugins$1}),y=preServerRender({context:Object.assign({},f),plugins:plugins$1}),m=ReactDOMServer__default.renderToString(React.createElement(Hypr,{store:i,router:r,location:e.url},React.createElement(v,_extends({},f,y)))),g=postServerRender({context:Object.assign({},f),plugins:plugins$1}),_=createDocument(Object.assign({context:Object.assign({},f,a.context),plugins:plugins$1},y,g,a.tags));o.end(doc(Object.assign({},_,{context:f,view:m})))}).catch(function(e){o.statusCode=500,o.setHeader("Content-Type","text/plain"),o.end("rola error"),console.log(e)}))}}function Link(e){var t=e.children,r=e.href,o=e.className,n=e.target,a=e.download,i=_objectWithoutProperties(e,["children","href","className","target","download"]),c=cx([o,history.location===r&&"active"]),s={};return n&&(s.target=n),a&&(s.download=a),React.createElement("a",_extends({href:r,className:c,onClick:function(e){e.ctrlKey||e.metaKey||e.altKey||e.shiftKey||e.defaultPrevented||"_blank"===s.target||s.download||/mailto|tel/.test(r)||/^(https?:)?\/\//.test(r)||(e.preventDefault(),history.pushState(r))}},s,i),t)}var Router$1=function(e){function t(e){var r;return _classCallCheck(this,t),(r=_possibleConstructorReturn(this,_getPrototypeOf(t).call(this,e))).state={children:e.children.pop?e.children[0]:e.children},r.currentLocation=e.location,store.listen(function(t){var o=t.location,n=_slicedToArray(e.router(o),2),a=n[0],i=n[1];if(o!==r.currentLocation)return a.redirect?history.pushState(a.redirect.to):void e.resolve({location:o,params:i,route:a},function(e){r.currentLocation=o,r.setState({children:e})})}),r}return _inherits(t,React.Component),_createClass(t,[{key:"componentDidMount",value:function(){window.addEventListener("popstate",function(e){e.target.window&&history.pushState(e.target.location.href,!0)})}},{key:"render",value:function(){var e=this.state.children;return"function"==typeof e?React.createElement(e,store$1.state):e}}]),t}(),doc$1=require("@rola/util/document.js"),createDocument$1=require("@rola/util/createDocument.js"),createServerRoot$1=require("@rola/util/createServerRoot.js"),postServerRender$1=require("@rola/util/postServerRender.js"),preServerRender$1=require("@rola/util/preServerRender.js");function createStatic(e){return function(t,r){var o=createServerRoot$1({root:e,context:Object.assign({},t),plugins:plugins$2}),n=preServerRender$1({context:Object.assign({},t),plugins:plugins$2}),a=ReactDOMServer.renderToString(React.createElement(Hypr,{store:createStore(t.state),router:matcher([]),location:t.pathname},React.createElement(o,_extends({},t,n)))),i=postServerRender$1({context:Object.assign({},t),plugins:plugins$2}),c=createDocument$1(Object.assign({context:Object.assign({},t,r.context),plugins:plugins$2},n,i,r.tags),!0);return doc$1(Object.assign({},c,{context:t,view:a}))}}exports.Link=Link,exports.Router=Router$1,exports.client=client,exports.createStatic=createStatic,exports.history=history,exports.server=server,exports.store=store$1,exports.withHistory=withHistory,exports.withState=withState;
2//# sourceMappingURL=index.js.map