|
1 | 1 | const path = require("path");
|
2 | 2 | const resolve = require("resolve");
|
3 |
| -const fs = require("fs"); |
4 | 3 | const debug = require("debug");
|
5 |
| - |
6 | 4 | const namespace = "eslint-plugin-import:resolver:vite";
|
7 |
| - |
8 | 5 | const log = debug(namespace);
|
9 | 6 |
|
10 |
| -const logError = (message) => { |
11 |
| - log(message); |
12 |
| - console.error(`[${namespace}] ${message}`); |
| 7 | +const processAlias = (alias, source) => { |
| 8 | + if (alias) { |
| 9 | + const pathParts = path.normalize(source).split(path.sep); |
| 10 | + if (Array.isArray(alias)) { |
| 11 | + for (let i = 0; i < pathParts.length; i++) { |
| 12 | + alias.forEach(({find, replacement}) => { |
| 13 | + if (pathParts[i] === find) { |
| 14 | + pathParts[i] = replacement; |
| 15 | + } |
| 16 | + }); |
| 17 | + } |
| 18 | + } |
| 19 | + else if (typeof alias === "object") { |
| 20 | + for (let i = 0; i < pathParts.length; i++) { |
| 21 | + if (alias.hasOwnProperty(pathParts[i])) { |
| 22 | + pathParts[i] = alias[pathParts[i]]; |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + else { |
| 27 | + throw new Error("The alias must be either an object, or an array of objects."); |
| 28 | + } |
| 29 | + return pathParts.join(path.sep); |
| 30 | + } |
| 31 | + return source; |
| 32 | +}; |
| 33 | + |
| 34 | +const resolveSync = (source, resolveOptions, label) => { |
| 35 | + log("resolving:\t", `(${label})`, source); |
| 36 | + const resolvedPath = resolve.sync(source, resolveOptions); |
| 37 | + log("resolved:\t", resolvedPath); |
| 38 | + return { found: true, path: resolvedPath }; |
13 | 39 | };
|
14 | 40 |
|
15 | 41 | exports.interfaceVersion = 2;
|
16 | 42 |
|
17 | 43 | exports.resolve = (source, file, config) => {
|
18 |
| - log("resolving:", source); |
19 |
| - log("in file:", file); |
| 44 | + log("\nin file:\t", file); |
20 | 45 |
|
21 | 46 | if (resolve.isCore(source)) {
|
| 47 | + log("resolved:\t", source); |
22 | 48 | return { found: true, path: null };
|
23 | 49 | }
|
24 | 50 |
|
25 |
| - try { |
26 |
| - // combine default config with user defined config |
27 |
| - const pluginConfig = { |
28 |
| - configPath: "vite.config.js", |
29 |
| - ...(config ?? {}), |
30 |
| - }; |
31 |
| - |
32 |
| - // load vite config |
33 |
| - const viteConfigPath = path.resolve(pluginConfig.configPath); |
34 |
| - if (!fs.existsSync(viteConfigPath)) { |
35 |
| - throw new Error(`Vite config file doesn't exist at '${viteConfigPath}'`) |
36 |
| - } |
37 |
| - const viteConfigFile = require(viteConfigPath); |
38 |
| - |
39 |
| - let viteConfig; |
40 |
| - if (pluginConfig.namedExport) { |
41 |
| - viteConfig = viteConfigFile[pluginConfig.namedExport] |
42 |
| - } |
43 |
| - else { |
44 |
| - const viteConfigObj = viteConfigFile.default ?? viteConfigFile |
45 |
| - viteConfig = typeof viteConfigObj === "function" ? viteConfigObj() : viteConfigObj; |
46 |
| - } |
| 51 | + const { viteConfig } = config; |
| 52 | + if (!viteConfig) { |
| 53 | + throw new Error("'viteConfig' option must be a vite config object."); |
| 54 | + } |
47 | 55 |
|
48 |
| - const defaultExtensions = [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"]; |
49 |
| - const { alias, extensions = defaultExtensions } = viteConfig.resolve ?? {}; |
| 56 | + const defaultExtensions = [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"]; |
| 57 | + const { alias, extensions = defaultExtensions } = viteConfig.resolve ?? {}; |
| 58 | + const resolveOptions = { basedir: path.dirname(file), extensions }; |
50 | 59 |
|
51 |
| - let actualSource = path.normalize(source); |
| 60 | + // try to resolve the source as is |
| 61 | + try { |
| 62 | + return resolveSync(source, resolveOptions, "as is"); |
| 63 | + } |
| 64 | + catch {} |
52 | 65 |
|
53 |
| - // parse and replace alias |
54 |
| - if (alias) { |
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); |
| 66 | + // try to resolve the source with alias |
| 67 | + const parsedSource = processAlias(alias, source); |
| 68 | + if (parsedSource !== source) { |
| 69 | + try { |
| 70 | + return resolveSync(parsedSource, resolveOptions, "with alias"); |
76 | 71 | }
|
| 72 | + catch {} |
| 73 | + } |
77 | 74 |
|
78 |
| - // resolve module |
79 |
| - let resolvedPath = ""; |
| 75 | + // try to resolve the source if it is an absolute path |
| 76 | + if (path.isAbsolute(parsedSource)) { |
| 77 | + const root = viteConfig.root ?? process.cwd(); |
| 78 | + const absoluteSource = path.join(path.resolve(root), parsedSource); |
80 | 79 | try {
|
81 |
| - resolvedPath = resolve.sync(actualSource, { |
82 |
| - basedir: path.dirname(file), |
83 |
| - extensions, |
84 |
| - }); |
85 |
| - } |
86 |
| - catch (err) { |
87 |
| - if (viteConfig.publicDir !== false) { |
88 |
| - const publicDir = viteConfig.publicDir ?? "public"; |
89 |
| - const publicActualSource = path.join(path.resolve(publicDir), actualSource); |
90 |
| - resolvedPath = resolve.sync(publicActualSource, { |
91 |
| - basedir: path.dirname(file), |
92 |
| - extensions, |
93 |
| - }); |
94 |
| - } |
95 |
| - else { |
96 |
| - throw new Error("source cannot be resolved in actual path nor in 'Public' path."); |
97 |
| - } |
| 80 | + return resolveSync(absoluteSource, resolveOptions, "absolute path"); |
98 | 81 | }
|
99 |
| - |
100 |
| - log("resolved to:", resolvedPath); |
101 |
| - return { found: true, path: resolvedPath }; |
| 82 | + catch {} |
102 | 83 | }
|
103 |
| - catch (err) { |
104 |
| - logError(err.message); |
105 |
| - return { found: false }; |
| 84 | + |
| 85 | + // try to resolve the source in public directory if all above failed |
| 86 | + if (viteConfig.publicDir !== false) { |
| 87 | + const publicDir = viteConfig.publicDir ?? "public"; |
| 88 | + const publicSource = path.join(path.resolve(publicDir), parsedSource); |
| 89 | + try { |
| 90 | + return resolveSync(publicSource, resolveOptions, "in public directory"); |
| 91 | + } |
| 92 | + catch {} |
106 | 93 | }
|
| 94 | + |
| 95 | + log("ERROR:\t", "Unable to resolve"); |
| 96 | + return { found: false }; |
107 | 97 | };
|
0 commit comments