-
-
Notifications
You must be signed in to change notification settings - Fork 41
feat: old import-x/resolve
options now use new built-in node resolver
#272
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
base: master
Are you sure you want to change the base?
Changes from 7 commits
85a9702
89e3937
2a66b0a
ffcc7d3
161c7cc
28189c5
2b0fce1
05fcf87
d21b8d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,10 +34,33 @@ export type { | |
|
||
export type ImportType = ImportType_ | 'object' | 'type' | ||
|
||
export interface NodeResolverOptions { | ||
export interface LegacyNodeResolverOptions { | ||
extensions?: readonly string[] | ||
moduleDirectory?: string[] | ||
/** set to `false` to exclude node core modules (e.g.` fs`) from the search */ | ||
includeCoreModules?: boolean | ||
/** directory (or directories) in which to recursively look for modules. Default "node_modules" */ | ||
moduleDirectory?: string | ||
/** if true, doesn't resolve basedir to real path before resolving. This is the way Node resolves dependencies when executed with the --preserve-symlinks flag. Default to true */ | ||
preserveSymlinks?: boolean | ||
/** Noop now, Previously a directory to begin resolving from */ | ||
basedir?: string | ||
/** Noop now. Previously for require.paths array to, it is now noop */ | ||
paths?: string[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both the This affects many unit tests IMHO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, we can mark There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The idea is that almost nobody uses https://github.com/search?q=%2F%5B%27%22%5Dimport%5C%2Fresolve%5B%27%22%5D%2F&type=code And only our unit tests use https://github.com/search?q=%2F%5B%27%22%5Dimport-x%5C%2Fresolve%5B%27%22%5D%2F&type=code This is because the So we can probably include this as a semi-breaking change and drop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO, let's just remove it first, and probably no Implementing fallback will result in inconsistent behaviors (sometimes use // eslint.config.js
export default [
{ settings: { 'import-x/resolve': legacy } },
{ settings: { 'import-x/resolve': lessLegacy } }
]; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But we will still need to implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get it. I just tried adding |
||
|
||
/** Noop now. Previously for how to read files asynchronously */ | ||
readFile?: never | ||
/** Noop now. Previously a function to asynchronously test whether a file exists */ | ||
isFile?: never | ||
/** Noop now. Previously a function to asynchronously whether a file exists and is a directory */ | ||
isDirectory?: never | ||
/** Noop now. Previously a function to asynchronously resolve a potential symlink to its real path */ | ||
realpath?: never | ||
/** Noop now. Previously a function to asynchronously read a package.json file */ | ||
readPackage?: never | ||
/** Noop now. Previously a function to transform the parsed package.json contents before looking at the "main" field */ | ||
packageFilter?: never | ||
/** Noop now. Previously a function to transform the resolved path before returning it */ | ||
pathFilter?: never | ||
} | ||
|
||
export interface WebpackResolverOptions { | ||
|
@@ -98,7 +121,7 @@ export interface ImportSettings { | |
ignore?: string[] | ||
internalRegex?: string | ||
parsers?: Record<string, readonly FileExtension[]> | ||
resolve?: NodeResolverOptions | ||
resolve?: LegacyNodeResolverOptions | ||
SukkaW marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resolver?: LegacyImportResolver | ||
'resolver-legacy'?: LegacyImportResolver | ||
'resolver-next'?: NewResolver[] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url' | |
|
||
import { stableHash } from 'stable-hash' | ||
|
||
import { createNodeResolver } from '../node-resolver.js' | ||
import type { | ||
ImportSettings, | ||
LegacyResolver, | ||
|
@@ -110,6 +111,8 @@ function isValidNewResolver(resolver: unknown): resolver is NewResolver { | |
return true | ||
} | ||
|
||
let fallbackLegacyNodeResolver: NewResolver | ||
|
||
function fullResolve( | ||
modulePath: string, | ||
sourceFile: string, | ||
|
@@ -140,10 +143,7 @@ function fullResolve( | |
return { found: true, path: cachedPath } | ||
} | ||
|
||
if ( | ||
Object.prototype.hasOwnProperty.call(settings, 'import-x/resolver-next') && | ||
settings['import-x/resolver-next'] | ||
) { | ||
if (settings['import-x/resolver-next']) { | ||
const configResolvers = settings['import-x/resolver-next'] | ||
|
||
for (let i = 0, len = configResolvers.length; i < len; i++) { | ||
|
@@ -169,33 +169,53 @@ function fullResolve( | |
fileExistsCache.set(cacheKey, resolved.path as string | null) | ||
return resolved | ||
} | ||
} else { | ||
const configResolvers = settings['import-x/resolver-legacy'] || | ||
settings['import-x/resolver'] || { | ||
node: settings['import-x/resolve'], | ||
} // backward compatibility | ||
|
||
for (const { enable, options, resolver } of normalizeConfigResolvers( | ||
configResolvers, | ||
sourceFile, | ||
)) { | ||
if (!enable) { | ||
continue | ||
} | ||
} else if ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems not covering There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to leave those three in the major version bump. These could potentially be a breaking change (unlike There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO, they can still use It is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Then no breaking changes, and no regressions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The only default thing that happened here is in the Here is the thing, it is possible to have both node resolvers enabled: // eslint.config.js
export default [
{ files, settings: { 'import-x/resolve': legacy } },
{ files, settings: { 'import-x/resolve': lessLegacy } }
]; Then it just becomes "why my Let's just not overcomplicate things. If they explicitly declare There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing We can warn and document if unsupported options passed, it'll fallback to original I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmmm. You are right. Let's move this to the breaking change then, and no legacy options will be supported. Closing now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still want to support it with my proposal, so I'll continue your work to finish it. |
||
// backward compatibility for very old `import-x/resolve` options that is no longer supported | ||
settings['import-x/resolve'] | ||
) { | ||
const resolveSettings = settings['import-x/resolve'] | ||
|
||
fallbackLegacyNodeResolver ||= createNodeResolver({ | ||
extensions: (resolveSettings.extensions || | ||
settings['import-x/extensions']) as string[] | undefined, | ||
builtinModules: resolveSettings.includeCoreModules !== false, | ||
modules: resolveSettings.moduleDirectory, | ||
symlinks: resolveSettings.preserveSymlinks ?? true, | ||
}) | ||
|
||
const resolved = fallbackLegacyNodeResolver.resolve(modulePath, sourceFile) | ||
if (resolved.found) { | ||
fileExistsCache.set(cacheKey, resolved.path as string | null) | ||
return resolved | ||
} | ||
|
||
const resolved = resolveWithLegacyResolver( | ||
resolver, | ||
options, | ||
modulePath, | ||
// not found, don't do anything | ||
} else { | ||
const configResolvers = | ||
settings['import-x/resolver-legacy'] || settings['import-x/resolver'] | ||
if (configResolvers) { | ||
for (const { enable, options, resolver } of normalizeConfigResolvers( | ||
configResolvers, | ||
sourceFile, | ||
) | ||
if (!resolved.found) { | ||
continue | ||
} | ||
)) { | ||
if (!enable) { | ||
continue | ||
} | ||
|
||
const resolved = resolveWithLegacyResolver( | ||
resolver, | ||
options, | ||
modulePath, | ||
sourceFile, | ||
) | ||
if (!resolved.found) { | ||
continue | ||
} | ||
|
||
// else, counts | ||
fileExistsCache.set(cacheKey, resolved.path as string | null) | ||
return resolved | ||
// else, counts | ||
fileExistsCache.set(cacheKey, resolved.path as string | null) | ||
return resolved | ||
} | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.