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

[Refactor] no-extraneous-dependencies improve performance #2374

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -36,6 +36,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [Docs] [`no-useless-path-segments`]: fix paths ([#2424], thanks [@s-h-a-d-o-w])
- [Tests] [`no-cycle`]: add passing test cases ([#2438], thanks [@georeith])
- [Refactor] [`no-extraneous-dependencies`] improve performance using cache ([#2374], thanks [@meowtec])
- [Refactor] `importType`: resolve path lazily ([#2374], thanks [@meowtec])

## [2.26.0] - 2022-04-05

Expand Down
23 changes: 13 additions & 10 deletions src/core/importType.js
Expand Up @@ -31,17 +31,17 @@ export function isBuiltIn(name, settings, path) {
}

export function isExternalModule(name, path, context) {
if (arguments.length < 3) {
if (arguments.length < 3) {
throw new TypeError('isExternalModule: name, path, and context are all required');
}
return (isModule(name) || isScoped(name)) && typeTest(name, context, path) === 'external';
return (isModule(name) || isScoped(name)) && typeTest(name, context, () => path) === 'external';
}

export function isExternalModuleMain(name, path, context) {
if (arguments.length < 3) {
if (arguments.length < 3) {
throw new TypeError('isExternalModule: name, path, and context are all required');
}
return isModuleMain(name) && typeTest(name, context, path) === 'external';
return isModuleMain(name) && typeTest(name, context, () => path) === 'external';
}

const moduleRegExp = /^\w/;
Expand Down Expand Up @@ -109,20 +109,23 @@ function isExternalLookingName(name) {
return isModule(name) || isScoped(name);
}

function typeTest(name, context, path ) {
function typeTest(name, context, getPath) {
const { settings } = context;
if (isInternalRegexMatch(name, settings)) { return 'internal'; }
if (isAbsolute(name, settings, path)) { return 'absolute'; }
if (isAbsolute(name)) { return 'absolute'; }
if (isRelativeToParent(name)) { return 'parent'; }
if (isIndex(name)) { return 'index'; }
if (isRelativeToSibling(name)) { return 'sibling'; }

// `resolve` is expensive, do it only when necessary
const path = getPath();
if (isBuiltIn(name, settings, path)) { return 'builtin'; }
Copy link
Member

Choose a reason for hiding this comment

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

this change moves isBuiltIn below absolute/relativeToParent/index/relativeToSibling - this might be a breaking change for anyone relying on the import/core-modules setting, which currently takes precedence.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could we make this change in next major version?

Copy link
Member

Choose a reason for hiding this comment

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

There's unlikely to be a major version any time soon, if ever.

if (isRelativeToParent(name, settings, path)) { return 'parent'; }
if (isIndex(name, settings, path)) { return 'index'; }
if (isRelativeToSibling(name, settings, path)) { return 'sibling'; }
if (isExternalPath(path, context)) { return 'external'; }
if (isInternalPath(path, context)) { return 'internal'; }
if (isExternalLookingName(name)) { return 'external'; }
return 'unknown';
}

export default function resolveImportType(name, context) {
return typeTest(name, context, resolve(name, context));
return typeTest(name, context, () => resolve(name, context));
}