/
gulpfile.js
227 lines (207 loc) · 6.15 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const gulp = require('gulp');
const packages = require('/www/package.json');
const $ = require('gulp-load-plugins')({
config: packages
});
const browserSync = require('browser-sync').create();
const nunjucks = require('nunjucks');
const path = require('path');
let dev = false;
let sourcemapDest = '../sourcemaps';
let src = 'src/',
assets = 'assets/',
docs = 'docs/',
docsTemplates = 'docs/templates/',
docsSrc = 'docs/src/',
docsAssets = 'docs/assets/',
tests = 'tests/',
testsTemplates = 'tests/templates/',
testsSyntax = 'test/tests-syntax/';
let NAMES = {
cssMain: 'main.css',
svgSprites: 'sprites.svg',
};
function errorlog (error) {
console.error.bind(error);
this.emit('end');
}
function requireUncached( $module ) {
delete require.cache[require.resolve( $module )];
return require( $module );
}
// scss to css
gulp.task('test', function (e) {
return gulp.src(tests + 'scss/*.scss')
.pipe($.plumber())
.pipe($.if(dev, $.sourcemaps.init()))
.pipe($.sass({
outputStyle: 'expanded',
precision: 7
}).on('error', $.sass.logError))
.pipe($.if(dev, $.sourcemaps.write(sourcemapDest)))
.pipe(gulp.dest(tests + '/css'));
});
// Inject Task
gulp.task('inject', function () {
let svg4everybody = gulp.src('bower_components/svg4everybody/dist/svg4everybody.legacy.min.js')
.pipe($.uglify());
return gulp.src(templates + 'parts/layout.njk')
.pipe($.inject(svg4everybody, {
starttag: '/* svg4everybody:js */',
endtag: '/* endinject */',
transform: function (filePath, file) {
return file.contents.toString().replace('height:100%;width:100%', '');
}
}))
.pipe(gulp.dest(templates + 'parts'));
});
// Server & Watch Tasks
gulp.task('server', function() {
browserSync.init({
server: {
baseDir: './'
},
ghostMode: {
clicks: false,
forms: false,
scroll: false
},
open: false,
notify: false
});
// yml to json
gulp.watch([docsTemplates + 'code/*.yml'], function (e) {
if (e.type !== 'deleted') {
return gulp.src(e.path)
.pipe($.yaml({space: 2}))
.pipe(gulp.dest(docsTemplates + 'code'));
}
});
// scss to njk
gulp.watch([tests + 'scss/*.scss'], function (e) {
if (e.type !== 'deleted') {
return gulp.src(e.path)
.pipe($.change(function(content) {
return content.replace(/(\/\/=>\s+)/g, '');
}))
.pipe($.rename(function (path) {
path.basename += "-code";
path.extname = '.njk';
}))
.pipe(gulp.dest(testsTemplates + 'code'));
}
});
// njk to html
gulp.watch([testsTemplates + '**/*.njk'], function (e) {
if (e.type !== 'deleted') {
let src, data, dest, dir = path.dirname(e.path);
// if (path.extname(e.path) === '.json') {
// src = dir.replace('/code', '') + '/*.njk';
// }
if (dir.indexOf('parts') >= 0) {
src = dir.replace('/parts', '') + '*.njk';
} else if(dir.indexOf('/code') >= 0 ) {
src = e.path.replace('/code', '').replace('-code', '');
} else {
src = e.path;
}
if (e.path.indexOf('docs/') !== -1) {
data = requireUncached('./' + docsTemplates + 'code/data.json');
dest = docs;
} else {
data = requireUncached('./' + testsTemplates + 'tree.json');
dest = tests;
}
data.is = function (type, obj) {
var clas = Object.prototype.toString.call(obj).slice(8, -1);
return obj !== undefined && obj !== null && clas === type;
};
data.keys = function (obj) {
return Object.keys(obj);
};
data.belongTo = function (str, arr) {
return arr.indexOf(str) !== -1;
};
return gulp.src(src)
.pipe($.plumber())
.pipe($.nunjucks.compile(data, {
watch: true,
noCache: true,
}))
.pipe($.rename(function (path) { path.extname = ".html"; }))
.pipe($.htmltidy({
doctype: 'html5',
wrap: 0,
hideComments: false,
indent: true,
'indent-attributes': false,
'drop-empty-elements': false,
'force-output': true
}))
.pipe(gulp.dest(dest));
}
});
// scss to css
gulp.watch([docs + '**/*.scss', tests + '**/*.scss'], function (e) {
if (e.type !== 'deleted') {
let src,
dest,
pathParsed = path.parse(e.path),
dir = pathParsed.dir,
name = pathParsed.name;
if(dir.indexOf('docs/') !== -1) {
if (dir.indexOf('video') !== -1) {
src = e.path;
dest = docsAssets + 'css/video';
} else {
src = docsSrc + 'scss/main.scss';
dest = docsAssets + 'css';
}
} else if (dir.indexOf('tests-syntax/') !== -1) {
src = testsSyntax + 'tests.scss';
dest = testsSyntax;
} else {
// src = (name.indexOf('_') === -1) ? e.path : tests + 'scss/*.scss';
src = e.path;
dest = tests + 'css';
}
return gulp.src(src)
.pipe($.plumber())
.pipe($.if(dev, $.sourcemaps.init()))
.pipe($.sass({
outputStyle: 'expanded',
precision: 7
}).on('error', $.sass.logError))
.pipe($.if(dev, $.sourcemaps.write(sourcemapDest)))
.pipe(gulp.dest(dest));
}
});
// docs: svg-sprites
gulp.watch(docsSrc + 'svg/sprites/*.svg', function (e) {
if (e.type !== 'deleted') {
return gulp.src(docsSrc + 'svg/sprites/*.svg')
.pipe($.svgmin(function (file) {
let prefix = path.basename(file.relative, path.extname(file.relative));
return {
plugins: [{
cleanupIDs: {
prefix: prefix + '-',
minify: true
}
}],
// js2svg: { pretty: true }
}
}))
.pipe($.svgstore({ inlineSvg: true }))
.pipe($.rename(NAMES.svgSprites))
.pipe(gulp.dest(docsAssets + 'svg'));
}
});
// reload
gulp.watch(['**/*.html', '**/css/*.css', '**/js/*.js']).on('change', browserSync.reload);
});
// Default Task
gulp.task('default', [
// 'inject',
'server',
]);