Skip to content

Commit 472d33f

Browse files
committed
add deployment
1 parent fc3f919 commit 472d33f

17 files changed

+246
-100
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
build
1+
dist
22
.idea
33
node_modules
44
npm-debug.log

Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: node server/index.js

gulpfile.js

+25-84
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,42 @@
11
'use strict';
2-
32
let gulp = require('gulp');
4-
let watch = require('gulp-watch');
5-
let util = require('gulp-util');
6-
let webpack = require('webpack');
7-
let nodemon = require('gulp-nodemon');
83
let runSequence = require('run-sequence');
9-
let del = require('del');
10-
let miniLr = require('mini-lr');
11-
12-
let webPackConfig = require('./webpack.config');
13-
14-
let sourceFolder = 'client';
154

16-
let source = [
17-
'client/**/*.{html,css}', '!**/app/**',
18-
'node_modules/todomvc-common/base.css'
19-
];
20-
let destinationFolder = 'build';
21-
let liveReload = miniLr();
22-
23-
function notifyChanged(files) {
24-
liveReload.changed({
25-
body: {
26-
files: files
27-
}
28-
});
29-
}
30-
31-
gulp.task('server-start', function () {
32-
return nodemon({
33-
script: 'server/index.js',
34-
watch: ['server'],
35-
ignore: ['node_modules/**'],
36-
ext: 'js html',
37-
env: {
38-
'NODE_ENV': 'development'
39-
}
40-
});
41-
});
42-
43-
gulp.task('livereload', function() {
44-
liveReload.listen(35729);
45-
});
46-
47-
gulp.task('client-copy', function() {
48-
let clientWatch = watch(source, {verbose: true});
49-
clientWatch.on('change', function(fileName) {
50-
notifyChanged([fileName]);
51-
});
52-
53-
gulp.src(source, {})
54-
.pipe(clientWatch)
55-
.pipe(gulp.dest(destinationFolder));
56-
});
5+
let clientCopyTask = require('./tasks/client_copy');
6+
let clientBuildTask = require('./tasks/client_build');
7+
let liveReloadTask = require('./tasks/livereload');
8+
let serverStartTask = require('./tasks/server_start');
9+
let serverCopyTask = require('./tasks/server_copy');
10+
let generalCopyTask = require('./tasks/general_copy');
11+
let cleanTask = require('./tasks/clean');
5712

