Skip to content

Commit be9c3e8

Browse files
authored
fix: move enhanced-resolve to prod deps (#211)
1 parent c3950d4 commit be9c3e8

File tree

5 files changed

+43
-31
lines changed

5 files changed

+43
-31
lines changed

.changeset/kind-swans-invent.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-import-x": patch
3+
---
4+
5+
Fix enhanced-resolve dependency

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"get-tsconfig": "^4.7.3",
5858
"is-glob": "^4.0.3",
5959
"minimatch": "^9.0.3",
60+
"enhanced-resolve": "^5.17.1",
6061
"semver": "^7.6.3",
6162
"stable-hash": "^0.0.4",
6263
"tslib": "^2.6.3"
@@ -96,7 +97,6 @@
9697
"@typescript-eslint/rule-tester": "^8.15.0",
9798
"@unts/patch-package": "^8.0.0",
9899
"cross-env": "^7.0.3",
99-
"enhanced-resolve": "^5.17.1",
100100
"escope": "^4.0.0",
101101
"eslint": "^9.15.0",
102102
"eslint-config-prettier": "^9.1.0",

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import stage0 from './config/stage-0'
1919
import typescript from './config/typescript'
2020
import warnings from './config/warnings'
2121
// rules
22+
import { createNodeResolver } from './node-resolver'
2223
import consistentTypeSpecifierStyle from './rules/consistent-type-specifier-style'
2324
import default_ from './rules/default'
2425
import dynamicImportChunkname from './rules/dynamic-import-chunkname'
@@ -72,7 +73,6 @@ import type {
7273
PluginFlatConfig,
7374
} from './types'
7475
import { importXResolverCompat } from './utils'
75-
import { createNodeResolver } from './node-resolver'
7676

7777
const rules = {
7878
'no-unresolved': noUnresolved,
@@ -184,5 +184,5 @@ export = {
184184
flatConfigs,
185185
rules,
186186
importXResolverCompat,
187-
createNodeResolver
187+
createNodeResolver,
188188
}

src/node-resolver.ts

+27-24
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1-
import { ResolverFactory, CachedInputFileSystem, type ResolveOptions } from 'enhanced-resolve';
2-
import fs from 'node:fs';
3-
import type { NewResolver } from './types';
4-
import { isBuiltin } from 'node:module';
5-
import { dirname } from 'node:path';
1+
import fs from 'node:fs'
2+
import { isBuiltin } from 'node:module'
3+
import path from 'node:path'
64

7-
interface NodeResolverOptions extends Omit<ResolveOptions, 'useSyncFileSystemCalls'> {
5+
import { ResolverFactory, CachedInputFileSystem } from 'enhanced-resolve'
6+
import type { ResolveOptions } from 'enhanced-resolve'
7+
8+
import type { NewResolver } from './types'
9+
10+
type NodeResolverOptions = {
811
/**
912
* The allowed extensions the resolver will attempt to find when resolving a module
1013
* @type {string[] | undefined}
1114
* @default ['.mjs', '.cjs', '.js', '.json', '.node']
1215
*/
13-
extensions?: string[];
16+
extensions?: string[]
1417
/**
1518
* The import conditions the resolver will used when reading the exports map from "package.json"
1619
* @type {string[] | undefined}
1720
* @default ['default', 'module', 'import', 'require']
1821
*/
19-
conditionNames?: string[];
20-
}
22+
conditionNames?: string[]
23+
} & Omit<ResolveOptions, 'useSyncFileSystemCalls'>
2124

2225
export function createNodeResolver({
2326
extensions = ['.mjs', '.cjs', '.js', '.json', '.node'],
2427
conditionNames = ['default', 'module', 'import', 'require'],
25-
mainFields = ['main'],
26-
exportsFields = ['exports'],
27-
mainFiles = ['index'],
28+
mainFields: _mainFields = ['main'],
29+
exportsFields: _exportsFields = ['exports'],
30+
mainFiles: _mainFiles = ['index'],
2831
fileSystem = new CachedInputFileSystem(fs, 4 * 1000),
2932
...restOptions
3033
}: Partial<NodeResolverOptions> = {}): NewResolver {
@@ -34,7 +37,7 @@ export function createNodeResolver({
3437
conditionNames,
3538
useSyncFileSystemCalls: true,
3639
...restOptions,
37-
});
40+
})
3841

3942
// shared context across all resolve calls
4043

@@ -43,26 +46,26 @@ export function createNodeResolver({
4346
name: 'eslint-plugin-import-x built-in node resolver',
4447
resolve: (modulePath, sourceFile) => {
4548
if (isBuiltin(modulePath)) {
46-
return { found: true, path: null };
49+
return { found: true, path: null }
4750
}
4851

4952
if (modulePath.startsWith('data:')) {
50-
return { found: true, path: null };
53+
return { found: true, path: null }
5154
}
5255

5356
try {
54-
const path = resolver.resolveSync(
57+
const resolved = resolver.resolveSync(
5558
{},
56-
dirname(sourceFile),
57-
modulePath
58-
);
59-
if (path) {
60-
return { found: true, path };
59+
path.dirname(sourceFile),
60+
modulePath,
61+
)
62+
if (resolved) {
63+
return { found: true, path: resolved }
6164
}
62-
return { found: false };
65+
return { found: false }
6366
} catch {
64-
return { found: false };
67+
return { found: false }
6568
}
66-
}
69+
},
6770
}
6871
}

test/node-resolver.spec.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import path from 'node:path'
22
import { cwd } from 'node:process'
3-
import { createNodeResolver } from '../src/node-resolver';
3+
4+
import { createNodeResolver } from '../src/node-resolver'
45

56
const resolver = createNodeResolver()
67

78
function expectResolve(source: string, expected: boolean | string) {
89
it(`${source} => ${expected}`, () => {
910
try {
10-
console.log({ source, expected, requireResolve: require.resolve(source, { paths: [__dirname] }) })
11-
11+
console.log({
12+
source,
13+
expected,
14+
requireResolve: require.resolve(source, { paths: [__dirname] }),
15+
})
1216
} catch {
1317
console.log({ source, expected, requireResolve: null })
1418
}
15-
const result = resolver.resolve(source, __filename);
19+
const result = resolver.resolve(source, __filename)
1620
console.log({ source, expected, result })
1721

1822
if (typeof expected === 'string') {

0 commit comments

Comments
 (0)