Skip to content
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
11 changes: 11 additions & 0 deletions .changeset/loose-phones-sing.md
Original file line number Diff line number Diff line change
@@ -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 by including re-export declaration files.

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.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ node_modules/
/scripts/__pycache__/

/*.d.ts
/*.d.cts
/*.js
/*.js.map
/*.cjs
Expand Down
12 changes: 3 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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",
"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",
"bundle-stats:emoji": "node benchmark/emoji/bundle-stats.js",
Expand Down
33 changes: 32 additions & 1 deletion scripts/build-exports.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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';

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 });

Expand All @@ -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 => {
Expand Down Expand Up @@ -71,6 +74,8 @@ function rewriteCjs(content) {
}

{
console.log('Build browser bundles...');

let bundleEntryPoints = [
src('index.js'),
src('emoji.js'),
Expand Down Expand Up @@ -101,3 +106,29 @@ function rewriteCjs(content) {
sourcemap: false,
});
}

if (!process.argv.includes('--no-tsc')) {
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('Building shims for CommonJS type resolution...');
for await (const file of fs.glob('*.d.ts', { cwd: rootDir })) {
await fs.writeFile(
dist(file.replace('.d.ts', '.d.cts')),
`export * from "./${file.replace('.d.ts', '.js')}";`,
'utf8',
);
}
}
3 changes: 2 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"allowJs": true,
"noImplicitAny": true,
"declaration": true,
"emitDeclarationOnly": true
"emitDeclarationOnly": true,
"skipLibCheck": true
},
"include": [
"./src/**/*"
Expand Down