Skip to content

Commit 97cef8a

Browse files
committed
feat: update ts
1 parent 0e83cbc commit 97cef8a

File tree

22 files changed

+197
-207
lines changed

22 files changed

+197
-207
lines changed

.babelrc

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"presets": ["@babel/env", "@babel/typescript", "@babel/react"],
3-
"plugins": ["@babel/plugin-transform-runtime"],
3+
"plugins": [
4+
"@babel/plugin-transform-runtime",
5+
"@babel/plugin-proposal-class-properties"
6+
],
47
"env": {
58
"esm": {
69
"presets": [
@@ -31,7 +34,6 @@
3134
}
3235
]
3336
]
34-
// "plugins": ["./build/babel/plugin/no-require-css"]
3537
}
3638
}
3739
}

.eslintrc.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,28 @@
125125
"no-irregular-whitespace": "off",
126126
"no-multiple-empty-lines": "off",
127127
"no-new-wrappers": "error",
128+
"no-lonely-if": "off",
128129
"no-shadow": [
129130
"off",
130131
{
131132
"hoist": "all"
132133
}
133134
],
135+
"no-return-await": "off",
134136
"no-throw-literal": "error",
135137
"no-trailing-spaces": "off",
136138
"no-undef-init": "error",
139+
"class-methods-use-this": "off",
137140
"no-underscore-dangle": [
138141
"error",
139142
{
140-
"allow": ["__INITIAL_DATA__", "__STORE__", "_insertCss"],
143+
"allow": [
144+
"__INITIAL_DATA__",
145+
"__STORE__",
146+
"_insertCss",
147+
"__SERVER__",
148+
"__IS_PROD__"
149+
],
141150
"allowAfterThis": true,
142151
"allowAfterThisConstructor": true
143152
}
@@ -169,6 +178,7 @@
169178
"enforceDynamicLinks": "always"
170179
}
171180
],
181+
"react/button-has-type": "off",
172182
"no-restricted-syntax": "off",
173183
"import/extensions": "off",
174184
"import/no-unresolved": "off",

build/start.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
// 整个编译服务的启动入口
2-
// 包括以下功能----
3-
// 服务端代码的首次编译
4-
// 文件的监听
5-
// 前端代码的打包和 watch
6-
// node服务的启动
7-
8-
// 服务度代码编译完成后 启动node 服务
9-
// 服务代码每次编译后,重启node 服务
10-
111
const { spawn } = require('child_process') // 用于创建子进程
122
const constantCode = require('../config/constant')
133
const chalk = require('chalk') // 为控制台输出的信息增加点色彩
144
const log = console.log
15-
const proConfig = require('../src/common/pro-config')
16-
const getIp = require('../src/server/utils/get-ip')
5+
const proConfig = require('../config/pro-config')
6+
const os = require('os')
177

8+
function getIp() {
9+
const interfaces = os.networkInterfaces()
10+
for (const name of Object.keys(interfaces)) {
11+
for (const interface of interfaces[name]) {
12+
const { address, family, internal } = interface
13+
if (family === 'IPv4' && !internal) {
14+
return address
15+
}
16+
}
17+
}
18+
}
1819
// node server port
1920
const nodeServerPort = proConfig.nodeServerPort
2021

