-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
93 lines (82 loc) · 2.68 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
const { src, dest, series, parallel } = require('gulp');
const webpack = require('webpack');
const gulpWebpack = require('webpack-stream');
const clean = require('gulp-clean');
const brotli = require('gulp-brotli');
const gzip = require('gulp-gzip');
const stream = require('stream');
const crypto = require('crypto');
const path = require('path');
function buildDev()
{
var config = require('./webpack.config');
config.mode = 'development';
delete config.optimization;
return src('./src/**/*.ts')
.pipe(gulpWebpack(config, require('webpack')))
.pipe(dest('./dist/'));
}
function buildProd()
{
var config = require('./webpack.config');
config.mode = 'production';
delete config.devtool;
return src('./src/**/*.ts')
.pipe(gulpWebpack(config, webpack))
.pipe(dest('./dist/'));
}
function compressBrotli()
{
return src(['./dist/**/*.js', './dist/**/*.css'])
.pipe(brotli.compress({
extension: 'br',
quality: 11,
}))
.pipe(dest('./dist'));
}
function compressGzip()
{
return src(['./dist/**/*.js', './dist/**/*.css'])
.pipe(gzip())
.pipe(dest('./dist'));
}
function taskClean()
{
return src(['./dist/**/*','!./dist/index.html','!./dist/CookieLaw-Custom.css','!./dist/mockup.jpg', '!./dist/cookielaw.json', '!./dist/fake-analytics.js'], { read: false }).pipe(clean());
}
function buildHashes()
{
const assemblyStream = new stream.Transform({objectMode: true});
assemblyStream._transform = function (file, _, callback)
{
const ext = path.extname(file.path);
const fileName = path.basename(file.path);
const sha = crypto.createHash('sha384');
sha.update(file.contents);
const hash = sha.digest('base64');
if (/\.js/i.test(ext))
{
console.info(`<script src="${fileName}" integrity="sha384-${hash}" crossorigin="anonymous" referrerpolicy="origin" async></script>`);
}
else
{
console.info(`<link rel="stylesheet" href="${fileName}" integrity="sha384-${hash}" crossorigin="anonymous" referrerpolicy="origin" />`);
}
callback();
}
return assemblyStream;
}
function htmlTags()
{
return src(['./dist/**/*.js', './dist/**/*.css'])
.pipe(buildHashes())
}
exports.clean = taskClean;
exports.buildDev = buildDev;
exports.buildProd = buildProd;
exports.compressBrotli = compressBrotli
exports.compressGzip = compressGzip;
exports.compress = parallel(exports.compressBrotli, exports.compressGzip);
exports.dev = series(exports.clean, buildDev);
exports.prod = series(exports.clean, buildProd, exports.compress, htmlTags);
exports.default = exports.prod;