Skip to content

Commit

Permalink
fix: correct @types package name for scoped package (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
xc2 authored Nov 3, 2024
1 parent 8eb3446 commit 1c48c89
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
42 changes: 41 additions & 1 deletion src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname, join } from 'node:path';
import { dirname, extname, join } from 'node:path';
import fs from '../compiled/fs-extra/index.js';
import { cwd, DIST_DIR } from './constant.js';
import type { Config, DependencyConfig, ParsedTask } from './types.js';
Expand Down Expand Up @@ -110,3 +110,43 @@ export function replaceFileContent(
fs.writeFileSync(filePath, newContent);
}
}

export function pkgNameToAtTypes(name: string) {
const mangled = name.replace(
// 111111 222222
/^@([^\/]+)\/([^\/]+)/,
'$1__$2',
);
return `@types/${mangled}`;
}

/**
* Find the direct type file for a given file path.
* @param filepath
*/
export function findDirectTypeFile(filepath: string) {
if (/\.d\.[cm]?ts/.test(filepath)) {
return filepath;
}
const ext = extname(filepath);
const base = filepath.slice(0, -ext.length);
const _find = (list: string[]) => {
for (const f of list) {
try {
return require.resolve(f, { paths: [cwd] });
} catch {}
}
};
switch (ext) {
case '.js':
case '.ts':
return _find([base + '.d.ts']);
case '.mjs':
case '.mts':
return _find([base + '.d.mts', base + '.d.ts']);
case '.cjs':
case '.cts':
return _find([base + '.d.cts', base + '.d.ts']);
default:
}
}
26 changes: 22 additions & 4 deletions src/prebundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import fastGlob from '../compiled/fast-glob/index.js';
import fs from '../compiled/fs-extra/index.js';
import rslog from '../compiled/rslog/index.js';
import { DEFAULT_EXTERNALS, NODE_BUILTINS } from './constant.js';
import { findDepPath, pick } from './helper.js';
import {
findDepPath,
findDirectTypeFile,
pick,
pkgNameToAtTypes,
} from './helper.js';
import type { ParsedTask } from './types.js';
import { dts } from 'rollup-plugin-dts';
import { rollup, type InputOptions, type OutputOptions } from 'rollup';
Expand Down Expand Up @@ -57,8 +62,14 @@ async function emitDts(task: ParsedTask, externals: Record<string, string>) {
return;
}

const getTypes = (json: Record<string, string>) =>
json.types || json.typing || json.typings || null;
const getTypes = (json: Record<string, string>): string | null =>
(json &&
(json.types ||
json.typing ||
json.typings ||
// for those who use `exports` only
getTypes((json as any).exports?.['.']))) ||
null;

const getInput = () => {
const pkgPath = join(task.depPath, 'package.json');
Expand All @@ -68,7 +79,14 @@ async function emitDts(task: ParsedTask, externals: Record<string, string>) {
return join(task.depPath, types);
}

const depTypesPath = findDepPath(`@types/${task.depName}/package.json`);
const directTypeFile = findDirectTypeFile(task.depEntry);
if (directTypeFile) {
return directTypeFile;
}

const depTypesPath = findDepPath(
`${pkgNameToAtTypes(task.depName)}/package.json`,
);
if (!depTypesPath) {
return null;
}
Expand Down

0 comments on commit 1c48c89

Please sign in to comment.