forked from pzmosquito/eslint-import-resolver-vite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
83 lines (74 loc) · 2.42 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import path from "path";
import { sync,isCore} from "resolve";
const log = require("debug")("eslint-plugin-import:resolver:vite");
/**
* enable the esbuild-register to read .ts config,this is my Special version,
* which use build api instand of transform api with esbuild,see:
* https://github.com/AisonSu/esbuild-register-build
*/
import { register } from "./esbuild-register/node";
/**
* From https://github.com/evanw/esbuild/issues/1492
* Use inject and define to slove the import.meta.url translate problem
*/
const { unregister } = register({
inject: [`${__dirname}/importurl.js`],
define: { "import.meta.url": "import_meta_url" },
});
export interface pluginConfig {
configPath?: string;
namedExport?: string;
}
export const interfaceVersion = 2;
export const resolve = function resolve(
targetIdentify: string,
importedPath: string,
config: pluginConfig
): { found: boolean; path: string|null} {
log("resolving:", targetIdentify);
log("in file:", importedPath);
if(isCore(targetIdentify)){
log(`${targetIdentify} is a core module`)
return {found:true,path:null}
}
try {
// combine default config with user defined config
const configs = {
configPath: "vite.config",
...config,
};
let viteConfigFile;
configs.configPath = path.resolve(configs.configPath);
viteConfigFile = require(configs.configPath);
log("required vite config in:", configs.configPath);
let viteConfig;
if (configs.namedExport) {
viteConfig = viteConfigFile[configs.namedExport];
} else {
viteConfig =
typeof viteConfigFile.default === "function"
? viteConfigFile.default()
: viteConfigFile.default;
}
const defaultExtensions = [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"];
const { alias, extensions } = viteConfig.resolve ?? {};
let actualSource = targetIdentify;
// parse and replace alias
if (alias) {
Object.entries(alias as Record<string, string>).forEach(
([find, replacement]) => {
actualSource = actualSource.replace(find, replacement);
}
);
}
const resolvedPath = sync(actualSource, {
basedir: path.dirname(path.resolve(importedPath)),
extensions: extensions ?? defaultExtensions,
});
log("resolved to:", resolvedPath);
return { found: true, path: resolvedPath };
} catch (err) {
log("Path not found");
return { found: false,path:null};
}
};