Browse Source

Reduces bundle size & updates emoji

- LZ compresses emoji data
main
Eric Amodio 1 year ago
parent
commit
5f5d2da676
8 changed files with 296 additions and 101 deletions
  1. +0
    -1
      .prettierignore
  2. +2
    -1
      package.json
  3. +12
    -10
      scripts/generateEmojiShortcodeMap.mjs
  4. +1
    -0
      src/emojis.compressed.ts
  5. +0
    -1
      src/emojis.json
  6. +7
    -2
      src/emojis.ts
  7. +188
    -0
      src/system/string.ts

+ 0
- 1
.prettierignore View File

@ -1,4 +1,3 @@
emojis.json
git.d.ts
glicons.scss
images/icons/template/icons-contribution.hbs

+ 2
- 1
package.json View File

@ -13330,7 +13330,7 @@
"web:tunnel": "npx localtunnel -p 5000",
"update-dts": "pushd \"src/@types\" && npx vscode-dts dev && popd",
"update-dts:master": "pushd \"src/@types\" && npx vscode-dts master && popd",
"update-emoji": "node ./scripts/generateEmojiShortcodeMap.js",
"update-emoji": "node ./scripts/generateEmojiShortcodeMap.mjs",
"update-licenses": "node ./scripts/generateLicenses.mjs",
"-pretest": "yarn run build:tests",
"vscode:prepublish": "yarn run bundle"
@ -13397,6 +13397,7 @@
"html-webpack-plugin": "5.5.0",
"image-minimizer-webpack-plugin": "3.8.1",
"license-checker-rseidelsohn": "4.1.1",
"lz-string": "github:pieroxy/lz-string#1.4.4",
"mini-css-extract-plugin": "2.7.2",
"mocha": "10.2.0",
"prettier": "2.8.4",

scripts/generateEmojiShortcodeMap.js → scripts/generateEmojiShortcodeMap.mjs View File

