Skip to content

Commit 3a657d9

Browse files
committed
fix rare error when publicDir path has character in the 'alias' option
1 parent 6eb417c commit 3a657d9

File tree

4 files changed

+85
-28
lines changed

4 files changed

+85
-28
lines changed

index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const log = debug(namespace);
99

1010
const logError = (message) => {
1111
log(message);
12-
console.log(`[${namespace}] ${message}`);
12+
console.error(`[${namespace}] ${message}`);
1313
};
1414

1515
exports.interfaceVersion = 2;
@@ -32,7 +32,7 @@ exports.resolve = (source, file, config) => {
3232
// load vite config
3333
const viteConfigPath = path.resolve(pluginConfig.configPath);
3434
if (!fs.existsSync(viteConfigPath)) {
35-
logError(`vite config file doesn't exist at '${viteConfigPath}'`);
35+
throw new Error(`Vite config file doesn't exist at '${viteConfigPath}'`)
3636
}
3737
const viteConfigFile = require(viteConfigPath);
3838

@@ -50,19 +50,22 @@ exports.resolve = (source, file, config) => {
5050

5151
let actualSource = source;
5252

53-
// public dir
54-
const publicDir = viteConfig.publicDir || "public";
55-
if (actualSource.charAt(0) === "/" && !fs.existsSync(actualSource)) {
56-
actualSource = path.join(path.resolve(publicDir), actualSource);
57-
}
58-
5953
// parse and replace alias
6054
if (alias) {
6155
Object.entries(alias).forEach(([find, replacement]) => {
6256
actualSource = actualSource.replace(find, replacement);
6357
});
6458
}
6559

60+
// public dir
61+
// TODO this can lead to incorrect path error if `actualSource` is absolute path and DNE.
62+
if (viteConfig.publicDir !== false) {
63+
const publicDir = viteConfig.publicDir ?? "public";
64+
if (actualSource.charAt(0) === "/" && !fs.existsSync(actualSource)) {
65+
actualSource = path.join(path.resolve(publicDir), actualSource);
66+
}
67+
}
68+
6669
// resolve module
6770
const resolvedPath = resolve.sync(actualSource, {
6871
basedir: path.dirname(file),
@@ -73,7 +76,7 @@ exports.resolve = (source, file, config) => {
7376
return { found: true, path: resolvedPath };
7477
}
7578
catch (err) {
76-
log("Path not found:", err);
79+
logError(err.message);
7780
return { found: false };
7881
}
7982
};

index.test.js

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
const resolve = require("resolve");
2+
const fs = require("fs");
23
const resolver = require("./index");
34

45
jest.mock("resolve");
56
jest.mock("fs");
67

78
describe("Resolver Plugin Tests", () => {
8-
beforeEach(() => {
9-
jest.restoreAllMocks();
10-
});
11-
129
test("should resolve core module", () => {
1310
resolve.isCore.mockReturnValue(true);
1411

@@ -18,8 +15,9 @@ describe("Resolver Plugin Tests", () => {
1815
expect(result.path).toBe(null);
1916
});
2017

21-
test("should resolve non-core module with ESM vite config", () => {
18+
test("should resolve non-core module", () => {
2219
resolve.sync = jest.fn(() => "/path/to/resolved.js");
20+
fs.existsSync = jest.fn(() => true);
2321

2422
jest.mock("/path/to/vite.config.js", () => ({
2523
default: {
@@ -37,39 +35,89 @@ describe("Resolver Plugin Tests", () => {
3735

3836
expect(result.found).toBe(true);
3937
expect(result.path).toBe("/path/to/resolved.js");
40-
expect(resolve.sync).toHaveBeenCalledWith("/path/to/src/module", {
41-
basedir: "/path/to",
42-
extensions: [".js"],
38+
expect(resolve.sync).toHaveBeenCalledWith(
39+
"/path/to/src/module",
40+
{ basedir: "/path/to", extensions: [".js"] }
41+
);
42+
});
43+
44+
test("should resolve non-core module with publicDir", () => {
45+
resolve.sync = jest.fn(() => "/path/to/resolved.js");
46+
fs.existsSync = jest.fn((param) => {
47+
if (param === "/path/to/vite.config.js") {
48+
return true;
49+
}
50+
return false;
4351
});
52+
53+
jest.mock("/path/to/vite.config.js", () => ({
54+
default: {
55+
resolve: {
56+
extensions: [".js"],
57+
alias: {
58+
"_": "/path/to/src",
59+
},
60+
},
61+
publicDir: "/path/to/public",
62+
},
63+
}), { virtual: true });
64+
65+
// JS module
66+
const result = resolver.resolve("/_/module", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });
67+
68+
expect(result.found).toBe(true);
69+
expect(result.path).toBe("/path/to/resolved.js");
70+
expect(resolve.sync).toHaveBeenCalledWith(
71+
"/path/to/public/path/to/src/module",
72+
{ basedir: "/path/to", extensions: [".js"] }
73+
);
4474
});
4575

46-
test("should resolve non-core module with CJS vite config", () => {
76+
test("should resolve non-core module with absolute path", () => {
4777
resolve.sync = jest.fn(() => "/path/to/resolved.js");
78+
fs.existsSync = jest.fn(() => true);
4879

4980
jest.mock("/path/to/vite.config.js", () => ({
50-
resolve: {
51-
extensions: [".js"],
52-
alias: {
53-
"_": "/path/to/src",
81+
default: {
82+
resolve: {
83+
extensions: [".js"],
84+
alias: {
85+
"_": "/path/to/src",
86+
},
5487
},
88+
publicDir: "/path/to/public",
5589
},
5690
}), { virtual: true });
5791

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

6195
expect(result.found).toBe(true);
6296
expect(result.path).toBe("/path/to/resolved.js");
63-
expect(resolve.sync).toHaveBeenCalledWith("/path/to/src/module", {
64-
basedir: "/path/to",
65-
extensions: [".js"],
97+
expect(resolve.sync).toHaveBeenCalledWith(
98+
"/path/to/module",
99+
{ basedir: "/path/to", extensions: [".js"] }
100+
);
101+
});
102+
103+
test("should handle vite config DNE error", () => {
104+
fs.existsSync = jest.fn((param) => {
105+
if (param === "/path/to/vite.config.js") {
106+
return false;
107+
}
108+
return true;
66109
});
110+
111+
const result = resolver.resolve("module", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });
112+
113+
expect(result.found).toBe(false);
67114
});
68115

69116
test("should handle resolve error", () => {
70117
resolve.sync = jest.fn(() => {
71118
throw new Error("Resolve error");
72119
});
120+
fs.existsSync = jest.fn((param) => true);
73121

74122
const result = resolver.resolve("module", "/path/to/file.js", {});
75123

jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
clearMocks: true,
3+
resetMocks: true,
4+
restoreMocks: true,
5+
resetModules: true,
6+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-import-resolver-vite",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Vite module resolution plugin for eslint-plugin-import.",
55
"keywords": [
66
"eslint",
@@ -32,6 +32,6 @@
3232
"jest": "^29.5.0"
3333
},
3434
"scripts": {
35-
"test": "jest"
35+
"test": "jest --watchAll"
3636
}
3737
}

0 commit comments

Comments
 (0)