Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bloat in the artifact #67

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .changeset/nasty-turkeys-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"unicode-segmenter": patch
---

Fix bloat in the NPM package.

`package.tgz` was mostly bloated by CommonJS interop and sourcemap.

However, sourcemap isn't necessary here as it uses sources as is,
and the CommonJS shouldn't be different.

Now fixed by simpler transpilation for CommoJS entries, and removed sourcemap files.
Also removed inaccessible entries.

So the unpacked total package size has been **down to 135 KB from 250 KB**

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.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ node_modules/
/*.cjs
/*.cjs.map
/bundle/
*.tgz

lcov.info

Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@
},
"sideEffects": [
"./intl-polyfill.js",
"./intl-polyfill.cjs",
"./bundle/intl-polyfill.js"
"./intl-polyfill.cjs"
],
"publishConfig": {
"main": "./index.js",
"types": "./index.d.ts",
"exports": {
".": {
"types": "./grapheme.d.ts",
"types": "./index.d.ts",
"import": "./index.js",
"require": "./index.cjs"
},
Expand Down Expand Up @@ -81,16 +80,13 @@
"import": "./intl-polyfill.js",
"require": "./intl-polyfill.cjs"
},
"./bundle/*": "./bundle/*",
"./package.json": "./package.json"
}
},
"files": [
"/*.js",
"/*.cjs",
"/*.cjs.map",
"/*.d.ts",
"/src",
"/licenses"
],
"scripts": {
Expand All @@ -114,10 +110,13 @@
"process": false
},
"devDependencies": {
"@babel/core": "^7.26.0",
"@babel/plugin-transform-modules-commonjs": "^7.26.3",
"@changesets/cli": "^2.27.10",
"@formatjs/intl-segmenter": "11.5.7",
"@mitata/counters": "^0.0.1",
"@react-native/metro-babel-transformer": "^0.75.2",
"@types/babel__core": "^7",
"@types/node": "^22.10.1",
"@types/xregexp": "^4.4.0",
"emoji-regex": "10.4.0",
Expand Down
48 changes: 37 additions & 11 deletions scripts/build-exports.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'node:path';
import * as fs from 'node:fs/promises';

import { transformFileAsync } from '@babel/core';
import { build } from 'esbuild';

let rootDir = path.join(import.meta.dirname, '..');
Expand Down Expand Up @@ -28,20 +29,45 @@ function rewriteCjs(content) {
),
);

let { outputFiles: cjsOutputs } = await build({
entryPoints: modules.map(src),
outdir: distDir,
outExtension: { '.js': '.cjs' },
format: 'cjs',
treeShaking: true,
write: false,
sourcemap: true,
});
await Promise.all(
cjsOutputs.map(
({ path, text }) => fs.writeFile(path, rewriteCjs(text), 'utf8'),
modules.map(
async module => {
const result = await transformFileAsync(dist(module), {
plugins: [
['@babel/plugin-transform-modules-commonjs', {
loose: true,
strict: true,
lazy: false,
importInterop: 'none',
}],
],
assumptions: {
enumerableModuleMeta: true,
constantReexports: true,
},
});
await fs.writeFile(
dist(module).replace('.js', '.cjs'),
rewriteCjs(result.code),
);
},
),
);
// let { outputFiles: cjsOutputs } = await build({
// entryPoints: modules.map(dist),
// outdir: distDir,
// bundle: false,
// outExtension: { '.js': '.cjs' },
// format: 'cjs',
// platform: 'node',
// write: false,
// sourcemap: true,
// });
// await Promise.all(
// cjsOutputs.map(
// ({ path, text }) => fs.writeFile(path, rewriteCjs(text), 'utf8'),
// ),
// );
}

{
Expand Down
86 changes: 85 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ __metadata:
languageName: node
linkType: hard

"@babel/core@npm:^7.25.2":
"@babel/core@npm:^7.25.2, @babel/core@npm:^7.26.0":
version: 7.26.0
resolution: "@babel/core@npm:7.26.0"
dependencies:
Expand Down Expand Up @@ -351,6 +351,13 @@ __metadata:
languageName: node
linkType: hard

"@babel/helper-plugin-utils@npm:^7.25.9":
version: 7.25.9
resolution: "@babel/helper-plugin-utils@npm:7.25.9"
checksum: 10c0/483066a1ba36ff16c0116cd24f93de05de746a603a777cd695ac7a1b034928a65a4ecb35f255761ca56626435d7abdb73219eba196f9aa83b6c3c3169325599d
languageName: node
linkType: hard

"@babel/helper-remap-async-to-generator@npm:^7.24.7":
version: 7.24.7
resolution: "@babel/helper-remap-async-to-generator@npm:7.24.7"
Expand Down Expand Up @@ -550,6 +557,17 @@ __metadata:
languageName: node
linkType: hard

"@babel/parser@npm:^7.1.0, @babel/parser@npm:^7.20.7":
version: 7.26.3
resolution: "@babel/parser@npm:7.26.3"
dependencies:
"@babel/types": "npm:^7.26.3"
bin:
parser: ./bin/babel-parser.js
checksum: 10c0/48f736374e61cfd10ddbf7b80678514ae1f16d0e88bc793d2b505d73d9b987ea786fc8c2f7ee8f8b8c467df062030eb07fd0eb2168f0f541ca1f542775852cad
languageName: node
linkType: hard

"@babel/parser@npm:^7.13.16, @babel/parser@npm:^7.20.0, @babel/parser@npm:^7.24.7":
version: 7.24.7
resolution: "@babel/parser@npm:7.24.7"
Expand Down Expand Up @@ -948,6 +966,18 @@ __metadata:
languageName: node
linkType: hard

"@babel/plugin-transform-modules-commonjs@npm:^7.26.3":
version: 7.26.3
resolution: "@babel/plugin-transform-modules-commonjs@npm:7.26.3"
dependencies:
"@babel/helper-module-transforms": "npm:^7.26.0"
"@babel/helper-plugin-utils": "npm:^7.25.9"
peerDependencies:
"@babel/core": ^7.0.0-0
checksum: 10c0/82e59708f19f36da29531a64a7a94eabbf6ff46a615e0f5d9b49f3f59e8ef10e2bac607d749091508d3fa655146c9e5647c3ffeca781060cdabedb4c7a33c6f2
languageName: node
linkType: hard

"@babel/plugin-transform-named-capturing-groups-regex@npm:^7.0.0":
version: 7.24.7
resolution: "@babel/plugin-transform-named-capturing-groups-regex@npm:7.24.7"
Expand Down Expand Up @@ -1364,6 +1394,16 @@ __metadata:
languageName: node
linkType: hard

"@babel/types@npm:^7.0.0, @babel/types@npm:^7.20.7, @babel/types@npm:^7.26.3":
version: 7.26.3
resolution: "@babel/types@npm:7.26.3"
dependencies:
"@babel/helper-string-parser": "npm:^7.25.9"
"@babel/helper-validator-identifier": "npm:^7.25.9"
checksum: 10c0/966c5242c5e55c8704bf7a7418e7be2703a0afa4d19a8480999d5a4ef13d095dd60686615fe5983cb7593b4b06ba3a7de8d6ca501c1d78bdd233a10d90be787b
languageName: node
linkType: hard

"@babel/types@npm:^7.24.7":
version: 7.24.7
resolution: "@babel/types@npm:7.24.7"
Expand Down Expand Up @@ -2246,6 +2286,47 @@ __metadata:
languageName: node
linkType: hard

"@types/babel__core@npm:^7":
version: 7.20.5
resolution: "@types/babel__core@npm:7.20.5"
dependencies:
"@babel/parser": "npm:^7.20.7"
"@babel/types": "npm:^7.20.7"
"@types/babel__generator": "npm:*"
"@types/babel__template": "npm:*"
"@types/babel__traverse": "npm:*"
checksum: 10c0/bdee3bb69951e833a4b811b8ee9356b69a61ed5b7a23e1a081ec9249769117fa83aaaf023bb06562a038eb5845155ff663e2d5c75dd95c1d5ccc91db012868ff
languageName: node
linkType: hard

"@types/babel__generator@npm:*":
version: 7.6.8
resolution: "@types/babel__generator@npm:7.6.8"
dependencies:
"@babel/types": "npm:^7.0.0"
checksum: 10c0/f0ba105e7d2296bf367d6e055bb22996886c114261e2cb70bf9359556d0076c7a57239d019dee42bb063f565bade5ccb46009bce2044b2952d964bf9a454d6d2
languageName: node
linkType: hard

"@types/babel__template@npm:*":
version: 7.4.4
resolution: "@types/babel__template@npm:7.4.4"
dependencies:
"@babel/parser": "npm:^7.1.0"
"@babel/types": "npm:^7.0.0"
checksum: 10c0/cc84f6c6ab1eab1427e90dd2b76ccee65ce940b778a9a67be2c8c39e1994e6f5bbc8efa309f6cea8dc6754994524cd4d2896558df76d92e7a1f46ecffee7112b
languageName: node
linkType: hard

"@types/babel__traverse@npm:*":
version: 7.20.6
resolution: "@types/babel__traverse@npm:7.20.6"
dependencies:
"@babel/types": "npm:^7.20.7"
checksum: 10c0/7ba7db61a53e28cac955aa99af280d2600f15a8c056619c05b6fc911cbe02c61aa4f2823299221b23ce0cce00b294c0e5f618ec772aa3f247523c2e48cf7b888
languageName: node
linkType: hard

"@types/estree@npm:1.0.6":
version: 1.0.6
resolution: "@types/estree@npm:1.0.6"
Expand Down Expand Up @@ -5820,10 +5901,13 @@ __metadata:
version: 0.0.0-use.local
resolution: "unicode-segmenter@workspace:."
dependencies:
"@babel/core": "npm:^7.26.0"
"@babel/plugin-transform-modules-commonjs": "npm:^7.26.3"
"@changesets/cli": "npm:^2.27.10"
"@formatjs/intl-segmenter": "npm:11.5.7"
"@mitata/counters": "npm:^0.0.1"
"@react-native/metro-babel-transformer": "npm:^0.75.2"
"@types/babel__core": "npm:^7"
"@types/node": "npm:^22.10.1"
"@types/xregexp": "npm:^4.4.0"
emoji-regex: "npm:10.4.0"
Expand Down