Skip to content
This repository was archived by the owner on Jan 16, 2018. It is now read-only.

Commit 11d4269

Browse files
committed
More stuff
1 parent fa010d7 commit 11d4269

28 files changed

+384
-89
lines changed

README.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# webpack/react-starter
2+
3+
Starter template for react and webpack.
4+
5+
## Features
6+
7+
* Compilation with webpack
8+
* React and jsx
9+
* Also includes reflux, but this can be easily replaced
10+
* Stylesheets can be CSS, LESS, SASS, Stylus or mixed
11+
* Embedded resources like images or fonts use DataUrls if appropriate
12+
* A simple flag loads a react component (and dependencies) on demand.
13+
* Development
14+
* Development server
15+
* Optionally Hot Module Replacement development server (LiveReload for Stylesheets and React components enabled)
16+
* Uses SourceUrl for performance, but you may switch to SourceMaps easily
17+
* Production
18+
* Server example for prerendering for React components
19+
* Long Term Cacheing through file hashes enabled
20+
* Generate separate css file to avoid FOUC
21+
* Minized CSS, HTML and javascript
22+
* Also supports coffee-script files if you are more a coffee-script guy.
23+
* You can also require markdown or text files for your content.
24+
* Just require the files...
25+
26+
## Installation
27+
28+
Just clone this repo and change the `origin` git remote.
29+
30+
``` text
31+
npm install
32+
```
33+
34+
35+
## Development server
36+
37+
``` text
38+
npm run dev-server
39+
http://localhost:2992/
40+
```
41+
42+
The configuration is `webpack-dev-server.config.js`.
43+
44+
Static HTML is served from `config/dev-server-public`.
45+
46+
It automatically recompiles and refreshes the page when files are changed.
47+
48+
49+
## Hot Module Replacement development server
50+
51+
``` text
52+
npm run dev-server
53+
http://localhost:2992/
54+
```
55+
56+
The configuration is `webpack-hot-dev-server.config.js`.
57+
58+
Static HTML is served from `config/dev-server-public`.
59+
60+
It automatically recompiles when files are changed. When a hot-replacement-enabled file is changed (i. e. stylesheets or React components) the module is hot-replaced. If Hot Replacement is not possible the page is refreshed.
61+
62+
Hot Module Replacement has a performance impact on compilation.
63+
64+
65+
## Production compilation and server
66+
67+
``` text
68+
npm run build
69+
npm start
70+
http://localhost:8080/
71+
```
72+
73+
The configuration is `webpack-production.config.js`.
74+
75+
The server is at `lib/server.js`
76+
77+
The production setting builds two configurations: one for the client (`build/public`) and one for the serverside prerendering (`build/prerender`).
78+
79+
> WIP: Serverside data fetching and embedding data into served HTML.
80+
81+
82+
## Build visualisation
83+
84+
85+
86+
87+
## Loaders and file types
88+
89+
Many file types are preconfigured, but not every loader is installed. If you get an error like `Cannot find module "xxx-loader"`, you'll need to install the loader with `npm install xxx-loader --save` and restart the compilation.
90+
91+
92+
## Common changes to the configuration
93+
94+
### Add more entry points
95+
96+
(for a multi page app)
97+
98+
1. Add an entry point to `make-webpack-config.js` (`var entry`).
99+
2. Add a new top-level react component in `app`.
100+
3. (Optional) Enable `commonsChunk` in `webpack-production.config.js`.
101+
4. Add a new HTML file in `config/dev-server-public` that references the new output file.
102+
5. Restart compilation.
103+
104+
### Switch devtool to SourceMaps
105+
106+
Change `devtool` property in `webpack-dev-server.config.js` and `webpack-hot-dev-server.config.js` to `"source-map"` (better module names) or `"eval-source-map"` (faster compilation).
107+
108+
SourceMaps have a performance impact on compilation.
109+
110+
### Enable SourceMaps in production
111+
112+
1. Uncomment the `devtool` line in `webpack-production.config.js`.
113+
2. Make sure that the folder `build\public\debugging` is access controlled, i. e. by password.
114+
115+
SourceMaps have a performance impact on compilation.
116+
117+
SourceMaps contains your unminimized source code, so you need to restrict access to `build\public\debugging`.
118+
119+
120+
## License
121+
122+
Copyright (c) 2012-2014 Tobias Koppers [![Gittip donate button](http://img.shields.io/gittip/sokra.png)](https://www.gittip.com/sokra/)
123+
124+
MIT (http://www.opensource.org/licenses/mit-license.php)

app/Application.jsx

-11
This file was deleted.

app/Example/Actions.js

+29-16
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
var Reflux = require("reflux");
22

33
var Actions = module.exports = Reflux.createActions([
4-
"APPLY_OFFSET",
5-
"SET",
4+
"applyOffset",
5+
"set",
66
]);
77

8-
Actions.SET.shouldEmit = function(value) {
8+
Actions.view = Reflux.createActions([
9+
"increment",
10+
"decrement",
11+
"reset",
12+
]);
13+
14+
Actions.server = Reflux.createActions([
15+
"update",
16+
]);
17+
18+
// Hooks
19+
Actions.set.shouldEmit = function(value) {
920
return Math.floor(value) === value;
1021
};
1122

12-
// Public methods
13-
Actions.set = function(value) {
14-
Actions.SET(value);
15-
};
23+
// View actions
24+
Actions.view.reset.listen(function() {
25+
Actions.set(0);
26+
});
1627

17-
Actions.reset = function() {
18-
Actions.SET(0);
19-
};
28+
Actions.view.increment.listen(function() {
29+
Actions.applyOffset(1);
30+
});
2031

21-
Actions.increment = function() {
22-
Actions.APPLY_OFFSET(1);
23-
};
32+
Actions.view.decrement.listen(function() {
33+
Actions.applyOffset(-1);
34+
});
2435

25-
Actions.decrement = function() {
26-
Actions.APPLY_OFFSET(-1);
27-
};
36+
// Server actions
37+
38+
Actions.server.update.listen(function(value) {
39+
Actions.set(value);
40+
});

app/Example/BigDisplay.jsx

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
/** @jsx React.DOM */
22

33
var React = require("react");
4+
var Reflux = require("reflux");
5+
var ClicksStore = require("./ClicksStore");
46

57
var BigDisplay = React.createClass({
8+
mixins: [Reflux.ListenerMixin],
9+
getInitialState: function() {
10+
return {
11+
clicks: ClicksStore.getClicks()
12+
};
13+
},
14+
componentDidMount: function() {
15+
this.listenTo(ClicksStore, this._onChange);
16+
},
17+
_onChange: function() {
18+
this.setState(this.getInitialState());
19+
},
620
render: function() {
7-
return <span>Yeah! <strong>{this.props.value}</strong> is really big.</span>;
21+
return <span>Yeah! <strong>{this.props.value}</strong> is really big. You clicked <i>{this.state.clicks}</i> times.</span>;
822
}
923
});
1024
module.exports = BigDisplay;

app/Example/ClicksStore.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var Reflux = require("reflux");
2+
var Actions = require("./Actions");
3+
4+
module.exports = Reflux.createStore({
5+
CLICK_RECOGNISED: 1,
6+
7+
init: function() {
8+
this._clicks = 0;
9+
10+
this.listenTo(Actions.view.increment, this._click);
11+
this.listenTo(Actions.view.decrement, this._click);
12+
this.listenTo(Actions.view.reset, this._click);
13+
},
14+
15+
_click: function() {
16+
this._clicks++;
17+
this.trigger(this.CLICK_RECOGNISED);
18+
},
19+
20+
getClicks: function() {
21+
return this._clicks;
22+
}
23+
});

app/Example/Control.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var Example = React.createClass({
88
return <button onClick={this._onClick}>{this.props.label}</button>;
99
},
1010
_onClick: function() {
11-
Actions[this.props.action]();
11+
Actions.view[this.props.action]();
1212
}
1313
});
1414
module.exports = Example;

app/Example/Display.jsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

33
var React = require("react");
44
var Reflux = require("reflux");
5-
var Store = require("./Store");
5+
var ValueStore = require("./ValueStore");
66
var BigDisplay = require("react-proxy!./BigDisplay");
77

88
var Display = React.createClass({
99
mixins: [Reflux.ListenerMixin],
1010
getInitialState: function() {
1111
return {
12-
value: Store.get()
12+
value: ValueStore.getValue()
1313
};
1414
},
1515
componentDidMount: function() {
16-
this.listenTo(Store, this._onChange);
16+
this.listenTo(ValueStore, this._onChange);
1717
},
1818
_onChange: function() {
1919
this.setState(this.getInitialState());
2020
},
2121
render: function() {
2222
if(this.state.value < 10) {
23-
return <span><strong>{this.state.value}</strong> Try to get 10 or more.</span>;
23+
return <span><strong>{this.state.value}</strong> (Try to get 10 or more)</span>;
2424
} else {
2525
// BigDisplay loads on demand because of "react-proxy"
26-
return <BigDisplay value={this.state.value} />
26+
return <BigDisplay value={this.state.value}/>
2727
}
2828
}
2929
});

app/Example/ServerSyncStore.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var Reflux = require("reflux");
2+
var Actions = require("./Actions");
3+
4+
module.exports = Reflux.createStore({
5+
SENDING_STARTED: 1,
6+
SENDING_ACKNOWLEDGED: 2,
7+
UPDATE_RECEIVED: 3,
8+
9+
init: function() {
10+
Actions.view.increment.listen(this._increment);
11+
Actions.view.decrement.listen(this._decrement);
12+
Actions.view.reset.listen(this._reset);
13+
// TODO: Listen on server changes => this._serverUpdate
14+
// TODO: Request initial
15+
},
16+
17+
_increment: function(source) {
18+
// TODO: Send to server...
19+
// TODO: this.trigger(this.SENDING_STARTED) and isSyncing() === true
20+
// TODO: this.trigger(this.SENDING_ACKNOWLEDGED) and isSyncing() === false
21+
},
22+
23+
_decrement: function(source) {
24+
// TODO: Send to server...
25+
},
26+
27+
_reset: function(source) {
28+
// TODO: Send to server...
29+
},
30+
31+
_serverUpdate: function(value) {
32+
Actions.server.update(value);
33+
this.trigger(this.UPDATE_RECEIVED);
34+
},
35+
36+
isInitiallyLoaded: function() {
37+
// TODO return false during initial load
38+
return true;
39+
},
40+
41+
isSyncing: function() {
42+
// TODO return true while syncing
43+
return false;
44+
}
45+
});

app/Example/Store.js app/Example/ValueStore.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,27 @@ var Reflux = require("reflux");
22
var Actions = require("./Actions");
33

44
module.exports = Reflux.createStore({
5+
OFFSET_APPLIED: 1,
6+
VALUE_SET: 2,
7+
58
init: function() {
69
this._value = 1;
7-
8-
this.listenTo(Actions.APPLY_OFFSET, this._applyOffset);
9-
this.listenTo(Actions.SET, this._set);
10+
11+
this.listenTo(Actions.applyOffset, this._applyOffset);
12+
this.listenTo(Actions.set, this._set);
1013
},
11-
14+
1215
_applyOffset: function(offset) {
1316
this._value += offset;
14-
this.trigger();
17+
this.trigger(this.OFFSET_APPLIED);
1518
},
16-
19+
1720
_set: function(value) {
1821
this._value = value;
19-
this.trigger();
22+
this.trigger(this.VALUE_SET);
2023
},
21-
22-
get: function() {
24+
25+
getValue: function() {
2326
return this._value;
2427
}
2528
});

app/Example/index.jsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/** @jsx React.DOM */
22

33
var React = require("react");
4+
45
var Display = require("./Display");
56
var Control = require("./Control");
67

78
var Example = React.createClass({
89
render: function() {
9-
return <div>
10+
require("./style.less");
11+
return <div className="module-example">
1012
<Display />
1113
<div>
1214
<Control action="increment" label="+1" />

app/Example/init.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require("./ClicksStore");
2+
require("./ValueStore");
3+
require("./ServerSyncStore");

0 commit comments

Comments
 (0)