Skip to content
This repository was archived by the owner on May 7, 2022. It is now read-only.

Commit e8e9c65

Browse files
committed
封装代码切割函数
1 parent 6e9c494 commit e8e9c65

File tree

8 files changed

+69
-114
lines changed

8 files changed

+69
-114
lines changed

README.md

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,28 @@
1111

1212
欢迎 watch、star、fork,因为我自己也是基于这套框架做开发,所以我会长期维护该项目,跟随相关插件的升级而升级优化。
1313

14-
2017.6.10 更新
14+
2017.6.17 更新
1515

16-
1、测试Windows7上chrome热更新正常;
16+
1、使用webpack的import()实现代码切割,不只是在路由中使用,你可以在任意组件内部使用代码切割方法懒加载组件,。
1717

18-
2、增加状态提升的实现,代码在搜索页面;
18+
在路由route中,你可以这样
19+
```jsx harmony
20+
//封装好的异步方法,非原创,使用了一个大神写的函数。
21+
import { asyncComponent } from './AsyncComponent'
1922

20-
3、监听浏览器返回和前进按钮事件执行动画。
23+
//使用asyncComponent(),你就能将Promise的返回值赋给一个变量
24+
const Search = asyncComponent(() => import(/* webpackChunkName: "search" */ "./containers/Search/searchContainer"))
2125

22-
2017.6.7 更新
23-
24-
1、新增了global(action)来控制路由切换时调用的动画,我实现了左移动、右移动2种特效,你可以点击下面的demo查看效果。
25-
26-
2017.6.6 更新
27-
28-
1、热更新改成react-hot-loader3;
29-
30-
2、前端改成webpack-dev-server启动;
31-
32-
3、mock移到easy-mock。
26+
<Route path="/xx" component={Search} />
27+
```
3328

34-
4、如果你不喜欢这种启动和热更新方式,请切换tag版本(不会切换tag吗?)。
29+
请注意import()方法是异步的,你不能这样使用
3530

36-
5、更新了webpack插件和其他几个插件,你打包之后会发现js体积变小了。
31+
```javascript
32+
const Foo = import("./xx") // 错误的写法
3733

38-
6、新版本推送的比较仓促,有bug可以给我反馈。
34+
<Route path="/xx" component={import("./xxx")} /> //错误的写法
35+
```
3936

4037
==========================================
4138

@@ -83,36 +80,6 @@ npm run build-win
8380

8481
===========================================
8582

