dsherret / dsherret/ts-morph

How to skip resolving import declarations and quickly find default export

Open
#1,201 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

I need to parse a large number of files, but encountered some performance issues. My analysis can be roughly simplified to the following operations.

Given a ts file:

```typescript
import { View as V } from 'some-lib';

// other codes...

@V('/view')
@OtherDecorator()
export default class MyClass {}
```

I want to find all files which exports a class decorated with `@View` from specified module, and collect all necessary data from these files. But I find there are two operations cost a lot of time (usually 50ms - 400ms):

```typescript
const project = new Project({
tsConfigFilePath: path.join(opts.clientDir, 'tsconfig.json'),
skipAddingFilesFromTsConfig: true,
skipFileDependencyResolution: true,
skipLoadingLibFiles: true,
});

const sourceFile = project.createSourceFile(/** file fetched dynamically */);

sourceFile.getDefaultExportSymbol(); // the first performance issue point

// then get the default exported class declaration and its decorators. traverse all decorators.
// now parse the decorators[0], get the Identifier `V`, and confirm the `V` is the alias of `View` from 'some-lib'
const def = decoratorIdentifier.getDefinitions()[0]; // it's the second point which cost a lot of time
if (def && def.getContainerName().includes(PKG_NAME) && def.getName() === 'View') {
// ok
}
```

For the first point, I dont know why finding default export costs a lot of time. And for the second point, I want to skip resolving imported package (just find a way to confirm the identifier is imported from specified module's import clause in current file?), maybe its helpful to save time.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.