From 5134e7351c58d9f9f79595e1b4f2ed77bbc74fd0 Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 23 Aug 2024 17:45:53 +0800 Subject: [PATCH] Remove TLA and export proper types --- CHANGELOG.md | 5 +++++ package.json | 8 +++----- index.d.ts => src/index.d.cts | 0 src/index.d.ts | 4 ++++ src/index.js | 10 +++++----- src/sync.cjs | 8 ++++---- jsconfig.json => tsconfig.json | 0 7 files changed, 21 insertions(+), 14 deletions(-) rename index.d.ts => src/index.d.cts (100%) create mode 100644 src/index.d.ts rename jsconfig.json => tsconfig.json (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index a176024..3d0a31b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Unreleased + +- Remove top-level await to allow future compatibility to `require` ESM code +- Export proper ESM and CJS types + ## 0.2.5 (2023-10-13) - Align `findDepPkgJsonPath` implementation with Vite diff --git a/package.json b/package.json index 4fe697c..b584d5e 100644 --- a/package.json +++ b/package.json @@ -4,21 +4,19 @@ "version": "0.2.5", "license": "MIT", "type": "module", - "types": "./index.d.ts", + "types": "./src/index.d.ts", "exports": { ".": { - "types": "./index.d.ts", "import": "./src/index.js", "require": "./src/index.cjs" } }, "files": [ - "src", - "index.d.ts" + "src" ], "repository": { "type": "git", - "url": "https://github.com/svitejs/vitefu.git" + "url": "git+https://github.com/svitejs/vitefu.git" }, "bugs": { "url": "https://github.com/svitejs/vitefu/issues" diff --git a/index.d.ts b/src/index.d.cts similarity index 100% rename from index.d.ts rename to src/index.d.cts diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..5f085d5 --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,4 @@ +// TypeScript requires individual files for ESM and CJS, so we put the types +// in `.d.cts` and re-export from `.d.ts`. (Not the other way in case TypeScript +// doesn't allow re-exporting ESM from CJS) +export type * from './index.d.cts' diff --git a/src/index.js b/src/index.js index fc44a65..6645184 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ import fs from 'node:fs/promises' import fsSync from 'node:fs' +import { createRequire } from 'node:module' import path from 'node:path' import { isDepIncluded, @@ -12,14 +13,13 @@ import { let pnp if (process.versions.pnp) { try { - const { createRequire } = (await import('module')).default pnp = createRequire(import.meta.url)('pnpapi') } catch {} } export { isDepIncluded, isDepExcluded, isDepNoExternaled, isDepExternaled } -/** @type {import('..').crawlFrameworkPkgs} */ +/** @type {import('./index.d.ts').crawlFrameworkPkgs} */ export async function crawlFrameworkPkgs(options) { const pkgJsonPath = await findClosestPkgJsonPath(options.root) if (!pkgJsonPath) { @@ -193,7 +193,7 @@ export async function crawlFrameworkPkgs(options) { } } -/** @type {import('..').findDepPkgJsonPath} */ +/** @type {import('./index.d.ts').findDepPkgJsonPath} */ export async function findDepPkgJsonPath(dep, parent) { if (pnp) { try { @@ -221,7 +221,7 @@ export async function findDepPkgJsonPath(dep, parent) { return undefined } -/** @type {import('..').findClosestPkgJsonPath} */ +/** @type {import('./index.d.ts').findClosestPkgJsonPath} */ export async function findClosestPkgJsonPath(dir, predicate = undefined) { if (dir.endsWith('package.json')) { dir = path.dirname(dir) @@ -241,7 +241,7 @@ export async function findClosestPkgJsonPath(dir, predicate = undefined) { return undefined } -/** @type {import('..').pkgNeedsOptimization} */ +/** @type {import('./index.d.ts').pkgNeedsOptimization} */ export async function pkgNeedsOptimization(pkgJson, pkgJsonPath) { // only optimize if is cjs, using the below as heuristic // see https://github.com/sveltejs/vite-plugin-svelte/issues/162 diff --git a/src/sync.cjs b/src/sync.cjs index add6ba1..5c0cf7a 100644 --- a/src/sync.cjs +++ b/src/sync.cjs @@ -1,11 +1,11 @@ // contains synchronous API only so it can be exported as CJS and ESM -/** @type {import('..').isDepIncluded} */ +/** @type {import('./index.d.ts').isDepIncluded} */ function isDepIncluded(dep, optimizeDepsInclude) { return optimizeDepsInclude.some((id) => parseIncludeStr(id) === dep) } -/** @type {import('..').isDepExcluded} */ +/** @type {import('./index.d.ts').isDepExcluded} */ function isDepExcluded(dep, optimizeDepsExclude) { dep = parseIncludeStr(dep) return optimizeDepsExclude.some( @@ -13,7 +13,7 @@ function isDepExcluded(dep, optimizeDepsExclude) { ) } -/** @type {import('..').isDepNoExternaled} */ +/** @type {import('./index.d.ts').isDepNoExternaled} */ function isDepNoExternaled(dep, ssrNoExternal) { if (ssrNoExternal === true) { return true @@ -22,7 +22,7 @@ function isDepNoExternaled(dep, ssrNoExternal) { } } -/** @type {import('..').isDepExternaled} */ +/** @type {import('./index.d.ts').isDepExternaled} */ function isDepExternaled(dep, ssrExternal) { return ssrExternal.includes(dep) } diff --git a/jsconfig.json b/tsconfig.json similarity index 100% rename from jsconfig.json rename to tsconfig.json