Skip to content

Commit b8ef76a

Browse files
author
kenberkeley
committed
cache
1 parent c04bed3 commit b8ef76a

File tree

7 files changed

+41
-25
lines changed

7 files changed

+41
-25
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@
6060
请分别 `git clone`,打开**两个**命令窗口( Windows 下推荐使用 `Cygwin`**分别**切换到两者的目录下
6161
分别敲下 `npm install` 安装依赖(为避免 Windows 下的 npm 软链接问题,可加上 `--no-bin-link` 完全解构所有依赖)
6262

63-
6463
> 虽然我们已经切换到了淘宝 npm 源,但安装 `node-sass@3.8.0` 的时候还是很有可能卡住
6564
> 因为它的安装需要从 Github 的 AWS 服务器拉取二进制文件,因此您可以为它指定源:
6665
> `npm i node-sass@3.8.0 --registry=https://registry.npm.taobao.org`
6766
>
6867
> 如果您想简单粗暴一点,[这里](http://pan.baidu.com/s/1o8eu4t0)还提供了 `node_modules.zip`,直接解压即可
6968
69+
最后需要全局安装跨平台环境配置器:`npm i cross-env -g`
70+
7071
### <a name="start">⊙ 启动</a>
7172
先后在 `msg-board-api``react-demo` 的命令窗口下,敲下 `npm start`
7273
如无意外,默认浏览器就会自动打开 `localhost:9090`,您立即可以看到效果

build/dev-server.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
var path = require('path'),
2-
express = require('express'),
1+
var express = require('express'),
32
webpack = require('webpack'),
43
// favicon = require('express-favicon'),
54
config = require('./webpack.dev.conf'),
@@ -8,7 +7,7 @@ var path = require('path'),
87
var compiler = webpack(config);
98

109
// for highly stable resources
11-
app.use('/static', express.static(path.join(__dirname, '../static')));
10+
app.use('/static', express.static(config.commonPath.staticDir));
1211

1312
// app.use(favicon(path.join(__dirname, '../favicon.ico')));
1413

@@ -25,10 +24,10 @@ app.use(require('webpack-dev-middleware')(compiler, {
2524
// compilation error display
2625
app.use(require('webpack-hot-middleware')(compiler));
2726

28-
app.listen(9000, '127.0.0.1', function(err) {
27+
app.listen(8000, '127.0.0.1', function(err) {
2928
if (err) {
3029
console.log(err);
3130
return;
3231
}
33-
console.log('Listening at http://127.0.0.1:9000');
32+
console.log('Listening at http://127.0.0.1:8000');
3433
});

build/webpack.base.conf.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
var path = require('path'),
22
webpack = require('webpack');
33

4-
var srcPath = path.resolve(__dirname, '../src');
4+
var src = path.resolve(__dirname, '../src'); // 源码目录
5+
var commonPath = {
6+
dist: path.resolve(__dirname, '../dist'), // build 后输出目录
7+
indexHTML: path.join(src, 'index.html'), // 入口基页
8+
staticDir: path.resolve(__dirname, '../static') // 无需处理的静态资源目录
9+
};
510

611
module.exports = {
12+
commonPath: commonPath,
713
entry: {
8-
app: path.join(srcPath, 'app.js'),
14+
app: path.join(src, 'app.js'),
915

1016
// ================================
1117
// 框架 / 类库 分离打包
@@ -24,7 +30,7 @@ module.exports = {
2430
]
2531
},
2632
output: {
27-
path: path.resolve(__dirname, '../dist/static'),
33+
path: path.join(commonPath.dist, 'static'),
2834
publicPath: '/static/'
2935
},
3036
resolve: {
@@ -33,16 +39,16 @@ module.exports = {
3339
// ================================
3440
// 自定义路径别名
3541
// ================================
36-
COMPONENT: path.join(srcPath, 'components'),
37-
ACTION: path.join(srcPath, 'redux/actions'),
38-
REDUCER: path.join(srcPath, 'redux/reducers'),
39-
STORE: path.join(srcPath, 'redux/store'),
40-
ROUTE: path.join(srcPath, 'routes'),
41-
SERVICE: path.join(srcPath, 'services'),
42-
UTIL: path.join(srcPath, 'utils'),
43-
HOC: path.join(srcPath, 'utils/HoC'),
44-
MIXIN: path.join(srcPath, 'utils/mixins'),
45-
VIEW: path.join(srcPath, 'views')
42+
COMPONENT: path.join(src, 'components'),
43+
ACTION: path.join(src, 'redux/actions'),
44+
REDUCER: path.join(src, 'redux/reducers'),
45+
STORE: path.join(src, 'redux/store'),
46+
ROUTE: path.join(src, 'routes'),
47+
SERVICE: path.join(src, 'services'),
48+
UTIL: path.join(src, 'utils'),
49+
HOC: path.join(src, 'utils/HoC'),
50+
MIXIN: path.join(src, 'utils/mixins'),
51+
VIEW: path.join(src, 'views')
4652
}
4753
},
4854
resolveLoader: {
@@ -51,7 +57,7 @@ module.exports = {
5157
module: {
5258
loaders: [{
5359
test: /\.(js|jsx)$/,
54-
include: srcPath,
60+
include: src,
5561
loaders: ['react-hot', 'babel?' + JSON.stringify({
5662
cacheDirectory: true,
5763
plugins: [

build/webpack.dev.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ config.plugins = (config.plugins || []).concat([
3535
new ExtractTextPlugin('[name].css'),
3636
new HtmlWebpackPlugin({
3737
filename: 'index.html',
38-
template: 'src/index.html',
38+
template: config.commonPath.indexHTML,
3939
chunksSortMode: 'none'
4040
}),
4141
new BrowserSyncPlugin({

build/webpack.prod.conf.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
var webpack = require('webpack'),
2+
rimraf = require('rimraf'),
23
config = require('./webpack.base.conf'),
34
ExtractTextPlugin = require('extract-text-webpack-plugin'),
45
HtmlWebpackPlugin = require('html-webpack-plugin'),
56
CopyWebpackPlugin = require('copy-webpack-plugin'),
67
SOURCE_MAP = false;
78

9+
rimraf.sync(config.commonPath.dist);
10+
811
// naming output files with hashes for better caching.
912
// dist/index.html will be auto-generated with correct URLs.
1013
config.output.filename = '[name].[chunkhash].js';
@@ -23,10 +26,11 @@ config.plugins = (config.plugins || []).concat([
2326
// 复制高度静态资源
2427
new CopyWebpackPlugin([
2528
{
26-
from: 'static',
29+
from: config.commonPath.staticDir,
2730
ignore: ['*.md']
2831
}
2932
]),
33+
new webpack.optimize.DedupePlugin(),
3034
new webpack.optimize.UglifyJsPlugin({
3135
compress: {
3236
warnings: false
@@ -40,7 +44,7 @@ config.plugins = (config.plugins || []).concat([
4044
// see https://github.com/ampedandwired/html-webpack-plugin
4145
new HtmlWebpackPlugin({
4246
filename: '../index.html',
43-
template: 'src/index.html',
47+
template: config.commonPath.indexHTML,
4448
chunksSortMode: 'none'
4549
})
4650
]);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"author": "Ken Berkeley <kenberkeley@foxmail.com>",
55
"version": "0.1.0",
66
"scripts": {
7-
"start": "set NODE_ENV=development&& node build/dev-server.js",
8-
"build": "set NODE_ENV=production&& rimraf dist && webpack --progress --hide-modules --config build/webpack.prod.conf.js"
7+
"start": "cross-env NODE_ENV=development node build/dev-server.js",
8+
"build": "cross-env NODE_ENV=production && webpack --progress --hide-modules --config build/webpack.prod.conf.js"
99
},
1010
"repository": {
1111
"type": "git",

src/app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ if (__DEV__ && __WHY_DID_YOU_UPDATE__) {
1414
const { whyDidYouUpdate } = require('why-did-you-update')
1515
whyDidYouUpdate(React)
1616
}
17+
if (__DEV__) {
18+
console.info('[当前环境] 开发环境')
19+
}
20+
if (__PROD__) {
21+
console.info('[当前环境] 生产环境')
22+
}
1723

1824
// ================================
1925
// 将根组件挂载到 DOM,启动!

0 commit comments

Comments
 (0)