Skip to content

Commit

Permalink
Reduce production bundle size (#11)
Browse files Browse the repository at this point in the history
* Reduce production bundle size

* fix fc seed
  • Loading branch information
cometkim authored Apr 15, 2024
1 parent e3fbd6c commit 168319f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 28 deletions.
9 changes: 9 additions & 0 deletions .changeset/silver-coins-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"unicode-segmenter": patch
---

Reduce the production bundle size

Previously I did unescape `"\u{1F680}"` to `"🚀"` in the Unicode table. Since extra characters are required to escape, it reduces the output size.

However, escape sequences compress better. So leaving the build output as is makes more sense for production.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Tested on:

| Name | ESM? | Size | Size (min) | Size (min+gzip) | Size (min+br) |
|------------------------------|------|--------:|-----------------:|-----------------:|-----------------:|
| `unicode-segmenter/grapheme` | ✔️ | 44,669 | 30,698 | 9,696 | 5,917 |
| `unicode-segmenter/grapheme` | ✔️ | 54,383 | 40,412 | 9,586 | 5,321 |
| `graphemer` | ✖️ ️| 410,424 | 95,104 | 15,752 | 10,660 |
| `grapheme-splitter` | ✖️ | 122,241 | 23,680 | 7,852 | 4,841 |

Expand Down
36 changes: 9 additions & 27 deletions scripts/build-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,15 @@ let bundleDir = path.join(distDir, 'bundle');
await fs.mkdir(distDir, { recursive: true });

let src = name => path.join(srcDir, name);
let dist = name => path.join(distDir, name);
let bundle = name => path.join(bundleDir, name);

let modules = await fs.readdir(srcDir);

async function unescapeCodePoints(file) {
let content = await fs.readFile(file, 'utf8');
content = content.replace(/(\\u\{?[0-9a-zA-Z]+\}?)/g, (_match, group) => {
return eval(`"${group}"`);
});
await fs.writeFile(file, content, 'utf8');
}

async function rewriteCjsEntries(file) {
let content = await fs.readFile(file, 'utf8');
content = content.replace(/(require\("(?<id>.*)\.js"\))/g, (_match, _group1, id) => {
/**
* @param {string} content
*/
function rewriteCjsEntries(content) {
return content.replace(/(require\("(?<id>.*)\.js"\))/g, (_match, _group1, id) => {
return `require("${id}.cjs")`;
});
await fs.writeFile(file, content, 'utf8');
}

{
Expand All @@ -42,21 +32,16 @@ async function rewriteCjsEntries(file) {
treeShaking: true,
write: true,
});
await build({
let { outputFiles: cjsOutputFiles = [] } = await build({
entryPoints,
outdir: distDir,
outExtension: { '.js': '.cjs' },
format: 'cjs',
treeShaking: true,
write: true,
write: false,
});
for (let file of await fs.readdir(distDir)) {
if (file.startsWith('_')) {
await unescapeCodePoints(dist(file));
}
if (file.endsWith('.cjs')) {
await rewriteCjsEntries(dist(file));
}
for (let file of cjsOutputFiles) {
await fs.writeFile(file.path, rewriteCjsEntries(file.text), 'utf8');
}
}

Expand Down Expand Up @@ -86,7 +71,4 @@ async function rewriteCjsEntries(file) {
treeShaking: true,
write: true,
});
for (let file of await fs.readdir(bundleDir)) {
await unescapeCodePoints(bundle(file));
}
}
2 changes: 2 additions & 0 deletions test/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import fc from 'fast-check';
import { graphemeSegments } from 'unicode-segmenter/grapheme';

fc.configureGlobal({
// Fix seed here for stable coverage report
seed: 1713140942000,
numRuns: 100_000,
});

Expand Down

0 comments on commit 168319f

Please sign in to comment.