-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
162 lines (147 loc) · 4.37 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
"use strict";
// Load plugins
const autoprefixer = require("autoprefixer");
const browsersync = require("browser-sync").create();
const cp = require("child_process");
const cssnano = require("cssnano");
const del = require("del");
const eslint = require("gulp-eslint");
const gulp = require("gulp");
const imagemin = require("gulp-imagemin");
const newer = require("gulp-newer");
const plumber = require("gulp-plumber");
const postcss = require("gulp-postcss");
const rename = require("gulp-rename");
const sass = require("gulp-sass");
const nunjucksRender = require('gulp-nunjucks-render');
const data = require('gulp-data');
// Put this after including our dependencies
const paths = {
styles: {
src: "scss/*.scss",
dest: "css"
},
dir: {
sourceDir: "./source",
sourcePages: "./source/pages",
sourceSCSS: "./source/assets/scss",
sourceIMG: "./source/assets/images",
sourceJS: "./source/assets/js",
deployDir: "./deploy/public",
deployDirAssets: "./deploy/public/assets"
}
};
// BrowserSync
function browserSync(done) {
browsersync.init({
server: {
baseDir: paths.dir.deployDir
},
port: 8080,
https: {
key: ".localhost-ssl/localhost.key",
cert: ".localhost-ssl/localhost.crt"
}
});
done();
}
// BrowserSync Reload
function browserSyncReload(done) {
browsersync.reload();
done();
}
// Clean assets
function clean() {
return del([paths.dir.deployDir]);
}
// Optimize Images
function images() {
return gulp
.src(paths.dir.sourceIMG + "/**/*")
.pipe(newer(paths.dir.deployDirAssets + "/images"))
.pipe(
imagemin([
imagemin.gifsicle({ interlaced: true }),
imagemin.jpegtran({ progressive: true }),
imagemin.optipng({ optimizationLevel: 5 }),
imagemin.svgo({
plugins: [
{
removeViewBox: false,
collapseGroups: true
}
]
})
])
)
.pipe(gulp.dest(paths.dir.deployDirAssets + "/images"));
}
// CSS task
function css() {
return gulp
.src(paths.dir.sourceSCSS + "/**/*.scss")
.pipe(plumber())
.pipe(sass({ includePaths: ['node_modules/occlss/scss/'], outputStyle: "expanded" }))
.pipe(gulp.dest(paths.dir.deployDirAssets + "/css/"))
.pipe(rename({ suffix: ".min" }))
.pipe(postcss([autoprefixer(), cssnano()]))
.pipe(gulp.dest(paths.dir.deployDirAssets + "/css/"))
.pipe(browsersync.stream());
}
// Lint scripts
function scriptsLint() {
return gulp
.src([paths.dir.sourceJS])
.pipe(plumber())
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
}
// Transpile, concatenate and minify scripts
function scripts() {
return (
gulp
.src([paths.dir.sourceJS + "/**/*"])
.pipe(plumber())
.pipe(gulp.dest(paths.dir.deployDirAssets + "/js/"))
.pipe(browsersync.stream())
);
}
// Nunjucs template system
function nunjucks() {
// Gets .html and .nunjucks files in pages
return gulp.src(paths.dir.sourcePages + '/**/*.+(html|njk)')
// Adding data to Nunjucks
.pipe(data(function() {
return require(paths.dir.sourceDir + '/data/data.json')
}))
// Renders template with nunjucks
.pipe(nunjucksRender({
path: [paths.dir.sourceDir + '/templates']
}))
// output files in app folder
.pipe(gulp.dest(paths.dir.deployDir))
}
// Watch files
function watchFiles() {
gulp.watch(paths.dir.sourceSCSS + "/**/*", css);
gulp.watch(paths.dir.sourceJS + "/**/*", gulp.series(scriptsLint, scripts));
gulp.watch(paths.dir.sourceIMG + "/**/*", images);
gulp.watch(paths.dir.sourcePages + "/**/*.+(html|njk)", nunjucks);
gulp.watch(paths.dir.sourceDir + "/templates/**/*.+(njk)", nunjucks);
gulp.watch(paths.dir.deployDir + "/*.html", browserSyncReload);
gulp.watch(paths.dir.deployDirAssets + "/**/*.*", browserSyncReload);
}
// define complex tasks
const js = gulp.series(scriptsLint, scripts);
const build = gulp.series(clean, gulp.parallel(css, images, js, nunjucks));
const watch = gulp.parallel(watchFiles, nunjucks, browserSync);
// export tasks
exports.nunjucks = nunjucks;
exports.images = images;
exports.css = css;
exports.js = js;
exports.clean = clean;
exports.build = build;
exports.watch = watch;
exports.default = build;