import * as Immutable from "immutable" ; import * as React from "react" ; import * as ReactDom from "react-dom" ; import { History , createBrowserHistory , createHashHistory , createMemoryHistory } from "history" ; import { applyMiddleware , createStore } from "redux" ; import { Provider } from "react-redux" ; import * as ReactRouterRedux from "react-router-redux" ; import thunkMiddleware from "redux-thunk" ; import { createLogger } from "redux-logger" ; import { serverSideRender , handler as lambdaHandler } from "./server" ; import EnvChecker from "./helpers/envChecker" ; import CssInjector from "./helpers/cssInjector" ; import { rootReducer , initialState , IAppState } from "./rootReducer" ; import { RootRoutes } from "./routes" ; let history ; if ( EnvChecker . isServer ( ) ) { history = createMemoryHistory ( ) ; } else { if ( EnvChecker . isDev ( ) ) { history = createHashHistory ( ) ; } else { history = createBrowserHistory ( ) ; } } const routerMid = ReactRouterRedux . routerMiddleware ( history ) ; let AppInitialState ; if ( ! EnvChecker . isServer ( ) ) { try { const appInitialState = { } ; const __INITIAL_STATE__ = ( window as any ) . __INITIAL_STATE__ ; for ( let k in __INITIAL_STATE__ ) { if ( __INITIAL_STATE__ . hasOwnProperty ( k ) ) { appInitialState [ k ] = Immutable . fromJS ( __INITIAL_STATE__ [ k ] ) ; } } AppInitialState = appInitialState as IAppState ; } catch ( err ) { console . error ( err ) ; console . warn ( "There is no initial state from server" ) ; AppInitialState = initialState ; } } let store ; if ( EnvChecker . isServer ( ) || ! EnvChecker . isDev ( ) ) { store = createStore ( rootReducer , AppInitialState , applyMiddleware ( routerMid , thunkMiddleware ) ) ; } else { const logger = createLogger ( { stateTransformer : state => { const newState = { } ; for ( const i of Object . keys ( state ) ) { if ( Immutable . Iterable . isIterable ( state [ i ] ) ) { newState [ i ] = state [ i ] . toJS ( ) ; } else { newState [ i ] = state [ i ] ; } } return newState ; } , } ) ; store = createStore ( rootReducer , AppInitialState , applyMiddleware ( routerMid , thunkMiddleware , logger ) ) ; } export const appStore = store ; if ( ! EnvChecker . isServer ( ) ) { ReactDom . render ( < CssInjector > < Provider store = { store } > < ReactRouterRedux . ConnectedRouter history = { history } > < RootRoutes /> < /ReactRouterRedux.ConnectedRouter> < /Provider> < /CssInjector>, document.getElementById("react-app")); } if ( EnvChecker . isServer ( ) && process . env . SSR_TEST ) { serverSideRender ( "/users/tylorshin" , "mockedScriptPath" ) . then ( ( res ) => { console . log ( res ) ; } ) . catch ( err => { console . log ( err ) ; } ) ; } export const handler = lambdaHandler ;