Skip to content

Commit 30f37e5

Browse files
committed
feat: update
1 parent eadf6cb commit 30f37e5

18 files changed

+215
-126
lines changed

.babelrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
"modules": false
3131
}
3232
]
33-
],
34-
"plugins": ["./build/babel/plugin/no-require-css"]
33+
]
34+
// "plugins": ["./build/babel/plugin/no-require-css"]
3535
}
3636
}
3737
}

README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ npm run dev
1818
页面的显示内容需要刷新一次才能看到。 后续继续优化,增加热更新。
1919

2020

21-
引用umi文档构建
2221

23-
做成一个完整的项目
22+
## 待做
23+
24+
- 修复构建的strat.js bug
25+
- server koa===>改造成ts
26+
- build server 改造成server
27+
- 支持 redux
28+
- server端引入ejs进行渲染返回 ===> 参考nuxt
29+
- css
30+
- 按需加载
31+
- 引用umi文档构建
32+
- 单元测试
33+
34+
做成一个完整的项目

build/client/dev-server.js

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const main = function() {
1919

2020
const webpackDevMiddleware = devMiddleware(compiler, {
2121
// logLevel: 'silent',
22+
index:true,
2223
publicPath: webpackConfig.output.publicPath
2324
})
2425

@@ -28,6 +29,17 @@ const main = function() {
2829
path: '/__hmr'
2930
})
3031

32+
//设置跨域访问
33+
app.all('*', function(req, res, next) {
34+
res.header("Access-Control-Allow-Origin", "*");
35+
res.header("Access-Control-Allow-Headers", "X-Requested-With");
36+
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
37+
res.header("X-Powered-By",' 3.2.1')
38+
res.header("Content-Type", "application/json;charset=utf-8");
39+
next();
40+
});
41+
42+
3143
app.use(webpackDevMiddleware)
3244

3345
app.use(webpackHotMiddleware)
@@ -36,6 +48,7 @@ const main = function() {
3648
res.sendStatus(200)
3749
})
3850

51+
3952
const staticPath = path.posix.join(config.dev.assetsPublicPath, config.base.assetsSubDirectory)
4053
app.use(staticPath, express.static('./static'))
4154

