Created
May 30, 2022 15:30
-
-
Save cef62/a14c3003095fe56fba2b69d6c57e5777 to your computer and use it in GitHub Desktop.
Refactor `package.json` exports to be compatible with Typescript 4.7+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-check | |
import path from 'path' | |
import { readFile, writeFile } from 'fs/promises' | |
import glob from 'tiny-glob' | |
/** | |
* @typedef {object} LegacyExportField | |
* @property {string} [types] | |
* @property {string} [import] | |
* @property {string} [require] | |
*/ | |
/** | |
* @typedef {object} ExportFieldEntry | |
* @property {string} [types] | |
* @property {string} [default] | |
*/ | |
/** | |
* @typedef {object} ExportField | |
* @property {ExportFieldEntry | string} [import] | |
* @property {ExportFieldEntry | string} [require] | |
*/ | |
/** | |
* @typedef {object} PackageJson | |
* @property {string} [name] | |
* @property {Record<string, LegacyExportField> | Record<string, ExportField>} [exports] | |
*/ | |
async function fixPackageJsonExports() { | |
let packageJsonList = await glob('**/package.json') | |
for (const packageJsonPath of packageJsonList) { | |
try { | |
const rootDir = '.' | |
const pkgPath = path.join(rootDir, packageJsonPath) | |
const pkgUrl = new URL(pkgPath, import.meta.url) | |
/** @type {PackageJson} */ | |
const pkg = JSON.parse(await readFile(pkgUrl, { encoding: 'utf8' })) | |
// skip projects without mapped exports | |
if (!pkg.exports || Object.keys(pkg.exports).length === 0) continue | |
for (const key of Object.keys(pkg.exports)) { | |
const entry = pkg.exports?.[key] | |
if (entry && 'types' in entry) { | |
/** @type {LegacyExportField} */ | |
const field = entry | |
pkg.exports[key] = {} | |
if (field.types) { | |
// the entry has types definitions | |
if (field.import) { | |
pkg.exports[key].import = { types: field.types, default: field.import } | |
} | |
if (field.require) { | |
pkg.exports[key].require = { types: field.types, default: field.require } | |
} | |
} else { | |
// the entry is withou types definitions | |
if (field.import) { | |
pkg.exports[key].import = field.import | |
} | |
if (field.require) { | |
pkg.exports[key].require = field.require | |
} | |
} | |
} | |
} | |
const content = JSON.stringify(pkg, null, 2) + '\n' | |
await writeFile(pkgUrl, content, { encoding: 'utf8', flag: 'w' }) | |
} catch (e) { | |
console.log(e) | |
} | |
} | |
} | |
fixPackageJsonExports() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since version 4.7 TypeScript officially supports the
package.json
exports field.If you have projects that are using
Node exports
, it may be that your exports are something like:This script helps to refactor your
exports
to be compatible with TypeScript 4.7+The scripts will work on a single project or in a mono-repo.
I hope this will save some time for other people having many TypeScript projects with a lot of exports ;)