86-
压缩效果图
87-
![image](https://github.com/hyy1115/react-redux-webpack2/blob/master/public/fenxi.png)
88-
89-
===================================================
90-
91-
#### 关于服务端渲染的建议(内容来自react-router官方文档)
92-
93-
查看原文:https://reacttraining.cn/web/guides/code-splitting
83+
压缩效果图(非最新版效果)
9484

95-
Code-splitting + server rendering
96-
97-
代码分割 + 服务端渲染
98-
99-
We’ve tried and failed a couple of times. What we learned:
100-
101-
我们尝试了很多次都失败了,我们总结了下面几点:
102-
103-
1、You need synchronous module resolution on the server so you can get those bundles in the initial render.
104-
105-
你需要在服务端同步模块解析使得你可以在初始化渲染的时候得到那些文件。
106-
107-
2、You need to load all the bundles in the client that were involved in the server render before rendering so that the client render is the same as the server render. (The trickiest part, I think its possible but this is where I gave up.)
108-
为了保证服务端渲染和客户端渲染的同步,你需要在客户端渲染前加载和服务端渲染一致的所有文件。(这也是我认为最难搞的地方,所以我放弃了)
109-
110-
3、You need asynchronous resolution for the rest of the client app’s life.
111-
在客户端运行过程中,你需要异步解析其他没有在初始化渲染的部分。
112-
113-
We determined that google was indexing our sites well enough for our needs without server rendering, so we dropped it in favor of code-splitting + service worker caching. Godspeed those who attempt the server-rendered, code-split apps.
114-
我们确定谷歌大神对我们网站的索引做得足够好,并不需要服务端渲染来解决SEO的问题,所以我们放弃了这种模式,而是采用更有利的代码分割 + service worker 缓存5的方式。愿上帝保佑那些想要尝试服务端渲染+代码分割的小白鼠:
115-
116-
==================================================
117-
118-
####Finally, JavaScript is the world's best language, if demand, you can directly send me an email
85+
![image](https://github.com/hyy1115/react-redux-webpack2/blob/master/public/fenxi.png)

doc/react.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ app.css:基本的css配置,你也可以把reset.css或者其他初始化样
6161
6262
App.js:我们叫他根组件,在SPA应用中,通常只有一个根组件。
6363
64-
bundle.js:react-router4中使用的懒加载代码,目前我已经注释掉,有需求的可以自己尝试使用。
64+
AsyncComponent.js:react-router4中使用的懒加载代码,目前我已经注释掉,有需求的可以自己尝试使用。
6565
6666
index.js:webpack中entry使用的入口js文件,包括store的管理,根组件的渲染都在该文件中。
6767

src/App.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import React from 'react'
22
import { connect } from 'react-redux'
33
import { bindActionCreators } from 'redux';
4-
import './app.css'
54
import { Route, HashRouter as Router } from 'react-router-dom'
65
import CSSTransitionGroup from 'react-transition-group/CSSTransitionGroup'
76
import createHistory from 'history/createHashHistory'
87
const history = createHistory()
98

9+
/*
10+
全局导入less
11+
如果你发现你的样式没有起作用,那么很可能是没有在这里导入样式
12+
*/
13+
import './app.less'
14+
import './containers/Home/styles/home.less'
15+
import './containers/Search/styles/search.less'
16+
1017
import * as globalActions from 'actions/global'
18+
import { asyncComponent } from './AsyncComponent'
19+
20+
import homeContainer from './containers/Home/homeContainer'
1121

12-
/* application components */
13-
import HomeContainer from './containers/Home/homeContainer';
14-
import SearchContainer from './containers/Search/searchContainer';
15-
import BookListContainer from './containers/BookList/bookListContainer';
22+
const Search = asyncComponent(() => import(/* webpackChunkName: "search" */ "./containers/Search/searchContainer"))
23+
const BookList = asyncComponent(() => import(/* webpackChunkName: "bookList" */ "./containers/BookList/bookListContainer"))
1624

1725
@connect (
1826
state => state,
@@ -41,9 +49,9 @@ export default class App extends React.Component {
4149
transitionLeaveTimeout={400}
4250
>
4351
<div key={location.pathname}>
44-
<Route location={location} exact path="/" component={HomeContainer} />
45-
<Route location={location} path="/search" component={SearchContainer} />
46-
<Route location={location} path="/bookList/:bookId" component={BookListContainer} />
52+
<Route location={location} exact path="/" component={homeContainer} />
53+
<Route location={location} path="/search" component={Search} />
54+
<Route location={location} path="/bookList/:bookId" component={BookList} />
4755
</div>
4856
</CSSTransitionGroup>
4957
)

src/AsyncComponent.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from 'react'
2+
export const asyncComponent = loadComponent => (
3+
class AsyncComponent extends React.Component {
4+
state = {
5+
Component: null,
6+
}
7+
8+
componentWillMount() {
9+
if (this.hasLoadedComponent()) {
10+
return;
11+
}
12+
13+
loadComponent()
14+
.then(module => module.default)
15+
.then((Component) => {
16+
this.setState({ Component });
17+
})
18+
.catch((err) => {
19+
console.error(`Cannot load component in <AsyncComponent />`);
20+
throw err;
21+
});
22+
}
23+
24+
hasLoadedComponent() {
25+
return this.state.Component !== null;
26+
}
27+
28+
render() {
29+
const { Component } = this.state;
30+
return (Component) ? <Component {...this.props} /> : null;
31+
}
32+
}
33+
);
File renamed without changes.

src/bundle.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/containers/Home/homeContainer.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import { BookList } from 'components/Home/bookList'
2222
/*files*/
2323
const search = require('./files/search.svg');
2424

25-
require(`./styles/home.less`)
26-
2725
/**
2826
* connect中间件
2927
* connect一定要写在需要传递参数的组件头部,因为这是语法规则,只对当前关联的组件生效,和java的原理是一致的

src/containers/Search/searchContainer.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
/**
22
* Created by Administrator on 2016/7/2.
33
*/
4-
import React, { Component } from 'react';
4+
import React from 'react';
55
import { bindActionCreators } from 'redux';
66
import { connect } from 'react-redux';
77
import PropTypes from 'prop-types';
88

9-
import './styles/search.less'
10-
119
/*actions*/
1210
import * as searchActions from 'actions/search';
1311
import * as globalActions from 'actions/global';
@@ -31,6 +29,7 @@ export default class SearchContainer extends React.Component {
3129
}
3230

3331
componentWillMount() {
32+
console.log('进入搜索页面')
3433
this.props.receiveHotSearch()
3534
}
3635

0 commit comments

Comments
 (0)