@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const https = require('https');
const path = require('path');
import * as fs from 'fs';
import * as https from 'https';
import * as path from 'path';
import LZString from 'lz-string';
async function generate() {
/**
@ -23,8 +23,7 @@ async function generate() {
/**
* @type {Record<string, string | string[]>}}
*/
// eslint-disable-next-line import/no-dynamic-require
const data = require(path.join(process.cwd(), file));
const data = JSON.parse(fs.readFileSync(path.join(process.cwd(), file), 'utf8'));
for (const [emojis, codes] of Object.entries(data)) {
const emoji = emojis
.split('-')
@ -45,15 +44,14 @@ async function generate() {
// Get gitmoji data from https://github.com/carloscuesta/gitmoji
// https://github.com/carloscuesta/gitmoji/blob/master/src/data/gitmojis.json
await download(
'https://raw.githubusercontent.com/carloscuesta/gitmoji/master/src/data/gitmojis.json',
'https://raw.githubusercontent.com/carloscuesta/gitmoji/master/packages/gitmojis/src/gitmojis.json',
'gitmojis.json',
);
/**
* @type {({ code: string; emoji: string })[]}
*/
// eslint-disable-next-line import/no-dynamic-require
const gitmojis = require(path.join(process.cwd(), 'gitmojis.json')).gitmojis;
const gitmojis = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'gitmojis.json'), 'utf8')).gitmojis;
for (const emoji of gitmojis) {
if (emoji.code.startsWith(':') && emoji.code.endsWith(':')) {
emoji.code = emoji.code.substring(1, emoji.code.length - 2);
@ -77,7 +75,11 @@ async function generate() {
return m;
}, Object.create(null));
fs.writeFileSync(path.join(process.cwd(), 'src/emojis.json'), JSON.stringify(map), 'utf8');
fs.writeFileSync(
path.join(process.cwd(), 'src/emojis.compressed.ts'),
`export const emojis = '${LZString.compressToBase64(JSON.stringify(map))}';\n`,
'utf8',
);
}
function download(url, destination) {

+ 1
- 0
src/emojis.compressed.ts
File diff suppressed because it is too large
View File


+ 0
- 1
src/emojis.json
File diff suppressed because it is too large
View File


+ 7
- 2
src/emojis.ts View File

@ -1,7 +1,12 @@
import emojis from './emojis.json';
import { emojis as compressed } from './emojis.compressed';
import { decompressFromBase64LZString } from './system/string';
const emojiRegex = /:([-+_a-z0-9]+):/g;
let emojis: Record<string, string> | undefined = undefined;
export function emojify(message: string) {
return message.replace(emojiRegex, (s, code) => (emojis as Record<string, string>)[code] || s);
if (emojis == null) {
emojis = JSON.parse(decompressFromBase64LZString(compressed));
}
return message.replace(emojiRegex, (s, code) => emojis![code] || s);
}

+ 188
- 0
src/system/string.ts View File

@ -548,3 +548,191 @@ function isFullwidthCodePoint(cp: number) {
return false;
}
// Below adapted from https://github.com/pieroxy/lz-string
const keyStrBase64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const baseReverseDic: Record<string, Record<string, number>> = {};
function getBaseValue(alphabet: string, character: string | number) {
if (!baseReverseDic[alphabet]) {
baseReverseDic[alphabet] = {};
for (let i = 0; i < alphabet.length; i++) {
baseReverseDic[alphabet][alphabet.charAt(i)] = i;
}
}
return baseReverseDic[alphabet][character];
}
export function decompressFromBase64LZString(input: string | undefined) {
if (input == null || input === '') return '';
return (
_decompressLZString(input.length, 32, (index: number) => getBaseValue(keyStrBase64, input.charAt(index))) ?? ''
);
}
function _decompressLZString(length: number, resetValue: any, getNextValue: (index: number) => number) {
const dictionary = [];
let next;
let enlargeIn = 4;
let dictSize = 4;
let numBits = 3;
let entry: any = '';
const result = [];
let i;
let w: any;
let bits;
let resb;
let maxpower;
let power;
let c;
const data = { val: getNextValue(0), position: resetValue, index: 1 };
for (i = 0; i < 3; i += 1) {
dictionary[i] = i;
}
bits = 0;
maxpower = Math.pow(2, 2);
power = 1;
while (power != maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
const fromCharCode = String.fromCharCode;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
switch ((next = bits)) {
case 0:
bits = 0;
maxpower = Math.pow(2, 8);
power = 1;
while (power != maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
c = fromCharCode(bits);
break;
case 1:
bits = 0;
maxpower = Math.pow(2, 16);
power = 1;
while (power != maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
c = fromCharCode(bits);
break;
case 2:
return '';
}
dictionary[3] = c;
w = c;
result.push(c);
while (true) {
if (data.index > length) {
return '';
}
bits = 0;
maxpower = Math.pow(2, numBits);
power = 1;
while (power != maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
switch ((c = bits)) {
case 0:
bits = 0;
maxpower = Math.pow(2, 8);
power = 1;
while (power != maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
dictionary[dictSize++] = fromCharCode(bits);
c = dictSize - 1;
enlargeIn--;
break;
case 1:
bits = 0;
maxpower = Math.pow(2, 16);
power = 1;
while (power != maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
dictionary[dictSize++] = fromCharCode(bits);
c = dictSize - 1;
enlargeIn--;
break;
case 2:
return result.join('');
}
if (enlargeIn == 0) {
enlargeIn = Math.pow(2, numBits);
numBits++;
}
if (dictionary[c]) {
entry = dictionary[c]!;
} else if (c === dictSize) {
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
entry = w + w.charAt(0);
} else {
return undefined;
}
result.push(entry);
// Add w+entry[0] to the dictionary.
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
dictionary[dictSize++] = w + entry.charAt(0);
enlargeIn--;
w = entry;
if (enlargeIn == 0) {
enlargeIn = Math.pow(2, numBits);
numBits++;
}
}
}

Loading…
Cancel
Save