-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·88 lines (79 loc) · 2.23 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
"use strict";
const fs = require("fs");
const path = require("path");
const browserify = require("browserify");
const buffer = require("vinyl-buffer");
const cleanCSS = require('gulp-clean-css');
const data = require("gulp-data");
const del = require("del");
const glob = require("glob");
const gulp = require("gulp");
const htmlmin = require('gulp-htmlmin');
var merge = require('merge-stream');
const nunjucksRender = require("gulp-nunjucks-render");
const sourceStream = require("vinyl-source-stream");
const sourcemaps = require("gulp-sourcemaps");
const uglify = require("gulp-uglify");
const src = "src/"
const dest = "public";
const sharedTemplates = "src/shared/templates";
gulp.task("clean", () => {
return del(`${dest}/**`, {
force: true
});
});
const getContext = file => {
const ctx = `${path.dirname(file.path)}/ctx.json`;
if (fs.existsSync(ctx)) {
return JSON.parse(fs.readFileSync(ctx));
} else {
console.log("No context file at:", ctx)
return {};
}
};
gulp.task("html", () => {
return gulp.src(`${src}**/index.njk`)
.pipe(data(getContext))
.pipe(nunjucksRender({
path: sharedTemplates,
envOptions: {
noCache: true
}
}))
.pipe(htmlmin({
collapseWhitespace: true
}))
.pipe(gulp.dest(dest));
});
gulp.task("js", () => {
const files = glob.sync(`${src}**/*.js`);
return merge(files.map(function (file) {
return browserify({
entries: file
})
.bundle()
.pipe(sourceStream(file.replace(/src\//, "")))
.pipe(buffer())
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest(dest))
}));
});
gulp.task("css", () => {
return gulp.src(`${src}**/index.css`)
.pipe(sourcemaps.init())
.pipe(cleanCSS())
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest(dest));
});
gulp.task("build",
gulp.series(
"clean",
gulp.parallel(
"html",
"css",
"js"
)
)
);