From 3246fafaa468f0260346af6ad603bb6be6213b10 Mon Sep 17 00:00:00 2001 From: Peter Zhang Date: Tue, 17 Dec 2024 16:06:18 -0800 Subject: [PATCH] add support for eslint-plugin-import-x resolver interface v3 (#28) * implements `import-x` resolver interface version 3 (#26) * add support for eslint-plugin-import-x resolver interface v3 --------- Co-authored-by: Sukka --- README.md | 19 +++++++++++++++++-- index.js | 19 ++++++++++++++----- package.json | 4 ++-- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 60ea966..0c65c0d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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": { @@ -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. + }) + ] + } +} ``` diff --git a/index.js b/index.js index 44f5777..48b0e52 100644 --- a/index.js +++ b/index.js @@ -41,6 +41,11 @@ 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)) { @@ -48,11 +53,6 @@ exports.resolve = (source, file, config) => { 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 }; @@ -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) + }; +} diff --git a/package.json b/package.json index 556a0e5..c51c521 100644 --- a/package.json +++ b/package.json @@ -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",