Skip to content

Commit

Permalink
feat: Update Exports type and add getExports function
Browse files Browse the repository at this point in the history
This commit updates the Exports type in type.ts to include an optional
object with a '.' key. It also adds a new function getExports in utils.ts
which generates the exports field for JSR from package.json. The function
handles different formats of the exports field in package.json and converts
them into a suitable format for JSR. Error handling is also added for
unsupported formats.
  • Loading branch information
ryoppippi committed Aug 10, 2024
1 parent 4841039 commit 0362116
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Exports = Record<string, string> | string;
export type Exports = Record<string, string> | { '.': string };

export type JSR = {
name: string;
Expand All @@ -7,5 +7,5 @@ export type JSR = {
include: string[] | undefined;
exclude: string[] | undefined;
};
exports: Exports;
exports: Exports | undefined;
};
108 changes: 105 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import typia from 'typia';
import type { PackageJson } from 'pkg-types';
import type { JSR } from './type';
import consola from 'consola';
import type { Exports, JSR } from './type';

const isStartWithExclamation = typia.createIs<`!${string}`>();

Expand Down Expand Up @@ -31,7 +32,108 @@ export function getExclude(pkgJSON: PackageJson): string[] | undefined {
});
}

export function genJsrFromPkg({ pkgJSON, options }: { pkgJSON: PackageJson; options: PkgToJsrConfig }): JSR {
/**
* generate exports filed for JSR from package.json
* the import path should be string, { source: string }, { import: string }
*
* @example
* ```json
* {
* "exports": './index.js',
* }
* ```
* will get coverted into
* ```json
* {
* "exports": {
* ".": "./index.js"
* }
* }
* ```
*
* @example
* ```json
* {
* "exports": {
* ".": "./index.js",
* "./sub": "./sub.js"
* }
* }
* ```
* will get coverted into
* ```json
* {
* "exports": {
* ".": "./index.js",
* "./sub": "./sub.js"
* }
* }
* ```
*
* @example
* ```json
* {
* "exports": {
* ".": {
* "source": "./src/index.ts",
* "import": "./dist/index.js"
* "types": "./dist/index.d.ts"
* },
* "./sub": {
* "source": "./src/sub.ts"
* "import": "./dist/sub.js"
* "types": "./dist/sub.d.ts"
* }
* }
* }
* ```
* will get coverted into
* ```json
* {
* "exports": {
* ".": "./src/index.ts",",
* "./sub": "./src/sub.ts"
* }
* }
* ```
*/
export function getExports(pkgJSON: PackageJson): Exports | undefined {
const { exports } = pkgJSON;

if (exports == null) {
return;
}

if (typia.is<string>(exports)) {
return { '.': exports };
}

const _exports = {} as Record<string, string>;

for (const [key, value] of Object.entries(exports)) {
switch (true) {
case typia.is<string>(value):
_exports[key] = value;
break;
case typia.is<Record<string, string>>(value):
/* if source is defined, use it, otherwise use import */
_exports[key] = value.source ?? value.import;
break;
default:
consola.error(`Export key ${key} is ignored because it is not a string or object`);
}
}

if (Object.keys(_exports).length === 0) {
return;
}

return _exports;
}

/**
* generate JSR from package.json
*/
export function genJsrFromPkg({ pkgJSON }: { pkgJSON: PackageJson }): JSR {
const { name, version } = pkgJSON;
const jsr = {
Expand All @@ -41,7 +143,7 @@ export function genJsrFromPkg({ pkgJSON }: { pkgJSON: PackageJson }): JSR {
include: getInclude(pkgJSON),
exclude: getExclude(pkgJSON),
},
exports: options.exports,
exports: getExports(pkgJSON),
} as const satisfies JSR;

/* check the JSR object */
Expand Down

0 comments on commit 0362116

Please sign in to comment.