-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
163 lines (143 loc) · 4.35 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
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
var babelify = require('babelify');
var browserify = require('browserify');
var browserSync = require('browser-sync');
var buffer = require('vinyl-buffer');
var concatCss = require('gulp-concat-css');
var debug = require('gulp-debug');
var del = require('del');
var filter = require('gulp-filter');
var gulp = require('gulp');
var gulpif = require('gulp-if');
var gutil = require('gulp-util');
var karma = require('karma').server;
var lazypipe = require('lazypipe');
var minifyCss = require('gulp-minify-css');
var minifyHtml = require('gulp-minify-html');
var ngAnnotate = require('gulp-ng-annotate');
var partialify = require('partialify');
var pipe = require('multipipe');
var rev = require('gulp-rev');
var revReplace = require('gulp-rev-replace');
var runSequence = require('run-sequence');
var size = require('gulp-size');
var stripDebug = require('gulp-strip-debug');
var source = require('vinyl-source-stream');
var sourcemaps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');
var useref = require('gulp-useref');
var watchify = require('watchify');
// Helpers:
function bundleScripts(debug, watch) {
var bundler = browserify('./src/app.js', {debug: debug})
.transform(partialify)
.transform(babelify.configure({ignore: 'bower_components'}));
function rebundle() {
return bundler.bundle()
.on('error', function(error) {
gutil.log(gutil.colors.red(error));
if (watch) {
this.emit('end');
} else {
process.exit(1);
}
})
.on('log', gutil.log)
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(ngAnnotate({single_quotes: true}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./.tmp'))
.pipe(gulpif(watch, browserSync.stream({once: true})))
.pipe(size());
}
if (watch) bundler = watchify(bundler).on('update', rebundle);
return rebundle();
}
// Tasks:
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: ['src', 'assets', '.tmp']
},
port: 8080,
ghostMode: false
});
gulp.watch('index.html').on('change', browserSync.reload);
});
gulp.task('build', function(cb) {
runSequence('clean', ['scripts:build', 'styles:build'], 'html', cb);
});
gulp.task('clean', function(cb) {
del(['.tmp', 'dist'], cb);
});
gulp.task('html', function() {
var cssFilter = filter(['**/*.css'], {restore: true});
var htmlFilter = filter(['*.html'], {restore: true});
var jsFilter = filter(['**/*.js'], {restore: true});
var notHtmlFilter = filter(['**/*', '!**/*.html', '!**/*.map'], {restore: true});
var minifyHtmlOpts = { empty : true, comments : true };
return gulp.src('src/*.html')
.pipe(useref({}, lazypipe().pipe(function() {
return sourcemaps.init({loadMaps: true});
})))
.pipe(cssFilter)
.pipe(minifyCss())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('dist'))
.pipe(cssFilter.restore)
.pipe(jsFilter)
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('dist'))
.pipe(jsFilter.restore)
.pipe(notHtmlFilter)
.pipe(rev())
.pipe(notHtmlFilter.restore)
.pipe(revReplace())
.pipe(htmlFilter)
.pipe(minifyHtml(minifyHtmlOpts))
.pipe(htmlFilter.restore)
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('dist'))
.pipe(size());
});
gulp.task('scripts', function() {
return bundleScripts(true, true);
});
gulp.task('styles', function() {
return gulp.src('src/assets/**/*.css')
.pipe(concatCss('bundle.css'))
.pipe(gulp.dest('.tmp'))
.pipe(browserSync.stream({once: true}));
});
gulp.task('styles:build', function() {
return gulp.src('assets/**/*.css')
.pipe(concatCss('bundle.css'))
.pipe(gulp.dest('.tmp'));
});
gulp.task('scripts:build', function(cb) {
return bundleScripts(true, false);
});
gulp.task('serve', function(cb) {
runSequence('clean', ['scripts', 'styles'], 'watch', 'browser-sync', cb);
});
gulp.task('test:tdd', function(cb) {
karma.start({
configFile: __dirname + '/test/karma.conf.js',
singleRun: false,
autoWatch: true,
reporters: ['dots'],
}, cb);
});
gulp.task('test', function(cb) {
karma.start({
configFile: __dirname + '/test/karma.conf.js',
singleRun: true,
autoWatch: false,
reporters: ['dots'],
}, cb);
});
gulp.task('watch', function() {
gulp.watch('assets/**/*.css', ['styles']);
});