Skip to content

Commit 2f44eef

Browse files
committed
ad create injectable store
1 parent f7bac91 commit 2f44eef

File tree

21 files changed

+271
-227
lines changed

21 files changed

+271
-227
lines changed

build/base-config/environment.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const argv = require('yargs').argv;
21

32
const NODE_ENV = process.env.NODE_ENV || 'development';
43

@@ -7,5 +6,4 @@ module.exports = {
76
__DEV__: NODE_ENV === 'development',
87
__PROD__: NODE_ENV === 'production',
98
__TEST__: NODE_ENV === 'test',
10-
__COVERAGE__: !argv.watch && NODE_ENV === 'test'
119
};

build/base-config/path.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ module.exports = {
1111
root: base,
1212
src: base.bind(null, 'src'),
1313
dist: base.bind(null, 'dist'),
14-
server: base.bind(null, 'server')
14+
server: base.bind(null, 'server'),
15+
public: base.bind(null, 'src', 'static')
1516
};

build/server.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ if (env.__DEV__) {
2424
publicPath: webpackConfig.output.publicPath,
2525
contentBase: paths.src(),
2626
lazy: false,
27-
// historyApiFallback: true,
2827
stats: {
2928
chunkModules: false,
3029
colors: true,
@@ -33,22 +32,11 @@ if (env.__DEV__) {
3332
}))
3433
app.use(require('webpack-hot-middleware')(compiler))
3534

36-
// Serve static assets from ~/src/static since Webpack is unaware of
37-
// these files. This middleware doesn't need to be enabled outside
38-
// of development since this directory will be copied into ~/dist
39-
// when the application is compiled.
40-
app.use(express.static(paths.src('static')))
35+
app.use(express.static(paths.public()))
4136
} else {
4237
debug(
43-
'Server is being run outside of live development mode, meaning it will ' +
44-
'only serve the compiled application bundle in ~/dist. Generally you ' +
45-
'do not need an application server for this and can instead use a web ' +
46-
'server such as nginx to serve your static files. See the "deployment" ' +
47-
'section in the README for more information on deployment strategies.'
38+
'Server is being run outside of live development mode ! ' +
4839
)
49-
// Serving ~/dist by default. Ideally these files should be served by
50-
// the web server and not the app server, but this helps to demo the
51-
// server in production.
5240
app.use(express.static(paths.dist()))
5341
}
5442

