Skip to content

Commit 6fb8ff1

Browse files
committed
support array type alias
1 parent 3a657d9 commit 6fb8ff1

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,27 @@ exports.resolve = (source, file, config) => {
5252

5353
// parse and replace alias
5454
if (alias) {
55-
Object.entries(alias).forEach(([find, replacement]) => {
56-
actualSource = actualSource.replace(find, replacement);
57-
});
55+
const pathParts = actualSource.split(path.sep);
56+
if (Array.isArray(alias)) {
57+
for (let i = 0; i < pathParts.length; i++) {
58+
alias.forEach(({find, replacement}) => {
59+
if (pathParts[i] === find) {
60+
pathParts[i] = replacement;
61+
}
62+
});
63+
}
64+
}
65+
else if (typeof alias === "object") {
66+
for (let i = 0; i < pathParts.length; i++) {
67+
if (alias.hasOwnProperty(pathParts[i])) {
68+
pathParts[i] = alias[pathParts[i]];
69+
}
70+
}
71+
}
72+
else {
73+
throw new Error("The alias must be either an object, or an array of objects.");
74+
}
75+
actualSource = pathParts.join(path.sep);
5876
}
5977

6078
// public dir

index.test.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,69 @@ describe("Resolver Plugin Tests", () => {
2525
extensions: [".js"],
2626
alias: {
2727
"_": "/path/to/src",
28+
"@": "assets/images",
2829
},
2930
},
3031
},
3132
}), { virtual: true });
3233

3334
// JS module
34-
let result = resolver.resolve("_/module", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });
35+
let result = resolver.resolve("_/@/_module@", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });
36+
37+
expect(result.found).toBe(true);
38+
expect(result.path).toBe("/path/to/resolved.js");
39+
expect(resolve.sync).toHaveBeenCalledWith(
40+
"/path/to/src/assets/images/_module@",
41+
{ basedir: "/path/to", extensions: [".js"] }
42+
);
43+
});
44+
45+
test("should resolve non-core module (array alias pairs)", () => {
46+
resolve.sync = jest.fn(() => "/path/to/resolved.js");
47+
fs.existsSync = jest.fn(() => true);
48+
49+
jest.mock("/path/to/vite.config.js", () => ({
50+
default: {
51+
resolve: {
52+
extensions: [".js"],
53+
alias: [
54+
{ find: "_", replacement: "/path/to/src" },
55+
{ find: "@", replacement: "assets/images" },
56+
],
57+
},
58+
},
59+
}), { virtual: true });
60+
61+
// JS module
62+
let result = resolver.resolve("_/@/_module@", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });
3563

3664
expect(result.found).toBe(true);
3765
expect(result.path).toBe("/path/to/resolved.js");
3866
expect(resolve.sync).toHaveBeenCalledWith(
39-
"/path/to/src/module",
67+
"/path/to/src/assets/images/_module@",
4068
{ basedir: "/path/to", extensions: [".js"] }
4169
);
4270
});
4371

72+
test("should throw error when alias type is invalid", () => {
73+
resolve.sync = jest.fn(() => "/path/to/resolved.js");
74+
fs.existsSync = jest.fn(() => true);
75+
76+
jest.mock("/path/to/vite.config.js", () => ({
77+
default: {
78+
resolve: {
79+
extensions: [".js"],
80+
alias: "test",
81+
},
82+
},
83+
}), { virtual: true });
84+
85+
// JS module
86+
let result = resolver.resolve("_/module", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });
87+
88+
expect(result.found).toBe(false);
89+
});
90+
4491
test("should resolve non-core module with publicDir", () => {
4592
resolve.sync = jest.fn(() => "/path/to/resolved.js");
4693
fs.existsSync = jest.fn((param) => {

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.2.1",
3+
"version": "1.3.0",
44
"description": "Vite module resolution plugin for eslint-plugin-import.",
55
"keywords": [
66
"eslint",

0 commit comments

Comments
 (0)