@@ -32,6 +33,7 @@ const feCodeWatchProcess = spawn('npm', ['run', 'client:dev', localHostIp], {
3233
const svrCodeWatchProcess = spawn('npm', ['run', 'server-dev:build'], {
3334
shell: process.platform === 'win32'
3435
})
36+
3537
// node 服务进程
3638
let nodeServerProcess = null
3739
// 启动 node 服务

src/common/pro-config.ts config/pro-config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
__IS_SSR__: true, // 是否为 ssr 模式
33
wdsPort: 9002, // wds 服务的运行端口
44
nodeServerPort: 9001, // node server 的监听端口

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
},
4747
"devDependencies": {
4848
"@babel/core": "^7.13.16",
49+
"@babel/plugin-proposal-class-properties": "^7.13.0",
4950
"@babel/plugin-proposal-decorators": "^7.13.15",
5051
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
5152
"@babel/plugin-transform-react-jsx": "^7.13.12",
@@ -57,6 +58,8 @@
5758
"@commitlint/config-conventional": "^12.1.1",
5859
"@hot-loader/react-dom": "^17.0.1",
5960
"@types/classnames": "^2.3.1",
61+
"@types/koa": "^2.13.1",
62+
"@types/koa-static": "^4.0.1",
6063
"@types/node": "^15.0.1",
6164
"@types/react-dom": "^17.0.3",
6265
"@types/react-router-dom": "^5.1.7",
+18-26
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,43 @@
1-
2-
import React from 'react';
1+
import React from 'react'
32
import LoadingCompoent from '../loading'
43

5-
64
/**
75
* 动态加载组件一个组的容器
86
*
97
* @class Bundle
108
* @extends {Component}
119
*/
12-
export default class AsyncBundle extends React.Component<any, any> {
13-
constructor(props: any) {
14-
super(props);
15-
this.state = {
16-
mod: null
17-
};
10+
11+
interface IPops {
12+
children: any
13+
}
14+
export default class AsyncBundle extends React.PureComponent<IPops, any> {
15+
state = {
16+
mod: null
1817
}
1918

2019
componentDidMount() {
21-
if (!this.state.mod) {
22-
this.load(this.props);
20+
const { mod } = this.state
21+
if (mod) {
22+
this.load(this.props)
2323
}
2424
}
2525

26-
// UNSAFE_componentWillReceiveProps(nextProps) {
27-
28-
// //路由改变才会按需
29-
// if (nextProps.match && this.props.match && (nextProps.match.url !== this.props.match.url)) {
30-
// this.load(nextProps);
31-
// }
32-
// }
33-
34-
3526
load(props: any) {
3627
this.setState({
3728
mod: null
38-
});
39-
//注意这里,使用Promise对象; mod.default导出默认
29+
})
30+
// 注意这里,使用Promise对象; mod.default导出默认
4031
props.load().then((mod: any) => {
4132
this.setState({
4233
mod: mod.default ? mod.default : mod
43-
});
44-
});
34+
})
35+
})
4536
}
4637

4738
render() {
48-
// @ts-ignore
49-
return this.state.mod ? this.props.children(this.state.mod) : <LoadingCompoent />;
39+
const { mod } = this.state
40+
const { children } = this.props
41+
return mod ? children(mod) : <LoadingCompoent />
5042
}
5143
}

