Skip to content

Commit 3246faf

Browse files
pzmosquitoSukkaW
andauthored
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 <github@skk.moe>
1 parent 97b8111 commit 3246faf

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# eslint-import-resolver-vite
22

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

55

66
### Installation
@@ -24,9 +24,11 @@ export const viteConfigObj = {
2424

2525
#### ESLint config file
2626
NOTE:
27-
- 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.
27+
- Since ESLint requires rules to be synchronous, Vite's [ResolvedConfig API](https://vitejs.dev/guide/api-javascript.html#resolvedconfig) cannot be utilized.
2828
- This plugin accepts a Vite config object to accommodate various setups, e.g. CJS, ESM, or mixed.
29+
2930
```js
31+
// for using `eslint-plugin-import`
3032
module.exports = {
3133
settings: {
3234
"import/resolver": {
@@ -37,4 +39,17 @@ module.exports = {
3739
}
3840
}
3941

42+
43+
// for using `eslint-plugin-import-x` resolver interface v3
44+
const { createViteImportResolver } = require("eslint-import-resolver-vite");
45+
46+
module.exports = {
47+
settings: {
48+
"import-x/resolver-next": [
49+
createViteImportResolver({
50+
viteConfig: require("./vite.config").viteConfigObj, // named export of the Vite config object.
51+
})
52+
]
53+
}
54+
}
4055
```

index.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ const resolveSync = (source, resolveOptions, label) => {
4141
exports.interfaceVersion = 2;
4242

4343
exports.resolve = (source, file, config) => {
44+
const { viteConfig } = config;
45+
if (!viteConfig) {
46+
throw new Error("'viteConfig' option must be a vite config object.");
47+
}
48+
4449
log("\nin file:\t", file);
4550

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

51-
const { viteConfig } = config;
52-
if (!viteConfig) {
53-
throw new Error("'viteConfig' option must be a vite config object.");
54-
}
55-
5656
const defaultExtensions = [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"];
5757
const { alias, extensions = defaultExtensions } = viteConfig.resolve ?? {};
5858
const resolveOptions = { basedir: path.dirname(file), extensions };
@@ -95,3 +95,12 @@ exports.resolve = (source, file, config) => {
9595
log("ERROR:\t", "Unable to resolve");
9696
return { found: false };
9797
};
98+
99+
// for `eslint-plugin-import-x` resolver interface v3
100+
exports.createViteImportResolver = (config) => {
101+
return {
102+
interfaceVersion: 3,
103+
name: "eslint-import-resolver-vite",
104+
resolve: (source, file) => exports.resolve(source, file, config)
105+
};
106+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "eslint-import-resolver-vite",
3-
"version": "2.0.1",
4-
"description": "Vite module resolution plugin for eslint-plugin-import.",
3+
"version": "2.1.0",
4+
"description": "Vite module resolution plugin for eslint-plugin-import and eslint-plugin-import-x.",
55
"keywords": [
66
"eslint",
77
"vite",

0 commit comments

Comments
 (0)