-
Notifications
You must be signed in to change notification settings - Fork 12
/
gulpfile.js
95 lines (83 loc) · 2.25 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
var path = require('path');
var gulp = require('gulp');
var jsBundler = require('js-bundler');
var minifyCSS = require('gulp-minify-css');
var gzip = require('gulp-gzip');
var htmlTransform = require('html-transform');
var through = require('through2');
var srcOptions = {base: './'};
var outPath = './out';
var production = process.argv.indexOf('--production') !== -1;
function np(file) {
return path.resolve(path.join('node_modules', file));
}
gulp.task('js', function() {
return gulp.src('./js/*.js', srcOptions)
.pipe(jsBundler({
uglify: production,
sourceMap: !production,
noParse: [
np('codemirror-movie/dist/movie.js'),
np('emmet-codemirror/dist/emmet.js'),
np('codemirror/lib/codemirror.js')
]
}))
.pipe(gulp.dest(outPath));
});
gulp.task('css', function() {
return gulp.src('./css/*.css', srcOptions)
.pipe(minifyCSS({processImport: true}))
.pipe(gulp.dest(outPath));
});
gulp.task('files', function() {
return gulp.src('./css/entypo/**', srcOptions)
.pipe(gulp.dest(outPath));
});
gulp.task('html', ['static'], function() {
return gulp.src('./out/**/*.html')
.pipe(htmlTransform.stream({
transformUrl: function(url, file, ctx) {
if (ctx.stats) {
url = '/-/' + ctx.stats.hash + url;
}
return url;
},
mode: 'xhtml',
transform: function() {
return through.obj(function(file, enc, next) {
findScripNodes(file.dom).forEach(function(node) {
// replace & with & since CarbonAds can’t handle entities
node.attribs.src = node.attribs.src.replace(/&/g, '&');
});
next(null, file);
});
}
}))
.pipe(gulp.dest('./out'));
});
gulp.task('full', ['html'], function() {
return gulp.src('./out/**/*.{html,css,js,ico,xml}')
.pipe(gzip({
threshold: '1kb',
gzipOptions: {level: 7}
}))
.pipe(gulp.dest(outPath));
});
gulp.task('watch', function() {
gulp.watch('./css/**/*.css', ['css']);
gulp.watch('./js/**/*.js', ['js']);
});
gulp.task('static', ['css', 'js', 'files']);
gulp.task('default', ['static']);
function findScripNodes(nodes, out) {
out = out || [];
nodes.forEach(function(node) {
if (/^script$/i.test(node.name || '') && node.attribs.src) {
out.push(node);
}
if (node.children) {
findScripNodes(node.children, out);
}
});
return out;
}