-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
116 lines (92 loc) · 3.27 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*global console, require */
'use strict';
var _ = require('lodash');
var browserify = require('browserify');
var colors = require('colors');
var concatCss = require('gulp-concat-css');
var del = require('del');
var gulp = require('gulp');
var gulpif = require('gulp-if');
var gutil = require('gulp-util');
var lrload = require('livereactload');
var nodemon = require('gulp-nodemon');
var path = require('path');
var source = require('vinyl-source-stream');
var sourcemaps = require('gulp-sourcemaps');
var ts = require('gulp-typescript');
var tsd = require('gulp-tsd');
var watchify = require('watchify');
var project = require('./tsconfig.json');
var watching = false;
var tsSources = project.files.slice(0, -1);
var jsSources = relativeToRoot(tsSources.map(ts2js));
console.log('jsSources', jsSources);
var bundler = browserify({
entries: jsSources,
transform: gutil.env.type === 'production' ? [] : [lrload], // For production this needs to be false
debug: true,
cache: {},
packageCache: {},
fullPaths: true
});
var tsProject = ts.createProject('tsconfig.json');
gulp.task('fetchTypings', function (cb) {
tsd({ command: 'reinstall', config: './tsd.json' }, cb);
});
gulp.task('copyHtml', function() {
gulp.src(project.html).pipe(gulp.dest('dist'));
});
gulp.task('build', function() {
return tsProject.src()
.pipe(sourcemaps.init())
.pipe(ts(tsProject)).js
.pipe(sourcemaps.write())
.pipe(gulp.dest('js'));
});
gulp.task('css', function () {
var stream = gulp.src(project.css)
.pipe(concatCss("bundle.css"))
.pipe(gulp.dest('./dist'));
return stream;
});
gulp.task('bundle', ['vendor', 'build'], _.partial(bundle, 'app.js', bundler, true));
gulp.task('vendor', _.partial(bundle, 'vendor.js', browserify(), false));
gulp.task('watch', ['deploy', 'serve'], function() {
watching = true;
gulp.watch(project.files, ['build']);
gulp.watch(project.html, ['copyHtml']);
gulp.watch(project.css, ['css']);
lrload.listen();
watchify(bundler)
.on('error', gutil.log)
.on('update', _.partial(bundle, 'app.js', bundler, true));
});
gulp.task('serve', function() {
nodemon({ script: 'server.js', ext: 'js', ignore: ['gulpfile.js', 'dist/app.js', 'dist/vendor.js','js/*', 'node_modules/*'] })
.on('restart', function () { gutil.log('Server restarted'); });
});
gulp.task('clean', function() { del(['js', 'dist']); });
gulp.task('deploy', ['copyHtml', 'css', 'bundle']);
gulp.task('default', ['watch']);
function bundle(output, b, isExternal) {
console.log('Bundling', output);
project.libraries.forEach(function(lib) {
if (isExternal) {
b.external(lib);
} else {
b.require(lib);
}
});
b.bundle()
.on('error', function(err) { gutil.log(err.message.bgRed); })
.pipe(source(output))
.pipe(gulp.dest('dist'))
.pipe(gulpif(watching, lrload.gulpnotify()));
}
function ts2js (f) {
return 'js/' + f.replace('.ts', '.js');
}
function relativeToRoot (files) {
var splitPath = function(p) { return path.join.apply(undefined, p.split('/')); };
return _.map(files, _.compose(path.resolve, splitPath));
}