Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove TLA and export proper types #10

Merged
merged 2 commits into from
Aug 25, 2024
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -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'
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/sync.cjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// 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(
(id) => id === dep || dep.startsWith(`${id}/`)
)
}

/** @type {import('..').isDepNoExternaled} */
/** @type {import('./index.d.ts').isDepNoExternaled} */
function isDepNoExternaled(dep, ssrNoExternal) {
if (ssrNoExternal === true) {
return true
Expand All @@ -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)
}
Expand Down
File renamed without changes.