-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
140 lines (121 loc) · 3.91 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
const bs = require('browser-sync').create();
const sass = require('gulp-sass');
const gulp = require('gulp');
const plumber = require('gulp-plumber');
const beep = require('beepbeep');
const pug = require('gulp-pug');
const uglify = require('gulp-uglify');
const babel = require('gulp-babel');
const concat = require('gulp-concat');
const uglifyCSS = require('gulp-uglifycss');
const rename = require('gulp-rename');
const Karma = require('karma').Server;
const autoprefixer = require('gulp-autoprefixer');
const annotate = require('gulp-ng-annotate');
'use strict';
const errorHandler = (err) => {
console.log('ERROR', err.message);
console.log('CAUSE', err.cause);
console.log(err);
bs.notify( `Error in ${err.relativePath}:${err.line}`, 5000);
beep();
}
const karmaCfg = {
configFile: __dirname + '/karma.conf.js'
};
const APPNAME = 'musicsearch';
const paths = {
sass: [ './sass/main.sass', './sass/**/*.sass', './www/components/**/*.sass' ],
pug: [ './www/index.pug', './www/components/**/*.pug', './www/includes/*.pug' ],
html: [ './www/index.html', './www/components/**/*.html' ],
scripts: [ './www/js/**/*.js', '!./www/js/external/*', './www/components/**/*.js' ],
barrels: {},
prod: {
scripts: './www/build/prod/',
styles: './www/build/prod/'
}, dev: {
scripts: './www/build/dev/js/',
styles: './www/build/dev/css/'
}
};
paths.barrels.sass = paths.sass[0];
const emptyFn = function(){};
const compilePug = ( target, cb = emptyFn ) => {
return gulp.src(target)
.pipe(plumber({ errorHandler }))
.pipe(pug({ pretty: true }))
// compile file in the origin folder
.pipe(gulp.dest( (file) => file.base ))
.on('end', cb);
}
const transpileJs = (target, cb = emptyFn) => {
return gulp.src(target)
.pipe(plumber({ errorHandler }))
.pipe(babel())
.pipe(rename({dirname: ''}))
.pipe(gulp.dest( paths.dev.scripts ))
.on('end', cb);
}
const onPugChange = (e) => {
// converting absolute in relative path
// the base dir is ./www, removing everything before
const relPath = e.path.slice( e.path.indexOf('www') );
console.log(`pug: ${ relPath } updated`);
compilePug(relPath);
// if the file which triggered the task was included
// in the index we need to compile it for updates to show
compilePug('./www/index.pug', bs.reload);
}
const onJsChange = (e) => {
// converting absolute in relative path
// the base dir is ./www, removing everything before
const relPath = e.path.slice( e.path.indexOf('www') );
console.log(`js: ${ relPath } updated`);
transpileJs(relPath, bs.reload);
}
gulp.task('serve', ['sass', 'pug', 'babel'], function(){
bs.init({ server: './www/' });
gulp.watch(paths.sass, ['sass']);
gulp.watch(paths.pug, onPugChange);
gulp.watch(paths.scripts, onJsChange, bs.reload);
});
// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function(){
const options = {
browsers: ['last 2 versions'],
cascade: false
};
gulp.src(paths.barrels.sass)
.pipe(plumber({ errorHandler }))
.pipe(sass())
.pipe(autoprefixer( options ))
.pipe(gulp.dest( paths.dev.styles ))
.pipe(bs.stream());
});
gulp.task('pug', function() {
compilePug( paths.pug );
});
gulp.task('babel', function() {
transpileJs( paths.scripts );
});
gulp.task('scripts-prod', ['babel'], function(){
gulp.src(`${ paths.dev.scripts }*.js`)
.pipe(plumber({ errorHandler }))
.pipe(babel())
.pipe(concat(`${APPNAME}.bundle.js`))
.pipe(annotate())
.pipe(uglify())
.pipe(gulp.dest(paths.prod.scripts));
});
gulp.task('styles-prod', ['sass'], function(){
gulp.src(`${ paths.dev.styles }main.css`)
.pipe(plumber({ errorHandler }))
.pipe(rename(`${APPNAME}.min.css`))
.pipe(uglifyCSS())
.pipe(gulp.dest(paths.prod.styles));
});
gulp.task('test', function(done) {
new Karma(karmaCfg, done).start();
});
gulp.task('build', ['scripts-prod', 'styles-prod']);
gulp.task('default', ['serve']);