Skip to content

Commit 9f7d7bd

Browse files
committed
项目重构
1 parent 41af10b commit 9f7d7bd

File tree

183 files changed

+4185
-4409
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

183 files changed

+4185
-4409
lines changed

.babelrc

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
{
2-
"presets": [
3-
"react",
4-
"es2015",
5-
"stage-0",
6-
],
7-
"plugins": [
8-
"transform-decorators-legacy",
9-
"transform-class-properties"
10-
]
11-
}
2+
"presets": ["react"],
3+
"plugins": ["syntax-dynamic-import"]
4+
}

.gitignore

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
node_modules
2-
npm-debug.log
3-
data
1+
# See https://help.github.com/ignore-files/ for more about ignoring files.
42

3+
# dependencies
4+
/node_modules
5+
6+
# testing
7+
/coverage
8+
9+
# production
10+
/build
11+
12+
# misc
13+
.DS_Store
14+
.env.local
15+
.env.development.local
16+
.env.test.local
17+
.env.production.local
18+
*.idea
19+
*.iml
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
24+
package-lock.json
25+
yarn.lock

README.md

100755100644
Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11

22
## 技术栈:
3+
```
4+
react: v16.2
5+
redux: v3.7
6+
webpack: v3.8
7+
react-router: v4.2
8+
ES6/7/8
9+
code split
10+
hot loader
11+
less: v2.7
12+
axios: v0.17
13+
immutable: v3.8
314
4-
react + react-router + redux + immutable + less + ES6/7 + webpack + fetch
5-
15+
```
616

717
## 运行项目(nodejs 6.0+)
818

