forked from import-js/eslint-plugin-import
-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathnode-resolver.ts
47 lines (40 loc) · 1.18 KB
/
node-resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import module from 'node:module'
import path from 'node:path'
import { ResolverFactory } from 'unrs-resolver'
import type { NapiResolveOptions } from 'unrs-resolver'
import type { NewResolver } from './types.js'
export function createNodeResolver({
extensions = ['.mjs', '.cjs', '.js', '.json', '.node'],
conditionNames = ['import', 'require', 'default'],
mainFields = ['module', 'main'],
...restOptions
}: NapiResolveOptions = {}): NewResolver {
const resolver = new ResolverFactory({
extensions,
conditionNames,
mainFields,
...restOptions,
})
// shared context across all resolve calls
return {
interfaceVersion: 3,
name: 'eslint-plugin-import-x:node',
resolve(modulePath, sourceFile) {
if (module.isBuiltin(modulePath)) {
return { found: true, path: null }
}
if (modulePath.startsWith('data:')) {
return { found: true, path: null }
}
try {
const resolved = resolver.sync(path.dirname(sourceFile), modulePath)
if (resolved.path) {
return { found: true, path: resolved.path }
}
return { found: false }
} catch {
return { found: false }
}
},
}
}