-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·140 lines (116 loc) · 3.5 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
'use strict';
var gulp = require('gulp');
var sass = require('gulp-sass');
var prefix = require('gulp-autoprefixer');
var rename = require('gulp-rename');
var svgstore = require('gulp-svgstore');
var inject = require('gulp-inject');
var svgmin = require('gulp-svgmin');
var argv = require('yargs').argv;
var template = require('gulp-template');
var source = require('vinyl-source-stream');
var gulpif = require('gulp-if');
var xtend = require('xtend');
var browserify = require('browserify');
var browserifyInc = require('browserify-incremental');
var execSync = require('child_process').execSync,
commitHash = execSync('git log -n 1', {'encoding': 'UTF8'});
var paths = {
sass: 'sass/{,*/}*.scss',
svgs: 'svg/*.svg'
};
// append ?noCache=<hash-from-git-commit> on app.js and app.css files
var noCache = "?noCache=" + commitHash.split("\n")[0].split(" ")[1].substring(0,8);
// browserify
var debug = !argv.env || 'prod'.indexOf(argv.env) == -1;
debug = false;
var bundler = browserify(xtend(browserifyInc.args, {
debug: debug
}));
var watching = false;
bundler.add('app/index.js');
browserifyInc(bundler, {cacheFile: './cache/browserify-cache.json'});
gulp.task('browserify', function() {
return bundler.bundle()
.on('error', onError)
.pipe(source('app.js'))
//.pipe(gulpif(!debug, streamify(uglify())))
.pipe(gulp.dest(''));
// .pipe(gulpif(watching, reload({ stream: true })));
});
gulp.task('sass', function() {
return gulp.src('sass/index.scss')
.pipe(sass({
sourceComments: 'none'
}))
.on('error', onError)
.pipe(prefix(['last 2 versions', 'ie >= 10']))
.pipe(rename('app.css'))
.pipe(gulp.dest(''));
});
gulp.task('svgstore', function() {
var svgs = gulp
.src(paths.svgs)
.pipe(rename({prefix: 'icon-'}))
// problem with generated SVG that will not follow the stroke
// .pipe(svgmin({
// plugins: [
// {
// cleanupIDs: {
// prefix: prefix + '-',
// minify: true
// }
// },
// {
// removeTitle: true
// }, {
// removeDesc: true
// }
// ]
// }))
.pipe(svgstore({ inlineSvg: true }));
function fileContents(filePath, file) {
return file.contents.toString();
}
return gulp
.src('footer.php')
.pipe(inject(svgs, { transform: fileContents }))
.pipe(template({ killCache: noCache }))
.pipe(rename('footer-compiled.php'))
.pipe(gulp.dest(''));
});
gulp.task('headercache', function() {
return gulp
.src('header.php')
.pipe(template({ killCache: noCache }))
.pipe(rename('header-compiled.php'))
.pipe(gulp.dest(''));
});
gulp.task('default', ['headercache','sass','svgstore', 'browserify'], function() {
gulp.watch(paths.sass, ['sass']);
gulp.watch([paths.svgs, 'footer.php'], ['svgstore']);
gulp.watch(['header.php'], ['headercache']);
gulp.watch('app/{,*/}*.js', ['browserify']);
});
/**
* Utils.
*/
var gutil = require('gulp-util');
var notify = require('gulp-notify');
var isStream = require('isstream');
var os = require('os');
function onError() {
var args = Array.prototype.slice.call(arguments);
// error to notification center with gulp-notify
if ('win32' != os.platform()) {
notify.onError({
title: "Compile Error",
message: "<%= error.message %>"
}).apply(this, args);
}
else {
gutil.log("Compile Error", args[0].message, gutil.colors.red);
}
// keep gulp from hanging on this task
if (isStream(this)) this.emit('end');
}