forked from frctl/mandelbrot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
183 lines (148 loc) · 3.8 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
const {
dest,
parallel,
series,
src,
watch,
} = require('gulp');
const cleanCSS = require('gulp-clean-css');
const gulpif = require('gulp-if');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const autoprefixer = require('gulp-autoprefixer');
const sassGlob = require('gulp-sass-glob');
const stylelint = require('gulp-stylelint');
const uglify = require('gulp-uglify');
const browserify = require('browserify');
const watchify = require('watchify');
const babel = require('babelify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const del = require('del');
//
// JS
//
function compileJs() {
return bundleJs();
}
function cleanJs() {
return del(['./dist/js']);
}
const js = series(cleanJs, compileJs);
exports.js = js;
//
// CSS
//
function createCssSkins(cb) {
const fs = require('fs');
const skins = require('./assets/scss/skins/_skins.json');
for (let skin of skins) {
fs.writeFileSync(`./assets/scss/skins/${skin.name}.scss`,
`
$color-header-background: ${skin.accent};
$color-header-content: ${skin.complement};
$color-link: ${skin.links};
@import "../theme";
@import "../core/all";
@import "../components/**/*.scss";
`);
}
cb();
}
function bundleCss(watch) {
return src('./assets/scss/skins/*.scss')
.pipe(stylelint({
reporters: [{formatter: 'string', console: true}]
}))
.pipe(sassGlob())
.pipe(sass({
includePaths: 'node_modules'
}).on('error', sass.logError))
.pipe(autoprefixer())
.pipe(gulpif(!watch, cleanCSS()))
.pipe(sourcemaps.write('.'))
.pipe(dest('./dist/css'));
}
function compileAndMinifyCss() {
return bundleCss();
}
function compileCss() {
return bundleCss(true);
}
const css = series(createCssSkins, compileAndMinifyCss);
exports.css = css;
//
// Fonts
//
function copyFonts() {
return src('./assets/fonts/**/*')
.pipe(dest('./dist/fonts'));
}
function cleanFonts() {
return del(['./dist/fonts']);
}
const fonts = series(cleanFonts, copyFonts);
exports.fonts = fonts;
//
// Images
//
function copyImg() {
return src('./assets/img/**/*')
.pipe(dest('./dist/img'));
}
function copyFavicon() {
return src('./assets/favicon.ico')
.pipe(dest('./dist'));
}
function cleanImg() {
return del(['./dist/img']);
}
const img = series(cleanImg, parallel(copyImg, copyFavicon));
exports.img = img;
//
// Task sets
//
function watchFiles() {
watch('./assets/scss/**/*.scss', compileCss);
bundleJs(true);
watch('./assets/img/**/*', img);
watch('./assets/fonts/**/*', fonts);
}
const compile = series(css, js, img, fonts);
exports.watch = series(compile, watchFiles);
exports.default = compile;
//
// Utils
//
function bundleJs(watch) {
let bundler = browserify('./assets/js/mandelbrot.js', {
debug: true
}).transform(babel, {
presets: ["es2015"]
});
if (watch) {
bundler = watchify(bundler);
bundler.on('update', function () {
console.log('Rebundling JS....');
rebundle();
});
}
function rebundle() {
let bundle = bundler.bundle()
.on('error', function (err) {
console.error(err.message);
// this.emit('end');
})
.pipe(source('mandelbrot.js'))
.pipe(buffer());
if (!watch) {
bundle.pipe(uglify());
}
bundle.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write('./'))
.pipe(dest('./dist/js'));
return bundle;
}
return rebundle();
}