-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added redux-router from webpack-typescript-builder
- Loading branch information
1 parent
fbf5348
commit 99c34c8
Showing
21 changed files
with
488 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./common" | ||
export * from "./components" | ||
export * from "./common"; | ||
export * from "./components"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ConnectedRouter } from "connected-react-router"; | ||
import * as React from "react"; | ||
import { Provider } from "react-redux"; | ||
import { AppProps, HelmetWrapper } from "@mocoding/react-app-common"; | ||
|
||
import HmrProxyEntryModule from "@mocoding/react-app-common/lib/entry"; | ||
|
||
export class App extends React.Component<AppProps> { | ||
public render(): React.ReactNode { | ||
return ( | ||
<HelmetWrapper helmetContext={this.props.context.helmetContext}> | ||
<Provider store={this.props.context.store}> | ||
<ConnectedRouter history={this.props.context.history}> | ||
<HmrProxyEntryModule /> | ||
</ConnectedRouter> | ||
</Provider> | ||
</HelmetWrapper> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { connectRouter, RouterState, LocationChangeAction } from "connected-react-router"; | ||
import { routerMiddleware } from "connected-react-router"; | ||
import { createBrowserHistory, History } from "history"; | ||
import * as Redux from "redux"; | ||
import { Context } from "@mocoding/react-app-common"; | ||
|
||
import { middlewares, reducers } from "injected-app-entry/store"; | ||
import devMiddlewares from "@mocoding/react-app-router-redux/lib/middlewares"; | ||
|
||
export interface ReduxRouterState { | ||
// Allows any extra properties to be defined in an action. | ||
[extraProps: string]: unknown; | ||
router: RouterState<unknown>; | ||
} | ||
|
||
function createRootReducer<TState extends ReduxRouterState>( | ||
history: History<unknown>, | ||
appReducers: Redux.Reducer[], | ||
): Redux.Reducer<TState> { | ||
// https://github.com/reduxjs/redux/pull/3679 | ||
// Once the issue is fixed - we may remove casting to any | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const reducersMap: any = { | ||
...appReducers, | ||
router: connectRouter(history), | ||
}; | ||
return Redux.combineReducers<TState, Redux.AnyAction>(reducersMap); | ||
} | ||
|
||
function configureStore<TState extends ReduxRouterState>( | ||
history: History<unknown>, | ||
initialState?: Redux.PreloadedState<TState>, | ||
): Redux.Store<TState, Redux.AnyAction> { | ||
// Create reducers from root | ||
const rootReducers = createRootReducer<TState>(history, reducers); | ||
|
||
// Adding logger and router to middlewares | ||
const isSsr = typeof window === "undefined"; | ||
const defaultMiddlewares: Redux.Middleware[] = !isSsr ? devMiddlewares : []; | ||
defaultMiddlewares.push(routerMiddleware(history)); | ||
|
||
const pipeline = Redux.applyMiddleware(...defaultMiddlewares, ...middlewares); | ||
|
||
// create store | ||
const store = Redux.createStore(rootReducers, initialState, Redux.compose(pipeline)); | ||
|
||
if (module.hot) { | ||
module.hot.accept(["injected-app-entry/store"], () => { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const newStore = require("injected-app-entry/store"); | ||
const nextReducer = createRootReducer<TState>(history, newStore.reducers); | ||
|
||
store.replaceReducer(nextReducer); | ||
}); | ||
} | ||
|
||
return store; | ||
} | ||
|
||
export function createContext(abstractHistory?: History): Context { | ||
const initialState = | ||
typeof window === "undefined" || | ||
typeof (window as any).__PRELOADED_STATE__ === "undefined" | ||
? undefined | ||
: JSON.parse((window as any)?.__PRELOADED_STATE__); | ||
const history = abstractHistory ?? createBrowserHistory(); | ||
return { | ||
history, | ||
store: configureStore(history, initialState), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./createContext"; | ||
export * from "./app"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { logger } from "redux-logger"; | ||
|
||
export default [logger]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default []; |
7 changes: 0 additions & 7 deletions
7
packages/react-app-router-redux/lib/mocoding-app-router-redux.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { createMemoryHistory } from "history"; | ||
import * as React from "react"; | ||
import { renderToStaticMarkup, renderToString } from "react-dom/server"; | ||
import { | ||
Context, | ||
RenderCallback, | ||
RenderFuncProps, | ||
HelmetHtml, | ||
HelmetHtmlProps, | ||
} from "@mocoding/react-app-common"; | ||
import { App } from "./app"; | ||
import { createContext } from "./createContext"; | ||
|
||
export function render(callback: RenderCallback, props: RenderFuncProps): void { | ||
try { | ||
const history = createMemoryHistory(); | ||
history.replace(props.requestUrl); | ||
const context: Context = createContext(history); | ||
context.helmetContext = {}; // init helmet for ssr | ||
const markup = renderToString(<App context={context} />); | ||
|
||
const htmlProps: HelmetHtmlProps = { | ||
assets: props.assets, | ||
context, | ||
inlineScripts: props.inlineScripts, | ||
markup, | ||
}; | ||
|
||
const html = "<!DOCTYPE html>" + renderToStaticMarkup(<HelmetHtml {...htmlProps} />); | ||
|
||
callback(undefined, { | ||
html, | ||
}); | ||
} catch (error) { | ||
callback(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"jsx": "preserve", | ||
"noEmit": true, | ||
"strict": true | ||
}, | ||
"include": ["lib/**/*", "./typings.d.ts", "../react-app-common/typings.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
declare module "@mocoding/react-app-router-redux/lib/middlewares" { | ||
import { Middleware } from "redux"; | ||
const middlewares: Middleware[]; | ||
export default middlewares; | ||
} | ||
|
||
declare module "injected-app-entry/store" { | ||
import { Middleware, Reducer } from "redux"; | ||
export const middlewares: Middleware[]; | ||
export const reducers: Reducer[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.