Skip to content

Commit 764a64a

Browse files
committed
chore: optimize terser options and code for minify
1 parent cb348f6 commit 764a64a

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ansis",
3-
"version": "4.0.0-rc.2",
3+
"version": "4.0.0-rc.3",
44
"description": "A small and fast library for applying ANSI colors in terminal or browser console",
55
"keywords": [
66
"ansi",

package.npm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"ansis",
3-
"version":"4.0.0-rc.2",
3+
"version":"4.0.0-rc.3",
44
"description":"ANSI color lib",
55
"keywords":["ansi","color","cli"],
66
"license":"ISC",

rollup.config.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,23 @@ import { minify } from 'terser';
1010

1111
// last ECMA version compatible with node.js 12
1212
//const ecma = 2019;
13-
const ecma = 2021;
13+
const ecma = 2020;
1414

1515
const terserOptions = {
1616
ecma,
1717
// https://github.com/terser/terser#compress-options
1818
compress: {
1919
ecma,
20-
passes: 2,
20+
passes: 3,
2121
inline: true,
2222
//module: true, // omit 'use strict'
23+
pure_getters: true,
24+
// use these options to find potential for optimisations in code
25+
//unsafe: true,
26+
//unsafe_comps: true,
2327
},
2428
toplevel: true,
25-
}
29+
};
2630

2731
// use this options only for debugging
2832
const debugTerserOptions = {

src/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ let bgOffset = 10;
1111

1212
let styles = {};
1313
let stylePrototype;
14+
// @preserve
1415
let LF = '\n';
1516

1617
/**
@@ -186,8 +187,8 @@ const Ansis = function(level = detectedLevel) {
186187
let fnRgb = createRgbFn(3, closeCode);
187188
let fnBgRgb = createRgbFn(4, bgCloseCode);
188189

189-
let fnAnsi256 = (code) => esc(`38;5;${code}`, closeCode);
190-
let fnBgAnsi256 = (code) => esc(`48;5;${code}`, bgCloseCode);
190+
let fnAnsi256 = (code) => esc(`38;5;` + code, closeCode);
191+
let fnBgAnsi256 = (code) => esc(`48;5;` + code, bgCloseCode);
191192

192193
if (level === LEVEL_256COLORS) {
193194
fnRgb = createRgb256Fn(fnAnsi256);
@@ -228,7 +229,7 @@ const Ansis = function(level = detectedLevel) {
228229
bgName = 'bg' + name[0].toUpperCase() + name.slice(1);
229230

230231
// exclude bright colors for gray aliases as it is already "bright black"
231-
if (offset < 8) {
232+
if (8 > offset) {
232233
styleData[name + bright] = esc(90 + offset, closeCode);
233234
styleData[bgName + bright] = esc(100 + offset, bgCloseCode);
234235
} else {

src/utils.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export let hexToRgb = (value) => {
1818
let color = /([a-f\d]{3,6})/i.exec(value)?.[1] ?? '';
1919
let len = color.length;
2020

21-
let hex = len === 3
21+
let hex = 3 === len
2222
? color[0] + color[0] + color[1] + color[1] + color[2] + color[2]
2323
// faster than 6 !== len
2424
: 6 ^ len ? '0': color;
@@ -40,7 +40,7 @@ export let hexToRgb = (value) => {
4040
export let rgbToAnsi256 = (r, g, b) => {
4141
// grayscale
4242
if (r === g && g === b) {
43-
if (r < 8) return 16;
43+
if (8 > r) return 16;
4444
if (r > 248) return 231;
4545
return round(((r - 8) * 24) / 247) + 232;
4646
}
@@ -61,20 +61,20 @@ export let rgbToAnsi256 = (r, g, b) => {
6161
export let ansi256To16 = (code) => {
6262
let r, g, b, value, remainder;
6363

64-
if (code < 8) return 30 + code;
65-
if (code < 16) return 90 + (code - 8);
64+
if (8 > code) return 30 + code;
65+
if (16 > code) return 90 + (code - 8);
6666

67-
if (code >= 232) {
68-
// grayscale
69-
r = g = b = (((code - 232) * 10) + 8) / 255;
70-
} else {
67+
if (232 > code) {
7168
code -= 16;
7269
remainder = code % 36;
7370

7471
// (number | 0) is equivalent to Math.floor(number), we are sure that the code is >= 0
7572
r = (code / 36 | 0) / 5;
7673
g = (remainder / 6 | 0) / 5;
7774
b = (remainder % 6) / 5;
75+
} else {
76+
// grayscale
77+
r = g = b = (((code - 232) * 10) + 8) / 255;
7878
}
7979

8080
value = max(r, g, b) * 2;

0 commit comments

Comments
 (0)