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: get tsconfig in each pkg for monorepos #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
40 changes: 33 additions & 7 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ async function parseTreeRecursive(
});
}

function findPackageRoot(dirName: string, packageFileName = 'package.json') {
let currentDir = dirName;
while (currentDir !== path.dirname(currentDir)) {
const packageFilePath = path.join(currentDir, packageFileName);
if (fs.existsSync(packageFilePath)) {
return currentDir;
}
currentDir = path.dirname(currentDir);
}
return '';
}
type CompilerOptionsByPkg = {
compilerOptions: ts.CompilerOptions;
host: ts.CompilerHost;
};
const compilerOptionsByPkg = new Map<string, CompilerOptionsByPkg>();

/**
* @param entries - the entry glob list
* @param options
Expand All @@ -139,14 +156,23 @@ export async function parseDependencyTree(
const fullOptions = normalizeOptions(options);
let resolve = simpleResolver;
if (options.tsconfig) {
const compilerOptions = ts.parseJsonConfigFileContent(
ts.readConfigFile(options.tsconfig, ts.sys.readFile).config,
ts.sys,
path.dirname(options.tsconfig),
).options;

const host = ts.createCompilerHost(compilerOptions);
resolve = async (context, request, extensions) => {
const root = findPackageRoot(context);
if (!compilerOptionsByPkg.has(root)) {
let _compilerOptions = ts.parseJsonConfigFileContent(
ts.readConfigFile(path.join(root, 'tsconfig.json'), ts.sys.readFile)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tsconfig.json may not exists. If you want to do that, you may need to findTsconfigDir rather than findPackageRoot.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If tsconfig.json does not exist readConfigFile will return:

{
  config: {},
  error: {
    messageText: 'Cannot read file'
  }
}

In this case, the code will not break, and the module will still be parsed.


If you think this is not a good approach, we can pass tsconfig.json by specifying the root directory from the current working directory (where dpdm is run).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will skip the existing tsconfig in root directory if there is a sub directory has package.json.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback. I have taken your comments into account and updated the PR accordingly to ensure the root tsconfig is not skipped when there is a package.json in a subdirectory. Please let me know if there are any additional adjustments you'd like me to make.

.config,
ts.sys,
root,
).options;
compilerOptionsByPkg.set(root, {
compilerOptions: _compilerOptions,
host: ts.createCompilerHost(_compilerOptions),
});
}
const { compilerOptions, host } = compilerOptionsByPkg.get(
root,
) as CompilerOptionsByPkg;
const module = ts.resolveModuleName(
request,
path.join(context, 'index.ts'),
Expand Down