-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
142 lines (116 loc) · 3.77 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
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var gulpif = require('gulp-if');
var notify = require('gulp-notify');
var argv = require('yargs').argv;
// sass
var sass = require('gulp-sass');
var autoprefixer = require('autoprefixer-core');
var postcss = require('gulp-postcss');
var sourcemaps = require('gulp-sourcemaps');
// BrowserSync
var browserSync = require('browser-sync');
var htmlInjector = require("bs-html-injector");
// js
var watchify = require('watchify');
var browserify = require('browserify');
// image optimization
var imagemin = require('gulp-imagemin');
// gulp build --production
var production = !!argv.production;
// determine if we're doing a build
// and if so, bypass the livereload
var build = argv._.length ? argv._[0] === 'build' : false;
var watch = argv._.length ? argv._[0] === 'watch' : true;
// Error notification
var handleError = function (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));
};
};
var tasks = {
// --------------------------
// Delete build folder
// --------------------------
clean: function (cb) {
del(['build/'], cb);
},
// --------------------------
// Copy static assets
// --------------------------
assets: function () {
return gulp.src('./client/assets/**/*')
.pipe(gulp.dest('build/assets/'));
},
// --------------------------
// SASS (libsass)
// --------------------------
sass: function () {
return gulp.src('./scss/**/*.scss')
// sourcemaps + sass + error handling
.pipe(gulpif(!production, sourcemaps.init()))
.pipe(sass({
sourceComments: !production,
outputStyle: production ? 'compressed' : 'nested'
}))
.on('error', handleError('SASS'))
// generate .maps
.pipe(gulpif(!production, sourcemaps.write({
'includeContent': false,
'sourceRoot': '.'
})))
// autoprefixer
.pipe(gulpif(!production, sourcemaps.init({
'loadMaps': true
})))
.pipe(postcss([autoprefixer({browsers: ['last 2 versions']})]))
// we don't serve the source files
// so include scss content inside the sourcemaps
.pipe(sourcemaps.write({
'includeContent': true
}))
// write sourcemaps to a specific directory
// give it a file and save
.pipe(gulp.dest('./css'))
.pipe(browserSync.stream());
}
};
gulp.task('browser-sync', function () {
browserSync.use(htmlInjector, {
files: "./*.html"
});
browserSync({
port: process.env.PORT || 3000,
proxy: "awesome.dev"
});
});
gulp.task('reload-sass', ['sass'], function () {
browserSync.reload();
gutil.log(gutil.colors.bgGreen('Reloading SASS...'));
});
gulp.task('reload-js', function(){
browserSync.reload();
gutil.log(gutil.colors.bgGreen('Reloading JS...'));
});
gulp.task('sass', tasks.sass);
// --------------------------
// DEV/WATCH TASK
// --------------------------
gulp.task('watch', ['sass', 'browser-sync'], function () {
// --------------------------
// watch:sass
// --------------------------
gulp.watch('./scss/**/*.scss', ['reload-sass']);
// --------------------------
// watch:js
// --------------------------
gulp.watch('./js/**/*.js', ['reload-js']);
gulp.watch('./*.html', htmlInjector);
gutil.log(gutil.colors.bgGreen('Watching for changes...'));
});
gulp.task('default', ['watch']);