Skip to content

Commit

Permalink
add support for CJS vite config
Browse files Browse the repository at this point in the history
  • Loading branch information
pzmosquito committed Jun 23, 2023
1 parent a91c0f3 commit 8122440
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ exports.resolve = (source, file, config) => {
viteConfig = viteConfigFile[pluginConfig.namedExport]
}
else {
viteConfig = typeof viteConfigFile.default === "function" ? viteConfigFile.default() : viteConfigFile.default;
const viteConfigObj = viteConfigFile.default ?? viteConfigFile
viteConfig = typeof viteConfigObj === "function" ? viteConfigObj() : viteConfigObj;
}

const defaultExtensions = [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"];
Expand Down
32 changes: 28 additions & 4 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe("Resolver Plugin Tests", () => {
expect(result.path).toBe(null);
});

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

jest.mock("/path/to/vite.config.js", () => ({
default: {
Expand All @@ -29,10 +29,34 @@ describe("Resolver Plugin Tests", () => {
"_": "/path/to/src",
},
},
}
},
}), { virtual: true });

const result = resolver.resolve("_/module", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });
// JS module
let result = resolver.resolve("_/module", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });

expect(result.found).toBe(true);
expect(result.path).toBe("/path/to/resolved.js");
expect(resolve.sync).toHaveBeenCalledWith("/path/to/src/module", {
basedir: "/path/to",
extensions: [".js"],
});
});

test("should resolve non-core module with CJS vite config", () => {
resolve.sync = jest.fn(() => "/path/to/resolved.js");

jest.mock("/path/to/vite.config.js", () => ({
resolve: {
extensions: [".js"],
alias: {
"_": "/path/to/src",
},
},
}), { virtual: true });

// JS module
let result = resolver.resolve("_/module", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });

expect(result.found).toBe(true);
expect(result.path).toBe("/path/to/resolved.js");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-import-resolver-vite",
"version": "1.0.4",
"version": "1.1.0",
"description": "Vite module resolution plugin for eslint-plugin-import.",
"keywords": [
"eslint",
Expand Down

0 comments on commit 8122440

Please sign in to comment.