-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
54 lines (47 loc) · 1.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
"use strict";
const autoprefixer = require('gulp-autoprefixer');
const CleanCSS = require('clean-css');
const gulp = require('gulp');
const htmlmin = require('gulp-htmlmin');
const inline = require('gulp-inline-source');
const uncss = require('uncss');
const nodeSassMagicImporter = require('node-sass-magic-importer');
const rimraf = require('rimraf');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const stylesDestDirectory = 'static/css';
gulp.task('watch', () => {
gulp.watch('./sass/*.scss', ['styles']);
});
gulp.task('styles', ['clean:styles'], () =>
gulp.src('./sass/*.scss')
.pipe(sourcemaps.init())
.pipe(sass({
importer: nodeSassMagicImporter(),
}).on('error', sass.logError))
.pipe(autoprefixer())
.pipe(sourcemaps.write({ sourceRoot: '/scss' }))
.pipe(gulp.dest(stylesDestDirectory))
);
gulp.task('minify:markup', () =>
gulp.src('public/**/*.html')
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(inline({
rootpath: 'public/',
handlers: (source, context, next) => {
if (source.type === 'css' && source.fileContent && !source.content) {
uncss(context.html, { htmlroot: 'public' }, (error, css) => {
if (error) throw error;
source.content = '<style>${new CleanCSS({ level: 2 }).minify(css).styles}</style>';
next();
});
} else {
next();
}
},
}))
.pipe(gulp.dest('public'))
);
gulp.task('clean:styles', () => rimraf.sync(stylesDestDirectory));
gulp.task('build', ['styles', 'minify:markup']);
gulp.task('default', ['watch', 'build']);