forked from magul/empatia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
112 lines (102 loc) · 2.9 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
const gulp = require('gulp');
const sass = require('gulp-sass');
const concat = require('gulp-concat');
const minifyCss = require('gulp-minify-css');
const nunjucks = require('gulp-nunjucks');
const data = require('gulp-data');
const htmlmin = require('gulp-html-minifier');
const util = require('gulp-util');
const uglify = require('gulp-uglify');
const toString = require('./gulp-toString');
const connect = require('gulp-connect');
const svgSprite = require('gulp-svg-sprite');
const autoprefixer = require('gulp-autoprefixer');
const streamqueue = require('streamqueue');
const purecssModules = require('./purecss-config');
const path = require('path');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const PATH_DEST = './dest';
const FILE_NAME_CSS = 'styles.css';
const FILE_NAME_JAVASCRIPT = 'index.js';
const SVG_OPTIONS = {
mode: {
symbol: true
}
};
const SASS_INCLUDE_PATHS = [
'./node_modules/hamburgers/_sass/hamburgers',
'./node_modules/spinkit/scss/spinners'
];
let livereloadEnabled = false;
const purecss = () => gulp.src(purecssModules);
const styles = () => gulp
.src('./src/scss/*.scss')
.pipe(sass({ includePaths: SASS_INCLUDE_PATHS }))
.on('error', sass.logError)
.pipe(autoprefixer({
browsers: ['last 2 versions']
}));
function compileStyles() {
return new Promise((resolve) => {
streamqueue({ objectMode: true }, purecss(), styles())
.pipe(concat({ path: FILE_NAME_CSS }))
.pipe(minifyCss())
.pipe(toString(resolve));
});
}
function compileSvg() {
return new Promise((resolve) => {
gulp.src('./images/*.svg')
.pipe(svgSprite(SVG_OPTIONS))
.pipe(toString(resolve));
});
}
function getData() {
return new Promise((resolve) => {
Promise
.all([
compileStyles(),
compileSvg()
])
.then(([css, svg]) => {
resolve({
css,
svg,
livereloadEnabled,
jsPath: `${path.join(PATH_DEST, FILE_NAME_JAVASCRIPT)}?d=${Date.now()}`,
page: require('./page-data.json')
});
});
});
}
gulp.task('javascript', () =>
browserify('./src/javascript/index.js')
.bundle()
.pipe(source(FILE_NAME_JAVASCRIPT))
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest(PATH_DEST))
.pipe(connect.reload()));
gulp.task('html', () =>
gulp.src('./src/html/index.html')
.pipe(data(getData()))
.pipe(nunjucks.compile())
.on('error', e => util.log(e.message))
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(gulp.dest('./'))
.pipe(connect.reload()));
gulp.task('connect', () => {
connect.server({
root: './',
livereload: true,
port: 8888,
fallback: './index.html'
});
});
gulp.task('dev', ['default', 'connect'], () => {
livereloadEnabled = true;
gulp.watch('./src/**/*', ['default']);
});
gulp.task('default', ['javascript', 'html']);