From 78483142a5715084469c74bd5615f46d57fd2a25 Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Thu, 29 Jan 2026 02:34:25 +0900 Subject: [PATCH 1/6] fix TypeScript Node16 resolution for CommonJS Fixed #120 --- .gitignore | 1 + package.json | 10 ++-------- scripts/build-exports.js | 34 +++++++++++++++++++++++++++++++++- tsconfig.build.json | 3 ++- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index d641a1e..39fe102 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ node_modules/ /scripts/__pycache__/ /*.d.ts +/*.d.cts /*.js /*.js.map /*.cjs diff --git a/package.json b/package.json index 1b700c7..6184384 100644 --- a/package.json +++ b/package.json @@ -51,37 +51,30 @@ "types": "./index.d.ts", "exports": { ".": { - "types": "./index.d.ts", "import": "./index.js", "require": "./index.cjs" }, "./emoji": { - "types": "./emoji.d.ts", "import": "./emoji.js", "require": "./emoji.cjs" }, "./general": { - "types": "./general.d.ts", "import": "./general.js", "require": "./general.cjs" }, "./grapheme": { - "types": "./grapheme.d.ts", "import": "./grapheme.js", "require": "./grapheme.cjs" }, "./utils": { - "types": "./utils.d.ts", "import": "./utils.js", "require": "./utils.cjs" }, "./intl-adapter": { - "types": "./intl-adapter.d.ts", "import": "./intl-adapter.js", "require": "./intl-adapter.cjs" }, "./intl-polyfill": { - "types": "./intl-polyfill.d.ts", "import": "./intl-polyfill.js", "require": "./intl-polyfill.cjs" }, @@ -93,13 +86,14 @@ "/*.js", "/*.cjs", "/*.d.ts", + "/*.d.cts", "/licenses", "/bundle" ], "scripts": { "prepack": "yarn clean && yarn build", "clean": "rimraf -g \"*.js\" \"*.cjs\" \"*.map\" \"*.d.ts\" \"bundle\"", - "build": "node scripts/build-exports.js && tsc -p tsconfig.build.json", + "build": "node scripts/build-exports.js", "test": "node --test --test-reporter spec --test-reporter-destination=stdout", "test:coverage": "yarn test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info", "bundle-stats:emoji": "node benchmark/emoji/bundle-stats.js", diff --git a/scripts/build-exports.js b/scripts/build-exports.js index e9f69b7..b7e7e3b 100644 --- a/scripts/build-exports.js +++ b/scripts/build-exports.js @@ -1,5 +1,6 @@ import * as path from 'node:path'; import * as fs from 'node:fs/promises'; +import { spawn } from 'node:child_process'; import { transformFileAsync } from '@babel/core'; import { build } from 'esbuild'; @@ -7,7 +8,7 @@ import { build } from 'esbuild'; let rootDir = path.join(import.meta.dirname, '..'); let srcDir = path.join(rootDir, 'src'); let distDir = path.join(rootDir, ''); -let bundleDir = path.join(distDir, 'bundles'); +let bundleDir = path.join(distDir, 'bundle'); await fs.mkdir(distDir, { recursive: true }); @@ -23,12 +24,14 @@ function rewriteCjs(content) { { // use source modules as is + console.log('Copying source files...'); await Promise.all( modules.map( module => fs.copyFile(src(module), dist(module)), ), ); + console.log('Transforming CommonJS entries...'); await Promise.all( modules.map( async module => { @@ -71,6 +74,8 @@ function rewriteCjs(content) { } { + console.log('Build browser bundles...'); + let bundleEntryPoints = [ src('index.js'), src('emoji.js'), @@ -101,3 +106,30 @@ function rewriteCjs(content) { sourcemap: false, }); } + +if (process.argv.includes('--no-tsc')) { + process.exit(0); +} + +{ + console.log('Building type declarations...'); + const { promise, resolve, reject } = Promise.withResolvers(); + spawn( + 'yarn', + ['tsc', '-p', 'tsconfig.build.json'], + { cwd: rootDir, stdio: 'inherit', shell: process.platform === 'win32' } + ) + .on('error', reject) + .on('close', resolve); + + const exitCode = await promise; + if (exitCode !== 0) { + process.exit(exitCode); + } + + console.log('Copying declaration files for CommonJS...'); + for await (const file of fs.glob('*.d.ts', { cwd: rootDir })) { + // Expected they have the same content, but this is still required for TypeScript's Node16 resolution. + await fs.copyFile(dist(file), dist(file.replace('.d.ts', '.d.cts'))); + } +} diff --git a/tsconfig.build.json b/tsconfig.build.json index 682cfea..5c6725e 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -7,7 +7,8 @@ "allowJs": true, "noImplicitAny": true, "declaration": true, - "emitDeclarationOnly": true + "emitDeclarationOnly": true, + "skipLibCheck": true }, "include": [ "./src/**/*" From 94584e4a152972841b6d912254b264fa9a1ee1eb Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Thu, 29 Jan 2026 02:44:46 +0900 Subject: [PATCH 2/6] add changeset --- .changeset/loose-phones-sing.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .changeset/loose-phones-sing.md diff --git a/.changeset/loose-phones-sing.md b/.changeset/loose-phones-sing.md new file mode 100644 index 0000000..e1e66fe --- /dev/null +++ b/.changeset/loose-phones-sing.md @@ -0,0 +1,11 @@ +--- +"unicode-segmenter": patch +--- + +Fix TypeScript Node16 module resolution for CommonJS modules. + +More specifically, the "[Masquerading as CJS](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseCJS.md)" issue has been fixed. + +unicode-segmenter continues to support CommonJS (at least up to v1), so this change slightly increases the size of node_modules because it requires duplicate content. + +Also, pre-bundled files (`unicode-segmenter/bundle/*`) are included for browsers and miniprograms. They were missing in previous versions due to a path typo in the build script. From e2d8d5487e408b15b0134b40e615584caacb3a30 Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Thu, 29 Jan 2026 02:49:24 +0900 Subject: [PATCH 3/6] fix clean script --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6184384..c270c5a 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ ], "scripts": { "prepack": "yarn clean && yarn build", - "clean": "rimraf -g \"*.js\" \"*.cjs\" \"*.map\" \"*.d.ts\" \"bundle\"", + "clean": "rimraf -g \"*.js\" \"*.cjs\" \"*.map\" \"*.d.ts\" \"*.d.cts\" \"bundle\"", "build": "node scripts/build-exports.js", "test": "node --test --test-reporter spec --test-reporter-destination=stdout", "test:coverage": "yarn test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info", From 9c0ce33cb4b3ba47a5a7d4dd042614c153da9384 Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Thu, 29 Jan 2026 02:57:09 +0900 Subject: [PATCH 4/6] use re-export trick to reduce node_modules size --- scripts/build-exports.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/scripts/build-exports.js b/scripts/build-exports.js index b7e7e3b..0818f10 100644 --- a/scripts/build-exports.js +++ b/scripts/build-exports.js @@ -127,9 +127,12 @@ if (process.argv.includes('--no-tsc')) { process.exit(exitCode); } - console.log('Copying declaration files for CommonJS...'); + console.log('Building shims for CommonJS type resolution...'); for await (const file of fs.glob('*.d.ts', { cwd: rootDir })) { - // Expected they have the same content, but this is still required for TypeScript's Node16 resolution. - await fs.copyFile(dist(file), dist(file.replace('.d.ts', '.d.cts'))); + await fs.writeFile( + dist(file.replace('.d.ts', '.d.cts')), + `export * from "./${file.replace('.d.ts', '.js')}";`, + 'utf8', + ); } } From 7f89ff82d995a51e1328a734b137349db9da3093 Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Thu, 29 Jan 2026 02:59:34 +0900 Subject: [PATCH 5/6] update changeset --- .changeset/loose-phones-sing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/loose-phones-sing.md b/.changeset/loose-phones-sing.md index e1e66fe..0dcbffd 100644 --- a/.changeset/loose-phones-sing.md +++ b/.changeset/loose-phones-sing.md @@ -4,8 +4,8 @@ Fix TypeScript Node16 module resolution for CommonJS modules. -More specifically, the "[Masquerading as CJS](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseCJS.md)" issue has been fixed. +More specifically, the "[Masquerading as CJS](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseCJS.md)" issue has been fixed by including re-export declaration files. -unicode-segmenter continues to support CommonJS (at least up to v1), so this change slightly increases the size of node_modules because it requires duplicate content. +Due to the library continues to support CommonJS (at least up to v1), this change is necessary and slightly increases the size of node_modules. Also, pre-bundled files (`unicode-segmenter/bundle/*`) are included for browsers and miniprograms. They were missing in previous versions due to a path typo in the build script. From d8810e3a03dd95c277b1eb91da567fe0bfb9b2b7 Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Thu, 29 Jan 2026 03:02:26 +0900 Subject: [PATCH 6/6] chore --- scripts/build-exports.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/scripts/build-exports.js b/scripts/build-exports.js index 0818f10..27fa364 100644 --- a/scripts/build-exports.js +++ b/scripts/build-exports.js @@ -107,11 +107,7 @@ function rewriteCjs(content) { }); } -if (process.argv.includes('--no-tsc')) { - process.exit(0); -} - -{ +if (!process.argv.includes('--no-tsc')) { console.log('Building type declarations...'); const { promise, resolve, reject } = Promise.withResolvers(); spawn(