58-
gulp.task('client-build', function(callback) {
59-
let webpackBuild = webpack(webPackConfig);
60-
let firstRun = true;
13+
gulp.task('server-start', serverStartTask());
14+
gulp.task('server-copy-dist', serverCopyTask());
6115

62-
webpackBuild.watch({ aggregateTimeout: 100 }, function(err, stats) {
63-
if(err) {
64-
throw new util.PluginError("webpack:error", err);
65-
}
16+
gulp.task('general-copy-dist', generalCopyTask());
6617

67-
let statistics = stats.toJson({
68-
children: false,
69-
source: false,
70-
modules: false,
71-
chunkModules: false
72-
});
18+
gulp.task('livereload', liveReloadTask());
7319

74-
let elapsedTime = Math.round(statistics.time / 10) / 100;
20+
gulp.task('client-copy', clientCopyTask(false, liveReloadTask.notifyChanged));
21+
gulp.task('client-copy-dist', clientCopyTask(true));
22+
gulp.task('client-build', clientBuildTask(false, liveReloadTask.notifyChanged));
23+
gulp.task('client-build-dist', clientBuildTask(true));
7524

76-
if (firstRun) {
77-
callback();
78-
firstRun = false;
79-
}
80-
else {
81-
util.log(`webpack:build ${elapsedTime} s`);
25+
gulp.task('clean', cleanTask());
8226

83-
notifyChanged(
84-
statistics.assets.map((file) => file.name)
85-
);
86-
}
87-
});
88-
});
89-
90-
gulp.task('clean', function() {
91-
return del([destinationFolder]);
92-
});
93-
94-
gulp.task('start', function(callback) {
27+
gulp.task('serve', function(callback) {
9528
runSequence(
9629
'clean',
9730
['client-build', 'client-copy', 'livereload'],
9831
'server-start',
9932
callback
10033
)
10134
});
35+
36+
gulp.task('dist', function(callback) {
37+
runSequence(
38+
'clean',
39+
['client-build-dist', 'client-copy-dist', 'server-copy-dist', 'general-copy-dist'],
40+
callback
41+
)
42+
});

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
"license": "ISC",
1818
"keywords": [
1919
"angular2",
20-
"es6",
21-
"webpack",
20+
"todomvc",
2221
"gulp"
2322
],
24-
"description": "A skeleton Angular2 ES6 application built with Babel, Webpack, Gulp",
23+
"description": "TodoMVC application built with Angular 2 ES6.",
2524
"engines": {
2625
"node": "4.2.4"
2726
},
@@ -43,6 +42,7 @@
4342
"es6-promise": "^3.0.2",
4443
"es6-shim": "^0.33.13",
4544
"gulp": "^3.9.0",
45+
"gulp-delete-lines": "0.0.7",
4646
"gulp-nodemon": "^2.0.4",
4747
"gulp-util": "^3.0.7",
4848
"gulp-watch": "^4.3.5",

readme.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,24 @@ git clone https://github.com/blacksonic/angular2-es6-todomvc.git
1818
cd angular2-es6-todomvc
1919
npm install
2020

21-
npm start
22-
# or use it with Gulp
2321
gulp start
2422

2523
```
2624

2725
Open it in your browser [http://localhost:9000](http://localhost:9000).
26+
27+
### Deployment (to Heroku)
28+
29+
```bash
30+
31+
gulp dist
32+
33+
cd dist
34+
git init
35+
git add -A .
36+
git commit -m "Deploy #1" && echo Committed
37+
git push -f git@heroku.com:angular2-es6-todomvc.git master
38+
39+
```
40+
41+
Check out the [deployed version](https://angular2-es6-todomvc.herokuapp.com/).

server/config.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
let path = require('path');
3+
let environment = process.env.NODE_ENV || 'production';
4+
5+
let localStaticPath = environment == 'production' ? '../client' : '../dist/client';
6+
module.exports = {
7+
env: environment,
8+
port: process.env.PORT || 9000,
9+
staticPath: path.resolve(__dirname, localStaticPath)
10+
};

server/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
'use strict';
2-
32
let koa = require('koa');
43
let staticServe = require('koa-static');
5-
let path = require('path');
4+
let config = require('./config');
65

7-
let port = 9000;
86
let app = koa();
97

10-
app.use(staticServe(path.resolve(__dirname + '/../build')));
8+
app.use(staticServe(config.staticPath));
119

12-
app.listen(port);
13-
console.log(`Listening on port ${port}`);
10+
app.listen(config.port);
11+
console.log(`Listening on port ${config.port}`);

tasks/clean.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
let del = require('del');
3+
let config = require('./config').build;
4+
5+
module.exports = function() {
6+
return function() {
7+
return del([config.destination]);
8+
};
9+
};

tasks/client_build.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
let gulp = require('gulp');
3+
let webpack = require('webpack');
4+
let util = require('gulp-util');
5+
let config = require('./config').client;
6+
7+
module.exports = function(singleRun, callback) {
8+
return function(cb) {
9+
let webpackConfig = singleRun ? require('./config/webpack.dist') : require('./config/webpack');
10+
let webpackBuild = webpack(webpackConfig);
11+
let firstRun = true;
12+
13+
let callbackOnBuild = function(err, stats) {
14+
if (err) {
15+
throw new util.PluginError("webpack:error", err);
16+
}
17+
18+
let statistics = stats.toJson({
19+
children: false,
20+
source: false,
21+
modules: false,
22+
chunkModules: false
23+
});
24+
25+
let elapsedTime = Math.round(statistics.time / 10) / 100;
26+
27+
if (singleRun) {
28+
cb();
29+
}
30+
else {
31+
if (firstRun) {
32+
cb();
33+
firstRun = false;
34+
}
35+
else {
36+
util.log(`webpack:build ${elapsedTime} s`);
37+
38+
callback(
39+
statistics.assets.map((file) => file.name)
40+
);
41+
}
42+
}
43+
};
44+
45+
if (singleRun) {
46+
webpackBuild.run(callbackOnBuild);
47+
}
48+
else {
49+
webpackBuild.watch({ aggregateTimeout: 100 }, callbackOnBuild);
50+
}
51+
}
52+
};

tasks/client_copy.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
let gulp = require('gulp');
3+
let watch = require('gulp-watch');
4+
let deleteLines = require('gulp-delete-lines');
5+
let config = require('./config').client;
6+
7+
module.exports = function(singleRun, callback) {
8+
return function() {
9+
let gulpStream = gulp.src(config.source);
10+
11+
if (!singleRun) {
12+
let clientWatch = watch(config.source, {verbose: true});
13+
14+
if (callback) {
15+
clientWatch.on('change', function(fileName) {
16+
callback([fileName]);
17+
});
18+
}
19+
20+
gulpStream.pipe(clientWatch);
21+
}
22+
else {
23+
gulpStream.pipe(deleteLines({
24+
'filters': [
25+
/livereload/i
26+
]
27+
}));
28+
}
29+
30+
return gulpStream.pipe(gulp.dest(config.destination));
31+
}
32+
};

tasks/config/index.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
module.exports = {
4+
client: {
5+
source: ['client/**/*.{html,css}', '!**/app/**', 'node_modules/todomvc-common/base.css'],
6+
destination: 'dist/client'
7+
},
8+
server: {
9+
source: ['server/**/*.{js,json}', '!server/**/*.spec.*'],
10+
destination: 'dist/server'
11+
},
12+
general: {
13+
source: ['package.json', 'Procfile'],
14+
destination: 'dist'
15+
},
16+
liveReload: {
17+
port: 35729
18+
},
19+
build: {
20+
destination: 'dist'
21+
}
22+
};

tasks/config/webpack.dist.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
let webpack = require('webpack');
3+
let config = require('./webpack');
4+
5+
config.plugins = [
6+
new webpack.optimize.UglifyJsPlugin({
7+
mangle: false,
8+
comments: false
9+
})
10+
];
11+
12+
module.exports = config;

webpack.config.js renamed to tasks/config/webpack.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
'use strict';
22
let path = require('path');
3+
let config = require('./index').client;
34

45
module.exports = {
56
entry: {
67
boot: './client/boot.js'
78
},
89
output: {
9-
path: path.resolve(__dirname, './build'),
10+
path: path.resolve(__dirname, '../../', config.destination),
1011
filename: '[name].js'
1112
},
1213
module: {
1314
loaders: [
1415
{
1516
test: /\.js$/,
16-
loader: 'babel-loader',
17+
loader: 'babel',
1718
exclude: /node_modules/,
1819
query: {
1920
presets: ['es2015'],
@@ -27,7 +28,7 @@ module.exports = {
2728
},
2829
{
2930
test: /\.html$/,
30-
loader: "html-loader"
31+
loader: "html?minimize=false"
3132
}
3233
]
3334
},
@@ -38,4 +39,4 @@ module.exports = {
3839
},
3940

4041
devtool: 'source-map'
41-
};
42+
};

tasks/general_copy.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
let gulp = require('gulp');
3+
let config = require('./config').general;
4+
5+
module.exports = function() {
6+
return function() {
7+
return gulp.src(config.source)
8+
.pipe(gulp.dest(config.destination));
9+
}
10+
};

0 commit comments

Comments
 (0)