build/client/utils.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ exports.assetsPath = function(_path) {
1616

1717
exports.styleLoaders = function(options) {
1818
const baseLoaders = [
19-
options.extract ? MiniCssExtractPlugin.loader : 'style-loader',
19+
{
20+
loader:'isomorphic-style-loader'
21+
},
2022
{
2123
loader: 'css-loader',
2224
// options: {
@@ -38,12 +40,13 @@ exports.styleLoaders = function(options) {
3840
}
3941
]
4042
})
41-
4243
output.push({
4344
test: /\.less$/,
4445
include: /node_modules\/antd/,
4546
use: [
46-
options.extract ? MiniCssExtractPlugin.loader : 'style-loader',
47+
{
48+
loader:'isomorphic-style-loader'
49+
},
4750
{
4851
loader: 'css-loader'
4952
},

build/client/webpack.base.conf.js

-7
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,9 @@ module.exports = {
9393
inject: true
9494
})
9595
],
96-
9796
optimization: {
9897
splitChunks: {
9998
cacheGroups: {
100-
styles: {
101-
name: 'styles',
102-
test: /\.less$/,
103-
chunks: 'all',
104-
enforce: true,
105-
},
10699
libs: { // 抽离第三方库
107100
test: /node_modules/, // 指定是node_modules下的第三方包
108101
chunks: 'initial',

build/client/webpack.dev.conf.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,23 @@ module.exports = merge(baseWebpackConfig, {
2121
},
2222

2323
module: {
24-
rules: [...utils.styleLoaders({})]
24+
// rules: [...utils.styleLoaders({})]
25+
rules:[
26+
{
27+
test: /\.(le|c)ss$/,
28+
use: [
29+
'isomorphic-style-loader',
30+
{
31+
loader: 'css-loader',
32+
options: {
33+
importLoaders: 2
34+
}
35+
},
36+
'postcss-loader',
37+
'less-loader'
38+
]
39+
},
40+
]
2541
},
2642

2743
// devtool: '#cheap-module-eval-source-map',

build/server/webpack.server.config.js

+14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ module.exports = {
2424
externals: [nodeExternals()],
2525
module: {
2626
rules: [
27+
{
28+
test: /\.(le|c)ss$/,
29+
use: [
30+
'isomorphic-style-loader',
31+
{
32+
loader: 'css-loader',
33+
options: {
34+
importLoaders: 2
35+
}
36+
},
37+
'postcss-loader',
38+
'less-loader'
39+
]
40+
},
2741
{
2842
test: /\.jsx?$/,
2943
loader: 'babel-loader',

build/start.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ const startNodeServer = () => {
4646
)
4747
}
4848

49+
let flag=false
50+
4951
// 控制台输出信息
5052
function print(data) {
5153
let str = data.toString()
52-
if (str.indexOf(constantCode.SVRCODECOMPLETED) > -1) {
54+
if (str.indexOf(constantCode.SVRCODECOMPLETED) > -1&&!flag) {
55+
flag=true
5356
// 服务端代码编译完成
5457
startNodeServer() // 重启 node 服务
5558
} else {

config/base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var path = require('path')
33
module.exports = {
44
title: 'react-ssr',
55
projectName: 'ninth-studio-mobile',
6-
assetsPublicPath: '',
6+
assetsPublicPath: 'http://localhost:8080/', // 设计到热更新的host jsonp客户端的地址
77
assetsSubDirectory: 'static',
88
assetsRoot: path.resolve(__dirname, '../dist')
99
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
"get-port": "^5.1.1",
6868
"html-webpack-plugin": "^5.3.1",
6969
"husky": "^6.0.0",
70+
"isomorphic-style-loader": "^5.1.0",
71+
"koa2-proxy-middleware": "0.0.4",
7072
"less": "^4.1.1",
7173
"less-loader": "^8.1.1",
7274
"lint-staged": "^10.5.4",

src/client/main/index.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@ import routeList from './route-config'
1111
import matchRoute from '../../common/match-route'
1212
// @ts-ignore
1313
import proConfig from '../../common/pro-config'
14+
// @ts-ignore
15+
import StyleContext from 'isomorphic-style-loader/StyleContext'
1416

1517
function renderDom(routeList: any) {
18+
// @ts-ignore
19+
const insertCss = (...styles) => {
20+
const removeCss = styles.map(style => style._insertCss());//客户端执行,插入style
21+
return () => removeCss.forEach(dispose => dispose());//组件卸载时 移除当前的 style 标签
22+
}
1623
console.log('渲染index')
1724
// 渲染index
1825
// @ts-ignore
1926
const renderMethod = module.hot ? ReactDOM.render : ReactDOM.hydrate;
2027
console.log('routeList===>',routeList)
2128
renderMethod(
2229
<BrowserRouter>
23-
<App routeList={routeList} />
24-
</BrowserRouter>,
30+
<StyleContext.Provider value={{ insertCss }}>
31+
<App routeList={routeList} /></StyleContext.Provider>
32+
</BrowserRouter>,
2533
document.getElementById('app'),
2634
)
2735
}

src/client/main/layout.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Link, NavLink } from 'react-router-dom';
33
import { withRouter } from 'react-router';
44
import { hot } from 'react-hot-loader/root';
55

6-
import './layout.less';
6+
// import './layout.less';
77

88
class Index extends React.Component {
99
constructor(props: any) {

src/client/modules/home/index.less

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.home{
2+
background-color: aquamarine;
3+
}

src/client/modules/home/index.tsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import React, { FC,useState } from 'react'
22
import {Link} from 'react-router-dom'
3+
import css from './index.less'
4+
// @ts-ignore
5+
// import withStyles from 'isomorphic-style-loader/withStyles'
6+
import useStyles from 'isomorphic-style-loader/useStyles'
7+
8+
console.log('css',css)
39
export const Home: FC<any> = (props: any) => {
410
const [nums,setNums] = useState<number[]>([1,2,3,4,5,6])
11+
useStyles(css)
512
const handleClick = () => {
613
window.alert('handleClick')
714
}
815

916
return (
10-
<div>
17+
<div className="home">
1118
<h1>hello react-ssr</h1>
12-
<button onClick={handleClick}>click me</button>
19+
<button onClick={handleClick}>click me1</button>
1320
{
14-
[1,2,3,4,5,6,7,8,9,10].map((item:number)=><div key={item}>
21+
[11,2,3,4,5,6,17,8,1231].map((item:number)=><div key={item}>
1522
{item}
1623
</div>)
1724
}
18-
<Link to={'/history'}>click to 404 page</Link>
25+
<Link to={'/history'}>clqick to2 404 page1</Link>
1926
</div>
2027
)
2128
}

src/client/modules/home/routes.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ function pageNotFound({staticContext}:any) {
1313
export default [
1414
{
1515
path: '/',
16-
component: () => <Home />
16+
component: () => <Home />,
17+
exact:true
1718
},
1819
{
1920
path: '*',
2021
component: pageNotFound,
21-
exact: true
22+
// exact: true
2223
}
2324
]

src/server/app/index.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,23 @@ const port = proConfig.nodeServerPort || process.env.PORT;
77

88
const app = new Koa();
99

10-
10+
/* 代理配置 start */
11+
const proxy = require('koa2-proxy-middleware'); //引入代理模块
12+
const options = {
13+
targets: {
14+
// (.*) means anything
15+
'/__hmr': {
16+
target: 'http://localhost:8080',
17+
changeOrigin: true,
18+
},
19+
}
20+
}
21+
app.use(
22+
proxy(options)
23+
)
1124
// 设置可访问的静态资源
1225
// TODO 生产时打开
13-
// app.use(koaStatic('./dist/static'));
26+
app.use(koaStatic('./dist/static'));
1427

1528

1629
//ssr 中间件

0 commit comments

Comments
 (0)