Skip to content

Commit 8122440

Browse files
committed
add support for CJS vite config
1 parent a91c0f3 commit 8122440

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ exports.resolve = (source, file, config) => {
4141
viteConfig = viteConfigFile[pluginConfig.namedExport]
4242
}
4343
else {
44-
viteConfig = typeof viteConfigFile.default === "function" ? viteConfigFile.default() : viteConfigFile.default;
44+
const viteConfigObj = viteConfigFile.default ?? viteConfigFile
45+
viteConfig = typeof viteConfigObj === "function" ? viteConfigObj() : viteConfigObj;
4546
}
4647

4748
const defaultExtensions = [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"];

index.test.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ describe("Resolver Plugin Tests", () => {
1818
expect(result.path).toBe(null);
1919
});
2020

21-
test("should resolve non-core module", () => {
22-
resolve.sync = jest.fn((source) => "/path/to/resolved.js");
21+
test("should resolve non-core module with ESM vite config", () => {
22+
resolve.sync = jest.fn(() => "/path/to/resolved.js");
2323

2424
jest.mock("/path/to/vite.config.js", () => ({
2525
default: {
@@ -29,10 +29,34 @@ describe("Resolver Plugin Tests", () => {
2929
"_": "/path/to/src",
3030
},
3131
},
32-
}
32+
},
3333
}), { virtual: true });
3434

35-
const result = resolver.resolve("_/module", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });
35+
// JS module
36+
let result = resolver.resolve("_/module", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });
37+
38+
expect(result.found).toBe(true);
39+
expect(result.path).toBe("/path/to/resolved.js");
40+
expect(resolve.sync).toHaveBeenCalledWith("/path/to/src/module", {
41+
basedir: "/path/to",
42+
extensions: [".js"],
43+
});
44+
});
45+
46+
test("should resolve non-core module with CJS vite config", () => {
47+
resolve.sync = jest.fn(() => "/path/to/resolved.js");
48+
49+
jest.mock("/path/to/vite.config.js", () => ({
50+
resolve: {
51+
extensions: [".js"],
52+
alias: {
53+
"_": "/path/to/src",
54+
},
55+
},
56+
}), { virtual: true });
57+
58+
// JS module
59+
let result = resolver.resolve("_/module", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });
3660

3761
expect(result.found).toBe(true);
3862
expect(result.path).toBe("/path/to/resolved.js");

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-import-resolver-vite",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "Vite module resolution plugin for eslint-plugin-import.",
55
"keywords": [
66
"eslint",

0 commit comments

Comments
 (0)