Skip to content

Commit 38a37f2

Browse files
authored
fix TypeScript Node16 resolution for CommonJS (#121)
Fixed #120
1 parent 636ea91 commit 38a37f2

5 files changed

Lines changed: 49 additions & 11 deletions

File tree

.changeset/loose-phones-sing.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"unicode-segmenter": patch
3+
---
4+
5+
Fix TypeScript Node16 module resolution for CommonJS modules.
6+
7+
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.
8+
9+
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.
10+
11+
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.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ node_modules/
1212
/scripts/__pycache__/
1313

1414
/*.d.ts
15+
/*.d.cts
1516
/*.js
1617
/*.js.map
1718
/*.cjs

package.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,30 @@
5151
"types": "./index.d.ts",
5252
"exports": {
5353
".": {
54-
"types": "./index.d.ts",
5554
"import": "./index.js",
5655
"require": "./index.cjs"
5756
},
5857
"./emoji": {
59-
"types": "./emoji.d.ts",
6058
"import": "./emoji.js",
6159
"require": "./emoji.cjs"
6260
},
6361
"./general": {
64-
"types": "./general.d.ts",
6562
"import": "./general.js",
6663
"require": "./general.cjs"
6764
},
6865
"./grapheme": {
69-
"types": "./grapheme.d.ts",
7066
"import": "./grapheme.js",
7167
"require": "./grapheme.cjs"
7268
},
7369
"./utils": {
74-
"types": "./utils.d.ts",
7570
"import": "./utils.js",
7671
"require": "./utils.cjs"
7772
},
7873
"./intl-adapter": {
79-
"types": "./intl-adapter.d.ts",
8074
"import": "./intl-adapter.js",
8175
"require": "./intl-adapter.cjs"
8276
},
8377
"./intl-polyfill": {
84-
"types": "./intl-polyfill.d.ts",
8578
"import": "./intl-polyfill.js",
8679
"require": "./intl-polyfill.cjs"
8780
},
@@ -93,13 +86,14 @@
9386
"/*.js",
9487
"/*.cjs",
9588
"/*.d.ts",
89+
"/*.d.cts",
9690
"/licenses",
9791
"/bundle"
9892
],
9993
"scripts": {
10094
"prepack": "yarn clean && yarn build",
101-
"clean": "rimraf -g \"*.js\" \"*.cjs\" \"*.map\" \"*.d.ts\" \"bundle\"",
102-
"build": "node scripts/build-exports.js && tsc -p tsconfig.build.json",
95+
"clean": "rimraf -g \"*.js\" \"*.cjs\" \"*.map\" \"*.d.ts\" \"*.d.cts\" \"bundle\"",
96+
"build": "node scripts/build-exports.js",
10397
"test": "node --test --test-reporter spec --test-reporter-destination=stdout",
10498
"test:coverage": "yarn test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info",
10599
"bundle-stats:emoji": "node benchmark/emoji/bundle-stats.js",

scripts/build-exports.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import * as path from 'node:path';
22
import * as fs from 'node:fs/promises';
3+
import { spawn } from 'node:child_process';
34

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

78
let rootDir = path.join(import.meta.dirname, '..');
89
let srcDir = path.join(rootDir, 'src');
910
let distDir = path.join(rootDir, '');
10-
let bundleDir = path.join(distDir, 'bundles');
11+
let bundleDir = path.join(distDir, 'bundle');
1112

1213
await fs.mkdir(distDir, { recursive: true });
1314

@@ -23,12 +24,14 @@ function rewriteCjs(content) {
2324

2425
{
2526
// use source modules as is
27+
console.log('Copying source files...');
2628
await Promise.all(
2729
modules.map(
2830
module => fs.copyFile(src(module), dist(module)),
2931
),
3032
);
3133

34+
console.log('Transforming CommonJS entries...');
3235
await Promise.all(
3336
modules.map(
3437
async module => {
@@ -71,6 +74,8 @@ function rewriteCjs(content) {
7174
}
7275

7376
{
77+
console.log('Build browser bundles...');
78+
7479
let bundleEntryPoints = [
7580
src('index.js'),
7681
src('emoji.js'),
@@ -101,3 +106,29 @@ function rewriteCjs(content) {
101106
sourcemap: false,
102107
});
103108
}
109+
110+
if (!process.argv.includes('--no-tsc')) {
111+
console.log('Building type declarations...');
112+
const { promise, resolve, reject } = Promise.withResolvers();
113+
spawn(
114+
'yarn',
115+
['tsc', '-p', 'tsconfig.build.json'],
116+
{ cwd: rootDir, stdio: 'inherit', shell: process.platform === 'win32' }
117+
)
118+
.on('error', reject)
119+
.on('close', resolve);
120+
121+
const exitCode = await promise;
122+
if (exitCode !== 0) {
123+
process.exit(exitCode);
124+
}
125+
126+
console.log('Building shims for CommonJS type resolution...');
127+
for await (const file of fs.glob('*.d.ts', { cwd: rootDir })) {
128+
await fs.writeFile(
129+
dist(file.replace('.d.ts', '.d.cts')),
130+
`export * from "./${file.replace('.d.ts', '.js')}";`,
131+
'utf8',
132+
);
133+
}
134+
}

tsconfig.build.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"allowJs": true,
88
"noImplicitAny": true,
99
"declaration": true,
10-
"emitDeclarationOnly": true
10+
"emitDeclarationOnly": true,
11+
"skipLibCheck": true
1112
},
1213
"include": [
1314
"./src/**/*"

0 commit comments

Comments
 (0)