Skip to content

Commit

Permalink
feat: implement a resolver that supports exports
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 18, 2024
1 parent ac6d2e1 commit ed6efb1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"eslint": "^8.57.0 || ^9.0.0"
},
"dependencies": {
"@dual-bundle/import-meta-resolve": "^4.1.0",
"@types/doctrine": "^0.0.9",
"@typescript-eslint/scope-manager": "^8.1.0",
"@typescript-eslint/utils": "^8.1.0",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import type {
PluginFlatConfig,
} from './types'
import { importXResolverCompat } from './utils'
import { createNodeResolver } from './node-resolver'

const rules = {
'no-unresolved': noUnresolved,
Expand Down Expand Up @@ -183,4 +184,5 @@ export = {
flatConfigs,
rules,
importXResolverCompat,
createNodeResolver
}
60 changes: 60 additions & 0 deletions src/node-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { fileURLToPath, pathToFileURL } from "url";
import type { NewResolver } from "./types";

import type { ErrnoException } from '@dual-bundle/import-meta-resolve' with { "resolution-mode": "import" };

const importMetaResolveExports: typeof import('@dual-bundle/import-meta-resolve', { with: { "resolution-mode": "import" } }) = require('@dual-bundle/import-meta-resolve');
const { moduleResolve } = importMetaResolveExports;

interface NodeResolverOptions {
/**
* The import conditions the resolver will used when reading the exports map from "package.json"
* @default new Set(['default', 'module', 'import', 'require'])
*/
conditions?: Set<string>;
/**
* keep symlinks instead of resolving them
* @default false
*/
preserveSymlinks?: boolean;
}

const assertErrNoException = (error: unknown): error is ErrnoException => {
return (
typeof error === 'object' &&
error !== null &&
'code' in error &&
typeof error.code === 'string'
);
}

export function createNodeResolver({
conditions = new Set(['default', 'module', 'import', 'require']),
preserveSymlinks = false,
}: NodeResolverOptions = {}): NewResolver {
return {
interfaceVersion: 3,
name: 'eslint-plugin-import-x built-in node resolver',
resolve: (modulePath, sourceFile) => {
try {
const found = moduleResolve(
modulePath,
pathToFileURL(sourceFile),
conditions,
preserveSymlinks
);
return {
found: true,
path: fileURLToPath(found),
};
} catch (error) {
if (assertErrNoException(error) && error.code === 'ERR_MODULE_NOT_FOUND') {
return {
found: false,
}
}
throw error;
}
}
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,11 @@
dependencies:
"@jridgewell/trace-mapping" "0.3.9"

"@dual-bundle/import-meta-resolve@^4.1.0":
version "4.1.0"
resolved "https://registry.yarnpkg.com/@dual-bundle/import-meta-resolve/-/import-meta-resolve-4.1.0.tgz#519c1549b0e147759e7825701ecffd25e5819f7b"
integrity sha512-+nxncfwHM5SgAtrVzgpzJOI1ol0PkumhVo469KCf9lUi21IGcY90G98VuHm9VRrUypmAzawAHO9bs6hqeADaVg==

"@emnapi/core@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@emnapi/core/-/core-1.1.0.tgz#6b552ea3d2b303965c52661c05976d3072ccb6a3"
Expand Down

0 comments on commit ed6efb1

Please sign in to comment.