-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
141 lines (119 loc) · 3.62 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
var gulp = require('gulp');
var browserSync = require('browser-sync');
var runSequence = require('run-sequence');
var plumber = require('gulp-plumber');
var clean = require('gulp-clean');
var uglify = require('gulp-uglifyjs');
var jshint = require('gulp-jshint');
var sass = require('gulp-sass');
var SassString = require('node-sass').types.String;
var notifier = require('node-notifier');
var sourcemaps = require('gulp-sourcemaps');
// Script handling.
gulp.task('scripts:build', function(done) {
// main.min.js
return gulp.src('app/assets/scripts/**/*.js')
.pipe(plumber())
.pipe(uglify('main.min.js', {
outSourceMap: production ? false : true,
}))
.pipe(gulp.dest('dist/assets/scripts'));
});
gulp.task('scripts:lint', function() {
return gulp.src('app/assets/scripts/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
gulp.task('scripts', function(done) {
runSequence('scripts:lint', 'scripts:build', done);
});
// Style handling
gulp.task('compass', function() {
var task = gulp.src('app/assets/styles/main.scss')
.pipe(plumber(function (e) {
notifier.notify({
title: 'Oops! Sass errored:',
message: e.message
});
console.log('Sass error:', e.toString());
if (production) {
process.exit(1);
}
// Allows the watch to continue.
this.emit('end');
}))
if (!production) {
task = task.pipe(sourcemaps.init())
}
task = task.pipe(sass({
outputStyle: production ? 'compressed' : 'expanded',
precision: 10,
functions: {
'urlencode($url)': function (url) {
var v = new SassString();
v.setValue(encodeURIComponent(url.getValue()));
return v;
}
},
includePaths: require('node-bourbon').with('node_modules/jeet')
}));
if (!production) {
task = task.pipe(sourcemaps.write())
}
return task.pipe(gulp.dest('dist/assets/styles'));
});
// Setup browserSync.
gulp.task('browser-sync', function() {
browserSync({
server: {
baseDir: "./dist"
}
});
});
gulp.task('copy:app', function(done) {
return gulp.src(['app/**', '!./app/assets/scripts/**/*', '!./app/assets/styles/**/*'])
.pipe(gulp.dest('dist'));
});
// Main build task
gulp.task('build', function(done) {
runSequence(['copy:app', 'scripts', 'compass'], done);
});
// Build task prod version.
var production = false;
gulp.task('prod', function(done) {
production = true;
runSequence('clean', ['copy:app', 'scripts', 'compass'], done);
});
gulp.task('watch', function() {
gulp.watch('app/assets/styles/**/*.scss', function() {
runSequence('compass', browserReload);
});
gulp.watch('app/assets/scripts/**/*.js', function() {
runSequence('scripts', browserReload);
});
gulp.watch(['app/**/*', '!./app/**/*.scss', '!./app/**/*.js'], function() {
runSequence('copy:app', browserReload);
});
});
gulp.task('clean', function () {
return gulp.src('dist', {read: false})
.pipe(clean());
});
// Default task.
// Builds the website, watches for changes and starts browserSync.
gulp.task('default', function(done) {
runSequence('build', 'watch', 'browser-sync', done);
});
var shouldReload = true;
gulp.task('no-reload', function(done) {
shouldReload = false;
runSequence('build', 'watch', 'browser-sync', done);
});
////////////////////////////////////////////////////////////////////////////////
//------------------------- Helper functions ---------------------------------//
//----------------------------------------------------------------------------//
function browserReload() {
if (shouldReload) {
browserSync.reload();
}
}