Repository navigation
Expand file tree
/
Copy pathgulpfile.js
More file actions
164 lines (145 loc) · 3.97 KB
/
Copy pathgulpfile.js
File metadata and controls
164 lines (145 loc) · 3.97 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var gulp = require('gulp');
var ts = require('gulp-typescript');
var typescript = require('typescript');
var del = require('del');
var runSequence = require('run-sequence');
var sourcemaps = require('gulp-sourcemaps');
var connect = require('gulp-connect');
var open = require('gulp-open');
var sass = require('gulp-sass');
var plumber = require('gulp-plumber');
var KarmaServer = require('karma').Server;
var rimraf = require('gulp-rimraf');
// Add debounce to gulp watch for FTP
// (function ftp_debounce_fix() {
//
// var watch = gulp.watch;
// // Overwrite the local gulp.watch function
// gulp.watch = function (glob, opt, fn) {
// var _this = this, _fn, timeout;
// // This is taken from the gulpjs file, but needed to
// // qualify the "fn" variable
// if (typeof opt === 'function' || Array.isArray(opt)) {
// fn = opt;
// opt = null;
// }
// // Make a copy of the callback function for reference
// _fn = fn;
// // Create a new delayed callback function
// fn = function () {
// if (timeout) {
// clearTimeout(timeout);
// }
// timeout = setTimeout(Array.isArray(_fn) ? function () {
// _this.start.call(_this, _fn);
// } : _fn, 1);
// };
// return watch.call(this, glob, opt, fn);
// };
// })();
var serverOptions = {
root: '',
port: 8000,
livereload: true,
};
var srcPath = 'src/app';
var testPath = 'src/test';
var tasks = {
defaultTask: 'default',
typeScript: 'typeScript-compile',
clean: 'clean',
buildSass: 'build-sass',
startWebServer: 'start-webServer',
openBrowser: 'open-browser',
reload: 'reload',
watch: 'watch',
watcherRebuild: 'watcher-rebuild',
testBuild: 'test-build',
test: 'test'
};
var tsProject = ts.createProject('tsconfig.json', {
typescript: typescript
});
// Main task
gulp.task(tasks.defaultTask, function () {
runSequence(
tasks.clean,
[tasks.typeScript, tasks.buildSass],
//tasks.test,
tasks.startWebServer,
tasks.openBrowser,
tasks.watch
);
});
// default task starts watcher. in order not to start it each change
// watcher will run the task bellow
gulp.task(tasks.watcherRebuild, function (callback) {
runSequence(
[tasks.typeScript, tasks.buildSass],
//tasks.test,
tasks.reload
);
callback();
});
// compiles *.ts files by tsconfig.json file and creates sourcemap filse
gulp.task(tasks.typeScript, function () {
return gulp.src([srcPath + '/**/**.ts'])
.pipe(sourcemaps.init())
.pipe(ts(tsProject))
.pipe(sourcemaps.write('./', { includeContent: false, sourceRoot: '/' + srcPath }))
.pipe(gulp.dest(srcPath));
});
// build sass files into css files
gulp.task(tasks.buildSass, function () {
return gulp.src(srcPath + '/**/*.scss')
.pipe(plumber())
.pipe(sass())
.pipe(plumber.stop())
.pipe(gulp.dest(srcPath));
});
// reload the browser
gulp.task(tasks.reload, function () {
gulp.src(srcPath)
.pipe(connect.reload());
});
// sets watcher to the files
gulp.task(tasks.watch, function () {
gulp.watch([
'/index.html',
srcPath + '/**/**.ts',
srcPath + '/**/**.html',
srcPath + '/**/**.scss'], [tasks.watcherRebuild]);
});
// starts web server
gulp.task(tasks.startWebServer, function () {
connect.server(serverOptions);
});
// opens browser
gulp.task(tasks.openBrowser, function () {
gulp.src('./index.html')
.pipe(open({ uri: 'http://localhost:' + serverOptions.port }));
});
// removes .js, .js.map, .css, .css.map
gulp.task(tasks.clean, function () {
return gulp.src([
srcPath + '/**/**.js',
srcPath + '/**/**.js.map',
srcPath + '/**/**.css',
testPath + '/**/**.js',
testPath + '/**/**.js.map'])
.pipe(rimraf());
});
// tests
gulp.task(tasks.testBuild, function () {
return gulp.src([testPath + '/**/**.ts'])
.pipe(sourcemaps.init())
.pipe(ts(tsProject))
.pipe(sourcemaps.write('./', { includeContent: false, sourceRoot: '/' + testPath }))
.pipe(gulp.dest(testPath));
});
gulp.task(tasks.test, [tasks.testBuild], function (done) {
new KarmaServer({
configFile: __dirname + '/karma.conf.js',
singleRun: false
}, done).start();
});