-
Notifications
You must be signed in to change notification settings - Fork 0
/
moduleList.ts
59 lines (58 loc) 路 1.6 KB
/
moduleList.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
import type { PluginOption } from "vite";
import type { ModuleListOptions } from "../types";
import { resolve } from "path";
import { normalizeMode } from "./normalizeMode";
import { writeModuleList } from "./writeModuleList";
/**
* Vite plugin that writes a module that imports modules found in a folder.
*
* @param options See {@link ModuleListOptions}
* @returns A vite plugin that writes a module that imports modules found in a folder.
*/
export function moduleList({
mode: currentMode = "js",
rootPath = ".",
includeExtensions = ["js", "ts", "jsx", "tsx"],
include = /(?:)/,
exclude = /\.(?:tests?|spec)\.[^.]+$/,
outputPath = `${rootPath}/main.ts`,
formatOptions,
}: ModuleListOptions): PluginOption {
const resolvedRootPath = resolve(rootPath);
const mode = normalizeMode(currentMode);
return {
name: "module-list",
async configureServer(server) {
await writeModuleList(
rootPath,
includeExtensions,
include,
exclude,
outputPath,
formatOptions,
mode,
);
server.watcher.on("all", async (eventName, filePath) => {
if (eventName !== "add" && eventName !== "unlink") {
return;
}
const resolvedFilePath = resolve(filePath);
if (
resolvedFilePath === outputPath ||
!resolvedFilePath.startsWith(resolvedRootPath)
) {
return;
}
await writeModuleList(
rootPath,
includeExtensions,
include,
exclude,
outputPath,
formatOptions,
mode,
);
});
},
};
}