-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgulpfile.js
328 lines (293 loc) · 10.6 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
var gulp = require('gulp');
var RevAll = require('gulp-rev-all');
var argv = require('yargs').argv;
var gzip = require('gulp-gzip');
var jsonTransform = require('gulp-json-transform');
var merge = require('merge-stream');
var gulpIgnore = require('gulp-ignore');
var debug = require('gulp-debug');
var runSequence = require('run-sequence');
var del = require('del');
var postcss = require("gulp-postcss");
var uglify = require('gulp-uglify');
var fs = require('fs');
var path = require('path');
var os = require('os');
var {exec} = require('child_process');
var jsedn = require('jsedn');
function run(cmd, cb) {
var p = exec(cmd);
p.stdout.on('data', function (data) {
// Route child process's (cmd) stdout to parent (this script)
process.stdout.write(data.toString());
});
p.stderr.on('data', function (data) {
// Route child process's (cmd) stderr to parent (this script)
process.stderr.write(data.toString());
});
p.on('exit', function (code) {
// Route child process exit sig / error code to callback
cb(code === 0 ? null : ('child process exited with code ' + code.toString()));
});
}
var nodeVersion = process.versions.node.split('.').map((v) => parseInt(v));
var nodeMajorVersion = nodeVersion[0];
if (nodeMajorVersion < 12) {
console.error("Hey, you need to upgrade node to 14.x.x!");
console.error("");
console.error("You seem to be running", nodeVersion);
console.error("");
console.error("This means running the following:");
console.error(" brew upgrade node");
console.error(" rm -rf node_modules");
console.error(" npm install");
console.error("");
console.error("If you installed gulp globally (aka - you never run node_modules/gulp/bin/gulp.js directly), then you need to reinstall that too:");
console.error(" npm uninstall -g gulp");
console.error(" npm install -g gulp");
process.exit(1);
}
function readFile(filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, 'utf8', function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
function writeFile(filename, data) {
return new Promise((resolve, reject) => {
fs.writeFile(filename, data, 'utf8', function(err) {
if (err) {
reject(err);
} else {
resolve(true);
}
});
});
}
function renameFile(oldFilename, newFilename) {
return new Promise((resolve, reject) => {
fs.rename(oldFilename, newFilename, function(err) {
if (err) {
reject(err);
} else {
resolve(true);
}
});
});
}
async function rootJSFiles() {
// probably should be production, but this is probably easier
var config = jsedn.toJS(jsedn.parse(await readFile('dev.cljs.edn')));
let outputDir = config[':output-dir'];
let assetPath = config[':asset-path'].substring(1);
var jsRootFiles = [];
for (let [_, options] of Object.entries(config[':modules'])) {
jsRootFiles.push(assetPath + options[':output-to'].replace(outputDir, ''));
}
return jsRootFiles;
}
exports.css = css;
function css() {
return gulp.src(['./resources/css/*.css'])
.pipe(postcss([
require('postcss-import')(),
require('postcss-custom-media')(),
require('postcss-custom-properties')(),
require('postcss-calc')(),
require('postcss-color-function')(),
require('postcss-discard-comments')(),
require('postcss-inherit'),
require('postcss-inline-svg')(),
require('autoprefixer')({browsers: ['last 3 versions']}),
/* require('postcss-reporter')(), */
/* comment out cssnano to see uncompressed css */
require('cssnano')()
]))
.pipe(gulp.dest('./resources/public/css'));
}
exports.watch = gulp.series(css, watch);
function watch(cb) { // depends on css
gulp.watch(['./resources/css/*.css'], css);
}
exports.default = css;
/* Run this after you update node module versions. */
/* Maybe there's a preferred way of including node modules in cljs projects? */
exports['refresh-deps'] = function refreshDeps(cb) {
run([
"cp",
"./node_modules/jsqr/dist/jsQR.js",
"src-cljs/storefront/"].join(" "), cb);
}
exports['clean-min-js'] = cleanMinJs;
function cleanMinJs() {
return del(['./target/min-js']);
};
exports['minify-js'] = gulp.series(cleanMinJs, minifyJs);
function minifyJs() {
return gulp.src('src-cljs/storefront/*.js')
.pipe(uglify())
.pipe(gulp.dest('target/min-js/'));
}
exports['cljs-build'] = function cljsBuild(cb) {
run('lein cljsbuild once release', cb);
};
exports['copy-release-assets'] = copyReleaseAssets;
function copyReleaseAssets(){
console.log("Copy Release Assets:", __dirname);
return gulp.src(['./target/release/**'])
.pipe(gulp.dest('./resources/public/'));
}
exports['clean-hashed-assets'] = cleanHashedAssets;
function cleanHashedAssets() {
return del(['./resources/public/cdn', './resources/rev-manifest.json']);
}
exports['fix-source-map'] = fixSourceMap;
async function fixSourceMap() {
console.log("fix source dir:", __dirname);
var jsRootFiles = await rootJSFiles();
jsRootFiles = jsRootFiles.map(fn => "resources/public/" + fn + ".map");
await new Promise((resolve, reject) => {
gulp.src(jsRootFiles, {base: './'})
.pipe(jsonTransform(function(data) {
data["sources"] = data["sources"].map(function(f) {
return f.replace("\/", "/");
});
return data;
}))
.pipe(gulp.dest('./'))
.on("end", resolve);
});
}
exports['save-git-sha-version'] = saveGitShaVersion;
function saveGitShaVersion(cb) {
exec("git rev-parse --git-dir", function (code) {
if (code != 0 && !argv.sha)
{
// We are not in a git directory, therefore, we need to be passed a sha
console.error("Current directory is not a git directory nor was a --sha passed.");
// If there is no sha, error
process.exit(code);
}
else if (argv.sha){
console.log(argv.sha);
fs.writeFile('resources/client_version.txt', argv.sha, function (err) {
if (err) return cb(err);
return cb();
});
}
else {
exec('git show --pretty=format:%H -q', function (err, stdout) {
if (err) {
cb(err);
} else {
fs.writeFile('resources/client_version.txt', stdout, function (err) {
if (err) return cb(err);
return cb();
});
}
});
}
});
}
function hashedAssetSources () {
return merge(gulp.src('resources/public/{js,css,images,fonts}/**')
.pipe(gulpIgnore.exclude("*.map")),
gulp.src('resources/public/js/out/*.map'));
}
exports['rev-assets'] = revAssets;
function revAssets() {
if (!argv.host) {
throw "missing --host";
}
var options = {
prefix: "https://" + argv.host + "/cdn/",
includeFilesInManifest: ['.css', '.js', '.svg', '.png', '.gif', '.woff', '.woff2', '.cljs', '.cljc', '.map'],
dontSearchFile: ['.js']
};
return hashedAssetSources()
.pipe(RevAll.revision(options))
.pipe(gulp.dest('resources/public/cdn'))
.pipe(RevAll.manifestFile())
.pipe(gulp.dest('resources'));
}
exports['fix-main-js-pointing-to-source-map'] = fixMainJsPointingToSourceMap;
async function fixMainJsPointingToSourceMap() {
if (!argv.host) {
throw "missing --host";
}
var root = "https://" + argv.host + "/cdn/js/out/";
// because .js files are excluded from search and replace of sha-ed versions (so that
// the js code doesn't become really wrong), we need to take special care to update
// main.js to have the sha-ed version of the sourcemap in the file
var revManifest = JSON.parse(await readFile("resources/rev-manifest.json"));
var jsRootFiles = await rootJSFiles();
let base = "resources/public/cdn/";
await Promise.all(jsRootFiles.map(async (jsKey) => {
var fullJsFile = "resources/public/cdn/" + revManifest[jsKey];
var data = await readFile(fullJsFile);
var result = data.replace(new RegExp(escapeRegExp(path.basename(jsKey + '.map')), 'g'),
path.basename(revManifest[jsKey + '.map']));
await writeFile(fullJsFile, result);
fullJsFile = "resources/public/cdn/" + revManifest[jsKey + '.map'];
var sourceMap = JSON.parse(await readFile(fullJsFile));
sourceMap.sourceRoot = root;
await writeFile(fullJsFile, JSON.stringify(sourceMap));
}));
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
exports['gzip'] = function gzipTask(){
return gulp.src('resources/public/cdn/**')
.pipe(gzip({ append: false }))
.pipe(gulp.dest('resources/public/cdn'));
};
exports['write-js-stats'] = writeJsStats;
function writeJsStats(cb) {
fs.readFile('resources/rev-manifest.json', 'utf8', function(err, data) {
if (err) { cb(err); return console.log(err); }
let revManifest = JSON.parse(data),
mainJsFilePath = "resources/public/cdn/" + revManifest["js/out/main.js"],
cljsBaseFilePath = "resources/public/cdn/" + revManifest["js/out/cljs_base.js"];
exec('wc -c "' + mainJsFilePath + '" "' + cljsBaseFilePath + '" | awk \'{print $1}\' | tail -n 1', function(err, stdout){
if (err) {
cb(err);
} else {
var fileSize = stdout.trim();
exec('(time -p /bin/bash -c \'cat "' + cljsBaseFilePath + '" "' + mainJsFilePath + '" | gunzip -c | node --check\' 2>/dev/null 1>/dev/null) 2>&1 | head -n1 | awk \'{print $2}\'', {shell: '/bin/bash'}, function(err, stdout) {
var parseTime = stdout.trim();
fs.writeFile("resources/main.js.file_size.stat", fileSize, function(err) {
if (err) {
cb(err);
} else {
fs.writeFile("resources/main.js.parse_time.stat", parseTime, function(err) {
if (err) {
cb(err);
} else {
console.log("==== MAIN JS STATS ====");
console.log('File Size: ' + fileSize + ' bytes');
console.log('Relative Parse Time: ' + parseTime + ' seconds');
console.log("=======================");
cb();
}
});
}
});
});
}
});
});
}
exports['cdn'] = gulp.series(cleanHashedAssets, fixSourceMap, revAssets, exports['fix-main-js-pointing-to-source-map'], exports['gzip']);
exports['compile-assets'] = gulp.series(exports['css'],
exports['minify-js'],
exports['cljs-build'],
exports['copy-release-assets'],
exports['cdn'],
exports['save-git-sha-version'],
exports['write-js-stats']);