-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
153 lines (138 loc) · 5.18 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
150
151
152
153
'use strict';
var pkg = require('./package.json'),
autoprefixer = require('gulp-autoprefixer'),
browserify = require('browserify'),
buffer = require('vinyl-buffer'),
chmod = require('gulp-chmod'),
closureCompiler = require('google-closure-compiler').gulp(),
connect = require('gulp-connect'),
csso = require('gulp-csso'),
del = require('del'),
exec = require('gulp-exec'),
ghpages = require('gh-pages'),
gulp = require('gulp'),
gutil = require('gulp-util'),
jade = require('gulp-jade'),
path = require('path'),
plumber = require('gulp-plumber'), // plumber prevents pipe breaking caused by errors thrown by plugins
rename = require('gulp-rename'),
replace = require('gulp-replace'),
source = require('vinyl-source-stream'),
stylus = require('gulp-stylus'),
through = require('through'),
tidy = require('tidy-html5').tidy_html5,
uglify = require('gulp-uglify'),
closureCompilerOpts = {
compilation_level: 'SIMPLE', // ADVANCED breaks bespoke-fullscreen
warning_level: 'QUIET',
language_in: 'ES5_STRICT',
language_out: 'ES5'
},
tidyOpts = {
'anchor-as-name': 'false',
'coerce-endtags': 'false',
'drop-empty-elements': 'false',
'fix-uri': 'false',
'indent': 'false',
'literal-attributes': 'true',
'newline': 'LF',
'preserve-entities': 'true',
'quiet': 'true',
'show-warnings': 'false',
'tidy-mark': 'false',
'wrap': '0'
},
isDist = process.argv.indexOf('deploy') >= 0;
gulp.task('js', ['clean:js'], function() {
// see https://wehavefaces.net/gulp-browserify-the-gulp-y-way-bb359b3f9623
return browserify('src/scripts/main.js').bundle()
// NOTE this error handler fills the role of plumber() when working with browserify
.on('error', function(e) { if (isDist) { throw e; } else { gutil.log(e.stack); this.emit('end'); } })
.pipe(source('src/scripts/main.js'))
.pipe(buffer())
// hack to make bullets array available on deck object
.pipe(replace('activateBullet(0, 0);', 'deck.bullets = bullets; activateBullet(0, 0);'))
.pipe(isDist ? closureCompiler(closureCompilerOpts) : uglify())
.pipe(rename('build.js'))
.pipe(gulp.dest('dist/build'))
.pipe(connect.reload());
});
gulp.task('jade-html', ['clean:jade-html'], function() {
return gulp.src('src/index.jade')
.pipe(isDist ? through() : plumber())
.pipe(jade({ pretty: ' ' }))
.pipe(rename('index-jade.html'))
.pipe(gulp.dest('dist'))
.pipe(connect.reload());
});
gulp.task('asciidoc-html', ['clean:asciidoc-html'], function() {
return gulp.src('src/index.adoc')
.pipe(isDist ? through() : plumber())
// NOTE using stdin here would cause loss of context
.pipe(exec('bundle exec asciidoctor-bespoke -o - src/index.adoc', { pipeStdout: true }))
.pipe(exec.reporter({ stdout: false }))
.pipe(through(function(file) {
var html = tidy(file.contents.toString(), tidyOpts) // NOTE based on tidy 4.9.26
// strip extra newlines inside <pre> tags (fixed in tidy 5.1.2)
.replace(new RegExp('<pre([^>]*)>\\n([\\s\\S]*?)\\n</pre>', 'g'), '<pre$1>$2</pre>\n')
// strip extra newline after <script> start tag for empty and single-line content
.replace(new RegExp('>\\n(?:(.+)\\n)?</script>', 'g'), '>$1</script>')
// add newline before <script> tags
.replace(new RegExp('><script([^>]*)>', 'g'), '>\n<script$1>');
file.contents = new Buffer(html);
this.push(file);
}))
.pipe(rename('index.html'))
.pipe(chmod(644))
.pipe(gulp.dest('dist'))
.pipe(connect.reload());
});
gulp.task('css', ['clean:css'], function() {
return gulp.src('src/styles/main.styl')
.pipe(isDist ? through() : plumber())
.pipe(stylus({ 'include css': true, paths: ['./node_modules'] }))
.pipe(autoprefixer({ browsers: ['last 2 versions'], cascade: false }))
.pipe(isDist ? csso() : through())
.pipe(rename('build.css'))
.pipe(gulp.dest('dist/build'))
.pipe(connect.reload());
});
gulp.task('images', ['clean:images'], function() {
return gulp.src('src/images/**/*')
.pipe(gulp.dest('dist/images'))
.pipe(connect.reload());
});
gulp.task('clean', function() {
return del('dist');
});
gulp.task('clean:jade-html', function() {
return del('dist/index-jade.html');
});
gulp.task('clean:asciidoc-html', function() {
return del('dist/index.html');
});
gulp.task('clean:js', function() {
return del('dist/build/build.js');
});
gulp.task('clean:css', function() {
return del('dist/build/build.css');
});
gulp.task('clean:images', function() {
return del('dist/images');
});
gulp.task('connect', ['build'], function() {
connect.server({ root: 'dist', port: 8000, livereload: true });
});
gulp.task('watch', function() {
gulp.watch('src/**/*.adoc', ['asciidoc-html']);
gulp.watch('src/**/*.jade', ['jade-html']);
gulp.watch('src/scripts/**/*.js', ['js']);
gulp.watch('src/styles/**/*.styl', ['css']);
gulp.watch('src/images/**/*', ['images']);
});
gulp.task('deploy', ['clean', 'build'], function(done) {
ghpages.publish(path.join(__dirname, 'dist'), { logger: gutil.log }, done);
});
gulp.task('build', ['js', 'asciidoc-html', 'jade-html', 'css', 'images']);
gulp.task('serve', ['connect', 'watch']);
gulp.task('default', ['build']);