1 | import thunk from 'redux-thunk'
|
2 | import { browserHistory } from 'react-router'
|
3 | import { routerMiddleware, routerReducer } from 'react-router-redux'
|
4 |
|
5 |
|
6 |
|
7 | import { ReactApp } from 'super-project/ReactApp'
|
8 | import {
|
9 | reducer as realtimeLocationReducer,
|
10 | REALTIME_LOCATION_REDUCER_NAME,
|
11 | actionUpdate
|
12 | } from 'super-project/React/realtime-location'
|
13 | import {
|
14 | reducerLocaleId as i18nReducerLocaleId,
|
15 | reducerLocales as i18nReducerLocales,
|
16 | } from 'super-project/i18n/redux'
|
17 | import i18nRegister from 'super-project/i18n/register/isomorphic.client'
|
18 |
|
19 |
|
20 |
|
21 | import { SERVER_REDUCER_NAME, serverReducer } from '../server/redux'
|
22 |
|
23 | const ROUTER_REDUCDER_NAME = 'routing'
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | export default ({
|
29 | i18n = JSON.parse(process.env.SUPER_I18N) || false,
|
30 | router,
|
31 | redux,
|
32 | client
|
33 | }) => {
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | const reactApp = new ReactApp({ rootDom: 'root' })
|
39 |
|
40 | reactApp.redux.middleware.use(thunk)
|
41 | reactApp.redux.middleware.use(routerMiddleware(browserHistory))
|
42 | if (__CLIENT__) self.routerHistory = browserHistory
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | const reducers = {
|
52 |
|
53 | [ROUTER_REDUCDER_NAME]: routerReducer,
|
54 |
|
55 | [REALTIME_LOCATION_REDUCER_NAME]: realtimeLocationReducer,
|
56 |
|
57 | [SERVER_REDUCER_NAME]: serverReducer,
|
58 | }
|
59 | if (i18n) {
|
60 | reducers.localeId = i18nReducerLocaleId
|
61 | reducers.locales = i18nReducerLocales
|
62 | }
|
63 |
|
64 |
|
65 | if (!redux)
|
66 | redux = client.redux
|
67 |
|
68 | const { combineReducers } = redux
|
69 | if (typeof combineReducers === 'object') {
|
70 | for (let key in combineReducers) {
|
71 | reducers[key] = combineReducers[key]
|
72 | }
|
73 | }
|
74 | for (let key in reducers) {
|
75 | reactApp.redux.reducer.use(key, reducers[key])
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | if (typeof router !== 'object') {
|
85 | if (client.router)
|
86 | router = client.router
|
87 | else
|
88 | router = {}
|
89 | }
|
90 | reactApp.react.router.use({
|
91 | path: '',
|
92 |
|
93 | childRoutes: [router]
|
94 | })
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | if (__CLIENT__) {
|
104 | const {
|
105 | before,
|
106 | after,
|
107 | } = client
|
108 | const onRouterUpdate = client.routerUpdate || client.onRouterUpdate
|
109 | const onHistoryUpdate = client.historyUpdate || client.onHistoryUpdate
|
110 |
|
111 | reactApp.react.router.ext({
|
112 | onUpdate: (...args) => {
|
113 | if (__DEV__)
|
114 | console.log(
|
115 | `[super/client] ` +
|
116 | `callback: onRouterUpdate`,
|
117 | args
|
118 | )
|
119 |
|
120 | if (typeof onRouterUpdate === 'function')
|
121 | onRouterUpdate(...args)
|
122 | }
|
123 | })
|
124 |
|
125 | if (i18n) i18nRegister(__REDUX_STATE__)
|
126 |
|
127 | let beforePromise = before
|
128 | if (__DEV__)
|
129 | console.log(
|
130 | `[super/client] ` +
|
131 | `callback: before`,
|
132 |
|
133 | )
|
134 | if (typeof before === 'function') {
|
135 | beforePromise = new Promise(resolve => {
|
136 | before()
|
137 | resolve()
|
138 | })
|
139 | } else if (typeof before !== 'object' || typeof before.then !== 'function') {
|
140 | beforePromise = new Promise(resolve => resolve())
|
141 | }
|
142 |
|
143 | beforePromise.then(() =>
|
144 | reactApp.run({
|
145 | browserHistoryOnUpdate: (location, store) => {
|
146 |
|
147 |
|
148 | if (__DEV__) {
|
149 | console.log('🌏 browserHistory update', location)
|
150 | }
|
151 |
|
152 | store.dispatch(actionUpdate(location))
|
153 |
|
154 |
|
155 | if (__DEV__)
|
156 | console.log(
|
157 | `[super/client] ` +
|
158 | `callback: onHistoryUpdate`,
|
159 | [location, store]
|
160 | )
|
161 | if (typeof onHistoryUpdate === 'function')
|
162 | onHistoryUpdate(location, store)
|
163 | }
|
164 | })
|
165 | )
|
166 | .then((appData) => {
|
167 | if (__DEV__)
|
168 | console.log(
|
169 | `[super/client] ` +
|
170 | `callback: after`,
|
171 | [appData]
|
172 | )
|
173 | if (typeof after === 'function') after(appData)
|
174 | })
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | return reactApp
|
184 | }
|