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

fix(third-party-dts-extractor): correctly sets the source of the pack… #3467

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 .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 };
Loading