forked from pypi/warehouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.babel.js
489 lines (406 loc) · 14.7 KB
/
Gulpfile.babel.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import cssnano from "cssnano";
import debounce from "debounce";
import del from "del";
import gulp from "gulp";
import brotli from "gulp-brotli";
import gulpConcat from "gulp-concat";
import gzip from "gulp-gzip";
import gulpImage from "gulp-image";
import postcss from "gulp-postcss";
import manifest from "gulp-rev-all";
import manifestClean from "gulp-rev-napkin";
import gulpSass from "gulp-sass";
import sourcemaps from "gulp-sourcemaps";
import composer from "gulp-uglify/composer";
import path from "path";
import uglifyjs from "uglify-js";
import named from "vinyl-named";
import webpack from "webpack";
import gulpWebpack from "webpack-stream";
// Configure where our files come from, where they get saved too, and what path
// they are served from.
let staticPrefix = "warehouse/static/";
let distPath = path.join(staticPrefix, "dist");
let publicPath = "/static/";
// We pass in our own uglify instance rather than allow
// gulp-uglify to use it's own. This makes the usage slightly
// more complicated, however it means that we have explicit
// control over the exact version of uglify-js used.
var uglify = composer(uglifyjs, console);
// Configure what plugins are used for postcss
var postCSSPlugins = [
cssnano({
preset: ["default", {
discardComments: {
removeAll: true,
},
}],
}),
];
// Configure webpack so that it compiles all of our javascript into a bundle.
let webpackConfig = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
plugins: ["@babel/plugin-proposal-class-properties"],
},
},
},
],
},
plugins: [
new webpack.ProvidePlugin({
"fetch": "imports-loader?this=>global!exports-loader?global.fetch!whatwg-fetch",
"jQuery": "jquery",
"$": "jquery",
}),
],
// We tell it to use an inline source map, but only so that it can be loaded
// by gulp-sourcemaps later on. It will actually be written out to disk and
// *NOT* inlined when all is said and done.
devtool: "inline-source-map",
output: {
publicPath: publicPath + "js/",
filename: "[name].js",
chunkFilename: "chunks/[chunkhash].js",
},
resolve: {
modules: [path.resolve(staticPrefix, "js"), "node_modules"],
alias: {
"clipboard": "clipboard/dist/clipboard",
},
},
};
gulp.task("dist:js", () => {
let files = [
path.join(staticPrefix, "js", "warehouse", "index.js"),
];
return gulp.src(files)
// .pipe(named(() => { return "warehouse"; }))
.pipe(named((file) => {
// Get the filename that is relative to our js directory
let relPath = path.relative(
path.join(staticPrefix, "js"),
file.path
);
// If this is our main application, then we want to call it
// just "warehouse", otherwise, we'll use whatever the real
// name is.
if (relPath == "warehouse/index.js") { return "warehouse"; }
else { return path.parse(relPath).name; }
}))
.pipe(gulpWebpack(webpackConfig, webpack))
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify(
// We don't care about IE6-8 so there's no reason to have
// uglify contain to maintain compatibility for it.
{ compress: { ie8: false }, mangle: { ie8: false } }
))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(path.join(distPath, "js")));
});
gulp.task("dist:noscript", () => {
let sassPath = path.join(staticPrefix, "sass");
return gulp.src(path.join(sassPath, "noscript.scss"))
.pipe(sourcemaps.init())
.pipe(
gulpSass({ includePaths: [sassPath] })
.on("error", gulpSass.logError))
.pipe(postcss(postCSSPlugins))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(path.join(distPath, "css")));
});
gulp.task("dist:admin:fonts", (done) => {
gulp.src("warehouse/admin/static/fonts/*.*")
.pipe(gulp.dest("warehouse/admin/static/dist/fonts"));
gulp.src("warehouse/admin/static/webfonts/*.*")
.pipe(gulp.dest("warehouse/admin/static/dist/webfonts"));
done();
});
gulp.task("dist:admin:css", () => {
let files = [ // Order matters!
"warehouse/admin/static/css/bootstrap.min.css",
"warehouse/admin/static/css/fontawesome.min.css",
"warehouse/admin/static/css/ionicons.min.css",
"warehouse/admin/static/css/AdminLTE.min.css",
"warehouse/admin/static/css/skins/skin-purple.min.css",
];
return gulp.src(files)
.pipe(gulpConcat("all.css"))
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("warehouse/admin/static/dist/css"));
});
gulp.task("dist:admin:js", () => {
return gulp.src("warehouse/admin/static/js/*.js")
.pipe(named(() => { return "admin"; }))
.pipe(gulpWebpack(webpackConfig, webpack))
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(path.join("warehouse/admin/static/dist/js")));
});
gulp.task("dist:admin:compress:gz", () => {
return gulp.src(path.join("warehouse/admin/static/dist", "**", "*"))
.pipe(gzip({
skipGrowingFiles: true,
gzipOptions: { level: 9, memLevel: 9 },
}))
.pipe(gulp.dest("warehouse/admin/static/dist"));
});
gulp.task("dist:admin:compress:br:generic", () => {
let paths = [
path.join("warehouse/admin/static/dist", "fonts", "*.otf"),
path.join("warehouse/admin/static/dist", "fonts", "*.woff"),
path.join("warehouse/admin/static/dist", "fonts", "*.woff2"),
path.join("warehouse/admin/static/dist", "fonts", "*.ttf"),
path.join("warehouse/admin/static/dist", "fonts", "*.eot"),
path.join("warehouse/admin/static/dist", "fonts", "*.svg"),
path.join("warehouse/admin/static/dist", "images", "*.jpg"),
path.join("warehouse/admin/static/dist", "images", "*.png"),
path.join("warehouse/admin/static/dist", "images", "*.svg"),
path.join("warehouse/admin/static/dist", "images", "*.ico"),
];
return gulp.src(paths, { base: "warehouse/admin/static/dist" })
.pipe(brotli.compress({ skipLarger: true, mode: 0, quality: 11 }))
.pipe(gulp.dest("warehouse/admin/static/dist"));
});
gulp.task("dist:admin:compress:br:text", () => {
let paths = [
path.join("warehouse/admin/static/dist", "css", "*.css"),
path.join("warehouse/admin/static/dist", "css", "*.map"),
path.join("warehouse/admin/static/dist", "js", "*.js"),
path.join("warehouse/admin/static/dist", "js", "*.map"),
path.join("warehouse/admin/static/dist", "manifest.json"),
];
return gulp.src(paths, { base: "warehouse/admin/static/dist" })
.pipe(brotli.compress({ skipLarger: true, mode: 1, quality: 11 }))
.pipe(gulp.dest("warehouse/admin/static/dist"));
});
gulp.task(
"dist:admin:compress:br",
gulp.parallel("dist:admin:compress:br:generic", "dist:admin:compress:br:text")
);
gulp.task("dist:admin:compress", gulp.parallel("dist:admin:compress:gz", "dist:admin:compress:br"));
gulp.task("dist:admin:manifest", () => {
let paths = [
// Cachebust our CSS files and the source maps for them.
path.join("warehouse/admin/static/dist", "css", "*.css"),
path.join("warehouse/admin/static/dist", "css", "*.map"),
// Cachebust our Font files.
path.join("warehouse/admin/static/dist", "fonts", "*"),
path.join("warehouse/admin/static/dist", "webfonts", "*"),
// Cachebust our JS files and the source maps for them.
path.join("warehouse/admin/static/dist", "js", "*.js"),
path.join("warehouse/admin/static/dist", "js", "*.map"),
// Cachebust our vendored JS files and the source maps for them.
path.join("warehouse/admin/static/dist", "js", "vendor", "*.js"),
path.join("warehouse/admin/static/dist", "js", "vendor", "*.map"),
// Cachebust our Image files.
path.join("warehouse/admin/static/dist", "images", "*"),
];
return gulp.src(paths, { base: "warehouse/admin/static/dist" })
.pipe(manifest.revision({
fileNameManifest: "manifest.json",
includeFilesInManifest: [
".css",
".map",
".woff",
".woff2",
".svg",
".eot",
".ttf",
".otf",
".png",
".jpg",
".ico",
".js",
],
}))
.pipe(gulp.dest("warehouse/admin/static/dist"))
.pipe(manifestClean({ verbose: false }))
.pipe(manifest.manifestFile())
.pipe(gulp.dest("warehouse/admin/static/dist"));
});
gulp.task("dist:css", () => {
let sassPath = path.join(staticPrefix, "sass");
return gulp.src(path.join(sassPath, "warehouse.scss"))
.pipe(sourcemaps.init())
.pipe(
gulpSass({ includePaths: [sassPath] })
.on("error", gulpSass.logError))
.pipe(postcss(postCSSPlugins))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(path.join(distPath, "css")));
});
gulp.task("dist:fontawesome:css", () => {
let fABasePath = path.dirname(require.resolve("@fortawesome/fontawesome-free/package.json")); // eslint-disable-line no-undef
let fACSSPath = path.resolve(fABasePath, "css", "*.css");
return gulp.src(fACSSPath)
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(postcss(postCSSPlugins))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(path.join(distPath, "css")));
});
gulp.task("dist:fontawesome:fonts", () => {
let fABasePath = path.dirname(require.resolve("@fortawesome/fontawesome-free/package.json")); // eslint-disable-line no-undef
let faFontPath = path.resolve(fABasePath, "webfonts", "*.*");
return gulp.src(faFontPath)
.pipe(gulp.dest(path.join(distPath, "webfonts")));
});
gulp.task("dist:fontawesome", gulp.parallel("dist:fontawesome:css", "dist:fontawesome:fonts"));
gulp.task("dist:images", () => {
return gulp.src(path.join(staticPrefix, "images", "**", "*"))
.pipe(gulpImage({
"svgo": false, // SVGO is currently broken.
}))
.pipe(gulp.dest(path.join(distPath, "images")));
});
gulp.task("dist:vendor", () => {
return gulp.src(path.join(staticPrefix, "js", "vendor", "**", "*"))
.pipe(gulp.dest(path.join(distPath, "js", "vendor")));
});
gulp.task("dist:manifest", () => {
let paths = [
// Cachebust our CSS files and the source maps for them.
path.join(distPath, "css", "*.css"),
path.join(distPath, "css", "*.map"),
// Cachebust our Font files.
path.join(distPath, "fonts", "*"),
path.join(distPath, "webfonts", "*"),
// Cachebust our JS files and the source maps for them.
path.join(distPath, "js", "*.js"),
path.join(distPath, "js", "*.map"),
// Cachebust our vendored JS files and the source maps for them.
path.join(distPath, "js", "vendor", "*.js"),
path.join(distPath, "js", "vendor", "*.map"),
// Cachebust our Image files.
path.join(distPath, "images", "*"),
];
return gulp.src(paths, { base: distPath })
.pipe(manifest.revision({
fileNameManifest: "manifest.json",
includeFilesInManifest: [
".css",
".map",
".woff",
".woff2",
".svg",
".eot",
".ttf",
".otf",
".png",
".jpg",
".ico",
".js",
],
}))
.pipe(gulp.dest(distPath))
.pipe(manifestClean({ verbose: false }))
.pipe(manifest.manifestFile())
.pipe(gulp.dest(distPath));
});
gulp.task("dist:compress:gz", () => {
return gulp.src(path.join(distPath, "**", "*"))
.pipe(gzip({
skipGrowingFiles: true,
gzipOptions: { level: 9, memLevel: 9 },
}))
.pipe(gulp.dest(distPath));
});
gulp.task("dist:compress:br:generic", () => {
let paths = [
path.join(distPath, "fonts", "*.otf"),
path.join(distPath, "fonts", "*.woff"),
path.join(distPath, "fonts", "*.woff2"),
path.join(distPath, "fonts", "*.ttf"),
path.join(distPath, "fonts", "*.eot"),
path.join(distPath, "fonts", "*.svg"),
path.join(distPath, "images", "*.jpg"),
path.join(distPath, "images", "*.png"),
path.join(distPath, "images", "*.svg"),
path.join(distPath, "images", "*.ico"),
];
return gulp.src(paths, { base: distPath })
.pipe(brotli.compress({ skipLarger: true, mode: 0, quality: 11 }))
.pipe(gulp.dest(distPath));
});
gulp.task("dist:compress:br:text", () => {
let paths = [
path.join(distPath, "css", "*.css"),
path.join(distPath, "css", "*.map"),
path.join(distPath, "js", "*.js"),
path.join(distPath, "js", "*.map"),
path.join(distPath, "manifest.json"),
];
return gulp.src(paths, { base: distPath })
.pipe(brotli.compress({ skipLarger: true, mode: 1, quality: 11 }))
.pipe(gulp.dest(distPath));
});
gulp.task(
"dist:compress:br",
gulp.parallel("dist:compress:br:generic", "dist:compress:br:text")
);
gulp.task("dist:compress", gulp.parallel("dist:compress:gz", "dist:compress:br"));
gulp.task("clean", () => { return del([distPath, "warehouse/admin/static/dist"]); });
gulp.task("dist", gulp.series(
// Ensure that we have a good clean base to start out with, by blowing away
// any previously built files.
"clean",
// Build all of our static assets.
gulp.parallel(
"dist:fontawesome",
"dist:css",
"dist:noscript",
"dist:js",
"dist:admin:fonts",
"dist:admin:css",
"dist:admin:js",
"dist:vendor",
),
// We have this here, instead of in the list above even though there is no
// ordering dependency so that all of it's output shows up together which
// makes it easier to read.
"dist:images",
// This has to be on it's own, and it has to be one of the last things we do
// because otherwise we won't catch all of the files in the revisioning
// process.
gulp.parallel("dist:manifest", "dist:admin:manifest"),
// Finally, once we've done everything else, we'll compress everything that
// we've gotten.
gulp.parallel("dist:compress", "dist:admin:compress"),
));
gulp.task("watch", gulp.series(
// We need to build our static files at least once to start.
"dist",
// Finally we can start our watch task, which will watch our static files, and then
// kick off a new dist task whenever it finds changes.
() => {
let watchPaths = [
"warehouse/static/**/*",
"!warehouse/static/dist",
"warehouse/admin/static/**/*",
"!warehouse/admin/static/dist/**/*",
];
gulp.watch(watchPaths, debounce(gulp.series("dist"), 200));
},
));
gulp.task("default", gulp.series("dist"));