forked from framework7io/framework7-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
177 lines (162 loc) · 4.92 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
(function(){
'use strict';
var gulp = require('gulp');
var gulpData = require('gulp-data');
var connect = require('gulp-connect');
var open = require('gulp-open');
var less = require('gulp-less');
var gulpPug = require('gulp-pug');
var pug = require('pug');
var path = require('path');
var fs = require('fs');
var del = require('del');
var yaml = require('js-yaml');
var path = require('path');
var iconsManifest = require('./icons/manifest-icons.json');
var useCDN = true;
var cdnPath = '//cdn.framework7.io';
var pkg = require('./package.json');
const highlight = require('./src/highlight');
const releaseMeta = require('./src/release-meta');
// Get src file url
function getSrcFileUrl(file) {
const srcFileUrl = `${pkg.repository.url}/edit/master/src/pug/${file.path.split('/src/pug/')[1]}`;
return {
srcFileUrl: srcFileUrl,
};
}
// Pug Filter
pug.filters['code'] = function (code, { lang } = {}) {
code = code
.replace(/</g, '<')
.replace(/>/g, '>');
code = `<pre><code class="${lang || ''}">${highlight(code, lang)}</pre></code>`;
return code;
}
// Pug YAML Data
function getYamlData(ymlPath) {
var doc = yaml.safeLoad(fs.readFileSync(`./src/pug/${ymlPath}`, 'utf8'));
return doc;
}
/* ==================================================================
Check CDN
================================================================== */
function checkIsLocal(local) {
if (local) local = local.toString().replace('-', '');
if (local === 'local') {
useCDN = false;
}
}
/* ==================================================================
Build
================================================================== */
// Styles
gulp.task('less', function (cb) {
var cbs = 0;
gulp.src(['./src/less/main.less'])
.pipe(less({
paths: [ path.join(__dirname, 'less', 'includes') ]
}))
.pipe(gulp.dest('./css/'))
.pipe(connect.reload())
.on('end', function () {
if (cb) cb();
});
});
// All Pug Pages
function buildPages(cb) {
checkIsLocal(process.argv.slice(3));
var cbs = 0;
var time = Date.now();
console.log(`Starting pug: all`);
gulp.src(['**/*.pug', '!**/_*.pug', '!_*.pug'], { cwd: 'src/pug' })
.pipe(gulpData(getSrcFileUrl))
.pipe(gulpPug({
pug,
pretty: true,
locals: {
release: releaseMeta,
cdn: useCDN ? cdnPath : '',
icons: iconsManifest.icons,
getYamlData,
}
}))
.on('error', (err) => {
console.log(err);
})
.pipe(gulp.dest('./'))
.on('end', () => {
console.log(`Finished pug all in ${Date.now() - time}ms`);
if(cb) cb();
});
}
gulp.task('pug', function (cb) {
buildPages(cb);
});
// Build All
gulp.task('build', ['pug', 'less'], function (cb) {
cb();
});
/* =================================
Watch
================================= */
gulp.task('watch', function () {
checkIsLocal(process.argv.slice(3));
gulp.watch('./src/less/**/*.*', [ 'less' ]);
gulp.watch('./src/pug/**/*.pug', (data) => {
checkIsLocal(process.argv.slice(3));
if (data.type !== 'changed') return;
const filePath = data.path.split('/src/pug/')[1];
// if (filePath.indexOf('react') === 0) return;
if (filePath.indexOf('_') === 0 || filePath.indexOf('_layout.pug') >= 0) {
buildPages();
return;
}
const src = [];
if (filePath.split('/')[1] && filePath.split('/')[1].indexOf('_') === 0) {
src.push(`${filePath.split('/')[0]}/*.pug`);
src.push(`!${filePath.split('/')[0]}/_*.pug`);
} else {
src.push(filePath);
}
var time = Date.now();
console.log(`Starting pug "${src}"`);
gulp.src(src, { cwd: 'src/pug' })
.pipe(gulpData(getSrcFileUrl))
.pipe(gulpPug({
pug,
pretty: true,
locals: {
release: releaseMeta,
cdn: useCDN ? cdnPath : '',
icons: iconsManifest.icons,
getYamlData,
}
}))
.on('error', (err) => {
console.log(err);
})
.pipe(gulp.dest(filePath.split('/')[0] === filePath ? './' : path.parse(filePath).dir))
.pipe(connect.reload())
.on('end', () => {
console.log(`Finished pug "${src}" in ${Date.now() - time}ms`);
});
});
});
/* =================================
Server
================================= */
gulp.task('connect', function () {
return connect.server({
root: [ './' ],
livereload: true,
port:'3000'
});
});
gulp.task('open', function () {
return gulp.src('./index.html').pipe(open({ uri: 'http://localhost:3000/index.html'}));
});
gulp.task('server', [ 'watch', 'connect', 'open' ]);
gulp.task('default', [ 'server' ]);
gulp.task('test', [ 'build' ]);
})();