Skip to content

Commit a5f486f

Browse files
authored
fix bloat in the artifact (#67)
1 parent 28c3475 commit a5f486f

File tree

5 files changed

+145
-18
lines changed

5 files changed

+145
-18
lines changed

.changeset/nasty-turkeys-live.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"unicode-segmenter": patch
3+
---
4+
5+
Fix bloat in the NPM package.
6+
7+
`package.tgz` was mostly bloated by CommonJS interop and sourcemap.
8+
9+
However, sourcemap isn't necessary here as it uses sources as is,
10+
and the CommonJS shouldn't be different.
11+
12+
Now fixed by simpler transpilation for CommoJS entries, and removed sourcemap files.
13+
Also removed inaccessible entries.
14+
15+
So the unpacked total package size has been **down to 135 KB from 250 KB**
16+
17+
Note: Node.js v22 will stabilize `require(ESM)`, which will allow CommonJS projects to use this package without having to maintain separate entries. I'm very excited about that, and looking forward to it becoming more "common". The first major release may consider ending support for CommonJS entries and TypeScript's `"Node"` resolution.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ node_modules/
1717
/*.cjs
1818
/*.cjs.map
1919
/bundle/
20+
*.tgz
2021

2122
lcov.info
2223

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@
3939
},
4040
"sideEffects": [
4141
"./intl-polyfill.js",
42-
"./intl-polyfill.cjs",
43-
"./bundle/intl-polyfill.js"
42+
"./intl-polyfill.cjs"
4443
],
4544
"publishConfig": {
4645
"main": "./index.js",
4746
"types": "./index.d.ts",
4847
"exports": {
4948
".": {
50-
"types": "./grapheme.d.ts",
49+
"types": "./index.d.ts",
5150
"import": "./index.js",
5251
"require": "./index.cjs"
5352
},
@@ -81,16 +80,13 @@
8180
"import": "./intl-polyfill.js",
8281
"require": "./intl-polyfill.cjs"
8382
},
84-
"./bundle/*": "./bundle/*",
8583
"./package.json": "./package.json"
8684
}
8785
},
8886
"files": [
8987
"/*.js",
9088
"/*.cjs",
91-
"/*.cjs.map",
9289
"/*.d.ts",
93-
"/src",
9490
"/licenses"
9591
],
9692
"scripts": {
@@ -114,10 +110,13 @@
114110
"process": false
115111
},
116112
"devDependencies": {
113+
"@babel/core": "^7.26.0",
114+
"@babel/plugin-transform-modules-commonjs": "^7.26.3",
117115
"@changesets/cli": "^2.27.10",
118116
"@formatjs/intl-segmenter": "11.5.7",
119117
"@mitata/counters": "^0.0.1",
120118
"@react-native/metro-babel-transformer": "^0.75.2",
119+
"@types/babel__core": "^7",
121120
"@types/node": "^22.10.1",
122121
"@types/xregexp": "^4.4.0",
123122
"emoji-regex": "10.4.0",

scripts/build-exports.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as path from 'node:path';
22
import * as fs from 'node:fs/promises';
33

4+
import { transformFileAsync } from '@babel/core';
45
import { build } from 'esbuild';
56

67
let rootDir = path.join(import.meta.dirname, '..');
@@ -28,20 +29,45 @@ function rewriteCjs(content) {
2829
),
2930
);
3031

31-
let { outputFiles: cjsOutputs } = await build({
32-
entryPoints: modules.map(src),
33-
outdir: distDir,
34-
outExtension: { '.js': '.cjs' },
35-
format: 'cjs',
36-
treeShaking: true,
37-
write: false,
38-
sourcemap: true,
39-
});
4032
await Promise.all(
41-
cjsOutputs.map(
42-
({ path, text }) => fs.writeFile(path, rewriteCjs(text), 'utf8'),
33+
modules.map(
34+
async module => {
35+
const result = await transformFileAsync(dist(module), {
36+
plugins: [
37+
['@babel/plugin-transform-modules-commonjs', {
38+
loose: true,
39+
strict: true,
40+
lazy: false,
41+
importInterop: 'none',
42+
}],
43+
],
44+
assumptions: {
45+
enumerableModuleMeta: true,
46+
constantReexports: true,
47+
},
48+
});
49+
await fs.writeFile(
50+
dist(module).replace('.js', '.cjs'),
51+
rewriteCjs(result.code),
52+
);
53+
},
4354
),
4455
);
56+
// let { outputFiles: cjsOutputs } = await build({
57+
// entryPoints: modules.map(dist),
58+
// outdir: distDir,
59+
// bundle: false,
60+
// outExtension: { '.js': '.cjs' },
61+
// format: 'cjs',
62+
// platform: 'node',
63+
// write: false,
64+
// sourcemap: true,
65+
// });
66+
// await Promise.all(
67+
// cjsOutputs.map(
68+
// ({ path, text }) => fs.writeFile(path, rewriteCjs(text), 'utf8'),
69+
// ),
70+
// );
4571
}
4672

4773
{

yarn.lock

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ __metadata:
7373
languageName: node
7474
linkType: hard
7575

76-
"@babel/core@npm:^7.25.2":
76+
"@babel/core@npm:^7.25.2, @babel/core@npm:^7.26.0":
7777
version: 7.26.0
7878
resolution: "@babel/core@npm:7.26.0"
7979
dependencies:
@@ -351,6 +351,13 @@ __metadata:
351351
languageName: node
352352
linkType: hard
353353

354+
"@babel/helper-plugin-utils@npm:^7.25.9":
355+
version: 7.25.9
356+
resolution: "@babel/helper-plugin-utils@npm:7.25.9"
357+
checksum: 10c0/483066a1ba36ff16c0116cd24f93de05de746a603a777cd695ac7a1b034928a65a4ecb35f255761ca56626435d7abdb73219eba196f9aa83b6c3c3169325599d
358+
languageName: node
359+
linkType: hard
360+
354361
"@babel/helper-remap-async-to-generator@npm:^7.24.7":
355362
version: 7.24.7
356363
resolution: "@babel/helper-remap-async-to-generator@npm:7.24.7"
@@ -550,6 +557,17 @@ __metadata:
550557
languageName: node
551558
linkType: hard
552559

560+
"@babel/parser@npm:^7.1.0, @babel/parser@npm:^7.20.7":
561+
version: 7.26.3
562+
resolution: "@babel/parser@npm:7.26.3"
563+
dependencies:
564+
"@babel/types": "npm:^7.26.3"
565+
bin:
566+
parser: ./bin/babel-parser.js
567+
checksum: 10c0/48f736374e61cfd10ddbf7b80678514ae1f16d0e88bc793d2b505d73d9b987ea786fc8c2f7ee8f8b8c467df062030eb07fd0eb2168f0f541ca1f542775852cad
568+
languageName: node
569+
linkType: hard
570+
553571
"@babel/parser@npm:^7.13.16, @babel/parser@npm:^7.20.0, @babel/parser@npm:^7.24.7":
554572
version: 7.24.7
555573
resolution: "@babel/parser@npm:7.24.7"
@@ -948,6 +966,18 @@ __metadata:
948966
languageName: node
949967
linkType: hard
950968

969+
"@babel/plugin-transform-modules-commonjs@npm:^7.26.3":
970+
version: 7.26.3
971+
resolution: "@babel/plugin-transform-modules-commonjs@npm:7.26.3"
972+
dependencies:
973+
"@babel/helper-module-transforms": "npm:^7.26.0"
974+
"@babel/helper-plugin-utils": "npm:^7.25.9"
975+
peerDependencies:
976+
"@babel/core": ^7.0.0-0
977+
checksum: 10c0/82e59708f19f36da29531a64a7a94eabbf6ff46a615e0f5d9b49f3f59e8ef10e2bac607d749091508d3fa655146c9e5647c3ffeca781060cdabedb4c7a33c6f2
978+
languageName: node
979+
linkType: hard
980+
951981
"@babel/plugin-transform-named-capturing-groups-regex@npm:^7.0.0":
952982
version: 7.24.7
953983
resolution: "@babel/plugin-transform-named-capturing-groups-regex@npm:7.24.7"
@@ -1364,6 +1394,16 @@ __metadata:
13641394
languageName: node
13651395
linkType: hard
13661396

1397+
"@babel/types@npm:^7.0.0, @babel/types@npm:^7.20.7, @babel/types@npm:^7.26.3":
1398+
version: 7.26.3
1399+
resolution: "@babel/types@npm:7.26.3"
1400+
dependencies:
1401+
"@babel/helper-string-parser": "npm:^7.25.9"
1402+
"@babel/helper-validator-identifier": "npm:^7.25.9"
1403+
checksum: 10c0/966c5242c5e55c8704bf7a7418e7be2703a0afa4d19a8480999d5a4ef13d095dd60686615fe5983cb7593b4b06ba3a7de8d6ca501c1d78bdd233a10d90be787b
1404+
languageName: node
1405+
linkType: hard
1406+
13671407
"@babel/types@npm:^7.24.7":
13681408
version: 7.24.7
13691409
resolution: "@babel/types@npm:7.24.7"
@@ -2246,6 +2286,47 @@ __metadata:
22462286
languageName: node
22472287
linkType: hard
22482288

2289+
"@types/babel__core@npm:^7":
2290+
version: 7.20.5
2291+
resolution: "@types/babel__core@npm:7.20.5"
2292+
dependencies:
2293+
"@babel/parser": "npm:^7.20.7"
2294+
"@babel/types": "npm:^7.20.7"
2295+
"@types/babel__generator": "npm:*"
2296+
"@types/babel__template": "npm:*"
2297+
"@types/babel__traverse": "npm:*"
2298+
checksum: 10c0/bdee3bb69951e833a4b811b8ee9356b69a61ed5b7a23e1a081ec9249769117fa83aaaf023bb06562a038eb5845155ff663e2d5c75dd95c1d5ccc91db012868ff
2299+
languageName: node
2300+
linkType: hard
2301+
2302+
"@types/babel__generator@npm:*":
2303+
version: 7.6.8
2304+
resolution: "@types/babel__generator@npm:7.6.8"
2305+
dependencies:
2306+
"@babel/types": "npm:^7.0.0"
2307+
checksum: 10c0/f0ba105e7d2296bf367d6e055bb22996886c114261e2cb70bf9359556d0076c7a57239d019dee42bb063f565bade5ccb46009bce2044b2952d964bf9a454d6d2
2308+
languageName: node
2309+
linkType: hard
2310+
2311+
"@types/babel__template@npm:*":
2312+
version: 7.4.4
2313+
resolution: "@types/babel__template@npm:7.4.4"
2314+
dependencies:
2315+
"@babel/parser": "npm:^7.1.0"
2316+
"@babel/types": "npm:^7.0.0"
2317+
checksum: 10c0/cc84f6c6ab1eab1427e90dd2b76ccee65ce940b778a9a67be2c8c39e1994e6f5bbc8efa309f6cea8dc6754994524cd4d2896558df76d92e7a1f46ecffee7112b
2318+
languageName: node
2319+
linkType: hard
2320+
2321+
"@types/babel__traverse@npm:*":
2322+
version: 7.20.6
2323+
resolution: "@types/babel__traverse@npm:7.20.6"
2324+
dependencies:
2325+
"@babel/types": "npm:^7.20.7"
2326+
checksum: 10c0/7ba7db61a53e28cac955aa99af280d2600f15a8c056619c05b6fc911cbe02c61aa4f2823299221b23ce0cce00b294c0e5f618ec772aa3f247523c2e48cf7b888
2327+
languageName: node
2328+
linkType: hard
2329+
22492330
"@types/estree@npm:1.0.6":
22502331
version: 1.0.6
22512332
resolution: "@types/estree@npm:1.0.6"
@@ -5820,10 +5901,13 @@ __metadata:
58205901
version: 0.0.0-use.local
58215902
resolution: "unicode-segmenter@workspace:."
58225903
dependencies:
5904+
"@babel/core": "npm:^7.26.0"
5905+
"@babel/plugin-transform-modules-commonjs": "npm:^7.26.3"
58235906
"@changesets/cli": "npm:^2.27.10"
58245907
"@formatjs/intl-segmenter": "npm:11.5.7"
58255908
"@mitata/counters": "npm:^0.0.1"
58265909
"@react-native/metro-babel-transformer": "npm:^0.75.2"
5910+
"@types/babel__core": "npm:^7"
58275911
"@types/node": "npm:^22.10.1"
58285912
"@types/xregexp": "npm:^4.4.0"
58295913
emoji-regex: "npm:10.4.0"

0 commit comments

Comments
 (0)