Skip to content

Commit 14bd03a

Browse files
committed
fix: optimize ts type
1 parent ba6677e commit 14bd03a

File tree

11 files changed

+33
-22
lines changed

11 files changed

+33
-22
lines changed

.eslintrc.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
"no-new-wrappers": "error",
130130
"no-lonely-if": "off",
131131
"no-param-reassign": "off",
132+
"no-alert": "off",
132133
"no-shadow": [
133134
"off",
134135
{
@@ -246,7 +247,8 @@
246247
"react/prop-types": "off",
247248
"no-underscore-dangle": "off",
248249
"func-names": "off",
249-
"no-empty-function": "off"
250+
"no-empty-function": "off",
251+
"no-console": "off"
250252
}
251253
}
252254
// {

src/client/components/asyncBundle/index.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React from 'react'
1+
import React, { ReactNode } from 'react'
2+
import { ILoader } from '../asyncLoader'
23
import LoadingCompoent from '../loading'
34

45
/**
@@ -9,8 +10,8 @@ import LoadingCompoent from '../loading'
910
*/
1011

1112
interface IPops {
12-
children: any
13-
load: any
13+
children: (mod: any) => ReactNode
14+
load: ILoader
1415
}
1516

1617
interface IState {

src/client/components/asyncLoader/index.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import React from 'react'
22
import AsyncBundle from '../asyncBundle'
33
import proConfig from '../../../../config/pro-config'
44

5-
function AsyncLoader(loader: any) {
6-
const asyncFn: any = (props: any) => (
7-
<AsyncBundle load={loader}>{(Comp: any) => <Comp {...props} />}</AsyncBundle>
5+
interface IProps {}
6+
export type ILoader = () => Promise<any>
7+
function AsyncLoader(loader: ILoader) {
8+
const asyncFn: any = (props: IProps) => (
9+
<AsyncBundle load={loader}>
10+
{(Comp: typeof React.Component) => <Comp {...props} />}
11+
</AsyncBundle>
812
)
913
// 标记为异步组件
1014
asyncFn[proConfig.asyncComponentKey] = true

src/client/main/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function renderDom(routeList: IRoute[], initStoreState?: Store) {
3434

3535
function clientRender(routeList: IRoute[]) {
3636
let initialData: any = null
37-
let initStoreState:Store|undefined
37+
let initStoreState: Store | undefined
3838
const textDom = document.getElementById('ssrTextInitData') as HTMLTextAreaElement | null
3939
const storeDom = document.getElementById(
4040
'ssrTextInitStoreData'

src/client/main/layout.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Index extends React.PureComponent<Istate, IProps> {
1111
const { children } = this.props
1212
return (
1313
<div className="layout-box">
14-
<h1>koa+react+ssr</h1>
14+
<h1>ssr-layout</h1>
1515
{children}
1616
</div>
1717
)

src/client/main/route-config.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react'
2-
export interface IRoute{
3-
path:string
2+
3+
export interface IRoute {
4+
path: string
45
component: Function
5-
exact:boolean
6+
exact: boolean
67
}
78

89
let routes: IRoute[] = []

src/client/modules/home/routes.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import AsyncLoader from '@/components/asyncLoader'
22

3-
43
export default [
54
{
65
path: '/',

src/server/middlewares/genHtml.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Store } from 'redux'
1111

1212
interface IGenResult {
1313
html: string
14-
fetchResult:any
14+
fetchResult: any
1515
}
1616
export default async function genHtml(
1717
path: string,
@@ -27,7 +27,7 @@ export default async function genHtml(
2727

2828
// 进行数据预取,更新 store 内的数据
2929
let fetchDataFn
30-
let fetchResult= {}
30+
let fetchResult = {}
3131
if (targetRoute) {
3232
fetchDataFn = targetRoute.component ? targetRoute.component.getInitialProps : null
3333
if (fetchDataFn) {
@@ -36,8 +36,8 @@ export default async function genHtml(
3636
}
3737
}
3838

39-
// 将预取数据在这里传递过去 组内通过props.staticContext获取 IntrinsicAttributes
40-
const context: any= {
39+
// 将预取数据在这里传递过去 组内通过props.staticContext获取 IntrinsicAttributes
40+
const context: any = {
4141
initialData: fetchResult
4242
}
4343
const html = renderToString(

src/server/middlewares/ssr.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Koa from 'koa'
99

1010
const ejsPath = path.join(__dirname, '../../templates/server.ejs')
1111
const store = getStore()
12-
export default async (ctx:Koa.Context, next: Koa.Next): Promise<null> => {
12+
export default async (ctx: Koa.Context, next: Koa.Next): Promise<null> => {
1313
const { path } = ctx.request
1414

1515
if (path.indexOf('.') > -1) {

tests/genHtml.spec.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import getStore from '../src/client/store/reducers'
44
describe('test genHtml Function', () => {
55
it('should return the correct result when call genHtml', async () => {
66
const store = getStore()
7-
const insertCss = () => {}
7+
const insertCss = () => {
8+
console.log('insertCss')
9+
}
810
const { html } = await genHtml('/404', insertCss, store)
9-
expect(html).toMatch(`<div class="layout-box"><h1>koa+react+ssr</h1><div><p>404页面page22</p><p>12</p></div></div>`)
11+
expect(html).toMatch(
12+
`<div class="layout-box"><h1>koa+react+ssr</h1><div><p>404页面page22</p><p>12</p></div></div>`
13+
)
1014
expect(html).toMatchSnapshot() // 保存快照
1115
})
1216
})

tests/setupTests.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// 拓展对dom节点的断言
22
import '@testing-library/jest-dom'
3-
import React from "react"
3+
import React from 'react'
44
// 处理这种情况 https://stackoverflow.com/questions/58070996/how-to-fix-the-warning-uselayouteffect-does-nothing-on-the-server
5-
React.useLayoutEffect = React.useEffect
5+
React.useLayoutEffect = React.useEffect

0 commit comments

Comments
 (0)