Skip to content

Commit ee61997

Browse files
committed
Fix examples to work standalone
1 parent 2eb5aea commit ee61997

File tree

7 files changed

+79
-81
lines changed

7 files changed

+79
-81
lines changed

docs/introduction/Examples.md

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
11
# Examples
22

3-
Redux is distributed with a few examples in its [source code](https://github.com/rackt/redux/tree/master/examples).
4-
**To run any of them, clone the repo and run `npm install` both in the root and the example folder.**
3+
Redux is distributed with a few examples in its [source code](https://github.com/rackt/redux/tree/master/examples).
54

65
>##### Note on Copying
7-
>If you copy Redux examples outside their folders, remove these lines from their `webpack.config.js`:
8-
>
9-
>```js
10-
>alias: {
11-
> 'redux': path.join(__dirname, '..', '..', 'src')
12-
>},
13-
>```
14-
>and
15-
>```js
16-
>{
17-
> test: /\.js$/,
18-
> loaders: ['babel'],
19-
> include: path.join(__dirname, '..', '..', 'src')
20-
>},
21-
```
22-
>
23-
> Otherwise they’ll try to resolve Redux to a relative `src` folder, and the build will fail.
6+
>If you copy Redux examples outside their folders, you can delete some lines at the end of their `webpack.config.js` files. They follow a “You can safely delete these lines in your project.” comment.
247
258
## Counter
269

@@ -29,13 +12,10 @@ Run the [Counter](https://github.com/rackt/redux/tree/master/examples/counter) e
2912
```
3013
git clone https://github.com/rackt/redux.git
3114
32-
cd redux
33-
npm install
34-
35-
cd examples/counter
15+
cd redux/examples/counter
3616
npm install
37-
3817
npm start
18+
3919
open http://localhost:3000/
4020
```
4121

@@ -51,13 +31,10 @@ Run the [TodoMVC](https://github.com/rackt/redux/tree/master/examples/todomvc) e
5131
```
5232
git clone https://github.com/rackt/redux.git
5333
54-
cd redux
34+
cd redux/examples/todomvc
5535
npm install
56-
57-
cd examples/todomvc
58-
npm install
59-
6036
npm start
37+
6138
open http://localhost:3000/
6239
```
6340

@@ -74,13 +51,10 @@ Run the [Async](https://github.com/rackt/redux/tree/master/examples/async) examp
7451
```
7552
git clone https://github.com/rackt/redux.git
7653
77-
cd redux
54+
cd redux/examples/async
7855
npm install
79-
80-
cd examples/async
81-
npm install
82-
8356
npm start
57+
8458
open http://localhost:3000/
8559
```
8660

@@ -97,13 +71,10 @@ Run the [Real World](https://github.com/rackt/redux/tree/master/examples/real-wo
9771
```
9872
git clone https://github.com/rackt/redux.git
9973
100-
cd redux
74+
cd redux/examples/real-world
10175
npm install
102-
103-
cd examples/real-world
104-
npm install
105-
10676
npm start
77+
10778
open http://localhost:3000/
10879
```
10980

examples/async/webpack.config.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,29 @@ module.exports = {
1717
new webpack.HotModuleReplacementPlugin(),
1818
new webpack.NoErrorsPlugin()
1919
],
20-
resolve: {
21-
alias: {
22-
'redux': path.join(__dirname, '..', '..', 'src')
23-
},
24-
extensions: ['', '.js']
25-
},
2620
module: {
2721
loaders: [{
2822
test: /\.js$/,
2923
loaders: ['react-hot', 'babel'],
3024
exclude: /node_modules/,
3125
include: __dirname
32-
}, {
33-
test: /\.js$/,
34-
loaders: ['babel'],
35-
include: path.join(__dirname, '..', '..', 'src')
3626
}]
3727
}
3828
};
29+
30+
31+
// When inside Redux repo, prefer src to compiled version.
32+
// You can safely delete these lines in your project.
33+
var reduxSrc = path.join(__dirname, '..', '..', 'src');
34+
var reduxNodeModules = path.join(__dirname, '..', '..', 'node_modules');
35+
var fs = require('fs');
36+
if (fs.existsSync(reduxSrc) && fs.existsSync(reduxNodeModules)) {
37+
// Resolve Redux to source
38+
module.exports.resolve = { alias: { 'redux': reduxSrc } };
39+
// Compile Redux from source
40+
module.exports.module.loaders.push({
41+
test: /\.js$/,
42+
loaders: ['babel'],
43+
include: reduxSrc
44+
});
45+
}

examples/counter/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Redux counter example",
55
"scripts": {
66
"start": "node server.js",
7-
"test": "mocha --recursive --compilers js:babel/register",
7+
"test": "mocha --recursive --compilers js:babel-core/register",
88
"test:watch": "npm test -- --watch"
99
},
1010
"repository": {

examples/counter/webpack.config.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,28 @@ module.exports = {
1717
new webpack.HotModuleReplacementPlugin(),
1818
new webpack.NoErrorsPlugin()
1919
],
20-
resolve: {
21-
alias: {
22-
'redux': path.join(__dirname, '..', '..', 'src')
23-
},
24-
extensions: ['', '.js']
25-
},
2620
module: {
2721
loaders: [{
2822
test: /\.js$/,
2923
loaders: ['react-hot', 'babel'],
3024
exclude: /node_modules/,
3125
include: __dirname
32-
}, {
33-
test: /\.js$/,
34-
loaders: ['babel'],
35-
include: path.join(__dirname, '..', '..', 'src')
3626
}]
3727
}
3828
};
29+
30+
// When inside Redux repo, prefer src to compiled version.
31+
// You can safely delete these lines in your project.
32+
var reduxSrc = path.join(__dirname, '..', '..', 'src');
33+
var reduxNodeModules = path.join(__dirname, '..', '..', 'node_modules');
34+
var fs = require('fs');
35+
if (fs.existsSync(reduxSrc) && fs.existsSync(reduxNodeModules)) {
36+
// Resolve Redux to source
37+
module.exports.resolve = { alias: { 'redux': reduxSrc } };
38+
// Compile Redux from source
39+
module.exports.module.loaders.push({
40+
test: /\.js$/,
41+
loaders: ['babel'],
42+
include: reduxSrc
43+
});
44+
}

examples/real-world/webpack.config.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,29 @@ module.exports = {
1717
new webpack.HotModuleReplacementPlugin(),
1818
new webpack.NoErrorsPlugin()
1919
],
20-
resolve: {
21-
alias: {
22-
'redux': path.join(__dirname, '..', '..', 'src')
23-
},
24-
extensions: ['', '.js']
25-
},
2620
module: {
2721
loaders: [{
2822
test: /\.js$/,
2923
loaders: ['react-hot', 'babel'],
3024
exclude: /node_modules/,
3125
include: __dirname
32-
}, {
33-
test: /\.js$/,
34-
loaders: ['babel'],
35-
include: path.join(__dirname, '..', '..', 'src')
3626
}]
3727
}
3828
};
29+
30+
31+
// When inside Redux repo, prefer src to compiled version.
32+
// You can safely delete these lines in your project.
33+
var reduxSrc = path.join(__dirname, '..', '..', 'src');
34+
var reduxNodeModules = path.join(__dirname, '..', '..', 'node_modules');
35+
var fs = require('fs');
36+
if (fs.existsSync(reduxSrc) && fs.existsSync(reduxNodeModules)) {
37+
// Resolve Redux to source
38+
module.exports.resolve = { alias: { 'redux': reduxSrc } };
39+
// Compile Redux from source
40+
module.exports.module.loaders.push({
41+
test: /\.js$/,
42+
loaders: ['babel'],
43+
include: reduxSrc
44+
});
45+
}

examples/todomvc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Redux TodoMVC example",
55
"scripts": {
66
"start": "node server.js",
7-
"test": "mocha --recursive --compilers js:babel/register",
7+
"test": "mocha --recursive --compilers js:babel-core/register",
88
"test:watch": "npm test -- --watch"
99
},
1010
"repository": {

examples/todomvc/webpack.config.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,33 @@ module.exports = {
1717
new webpack.HotModuleReplacementPlugin(),
1818
new webpack.NoErrorsPlugin()
1919
],
20-
resolve: {
21-
alias: {
22-
'redux': path.join(__dirname, '..', '..', 'src')
23-
},
24-
extensions: ['', '.js']
25-
},
2620
module: {
2721
loaders: [{
2822
test: /\.js$/,
2923
loaders: ['react-hot', 'babel'],
3024
exclude: /node_modules/,
3125
include: __dirname
32-
}, {
33-
test: /\.js$/,
34-
loaders: ['babel'],
35-
include: path.join(__dirname, '..', '..', 'src')
3626
}, {
3727
test: /\.css?$/,
3828
loaders: ['style', 'raw'],
3929
include: __dirname
4030
}]
4131
}
4232
};
33+
34+
35+
// When inside Redux repo, prefer src to compiled version.
36+
// You can safely delete these lines in your project.
37+
var reduxSrc = path.join(__dirname, '..', '..', 'src');
38+
var reduxNodeModules = path.join(__dirname, '..', '..', 'node_modules');
39+
var fs = require('fs');
40+
if (fs.existsSync(reduxSrc) && fs.existsSync(reduxNodeModules)) {
41+
// Resolve Redux to source
42+
module.exports.resolve = { alias: { 'redux': reduxSrc } };
43+
// Compile Redux from source
44+
module.exports.module.loaders.push({
45+
test: /\.js$/,
46+
loaders: ['babel'],
47+
include: reduxSrc
48+
});
49+
}

0 commit comments

Comments
 (0)