diff --git a/.changeset/loose-phones-sing.md b/.changeset/loose-phones-sing.md new file mode 100644 index 0000000..0dcbffd --- /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 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. 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..c270c5a 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", + "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", diff --git a/scripts/build-exports.js b/scripts/build-exports.js index e9f69b7..27fa364 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,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', + ); + } +} 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/**/*"