Skip to content

Commit

Permalink
fix: move enhanced-resolve to prod deps (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrginglymus authored Dec 19, 2024
1 parent c3950d4 commit be9c3e8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-swans-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

Fix enhanced-resolve dependency
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"get-tsconfig": "^4.7.3",
"is-glob": "^4.0.3",
"minimatch": "^9.0.3",
"enhanced-resolve": "^5.17.1",
"semver": "^7.6.3",
"stable-hash": "^0.0.4",
"tslib": "^2.6.3"
Expand Down Expand Up @@ -96,7 +97,6 @@
"@typescript-eslint/rule-tester": "^8.15.0",
"@unts/patch-package": "^8.0.0",
"cross-env": "^7.0.3",
"enhanced-resolve": "^5.17.1",
"escope": "^4.0.0",
"eslint": "^9.15.0",
"eslint-config-prettier": "^9.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import stage0 from './config/stage-0'
import typescript from './config/typescript'
import warnings from './config/warnings'
// rules
import { createNodeResolver } from './node-resolver'
import consistentTypeSpecifierStyle from './rules/consistent-type-specifier-style'
import default_ from './rules/default'
import dynamicImportChunkname from './rules/dynamic-import-chunkname'
Expand Down Expand Up @@ -72,7 +73,6 @@ import type {
PluginFlatConfig,
} from './types'
import { importXResolverCompat } from './utils'
import { createNodeResolver } from './node-resolver'

const rules = {
'no-unresolved': noUnresolved,
Expand Down Expand Up @@ -184,5 +184,5 @@ export = {
flatConfigs,
rules,
importXResolverCompat,
createNodeResolver
createNodeResolver,
}
51 changes: 27 additions & 24 deletions src/node-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import { ResolverFactory, CachedInputFileSystem, type ResolveOptions } from 'enhanced-resolve';
import fs from 'node:fs';
import type { NewResolver } from './types';
import { isBuiltin } from 'node:module';
import { dirname } from 'node:path';
import fs from 'node:fs'
import { isBuiltin } from 'node:module'
import path from 'node:path'

interface NodeResolverOptions extends Omit<ResolveOptions, 'useSyncFileSystemCalls'> {
import { ResolverFactory, CachedInputFileSystem } from 'enhanced-resolve'
import type { ResolveOptions } from 'enhanced-resolve'

import type { NewResolver } from './types'

type NodeResolverOptions = {
/**
* The allowed extensions the resolver will attempt to find when resolving a module
* @type {string[] | undefined}
* @default ['.mjs', '.cjs', '.js', '.json', '.node']
*/
extensions?: string[];
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']
*/
conditionNames?: string[];
}
conditionNames?: string[]
} & Omit<ResolveOptions, 'useSyncFileSystemCalls'>

export function createNodeResolver({
extensions = ['.mjs', '.cjs', '.js', '.json', '.node'],
conditionNames = ['default', 'module', 'import', 'require'],
mainFields = ['main'],
exportsFields = ['exports'],
mainFiles = ['index'],
mainFields: _mainFields = ['main'],
exportsFields: _exportsFields = ['exports'],
mainFiles: _mainFiles = ['index'],
fileSystem = new CachedInputFileSystem(fs, 4 * 1000),
...restOptions
}: Partial<NodeResolverOptions> = {}): NewResolver {
Expand All @@ -34,7 +37,7 @@ export function createNodeResolver({
conditionNames,
useSyncFileSystemCalls: true,
...restOptions,
});
})

// shared context across all resolve calls

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

if (modulePath.startsWith('data:')) {
return { found: true, path: null };
return { found: true, path: null }
}

try {
const path = resolver.resolveSync(
const resolved = resolver.resolveSync(
{},
dirname(sourceFile),
modulePath
);
if (path) {
return { found: true, path };
path.dirname(sourceFile),
modulePath,
)
if (resolved) {
return { found: true, path: resolved }
}
return { found: false };
return { found: false }
} catch {
return { found: false };
return { found: false }
}
}
},
}
}
12 changes: 8 additions & 4 deletions test/node-resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import path from 'node:path'
import { cwd } from 'node:process'
import { createNodeResolver } from '../src/node-resolver';

import { createNodeResolver } from '../src/node-resolver'

const resolver = createNodeResolver()

function expectResolve(source: string, expected: boolean | string) {
it(`${source} => ${expected}`, () => {
try {
console.log({ source, expected, requireResolve: require.resolve(source, { paths: [__dirname] }) })

console.log({
source,
expected,
requireResolve: require.resolve(source, { paths: [__dirname] }),
})
} catch {
console.log({ source, expected, requireResolve: null })
}
const result = resolver.resolve(source, __filename);
const result = resolver.resolve(source, __filename)
console.log({ source, expected, result })

if (typeof expected === 'string') {
Expand Down

0 comments on commit be9c3e8

Please sign in to comment.