This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
207 lines (189 loc) · 5.45 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
// include plug-ins:
// SERVE: BrowserSync
const gulp = require("gulp");
const browserSync = require("browser-sync").create();
const { reload } = browserSync;
// html + img
const nunjucksRender = require("gulp-nunjucks-render");
const data = require("gulp-data");
const htmlclean = require("gulp-htmlclean");
const imagemin = require("gulp-imagemin");
// css
const sass = require("gulp-sass");
const sassGlob = require("gulp-sass-glob");
const sourcemaps = require("gulp-sourcemaps");
const postcss = require("gulp-postcss");
const cssnano = require("cssnano");
const postcssPresetEnv = require("postcss-preset-env");
const postcssCustomProperties = require("postcss-custom-properties");
const postcssdiscardDuplicates = require("postcss-discard-duplicates");
const resembleImage = require('postcss-resemble-image').default;
// const purgecss = require('gulp-purgecss'); //https://www.purgecss.com/with-gulp
// const postcsseasingradients = require("postcss-easing-gradients");
const plugins = [
resembleImage(),
postcssPresetEnv({
stage : 0,
autoprefixer: { grid: true },
}),
postcssdiscardDuplicates(),
postcssCustomProperties(),
cssnano(),
];
// other
const fs = require("fs");
const gzip = require('gulp-gzip');
const newer = require("gulp-newer");
const rename = require('gulp-rename');
const compress = require('compression');
const gulpSequence = require('gulp-sequence');
// js
const webpack = require("webpack");
const webpackStream = require("webpack-stream");
const webpackConfig = require("./webpack.config.js");
const webpackProd = require("./webpack.prod.js");
// FOLDERS :
const srcHTML = "src/html/*.njk";
const srcSASS = "src/scss/index.scss";
const srcJS = "src/js/index.js";
const srcIMG = "src/assets/images/**/*";
const outSASS = "src/assets";
const outJS = "src/assets";
const watchHTML = "src/html/**/**/*";
const watchSASS = "src/scss/**/*.scss";
const watchJS = "src/js/**/*.js";
const destHTML = "docs";
const destCSS = "docs/assets";
const destJS = "docs/assets";
const destIMG = "docs/assets/images";
// ========================================
const PORT = 7000;
const PORT_BUILD = 7700;
// ERROR function
function swallowError(error) {
// eslint-disable-next-line no-console
console.log(error.toString());
this.emit("end");
}
function serverStart() {
browserSync.init({
// will not try to determine if am online online: false
online: true,
server: {
baseDir : "./src",
middleware: [compress()],
},
port: `${PORT}`,
// don't auto-reload all browsers following a Browsersync restart
// reloadOnRestart: false,
// open: false,
});
}
function serverForBuild() {
browserSync.init({
online: true,
server: {
baseDir : "./docs",
middleware: [compress()],
},
port : `${PORT_BUILD}`,
reloadOnRestart: true,
});
}
function sassTocss() {
return gulp
.src(srcSASS)
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sassGlob())
.pipe(sass().on("error", sass.logError))
.pipe(rename('app.css'))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(outSASS));
}
function minifyHtml() {
return gulp
.src(srcHTML)
.pipe(nunjucksRender({ path: "src/html" }))
.on("error", swallowError)
.pipe(gulp.dest("./src/"))
.pipe(
htmlclean({
protect: /<!--%fooTemplate\b.*?%-->/g,
edit(html) {
return html.replace(/\begg(s?)\b/gi, "omelet$1");
},
}),
)
.pipe(gulp.dest(destHTML))
.pipe(browserSync.stream());
}
function minifyJs() {
return gulp
.src(srcJS)
.pipe(
webpackStream(webpackConfig),
webpack,
)
.on("error", swallowError)
.pipe(gulp.dest(outJS))
.pipe(browserSync.stream());
}
// function serviceWorker() {
// return gulp
// .src("src/sw.js")
// .pipe(uglify())
// .pipe(gulp.dest("docs"))
// .pipe(browserSync.stream());
// }
function minifyImg() {
return gulp
.src(srcIMG)
.pipe(newer(destIMG))
.pipe(imagemin())
.on("error", swallowError)
.pipe(gulp.dest(destIMG))
.pipe(browserSync.stream());
}
// ----------------build-----------------
function css() {
return gulp
.src("src/assets/app.css")
.pipe(postcss(plugins))
.pipe(gulp.dest(destCSS))
.pipe(gzip())
.pipe(gulp.dest(destCSS));
}
function jsToDocs() {
return gulp
.src("src/assets/app.js")
.pipe(
webpackStream(webpackProd),
webpack,
).on("error", swallowError)
.pipe(gulp.dest(destJS));
}
// watch .....................................
function watchChange() {
gulp.watch(srcIMG, ["minify-img", reload]);
gulp.watch(watchHTML, ["html", reload]);
gulp.watch("src/data.json", ["html", reload]);
gulp.watch(watchSASS, ["sass", reload]);
gulp.watch(watchJS, ["scripts", reload]);
// gulp.watch("src/sw.js", ["sw", reload]);
}
// -----------assign task name-------------
gulp.task("html", minifyHtml);
gulp.task("sass", sassTocss);
gulp.task("scripts", minifyJs);
gulp.task("minify-img", minifyImg);
gulp.task("serve", serverStart);
gulp.task('watch', watchChange);
// gulp.task("sw", serviceWorker);
gulp.task("seq", gulpSequence('html', 'minify-img', 'sass', 'scripts', 'serve'));
// run by npm run gulp: will run this default function
gulp.task("default", ['seq'], watchChange);
gulp.task("buildCss", css);
gulp.task("buildJs", jsToDocs);
gulp.task("buildServer", serverForBuild);
gulp.task("build", gulpSequence('buildCss', 'buildJs', 'buildServer'));
// ...............................................................