Skip to content

feat: migrate enhanced-resolve to oxc-resolver #237

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

Merged
merged 3 commits into from
Mar 14, 2025
Merged
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/dirty-rabbits-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": minor
---

feat: migrate `enhanced-resolve` to `oxc-resolver`
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
cancel-in-progress: true

jobs:
ci:
Expand All @@ -30,6 +30,7 @@ jobs:
- executeLint: true
node: 20
os: ubuntu-latest
fail-fast: false

runs-on: ${{ matrix.os }}
steps:
Expand Down
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default {
'^eslint-plugin-import-x/package.json$': `<rootDir>/package.json`,
'^eslint-plugin-import-x/(.+)$': `<rootDir>/${srcDir}/$1`,
},
snapshotSerializers: ['<rootDir>/test/jest.serializer.ts'],
testMatch: ['<rootDir>/test/**/*.spec.ts'],
transform: {
'^.+\\.(t|j)sx?$': ['@swc-node/jest', {} satisfies SwcOptions],
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
"@typescript-eslint/utils": "^8.1.0",
"debug": "^4.3.4",
"doctrine": "^3.0.0",
"enhanced-resolve": "^5.17.1",
"eslint-import-resolver-node": "^0.3.9",
"get-tsconfig": "^4.7.3",
"is-glob": "^4.0.3",
"minimatch": "^9.0.3",
"oxc-resolver": "^5.0.0",
"semver": "^7.6.3",
"stable-hash": "^0.0.4",
"tslib": "^2.6.3"
Expand Down Expand Up @@ -117,6 +117,7 @@
"jest": "^29.7.0",
"klaw-sync": "^6.0.0",
"npm-run-all2": "^6.1.2",
"path-serializer": "^0.3.4",
"prettier": "^3.2.5",
"redux": "^5.0.1",
"rimraf": "^5.0.10",
Expand Down
47 changes: 24 additions & 23 deletions src/node-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
import fs from 'node:fs'
import { isBuiltin } from 'node:module'
import path from 'node:path'

import { ResolverFactory, CachedInputFileSystem } from 'enhanced-resolve'
import type { ResolveOptions } from 'enhanced-resolve'
import { ResolverFactory } from 'oxc-resolver'
import type { NapiResolveOptions as ResolveOptions } from 'oxc-resolver'

import type { NewResolver } from './types'

type NodeResolverOptions = {
export type NodeResolverOptions = {
/**
* The allowed extensions the resolver will attempt to find when resolving a module
* @type {string[] | undefined}
* Attempt to resolve these extensions in order.
* If multiple files share the same name but have different extensions,
* will resolve the one with the extension listed first in the array and skip the rest.
*
* @default ['.mjs', '.cjs', '.js', '.json', '.node']
*/
extensions?: string[]
/**
* The import conditions the resolver will used when reading the exports map from "package.json"
* @type {string[] | undefined}
* @default ['default', 'module', 'import', 'require']
* Condition names for exports field which defines entry points of a package.
* The key order in the exports field is significant. During condition matching, earlier entries have higher priority and take precedence over later entries.
*
* @default ['import', 'require', 'default']
*/
conditionNames?: string[]
} & Omit<ResolveOptions, 'useSyncFileSystemCalls'>
/**
* A list of main fields in description files
*
* @default ['module', 'main']
*/
mainFields?: string[]
} & ResolveOptions

export function createNodeResolver({
extensions = ['.mjs', '.cjs', '.js', '.json', '.node'],
conditionNames = ['import', 'require', 'default'],
mainFields = ['module', 'main'],
fileSystem = new CachedInputFileSystem(fs, 4 * 1000),
...restOptions
}: Partial<NodeResolverOptions> = {}): NewResolver {
const resolver = ResolverFactory.createResolver({
}: NodeResolverOptions = {}): NewResolver {
const resolver = new ResolverFactory({
extensions,
fileSystem,
conditionNames,
mainFields,
useSyncFileSystemCalls: true,
...restOptions,
})

Expand All @@ -43,7 +48,7 @@ export function createNodeResolver({
return {
interfaceVersion: 3,
name: 'eslint-plugin-import-x built-in node resolver',
resolve: (modulePath, sourceFile) => {
resolve(modulePath, sourceFile) {
if (isBuiltin(modulePath)) {
return { found: true, path: null }
}
Expand All @@ -53,13 +58,9 @@ export function createNodeResolver({
}

try {
const resolved = resolver.resolveSync(
{},
path.dirname(sourceFile),
modulePath,
)
if (resolved) {
return { found: true, path: resolved }
const resolved = resolver.sync(path.dirname(sourceFile), modulePath)
if (resolved.path) {
return { found: true, path: resolved.path }
}
return { found: false }
} catch {
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
import type { ResolveOptions } from 'enhanced-resolve'
import type { MinimatchOptions } from 'minimatch'
import type { NapiResolveOptions as ResolveOptions } from 'oxc-resolver'
import type { KebabCase } from 'type-fest'

import type { ImportType as ImportType_, PluginName } from './utils'
Expand Down Expand Up @@ -40,7 +40,7 @@ export type NodeResolverOptions = {
}

export type WebpackResolverOptions = {
config?: string | { resolve: Omit<ResolveOptions, 'fileSystem'> }
config?: string | { resolve: ResolveOptions }
'config-index'?: number
env?: Record<string, unknown>
argv?: Record<string, unknown>
Expand All @@ -50,7 +50,7 @@ export type TsResolverOptions = {
alwaysTryTypes?: boolean
project?: string[] | string
extensions?: string[]
} & Omit<ResolveOptions, 'fileSystem' | 'useSyncFileSystemCalls'>
} & ResolveOptions

// TODO: remove prefix New in the next major version
export type NewResolverResolve = (
Expand Down
2 changes: 1 addition & 1 deletion src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function parse(
if (!ast || typeof ast !== 'object') {
console.warn(
// Can only be invalid for custom parser per imports/parser
`\`parseForESLint\` from parser \`${typeof parserOrPath === 'string' ? parserOrPath : '`context.languageOptions.parser`'}\` is invalid and will just be ignored`,
`\`parseForESLint\` from parser \`${typeof parserOrPath === 'string' ? parserOrPath : 'context.languageOptions.parser'}\` is invalid and will just be ignored`,
{ content, parserMeta: parser.meta },
)
} else {
Expand Down
207 changes: 207 additions & 0 deletions test/__snapshots__/node-resolver.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`builtin node:path => true 1`] = `
{
"expected": true,
"requireResolve": "node:path",
"source": "node:path",
}
`;

exports[`builtin node:path => true 2`] = `
{
"expected": true,
"result": {
"found": true,
"path": null,
},
"source": "node:path",
}
`;

exports[`builtin path => true 1`] = `
{
"expected": true,
"requireResolve": "path",
"source": "path",
}
`;

exports[`builtin path => true 2`] = `
{
"expected": true,
"result": {
"found": true,
"path": null,
},
"source": "path",
}
`;

exports[`modules @sukka/does-not-exists => false 1`] = `
{
"expected": false,
"requireResolve": undefined,
"source": "@sukka/does-not-exists",
}
`;

exports[`modules @sukka/does-not-exists => false 2`] = `
{
"expected": false,
"result": {
"found": false,
},
"source": "@sukka/does-not-exists",
}
`;

exports[`modules jest => true 1`] = `
{
"expected": true,
"requireResolve": "<ROOT>/node_modules/jest/build/index.js",
"source": "jest",
}
`;

exports[`modules jest => true 2`] = `
{
"expected": true,
"result": {
"found": true,
"path": "<ROOT>/node_modules/jest/build/index.js",
},
"source": "jest",
}
`;

exports[`relative ../.github/dependabot.yml => false 1`] = `
{
"expected": false,
"requireResolve": undefined,
"source": "../.github/dependabot.yml",
}
`;

exports[`relative ../.github/dependabot.yml => false 2`] = `
{
"expected": false,
"result": {
"found": false,
},
"source": "../.github/dependabot.yml",
}
`;

exports[`relative ../babel.config.js => babel.config.js 1`] = `
{
"expected": "babel.config.js",
"requireResolve": "<ROOT>/babel.config.js",
"source": "../babel.config.js",
}
`;

exports[`relative ../babel.config.js => babel.config.js 2`] = `
{
"expected": "babel.config.js",
"result": {
"found": true,
"path": "<ROOT>/babel.config.js",
},
"source": "../babel.config.js",
}
`;

exports[`relative ../inexistent.js => false 1`] = `
{
"expected": false,
"requireResolve": undefined,
"source": "../inexistent.js",
}
`;

exports[`relative ../inexistent.js => false 2`] = `
{
"expected": false,
"result": {
"found": false,
},
"source": "../inexistent.js",
}
`;

exports[`relative ../package.json => package.json 1`] = `
{
"expected": "package.json",
"requireResolve": "<ROOT>/package.json",
"source": "../package.json",
}
`;

exports[`relative ../package.json => package.json 2`] = `
{
"expected": "package.json",
"result": {
"found": true,
"path": "<ROOT>/package.json",
},
"source": "../package.json",
}
`;

exports[`relative ../test => test/index.js 1`] = `
{
"expected": "test/index.js",
"requireResolve": "<ROOT>/test/index.js",
"source": "../test",
}
`;

exports[`relative ../test => test/index.js 2`] = `
{
"expected": "test/index.js",
"result": {
"found": true,
"path": "<ROOT>/test/index.js",
},
"source": "../test",
}
`;

exports[`relative ../test/ => test/index.js 1`] = `
{
"expected": "test/index.js",
"requireResolve": "<ROOT>/test/index.js",
"source": "../test/",
}
`;

exports[`relative ../test/ => test/index.js 2`] = `
{
"expected": "test/index.js",
"result": {
"found": true,
"path": "<ROOT>/test/index.js",
},
"source": "../test/",
}
`;

exports[`relative ../test/index.js => test/index.js 1`] = `
{
"expected": "test/index.js",
"requireResolve": "<ROOT>/test/index.js",
"source": "../test/index.js",
}
`;

exports[`relative ../test/index.js => test/index.js 2`] = `
{
"expected": "test/index.js",
"result": {
"found": true,
"path": "<ROOT>/test/index.js",
},
"source": "../test/index.js",
}
`;
6 changes: 6 additions & 0 deletions test/jest.serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createSnapshotSerializer } from 'path-serializer'

const serializer: ReturnType<typeof createSnapshotSerializer> =
createSnapshotSerializer()

export = serializer
Loading
Loading