-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgulpfile.js
More file actions
173 lines (154 loc) · 5.15 KB
/
gulpfile.js
File metadata and controls
173 lines (154 loc) · 5.15 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
165
166
167
168
169
170
171
172
173
const del = require('del');
const gulp = require('gulp');
const typescript = require('gulp-typescript');
const flatten = require('gulp-flatten');
const sass = require('gulp-sass');
const tsconfig = require('tsconfig-glob');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const systemjsBuilder = require('systemjs-builder');
const concat = require('gulp-concat');
const merge = require('merge-stream');
const browserSync = require('browser-sync');
const url = require('url');
const proxy = require('proxy-middleware');
const cleanCSS = require('gulp-clean-css');
const seq = require('run-sequence');
const plumber = require('gulp-plumber');
const colors = require('colors');
const conf = require('./gulpfile.config.json');
const tsConfig = require(conf.tsconfig);
// clean the contents of the distribution directory
gulp.task('clean', function () {
return del(conf.target + "/**/*");
});
//cleans the distribution directory after bundling phase for the release
gulp.task('clean:release', function () {
return del([
conf.target + '/app',
conf.target + '/lib',
conf.target + '/app.*.js*',
conf.target + '/main.js*',
conf.target + '/*.js.map'
]);
});
//creates the tsconfig file and keeps it up to date
gulp.task('prep:tsConfig', function () {
return tsconfig({configPath: ".", indent: 2});
});
// copy dependencies
gulp.task('prep:libs', function () {
var pipes = [];
conf.libs.forEach(function (lib) {
pipes.push(gulp.src([lib.src])
.pipe(gulp.dest(lib.target)));
});
pipes.push(gulp.src(conf.vendor.src)
.pipe(concat('vendors.min.js'))
.pipe(uglify())
.pipe(gulp.dest(conf.vendor.target)));
return merge(pipes);
});
//compiles the typescript
gulp.task('compile:ts', function () {
var tsProject = typescript.createProject(conf.tsconfig);
var tsResult = tsProject.src()
.pipe(sourcemaps.init())
.pipe(tsProject(typescript.reporter.defaultReporter()));
return tsResult.js
.pipe(plumber({
errorHandler: function (err) {
console.error('>>> [tsc] Typescript compilation failed'.bold.green);
this.emit('end');
}
}))
.pipe(sourcemaps.write('.'))
//.pipe(flatten({subPath: [1]}))
.pipe(gulp.dest(conf.target));
});
// Compile SCSS to CSS, concatenate, and minify
gulp.task('compile:sass', function () {
var styles = [];
conf.styles.forEach(function (style) {
var g = gulp.src(style.src)
.pipe(sourcemaps.init({largeFile: true}))
.pipe(plumber({
errorHandler: function (err) {
console.error('>>> [sass] Sass style compilation failed'.bold.green);
console.error(err.message);
this.emit('end');
}
}))
.pipe(sass().on('error', sass.logError));
if (style.file) {
g.pipe(concat(style.file));
} else {
g.pipe(flatten({subPath: [0, 0]}));
}
g.pipe(cleanCSS())
.pipe(sourcemaps.write())
.pipe(gulp.dest(style.target));
styles.push(g);
});
return styles;
});
//bundle js files
gulp.task('bundle:js', function () {
var sysJSBuilder = new systemjsBuilder(conf.target, 'src/system.config.js');
return sysJSBuilder.buildStatic('main.js', conf.target + '/main.min.js');
});
// Minify JS bundle
gulp.task('post:minify:js', function () {
return gulp
.src(conf.target + '/main.min.js')
.pipe(uglify())
.pipe(gulp.dest(conf.target));
});
// copy static assets - i.e. non TypeScript compiled source
gulp.task('post:copy:assets', function () {
var pipes = [];
conf.assets.forEach(function (asset) {
pipes.push(
gulp.src(asset.src, {base: './'})
.pipe(flatten({subPath: asset.flatten}))
.pipe(gulp.dest(asset.target))
);
});
return pipes;
});
//recompiles typscript and javascript sources for dev environment
gulp.task('dev:compile:js', function (callback) {
seq('prep:tsConfig', 'compile:ts', 'bundle:js', callback);
});
// starts browsersync and watches scss, html and ts changes
gulp.task('dev:serve', function () {
var proxyOptions = url.parse(conf.proxy.url);
proxyOptions.route = conf.proxy.route;
browserSync({
server: {
baseDir: conf.target,
middleware: [proxy(proxyOptions)]
}
});
gulp.watch(['src/**/*.html', 'assets/**/*'], ['dev:reload:html']);
gulp.watch(['src/**/*.scss'], ['dev:reload:css']);
gulp.watch(['src/**/*.ts'], ['dev:reload:js']);
});
//on ts changes
gulp.task('dev:reload:js', ['dev:compile:js'], browserSync.reload);
//on css changes
gulp.task('dev:reload:css', ['compile:sass'], browserSync.reload);
//on html changes
gulp.task('dev:reload:html', ['post:copy:assets'], browserSync.reload);
// Run browsersync for development
gulp.task('serve', function (callback) {
return seq('build', 'dev:serve', callback);
});
// build the page without minifying the java script
gulp.task('build', function (callback) {
seq('clean', 'prep:tsConfig', 'prep:libs', 'compile:ts', 'bundle:js', ['compile:sass', 'post:copy:assets'], callback);
});
// build the page with minified java script
gulp.task('release', function (callback) {
seq('clean', 'prep:tsConfig', 'prep:libs', 'compile:ts', 'bundle:js', 'post:minify:js', ['compile:sass', 'post:copy:assets'], 'clean:release', callback);
});