-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
136 lines (120 loc) · 3.61 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
/**
* Usage: gulp [--noclipboard]
*/
// note that HTML is not in the list because both scripts and styles call HTML when they're done
var defaultTasks = ['watch'];
var argv = require('yargs').argv;
var gulp = require('gulp');
var gutil = require('gulp-util');
var gulpif = require('gulp-if');
var rename = require('gulp-rename');
var browserify = require('browserify');
var preprocess = require('gulp-preprocess');
var compass = require('gulp-compass');
var concat = require('gulp-concat');
var watch = require('gulp-watch');
var clipboard = require('gulp-clipboard');
var source = require('vinyl-source-buffer');
console.log('=====================================================================');
console.log(' Tumblr Template Sass - Gulp compilation script ');
console.log('=====================================================================');
console.log('Gulp will compile all assets into ./dist/theme.tumblr');
console.log('The HTML with embedded CSS/JS will be copied to your clipboard (or use --noclipboard)');
console.log('After making a change, edit your theme and "select all" then "paste"');
console.log('=====================================================================');
gulp.task('scripts', function () {
return compileScripts();
});
gulp.task('styles', function () {
return compileStyles();
});
gulp.task('html', ['scripts', 'styles'], function () {
return compileHtml();
});
gulp.task('watch', ['html', 'scripts', 'styles'], watchTask);
gulp.task('default', defaultTasks);
/**
* Compile HTML
*
* 'html' task code
*
* @returns {*}
*/
function compileHtml()
{
var returnObj = gulp.src('theme/templates/main.tumblr')
.pipe(preprocess())
.on('error', watchTask ) // restart watch task on error
//.pipe(gulpif(!(argv['noclipboard']), clipboard()))
//.pipe(rename('theme.tumblr'))
.pipe(gulp.dest('dist/'));
//if (!(argv['noclipboard'])) console.log('Clipboard being prepared... wait for "Finished \'html\'"!');
return returnObj;
}
/**
* Compile Styles
*
* 'styles' task code
*
* @returns {*}
*/
function compileStyles()
{
return gulp.src('theme/sass/*.scss')
.pipe(compass({
config_file: './config.rb',
css: 'build',
sass: 'theme/sass',
import_path: 'node_modules'
}))
.on('error', watchTask)
.pipe(gulp.dest('build/'));
}
/**
* Compile Scripts
*
* 'scripts' task code
*
* @returns {*}
*/
function compileScripts()
{
return browserify('./theme/js/main.js')
.bundle()
.on('error', watchTask) // restart watch task on error
//Pass desired output filename to vinyl-source-stream
.pipe(source('theme.js'))
.pipe(concat('./build/theme.js'))
.pipe(gulp.dest("./"));
}
/**
* Watch
*
* Re-compile styles, scripts, and HTML if anything changes
*
* @returns void
*/
function watchTask(error) {
handleError(error);
watchHtml();
watchScripts();
watchStyles();
}
function watchHtml(error) {
handleError(error);
gulp.watch(['theme/templates/**/*.tumblr'], ['html']);
}
function watchScripts(error) {
handleError(error);
gulp.watch(['theme/js/*.js', 'theme/js/**/*.js', 'theme/libs/**/*.js'], ['html']);
}
function watchStyles(error) {
handleError(error);
gulp.watch(['theme/sass/*.scss', 'theme/sass/**/*.scss'], ['html']);
}
function handleError(error) {
var message = error;
if (typeof error === 'function' ) return;
if (typeof error === 'object' && error.hasOwnProperty('message')) message = error.message;
if (message !== undefined) console.log('Error: ' + message);
}