forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-plugin.js
More file actions
44 lines (35 loc) · 1 KB
/
load-plugin.js
File metadata and controls
44 lines (35 loc) · 1 KB
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
import path from "node:path";
import { pathToFileURL } from "node:url";
import importFromDirectory from "../../utils/import-from-directory.js";
async function importPlugin(name, cwd) {
if (path.isAbsolute(name)) {
return import(pathToFileURL(name).href);
}
try {
// try local files
return await import(pathToFileURL(path.resolve(name)).href);
} catch {
// try node modules
return importFromDirectory(name, cwd);
}
}
async function loadPluginWithoutCache(plugin, cwd) {
const module = await importPlugin(plugin, cwd);
return { name: plugin, ...(module.default ?? module) };
}
const cache = new Map();
function loadPlugin(plugin) {
if (typeof plugin !== "string") {
return plugin;
}
const cwd = process.cwd();
const cacheKey = JSON.stringify({ name: plugin, cwd });
if (!cache.has(cacheKey)) {
cache.set(cacheKey, loadPluginWithoutCache(plugin, cwd));
}
return cache.get(cacheKey);
}
function clearCache() {
cache.clear();
}
export { clearCache, loadPlugin };