Skip to content

Commit

Permalink
fix(third-party-dts-extractor): correctly sets the source of the pack…
Browse files Browse the repository at this point in the history
…age types
  • Loading branch information
Tamás Ángyán committed Jan 29, 2025
1 parent f141396 commit e74c1e8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-eels-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/third-party-dts-extractor': patch
---

fix(third-party-dts-extractor): correctly sets the source of the package types
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ describe('ThirdPartyExtractor', () => {
const destDir = join(projectRoot, 'dist', 'third-party-extractor');
const thirdPartyExtractor = new ThirdPartyExtractor(destDir, projectRoot);

it('should correctly infer pkg dir with types field in package.json', () => {
it("should correctly infer pkg's types dir with types/typings field in package.json", () => {
const tsupDir = thirdPartyExtractor.inferPkgDir('tsup');
const pkgJson = fs.readJSONSync(`${tsupDir}/package.json`);

expect(pkgJson.name).toBe('tsup');
expect(Boolean(pkgJson.types || pkgJson.typings)).toEqual(true);
if (!tsupDir) {
throw new Error('tsup dir not found');
}

const dirContent = fs.readdirSync(tsupDir);
const dtsFiles = dirContent.filter((file) => file.endsWith('.d.ts'));

expect(dtsFiles.length).toBeGreaterThan(0);
});

it('should correctly infer pkg types dir without types field in package.json', () => {
Expand Down
10 changes: 6 additions & 4 deletions packages/third-party-dts-extractor/src/ThirdPartyExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import findPkg from 'find-pkg';
import fs from 'fs-extra';
import path from 'path';
import resolve from 'resolve';
import { getTypedName } from './utils';
import { getTypedName, getPackageRootDir } from './utils';

const ignoredPkgs = ['typescript'];

Expand Down Expand Up @@ -48,7 +48,8 @@ class ThirdPartyExtractor {
if (isNodeUtils(importEntry, importPath)) {
return;
}
const pkgJsonPath = findPkg.sync(importEntry) as string;
const packageDir = getPackageRootDir(importPath);
const pkgJsonPath = path.join(packageDir, 'package.json');

const dir = path.dirname(pkgJsonPath);
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8')) as Record<
Expand All @@ -61,8 +62,9 @@ class ThirdPartyExtractor {
}

if (types) {
this.addPkgs(pkg.name, dir);
return dir;
const typesDir = path.dirname(path.resolve(dir, types));
this.addPkgs(pkg.name, typesDir);
return typesDir;
} else if (fs.existsSync(path.resolve(dir, 'index.d.ts'))) {
this.addPkgs(pkg.name, dir);
return dir;
Expand Down
30 changes: 29 additions & 1 deletion packages/third-party-dts-extractor/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import path from 'node:path';

function getTypedName(name: string) {
return `@types/${name.replace(/^@/, '').replace('/', '__')}`;
}

export { getTypedName };
function getPackageRootDir(packageName: string | undefined): string {
if (!packageName) {
throw new Error('No package name provided.');
}

let packageRootDir: string;

try {
// First, try resolving the package's package.json
const packageJsonPath = require.resolve(`${packageName}/package.json`);
packageRootDir = path.dirname(packageJsonPath);
} catch (error) {
// If resolving package.json fails, fall back to resolving the package itself
try {
const packageEntryPath = require.resolve(packageName);
packageRootDir = path.dirname(packageEntryPath);
} catch (fallbackError) {
throw new Error(
`Could not resolve package "${packageName}" or its package.json.`,
);
}
}

return packageRootDir;
}

export { getTypedName, getPackageRootDir };

0 comments on commit e74c1e8

Please sign in to comment.