Skip to content

Commit

Permalink
add support for eslint-plugin-import-x resolver interface v3 (#28)
Browse files Browse the repository at this point in the history
* implements `import-x` resolver interface version 3 (#26)

* add support for eslint-plugin-import-x resolver interface v3

---------

Co-authored-by: Sukka <[email protected]>
  • Loading branch information
pzmosquito and SukkaW authored Dec 18, 2024
1 parent 97b8111 commit 3246faf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# eslint-import-resolver-vite

Vite module resolution plugin for `eslint-plugin-import`. This plugin will resolve the `resolve.alias` option.
Vite module resolution plugin for `eslint-plugin-import` and `eslint-plugin-import-x`. This plugin will resolve the `resolve.alias` option.


### Installation
Expand All @@ -24,9 +24,11 @@ export const viteConfigObj = {

#### ESLint config file
NOTE:
- Since `eslint-plugin-import` doesn't support an async resolver, Vite's [ResolvedConfig API](https://vitejs.dev/guide/api-javascript.html#resolvedconfig) cannot be utilized.
- Since ESLint requires rules to be synchronous, Vite's [ResolvedConfig API](https://vitejs.dev/guide/api-javascript.html#resolvedconfig) cannot be utilized.
- This plugin accepts a Vite config object to accommodate various setups, e.g. CJS, ESM, or mixed.

```js
// for using `eslint-plugin-import`
module.exports = {
settings: {
"import/resolver": {
Expand All @@ -37,4 +39,17 @@ module.exports = {
}
}


// for using `eslint-plugin-import-x` resolver interface v3
const { createViteImportResolver } = require("eslint-import-resolver-vite");

module.exports = {
settings: {
"import-x/resolver-next": [
createViteImportResolver({
viteConfig: require("./vite.config").viteConfigObj, // named export of the Vite config object.
})
]
}
}
```
19 changes: 14 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ const resolveSync = (source, resolveOptions, label) => {
exports.interfaceVersion = 2;

exports.resolve = (source, file, config) => {
const { viteConfig } = config;
if (!viteConfig) {
throw new Error("'viteConfig' option must be a vite config object.");
}

log("\nin file:\t", file);

if (resolve.isCore(source)) {
log("resolved:\t", source);
return { found: true, path: null };
}

const { viteConfig } = config;
if (!viteConfig) {
throw new Error("'viteConfig' option must be a vite config object.");
}

const defaultExtensions = [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"];
const { alias, extensions = defaultExtensions } = viteConfig.resolve ?? {};
const resolveOptions = { basedir: path.dirname(file), extensions };
Expand Down Expand Up @@ -95,3 +95,12 @@ exports.resolve = (source, file, config) => {
log("ERROR:\t", "Unable to resolve");
return { found: false };
};

// for `eslint-plugin-import-x` resolver interface v3
exports.createViteImportResolver = (config) => {
return {
interfaceVersion: 3,
name: "eslint-import-resolver-vite",
resolve: (source, file) => exports.resolve(source, file, config)
};
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "eslint-import-resolver-vite",
"version": "2.0.1",
"description": "Vite module resolution plugin for eslint-plugin-import.",
"version": "2.1.0",
"description": "Vite module resolution plugin for eslint-plugin-import and eslint-plugin-import-x.",
"keywords": [
"eslint",
"vite",
Expand Down

0 comments on commit 3246faf

Please sign in to comment.