/
Gulpfile.js
149 lines (125 loc) · 3.49 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
'use strict';
/* Usage:
*
* gulp [watch]
* For development with live reload (default task).
*
* gulp build
* Development build.
*
* gulp build --production
* TODO: Production-ready build (asset concatenation, minification, ...).
*/
/* Gulp dependencies */
var gulp = require('gulp');
var runSequence = require('gulp4-run-sequence');
var gutil = require('gulp-util');
var del = require('del');
var gulpif = require('gulp-if');
var notify = require('gulp-notify');
var argv = require('yargs').argv;
/* CSS */
var sass = require('gulp-sass');
var bulkSass = require('gulp-sass-bulk-import');
var moduleImporter = require('sass-module-importer');
var autoprefixer = require('gulp-autoprefixer');
var sourcemaps = require('gulp-sourcemaps');
/* Livereload */
var browserSync = require('browser-sync');
function handleError(task) {
return function (err) {
notify.onError({
message: task + ' failed, check the logs..',
sound: false
})(err);
gutil.log(gutil.colors.bgRed(task + ' error:'), gutil.colors.red(err));
this.emit('end');
};
}
/* Arguments: read the `production` env flag. */
var production = !!argv.production;
var sourcePaths = {
stylesheets: "./stylesheets/",
};
var buildPath = "./hawkpost/static/";
var tasks = {};
tasks.browser_sync = function () {
var config = {
proxy: {
target: "http://localhost:8000",
ws: true
},
ghostMode: {
clicks: false,
location: false,
forms: false,
scroll: false
}
};
browserSync(config);
};
tasks.clean = function (cb) {
return del([buildPath + "style.css"], cb);
};
tasks.sass = function () {
return gulp.src(sourcePaths.stylesheets + '*.scss')
/* Sourcemaps + SASS */
.pipe(gulpif(!production, sourcemaps.init()))
.pipe(bulkSass())
.pipe(sass({
importer: moduleImporter(),
sourceComments: !production,
outputStyle: production ? 'compressed' : 'nested'
}))
.on('error', handleError('SASS'))
/* Generate .maps */
.pipe(gulpif(!production, sourcemaps.write({
includeContent: false,
sourceRoot: '.'
})))
.pipe(gulpif(!production, sourcemaps.init({
loadMaps: true
})))
/* Autoprefixer */
.pipe(autoprefixer({
browsers: ['last 2 versions'],
}))
/* We don't serve the source files,
* so include SCSS content inside the sourcemaps. */
.pipe(sourcemaps.write({
includeContent: true
}))
.pipe(gulp.dest(buildPath + 'css/'))
.pipe(browserSync.reload({ stream: true }));
};
/* Compilation tasks */
gulp.task('clean', tasks.clean);
gulp.task('sass', tasks.sass);
/* Assets compilation and reload. */
gulp.task('browser-sync', tasks.browser_sync);
var browserSyncRefresh = browserSync.reload.bind(browserSync);
var browserSyncStream = function () { browserSync.reload({ stream: true }); };
gulp.task('reload-sass', gulp.series('sass'), browserSyncStream);
/* Watch task */
gulp.task('watch', function (cb) {
return runSequence(
'build',
'browser-sync',
function () {
/* Avoiding the compilation of the vendor styles on save, faster faster faster! */
gulp.watch(sourcePaths.stylesheets + '**/*.scss', ['reload-sass']);
gutil.log(gutil.colors.bgGreen('Watching for changes...'));
return cb();
}
);
});
/* Build task */
gulp.task('build', function (cb) {
/* Clean first, build afterwards. */
return runSequence(
'clean', [
'sass',
], cb);
});
/* Default task (`watch`) */
gulp.task('default', gulp.series('watch'));