build/webpack-config/base.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = (paths) => {
1818
return debug(
1919
`Package "${dep}" was not found as an npm dependency in package.json; ` +
2020
`it won't be included in the webpack vendor bundle.
21-
Consider removing it from \`Vendors\` in this file`
21+
Consider removing it from "Vendors" in this file`
2222
);
2323
});
2424
if (__DEV__) {
@@ -33,6 +33,10 @@ module.exports = (paths) => {
3333
filename: '[name].[hash:8].js',
3434
path: paths.dist(),
3535
publicPath: '/'
36+
},
37+
performance: {
38+
hints: false,
39+
maxAssetSize: 2000000,
3640
}
3741
};
3842
};

build/webpack-config/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ module.exports = {
1414
},
1515
output: base.output,
1616
resolve: {
17-
extensions: ['.js', '.jsx', '.json'],
1817
alias
1918
},
2019
devtool: base.devtool,
2120
module: {
2221
loaders
2322
},
24-
performance: { hints: false },
23+
performance: base.performance,
2524
devServer: base.devServer,
2625
plugins
2726
};

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "src/main.js",
66
"scripts": {
77
"start": "better-npm-run start",
8+
"compile": "better-npm-run compile",
89
"lintJs": "eslint --fix ./src"
910
},
1011
"keywords": [
@@ -18,6 +19,9 @@
1819
"env": {
1920
"DEBUG": "app:*"
2021
}
22+
},
23+
"compile": {
24+
"command": "webpack"
2125
}
2226
},
2327
"author": "likun",
@@ -56,12 +60,12 @@
5660
"line-debugger": "^0.0.1",
5761
"mocha": "^3.2.0",
5862
"redbox-react": "^1.3.3",
63+
"redux-logger": "^2.7.4",
5964
"style-loader": "^0.13.1",
6065
"url-loader": "^0.5.7",
61-
"webpack": "2.2.0-rc.1",
66+
"webpack": "2.2.0-rc.3",
6267
"webpack-dev-middleware": "^1.8.4",
63-
"webpack-hot-middleware": "^2.13.2",
64-
"yargs": "^6.5.0"
68+
"webpack-hot-middleware": "^2.13.2"
6569
},
6670
"dependencies": {
6771
"normalize.css": "^5.0.0",
@@ -70,7 +74,6 @@
7074
"react-redux": "^5.0.0",
7175
"react-router": "^3.0.0",
7276
"redux": "^3.6.0",
73-
"redux-logger": "^2.7.4",
7477
"redux-thunk": "^2.1.0",
7578
"whatwg-fetch": "^2.0.1"
7679
}

src/main.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@ import ReactDOM from 'react-dom';
33
import createStore from './store/createStore';
44
import App from './containers/AppContainer';
55

6-
// ========================================================
7-
// Store Instantiation
8-
// ========================================================
6+
// store initial
97
const initialState = window.INITIAL_STATE;
108
const store = createStore(initialState);
119

12-
// ========================================================
13-
// Render Setup
14-
// ========================================================
10+
// some setup
1511
const MOUNT_NODE = document.getElementById('root');
16-
console.log(ReactDOM);
1712
let render = () => {
1813
const routes = require('./routes/index').default(store); // !important
1914
ReactDOM.render(
@@ -39,7 +34,6 @@ if (__DEV__ && module.hot) {
3934
render();
4035
});
4136
}
42-
// ========================================================
43-
// Go!
44-
// ========================================================
37+
38+
// start render
4539
render();

src/routes/Home/components/HomeView.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import DuckImage from '../assets/magic.png';
32
import './HomeView.less';
43

54
export const HomeView = () => (
@@ -8,7 +7,7 @@ export const HomeView = () => (
87
<img
98
alt="This is a duck, because Redux!"
109
className="duck"
11-
src={DuckImage}
10+
src="/images/magic.png"
1211
/>
1312
</div>
1413
);

src/routes/increase/components/increaseComponent.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { PropTypes } from 'react';
22

33
export const Increase = props => (
44
<div style={{ margin: '0 auto' }} >
@@ -14,9 +14,9 @@ export const Increase = props => (
1414
);
1515

1616
Increase.propTypes = {
17-
increase: React.PropTypes.number.isRequired,
18-
doubleAsync: React.PropTypes.func.isRequired,
19-
increment: React.PropTypes.func.isRequired
17+
increase: PropTypes.number.isRequired,
18+
doubleAsync: PropTypes.func.isRequired,
19+
increment: PropTypes.func.isRequired
2020
};
2121

2222
export default Increase;

src/routes/increase/containers/increaseContainer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { connect } from 'react-redux';
2-
import { actions } from '../reducers/Increase';
2+
import { actionCreatorMaps } from '../reducers/increase';
33

44
import IncreaseComponents from '../components/IncreaseComponent';
55

6-
const mapDispatchToProps = actions;
6+
const mapDispatchToProps = actionCreatorMaps;
77

88
const mapStateToProps = state => ({
99
increase: state.increase

src/routes/increase/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import container from './containers/IncreaseContainer';
2-
import reducer from './reducers/Increase';
3-
import component from './components/IncreaseComponent';
4-
5-
export default component;
6-
export { reducer, container, component };
1+
export default store => ({
2+
path: 'increase',
3+
getComponent(nextState, cb) {
4+
Promise.all([
5+
System.import('./containers/IncreaseContainer'),
6+
System.import('./reducers')
7+
]).then(([container, reducers]) => {
8+
store.injectAll(reducers.default);
9+
cb(null, container.default);
10+
});
11+
}
12+
});

src/routes/increase/reducers/increase.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createReducer } from 'UTILS/reducerTool';
1+
import createReducers from 'UTILS/creators';
22

33
const COUNTER_INCREMENT = 'COUNTER_INCREMENT';
44
/**
@@ -21,11 +21,11 @@ const doubleAsync = () => (dispatch, getState) => {
2121
};
2222
const increment = () => incrementBase(1);
2323

24-
export const actions = {
24+
export const actionCreatorMaps = {
2525
increment,
2626
doubleAsync
2727
};
2828

29-
export default createReducer({
29+
export default createReducers({
3030
[COUNTER_INCREMENT]: (state, action) => state + action.payload
3131
}, 0);

src/routes/increase/reducers/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import increase from './increase';
2+
3+
export default {
4+
increase
5+
};

src/routes/index.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
1-
import CoreLayout from 'LAYOUTS/CoreLayout/CoreLayout';
2-
import { injectReducerFactory } from 'UTILS/reducerTool';
1+
import CoreLayout from 'LAYOUTS/CoreLayout';
32
import Home from './home';
3+
import Increase from './increase';
44

5-
const createChildRoute = (path, store) => ({
6-
path,
7-
getComponent(nextState, cb) {
8-
const injectReducer = injectReducerFactory(store);
9-
System.import(`./${path}/index`).then((routeModule) => {
10-
const { container, reducer } = routeModule;
11-
injectReducer(`${path}`, reducer);
12-
cb(null, container);
13-
});
14-
}
15-
});
16-
const paths = ['increase'];
175
export const createRoutes = store => ({
186
path: '/',
197
component: CoreLayout,
208
indexRoute: Home,
21-
childRoutes: paths.map(path => createChildRoute(path, store))
9+
childRoutes: [
10+
Increase(store)
11+
]
2212
});
2313

2414
export default createRoutes;
File renamed without changes.

src/store/createInjectAbleStore.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* author: lelandrichardson
3+
* https://github.com/lelandrichardson/redux-injectable-store
4+
*/
5+
import { createStore, combineReducers } from 'redux';
6+
7+
const FAKE_INITIAL_REDUCER_NAMESPACE = '___';
8+
const IDENTITY_REDUCER = (state = null) => state;
9+
10+
const makeEmptyReducerMap = () => ({
11+
// putting this here because `combineReducers` will complain if there isn't at least
12+
// one reducer initially.
13+
[FAKE_INITIAL_REDUCER_NAMESPACE]: IDENTITY_REDUCER
14+
});
15+
16+
const createInjectableStore = (preloadedState, enhancer, defaultReducers) => {
17+
let reducers = defaultReducers || makeEmptyReducerMap();
18+
const store = createStore(combineReducers(reducers), preloadedState, enhancer);
19+
20+
const replace = () => {
21+
store.replaceReducer(combineReducers(reducers));
22+
};
23+
24+
const clearReducers = () => {
25+
reducers = defaultReducers || makeEmptyReducerMap();
26+
};
27+
28+
const inject = (namespace, reducer) => {
29+
if (reducers[namespace] == null) {
30+
reducers[namespace] = reducer;
31+
replace();
32+
}
33+
};
34+
35+
const injectAll = (reducerMap) => {
36+
let hasChanged = false;
37+
Object.keys(reducerMap).forEach((namespace) => {
38+
if (reducers[namespace] == null) {
39+
reducers[namespace] = reducerMap[namespace];
40+
hasChanged = true;
41+
}
42+
});
43+
if (hasChanged) {
44+
replace();
45+
}
46+
};
47+
48+
return {
49+
...store,
50+
inject,
51+
injectAll,
52+
clearReducers
53+
};
54+
};
55+
56+
export default createInjectableStore;

0 commit comments

Comments
 (0)