-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfuncs.js
176 lines (163 loc) · 4.67 KB
/
gulpfuncs.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
// -------------------------------------
// Common Gulp functions used by the Library
// CSS projects.
// -------------------------------------
// -------------------------------------
// Modules
// -------------------------------------
// gulp : The streaming build system
// gulp-autoprefixer : Prefix CSS
// gulp-concat : Concatenate files
// gulp-clean-css : Minify CSS
// gulp-load-plugins : Automatically load Gulp plugins
// gulp-rename : Rename files
// gulp-sass : Compile Sass
// gulp-sass-glob : Provide Sass Globbing
// gulp-sass-lint : Lint Sass
// gulp-sourcemaps : Generate sourcemaps
// gulp-watch : Watch stream
// browser-sync : Device and browser testing tool
// del : delete
// gulp-eslint : JavaScript code quality tool
// gulp-uglify : Minify JavaScript with UglifyJS
// -------------------------------------
// $ = Convenient namespace for included modules.
const $ = {};
$.autoprefixer = require("gulp-autoprefixer");
$.cleanCSS = require("gulp-clean-css");
$.color = require("ansi-colors");
$.concat = require("gulp-concat");
$.crypto = require("crypto");
$.esLint = require("gulp-eslint7");
$.fsCache = require("gulp-fs-cache");
$.gulp = require("gulp");
$.log = require("fancy-log");
$.path = require("path");
$.plumber = require("gulp-plumber");
$.rename = require("gulp-rename");
$.sass = require("gulp-sass")(require("sass"));
$.sassGlob = require("gulp-sass-glob");
$.sourcemaps = require("gulp-sourcemaps");
$.stylelint = require("gulp-stylelint");
$.through = require("through2");
$.uglify = require("gulp-uglify");
// Exported functions
const gulpFuncs = {};
gulpFuncs.concatCSS = (sources, destination) => {
return $.gulp
.src(sources)
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.concat($.path.basename(destination)))
.pipe($.sourcemaps.write())
.pipe($.gulp.dest($.path.dirname(destination)));
};
/** Compile SASS files into CSS
* @param {array} sources a list of files to lint and compile
* @param {string} destination the directory into which to put the compiled results
* @return {stream} the Node stream representing the compiled files.
*/
gulpFuncs.lintAndCompileSASS = (sources, destination) => {
return $.gulp
.src(sources)
.pipe(
$.sourcemaps.init({
loadMaps: true,
largeFile: true,
})
) // Sourcemaps in Gulp 4 core is not working as well.
.pipe($.sassGlob())
.pipe(
$.stylelint({
reporters: [{ formatter: "string", console: true }],
})
)
.pipe(
$.sass({
errLogToConsole: true,
outputStyle: "expanded",
})
)
.pipe(
$.autoprefixer({
cascade: false,
})
)
.pipe($.sourcemaps.write("./"))
.pipe($.gulp.dest(destination));
};
// Validate the JavaScript sources
gulpFuncs.jsLint = (sources) => {
return $.gulp
.src(sources)
.pipe($.plumber())
.pipe($.esLint())
.pipe($.esLint.format())
.pipe($.esLint.failAfterError());
};
// Produce minified JavaScript files
gulpFuncs.jsUglify = (sources, destination) => {
return $.gulp
.src(sources)
.pipe($.plumber())
.pipe($.fsCache(".gulp-cache/js"))
.pipe($.uglify())
.pipe($.fsCache(".gulp-cache/js").restore)
.pipe(
$.rename({
suffix: ".min",
})
)
.pipe($.gulp.dest(destination));
};
gulpFuncs.minifyCSS = (sources, destination) => {
return $.gulp
.src(sources)
.pipe(
$.sourcemaps.init({
loadMaps: true,
largeFile: true,
})
)
.pipe(
$.cleanCSS(
{
compatibility: "ie11",
},
(details) => {
details.errors.forEach((msg) => {
$.log.error($.color.red(msg));
});
details.warnings.forEach((msg) => {
$.log.warn($.color.yellow(msg));
});
}
)
)
.pipe(
$.rename({
suffix: ".min",
})
)
.pipe($.sourcemaps.write("./"))
.pipe($.gulp.dest(destination));
};
// Record checksums of all the source files so
// CI/CD can later validate the build products
// against the sources.
gulpFuncs.updateSourceChecksums = (sources, checksumFile) => {
const root = process.cwd();
return $.gulp
.src(sources)
.pipe(
$.through.obj((file, enc, cb) => {
const shasum = $.crypto.createHash("sha256");
shasum.update(file.contents);
const relpath = $.path.relative(root, file.path);
file.contents = Buffer.from(`${shasum.digest("hex")} ${relpath}`);
cb(null, file);
})
)
.pipe($.concat($.path.basename(checksumFile)))
.pipe($.gulp.dest($.path.dirname(checksumFile)));
};
module.exports = gulpFuncs;