@@ -11,27 +21,25 @@ react + react-router + redux + immutable + less + ES6/7 + webpack + fetch
1121
1222
cd react-pxq
1323
14-
npm install
24+
npm i
1525
16-
npm run dev (正常编译模式) 或 npm run hot (热替换编译模式) 二选其一
17-
18-
访问 http://localhost:8088
26+
npm start
1927
20-
npm run dist (发布)
28+
npm run build (发布)
2129
```
2230

2331

2432
## 说明
2533

26-
> 本项目主要理解 react 和 redux 的原理,以及 react + redux 之间的配合方式
34+
> 本项目主要用于理解 react 和 redux 的编译方式,以及 react + redux 之间的配合方式
2735
2836
> 如果觉得不错的话,您可以点右上角 "Star" 支持一下 谢谢! ^_^
2937
3038
> 或者您可以 "follow" 一下,我会不断开源更多的有趣的项目
3139
3240
> 如有问题请直接在 Issues 中提,或者您发现问题并有非常好的解决方案,欢迎 PR 👍
3341
34-
> 开发环境 macOS 10.12.3 Chrome 56 nodejs 6.10.0
42+
> 开发环境 macOS 10.13.1 Chrome 63 nodejs 8.9.1
3543
3644
>  推荐一个 vue2 + vuex 构建的 45 个页面的大型开源项目。[地址在这里](https://github.com/bailicangdu/vue2-elm)
3745
@@ -43,7 +51,7 @@ react + react-router + redux + immutable + less + ES6/7 + webpack + fetch
4351
[查看演示效果](http://cangdu.org/pxq/)(请用chrome的手机模式预览)
4452

4553
### 移动端扫描下方二维码
46-
![](https://github.com/bailicangdu/pxq/blob/master/src/images/demo.png)
54+
![](https://github.com/bailicangdu/pxq/blob/master/screenshot/demo.png)
4755

4856

4957

@@ -68,7 +76,7 @@ react的diff算法用在什么地方呢?当组件更新的时候,react会创
6876

6977
对于列表的diff算法稍有不同,因为列表通常具有相同的结构,在对列表节点进行删除,插入,排序的时候,单个节点的整体操作远比一个个对比一个个替换要好得多,所以在创建列表的时候需要设置key值,这样react才能分清谁是谁。当然不写key值也可以,但这样通常会报出警告,通知我们加上key值以提高react的性能。
7078

71-
![](https://github.com/bailicangdu/pxq/blob/master/src/images/diff.png)
79+
![](https://github.com/bailicangdu/pxq/blob/master/screenshot/diff.png)
7280

7381

7482

@@ -77,7 +85,7 @@ react的diff算法用在什么地方呢?当组件更新的时候,react会创
7785

7886
组件的创造方法为React.createClass() ——创造一个类,react系统内部设计了一套类系统,利用它来创造react组件。但这并不是必须的,我们还可以用es6的class类来创造组件,这也是Facebook官方推荐的写法。
7987

80-
![](https://github.com/bailicangdu/pxq/blob/master/src/images/icon_class.png)
88+
![](https://github.com/bailicangdu/pxq/blob/master/screenshot/icon_class.png)
8189

8290
这两种写法实现的功能一样但是原理却是不同,es6的class类可以看作是构造函数的一个语法糖,可以把它当成构造函数来看,extends实现了类之间的继承 —— 定义一个类Main 继承React.Component所有的属性和方法,组件的生命周期函数就是从这来的。constructor是构造器,在实例化对象时调用,super调用了父类的constructor创造了父类的实例对象this,然后用子类的构造函数进行修改。这和es5的原型继承是不同的,原型继承是先创造一个实例化对象this,然后再继承父级的原型方法。了解了这些之后我们在看组件的时候就清楚很多。
8391

@@ -90,7 +98,7 @@ react的diff算法用在什么地方呢?当组件更新的时候,react会创
9098

9199
## 组件的生命周期
92100

93-
![](https://github.com/bailicangdu/pxq/blob/master/src/images/react-lifecycle.png)
101+
![](https://github.com/bailicangdu/pxq/blob/master/screenshot/react-lifecycle.png)
94102

95103
**组件在初始化时会触发5个钩子函数:**
96104

@@ -207,7 +215,7 @@ react推崇的是单向数据流,自上而下进行数据的传递,但是由
207215
#### 流程是这个样子的:
208216

209217

210-
![](https://github.com/bailicangdu/pxq/blob/master/src/images/simple_redux.jpg)
218+
![](https://github.com/bailicangdu/pxq/blob/master/screenshot/simple_redux.jpg)
211219

212220
值得注意的是connect,Provider,mapStateToProps,mapDispatchToProps是react-redux提供的,redux本身和react没有半毛钱关系,它只是数据处理中心,没有和react产生任何耦合,是react-redux让它们联系在一起。
213221

@@ -217,7 +225,7 @@ react推崇的是单向数据流,自上而下进行数据的传递,但是由
217225

218226
#### 先上一张图
219227

220-
![](https://github.com/bailicangdu/pxq/blob/master/src/images/all_redux.png)
228+
![](https://github.com/bailicangdu/pxq/blob/master/screenshot/all_redux.png)
221229

222230
明显比第一张要复杂,其实两张图说的是同一件事。从上而下慢慢分析:
223231

@@ -369,7 +377,7 @@ import { Router, Route, Redirect, IndexRoute, browserHistory, hashHistory } from
369377

370378
通常我们在顶层的ui组件打印props时可以看到一堆属性:
371379

372-
![](https://github.com/bailicangdu/pxq/blob/master/src/images/react_props.png)
380+
![](https://github.com/bailicangdu/pxq/blob/master/screenshot/react_props.png)
373381

374382
上图的顶层ui组件属性总共有18个,如果刚刚接触react,可能对这些属性怎么来的感到困惑,其实这些属性来自五个地方:
375383

bin/react-scripts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Copyright (c) 2015-present, Facebook, Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
const spawn = require('react-dev-utils/crossSpawn');
12+
const args = process.argv.slice(2);
13+
14+
const scriptIndex = args.findIndex(
15+
x => x === 'build' || x === 'eject' || x === 'start' || x === 'test'
16+
);
17+
const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
18+
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
19+
20+
switch (script) {
21+
case 'build':
22+
case 'eject':
23+
case 'start':
24+
case 'test': {
25+
const result = spawn.sync(
26+
'node',
27+
nodeArgs
28+
.concat(require.resolve('../scripts/' + script))
29+
.concat(args.slice(scriptIndex + 1)),
30+
{ stdio: 'inherit' }
31+
);
32+
if (result.signal) {
33+
if (result.signal === 'SIGKILL') {
34+
console.log(
35+
'The build failed because the process exited too early. ' +
36+
'This probably means the system ran out of memory or someone called ' +
37+
'`kill -9` on the process.'
38+
);
39+
} else if (result.signal === 'SIGTERM') {
40+
console.log(
41+
'The build failed because the process exited too early. ' +
42+
'Someone might have called `kill` or `killall`, or the system could ' +
43+
'be shutting down.'
44+
);
45+
}
46+
process.exit(1);
47+
}
48+
process.exit(result.status);
49+
break;
50+
}
51+
default:
52+
console.log('Unknown script "' + script + '".');
53+
console.log('Perhaps you need to update react-scripts?');
54+
console.log(
55+
'See: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#updating-to-new-releases'
56+
);
57+
break;
58+
}

config/env.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// @remove-on-eject-begin
2+
/**
3+
* Copyright (c) 2015-present, Facebook, Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
// @remove-on-eject-end
9+
'use strict';
10+
11+
const fs = require('fs');
12+
const path = require('path');
13+
const paths = require('./paths');
14+
15+
// Make sure that including paths.js after env.js will read .env variables.
16+
delete require.cache[require.resolve('./paths')];
17+
18+
const NODE_ENV = process.env.NODE_ENV;
19+
if (!NODE_ENV) {
20+
throw new Error(
21+
'The NODE_ENV environment variable is required but was not specified.'
22+
);
23+
}
24+
25+
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
26+
var dotenvFiles = [
27+
`${paths.dotenv}.${NODE_ENV}.local`,
28+
`${paths.dotenv}.${NODE_ENV}`,
29+
// Don't include `.env.local` for `test` environment
30+
// since normally you expect tests to produce the same
31+
// results for everyone
32+
NODE_ENV !== 'test' && `${paths.dotenv}.local`,
33+
paths.dotenv,
34+
].filter(Boolean);
35+
36+
// Load environment variables from .env* files. Suppress warnings using silent
37+
// if this file is missing. dotenv will never modify any environment variables
38+
// that have already been set.
39+
// https://github.com/motdotla/dotenv
40+
dotenvFiles.forEach(dotenvFile => {
41+
if (fs.existsSync(dotenvFile)) {
42+
require('dotenv').config({
43+
path: dotenvFile,
44+
});
45+
}
46+
});
47+
48+
// We support resolving modules according to `NODE_PATH`.
49+
// This lets you use absolute paths in imports inside large monorepos:
50+
// https://github.com/facebookincubator/create-react-app/issues/253.
51+
// It works similar to `NODE_PATH` in Node itself:
52+
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
53+
// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
54+
// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
55+
// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421
56+
// We also resolve them to make sure all tools using them work consistently.
57+
const appDirectory = fs.realpathSync(process.cwd());
58+
process.env.NODE_PATH = (process.env.NODE_PATH || '')
59+
.split(path.delimiter)
60+
.filter(folder => folder && !path.isAbsolute(folder))
61+
.map(folder => path.resolve(appDirectory, folder))
62+
.join(path.delimiter);
63+
64+
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
65+
// injected into the application via DefinePlugin in Webpack configuration.
66+
const REACT_APP = /^REACT_APP_/i;
67+
68+
function getClientEnvironment(publicUrl) {
69+
const raw = Object.keys(process.env)
70+
.filter(key => REACT_APP.test(key))
71+
.reduce(
72+
(env, key) => {
73+
env[key] = process.env[key];
74+
return env;
75+
},
76+
{
77+
// Useful for determining whether we’re running in production mode.
78+
// Most importantly, it switches React into the correct mode.
79+
NODE_ENV: process.env.NODE_ENV || 'development',
80+
// Useful for resolving the correct path to static assets in `public`.
81+
// For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
82+
// This should only be used as an escape hatch. Normally you would put
83+
// images into the `src` and `import` them in code to get their paths.
84+
PUBLIC_URL: publicUrl,
85+
STATIC_ENV: process.env.STATIC_ENV || 'development',
86+
}
87+
);
88+
// Stringify all values so we can feed into Webpack DefinePlugin
89+
const stringified = {
90+
'process.env': Object.keys(raw).reduce((env, key) => {
91+
env[key] = JSON.stringify(raw[key]);
92+
return env;
93+
}, {}),
94+
};
95+
96+
return { raw, stringified };
97+
}
98+
99+
module.exports = getClientEnvironment;

config/jest/babelTransform.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @remove-file-on-eject
2+
/**
3+
* Copyright (c) 2014-present, Facebook, Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
'use strict';
9+
10+
const babelJest = require('babel-jest');
11+
12+
module.exports = babelJest.createTransformer({
13+
presets: [require.resolve('babel-preset-react-app')],
14+
babelrc: false,
15+
});

config/jest/cssTransform.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @remove-on-eject-begin
2+
/**
3+
* Copyright (c) 2014-present, Facebook, Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
// @remove-on-eject-end
9+
'use strict';
10+
11+
// This is a custom Jest transformer turning style imports into empty objects.
12+
// http://facebook.github.io/jest/docs/tutorial-webpack.html
13+
14+
module.exports = {
15+
process() {
16+
return 'module.exports = {};';
17+
},
18+
getCacheKey() {
19+
// The output is always the same.
20+
return 'cssTransform';
21+
},
22+
};

config/jest/fileTransform.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @remove-on-eject-begin
2+
/**
3+
* Copyright (c) 2014-present, Facebook, Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
// @remove-on-eject-end
9+
'use strict';
10+
11+
const path = require('path');
12+
13+
// This is a custom Jest transformer turning file imports into filenames.
14+
// http://facebook.github.io/jest/docs/tutorial-webpack.html
15+
16+
module.exports = {
17+
process(src, filename) {
18+
return `module.exports = ${JSON.stringify(path.basename(filename))};`;
19+
},
20+
};

0 commit comments

Comments
 (0)