-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.js
105 lines (89 loc) · 2.63 KB
/
tool.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
'use strict';
const fs = require('fs');
const path = require('path');
const gzipSize = require('gzip-size');
const sass = require('sass');
const helpers = require('./helpers.js');
const NA = 'N/A';
let redPerfumeCss = function () {
return {
atomizedCss: '',
styleErrors: []
};
};
const relativeAtomizer = path.resolve(__dirname, '..', 'red-perfume-css', 'index.js');
if (fs.existsSync(relativeAtomizer)) {
redPerfumeCss = require(relativeAtomizer);
}
function getSavings (a, b) {
return Math.round(((a / b * 10000) - 10000) * -1) / 100 + '%';
}
function runBenchmark (input, atomizer) {
// Defaults
input = input || '';
atomizer = atomizer || redPerfumeCss;
// Settings
const uglify = true;
const style = 'compressed';
const styleErrors = [];
const customLogger = function (message) {
styleErrors.push(message);
}
// Input
let inputMinified = '';
let inputGzip = 0;
try {
inputMinified = sass.compileString(input, { style }).css;
} catch (error1) {
helpers.throwError('Error minifying input via Sass compile', error1);
}
const inputMinifiedLength = inputMinified?.length || 0;
try {
inputGzip = gzipSize.sync(inputMinified);
} catch (error2) {
helpers.throwError('Error gzipping input CSS', error2);
}
// Atomized
let atomized = {};
let atomizedMinified = '';
let atomizedGzip = 0;
const start = new Date();
try {
atomized = atomizer({ uglify, input, customLogger });
} catch (error3) {
helpers.throwError('Error atomizing input', error3);
}
const end = new Date();
const atomizedCss = atomized.atomizedCss;
const atomizedErrors = atomized.styleErrors?.length || styleErrors?.length || 0;
try {
atomizedMinified = sass.compileString(atomizedCss, { style }).css;
} catch (error4) {
helpers.throwError('Error minifying output via Sass compile', error4);
}
const atomizedMinifiedLength = atomizedMinified.length || 0;
try {
atomizedGzip = gzipSize.sync(atomizedMinified);
} catch (error5) {
helpers.throwError('Error gzipping output', error5);
}
const results = {
atomizedErrors,
timeToAtomizeMs: end - start,
inputBytes: inputMinifiedLength,
atomizedBytes: atomizedMinifiedLength,
savings: getSavings(atomizedMinifiedLength, inputMinifiedLength),
inputGzipBytes: inputGzip,
atomizedGzipBytes: atomizedGzip,
gzipSavings: getSavings(atomizedGzip, inputGzip),
atomizedCss
};
if (atomizedErrors && !atomizedMinifiedLength) {
results.atomizedBytes = NA;
results.savings = NA;
results.atomizedGzipBytes = NA;
results.gzipSavings = NA;
}
return results;
}
module.exports = runBenchmark;