-
Notifications
You must be signed in to change notification settings - Fork 35
/
gulpfile.js
187 lines (159 loc) · 5.33 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
/* globals require, exports */
const gulp = require("gulp");
const parallel = require("gulp").parallel;
const terser = require("gulp-terser");
const csso = require("gulp-csso");
const rename = require("gulp-rename");
const filterStream = require("through2-filter");
const PluginError = require("plugin-error");
const fs = require("fs");
const colors = require("ansi-colors");
const flog = require("fancy-log");
const del = require("del");
const terserOptions = {
"parse": {},
"compress": {
"dead_code": false,
"unused": false
},
"mangle": false,
"output": {},
"sourceMap": false,
"ecma": 6,
"keep_classnames": false,
"keep_fnames": false,
"ie8": false,
"module": false,
"nameCache": null,
"safari10": false,
"toplevel": false,
"warnings": false
};
// options:
// - includedExtensions (String) [required]
// Extension to include when detecting cache busted files,
// should contain the leading "dot".
// - cacheBustingPart (String) [required]
// Part of the filename that is used to detect files' duplicates,
// should contain the leading "dot".
// - deleteDuplicate (Boolean) [default: false]
// If a previous duplicate exists, it will be removed.
// - logChanges (Boolean) [default: false]
// Fill log all file changes to the STDOUT
//
function pluginHandleCacheBusting (options = {}) {
const PLUGIN_NAME = "gulp-handle-cachebusting";
const defaultOpts = {
includedExtensions: undefined,
cacheBustingPart: undefined,
deleteDuplicate: false,
logChanges: false
};
const opts = { ...defaultOpts, ...options };
const perDirPreviousBustedFilesCache = {};
const log = function (...args) {
if (!opts.logChanges) {
return;
}
flog(...args);
};
const stream = filterStream.obj(function (file) {
if (file.isStream()) {
this.emit("error", new PluginError(PLUGIN_NAME, "Streams are not supported!"));
return;
}
if (!file.isBuffer()) {
return;
}
try {
const dirname = file.dirname;
if (!perDirPreviousBustedFilesCache[dirname]) {
const dirExists = fs.existsSync(dirname);
if (dirExists) {
perDirPreviousBustedFilesCache[dirname] = fs.readdirSync(dirname)
.filter(function (filename) {
return filename.endsWith(opts.includedExtensions);
})
.filter(function (filename) {
return filename.includes(opts.cacheBustingPart);
});
} else {
perDirPreviousBustedFilesCache[dirname] = [];
}
}
const dirFiles = perDirPreviousBustedFilesCache[dirname];
const dupFilename = dirFiles.find(function (filename) {
return filename.includes(file.stem);
});
if (!dupFilename) {
log(PLUGIN_NAME + ":", colors.green("✔ ") + file.relative);
return true;
}
const dupFilepath = file.dirname + "/" + dupFilename;
const dupBuffer = fs.readFileSync(dupFilepath);
const isExactDuplicate = file.contents.equals(dupBuffer);
if (!isExactDuplicate) {
log(PLUGIN_NAME + ":", colors.green("✔ ") + file.relative);
}
if (!isExactDuplicate && opts.deleteDuplicate) {
log(PLUGIN_NAME + ":", colors.green("🗑️ ") + dupFilepath);
del.sync([ dupFilepath ]);
}
return (!isExactDuplicate);
} catch (err) {
this.emit("error", new PluginError(PLUGIN_NAME, err));
return;
}
});
return stream;
}
function taskMinifyJS () {
return gulp.src([
"./js/**/**.js",
"!./js/**/**.min.js"
], { base: process.cwd() })
.pipe(terser(terserOptions))
.pipe(rename(function (path) {
path.dirname = "dist/" + path.dirname;
}))
.pipe(pluginHandleCacheBusting({
includedExtensions: ".min.js",
cacheBustingPart: ".cachebuster-",
deleteDuplicate: true,
logChanges: true
}))
.pipe(rename({
extname: (
".cachebuster-" + (+(new Date())) + ".min.js"
)
}))
.pipe(gulp.dest("./"));
}
function taskMinifyCSS () {
const cssoOptions = {
restructure: false,
sourceMap: false,
debug: false
};
return gulp.src([
"./css/**/**.css",
"!./css/**/**.min.css"
], { base: process.cwd() })
.pipe(csso(cssoOptions))
.pipe(rename(function (path) {
path.dirname = "dist/" + path.dirname;
}))
.pipe(pluginHandleCacheBusting({
includedExtensions: ".min.css",
cacheBustingPart: ".cachebuster-",
deleteDuplicate: true,
logChanges: true
}))
.pipe(rename({
extname: (
".cachebuster-" + (+(new Date())) + ".min.css"
)
}))
.pipe(gulp.dest("./"));
}
exports.default = parallel(taskMinifyJS, taskMinifyCSS);