src/client/components/asyncLoader/index.tsx

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
//异步加载组件的高阶函数
2-
3-
import AsyncBundle from '../asyncBundle'
4-
// @ts-ignore
5-
import proConfig from '../../../common/pro-config.js'
61
import React from 'react'
2+
import AsyncBundle from '../asyncBundle'
3+
import proConfig from '../../../../config/pro-config'
4+
75
function AsyncLoader(loader: any) {
86
function asyncFn(props: any) {
97
return <AsyncBundle load={loader}>{(Comp: any) => <Comp {...props} />}</AsyncBundle>
108
}
11-
12-
//标记为异步组件
13-
// @ts-ignore
9+
// 标记为异步组件
1410
asyncFn[proConfig.asyncComponentKey] = true
1511

1612
return asyncFn
+39-43
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,82 @@
1-
//高阶组件 用于提取重复逻辑
21
import React from 'react'
32

4-
let _this = null
3+
let self: any = null
54

65
const popStateCallback = () => {
76
// 使用popStateFn保存函数防止addEventListener重复注册
8-
if (_this && _this.getInitialProps) {
7+
if (self && self.getInitialProps) {
98
console.log('popStateFn')
10-
_this.getInitialProps()
9+
self.getInitialProps()
1110
}
1211
}
1312

14-
export default SourceComponent => {
15-
return class HoComponent extends React.Component {
16-
constructor(props, context) {
17-
super(props)
18-
console.log('props', props)
19-
this.state = {
20-
initialData: {},
21-
canClientFetch: false //浏览器端是否需要请求数据
22-
}
13+
export default (SourceComponent: any) =>
14+
class HoComponent extends React.PureComponent<any, any> {
15+
state = {
16+
initialData: {},
17+
canClientFetch: false // 浏览器端是否需要请求数据
2318
}
2419

25-
//转接子组件的预取方法,服务端会调用这个方法来做数据预取
26-
static async getInitialProps(ctx) {
20+
// 转接子组件的预取方法,服务端会调用这个方法来做数据预取
21+
static async getInitialProps(ctx: any) {
2722
return SourceComponent.getInitialProps
2823
? await SourceComponent.getInitialProps(ctx)
2924
: {}
3025
}
26+
27+
async componentDidMount() {
28+
const { history } = this.props
29+
self = this // 修正self指向,保证self指向当前渲染的页面组件
30+
// 注册事件,用于在页面回退的时候触发
31+
window.addEventListener('popstate', popStateCallback)
32+
33+
const canClientFetch = history && history.action === 'PUSH' // 路由跳转的时候可以异步请求数据
34+
if (canClientFetch) {
35+
// 如果是 history PUSH 操作 则更新数据
36+
await this.getInitialProps()
37+
}
38+
}
39+
3140
// ! csr/ssr切换路由时才调用组件的getInitialProps方法
32-
//用于封装处理数据的更新逻辑
41+
// 用于封装处理数据的更新逻辑
3342
async getInitialProps() {
3443
// ssr首次进入页面以及csr/ssr切换路由时才调用组件的getInitialProps方法
35-
const props = this.props
36-
const store = window.__STORE__ //从全局得到 store
37-
44+
const store = window.__STORE__ // 从全局得到 store
3845
// 兼容不使用 redux 的页面
3946
// 通过props.getInitialData判断
4047
// ! 那就说明约定getInitialData
4148
const res = SourceComponent.getInitialProps
4249
? await SourceComponent.getInitialProps({ store })
4350
: {}
44-
//处理页面 title 显示
45-
let { tdk } = res.page || {}
51+
// 处理页面 title 显示
52+
const { tdk } = res.page || {}
4653
if (tdk) {
4754
document.title = tdk.title
4855
}
4956
}
5057

51-
async componentDidMount() {
52-
_this = this // 修正_this指向,保证_this指向当前渲染的页面组件
53-
//注册事件,用于在页面回退的时候触发
54-
window.addEventListener('popstate', popStateCallback)
55-
56-
const canClientFetch = this.props.history && this.props.history.action === 'PUSH' //路由跳转的时候可以异步请求数据
57-
if (canClientFetch) {
58-
//如果是 history PUSH 操作 则更新数据
59-
await this.getInitialProps()
60-
}
61-
}
62-
6358
render() {
6459
const props = {
6560
initialData: {},
6661
...this.props
6762
}
63+
const { staticContext } = this.props
64+
const { canClientFetch, initialData } = this.state
6865

69-
if (__SERVER__) {
70-
//服务端渲染
71-
props.initialData = this.props.staticContext.initialData || {}
66+
if (window.__SERVER__) {
67+
// 服务端渲染
68+
props.initialData = staticContext.initialData || {}
7269
} else {
73-
//客户端渲染
74-
if (this.state.canClientFetch) {
75-
//需要异步请求数据
76-
props.initialData = this.state.initialData || {}
70+
// 客户端渲染
71+
if (canClientFetch) {
72+
// 需要异步请求数据
73+
props.initialData = initialData || {}
7774
} else {
7875
props.initialData = window.__INITIAL_DATA__
79-
window.__INITIAL_DATA__ = null //使用过后清除数据,否则其他页面会使用
76+
window.__INITIAL_DATA__ = null // 使用过后清除数据,否则其他页面会使用
8077
}
8178
}
8279

83-
return <SourceComponent {...props}></SourceComponent>
80+
return <SourceComponent {...props} />
8481
}
8582
}
86-
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React from 'react';
1+
import React from 'react'
22

33
export default function LoadingComponent(props: any) {
44
return <div>loading......</div>
5-
}
5+
}

src/client/main/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import getStore from '@/store/reducers'
77
import App from './router'
88
import routeList from './route-config'
99
import matchRoute from '../../common/match-route'
10-
import proConfig from '../../common/pro-config'
10+
import proConfig from '../../../config/pro-config'
1111
import { Store } from 'redux'
1212

1313
function renderDom(routeList: any[], initStoreState?: Store) {

0 commit comments

Comments
 (0)