-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
155 lines (149 loc) · 4.69 KB
/
vite.config.ts
File metadata and controls
155 lines (149 loc) · 4.69 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { exec } from "node:child_process";
import { readdir, readFile } from "node:fs/promises";
import UnpluginTypia from "@ryoppippi/unplugin-typia/vite";
import tailwindcss from "@tailwindcss/vite";
import { devtools } from "@tanstack/devtools-vite";
import { tanstackRouter } from "@tanstack/router-plugin/vite";
import viteReact from "@vitejs/plugin-react";
import { regex } from "arkregex";
import { defineConfig } from "vite";
const GITHUB_REPO_URL_REGEX = regex(
"^git\\+https://github\\.com/(?<owner>[\\w-]+)/(?<repo>[\\w.-]+)\\.git$",
);
// https://vitejs.dev/config/
export default defineConfig({
base: "/",
plugins: [
UnpluginTypia(),
devtools(),
tanstackRouter({
target: "react",
autoCodeSplitting: true,
}),
viteReact({
babel: {
plugins: ["babel-plugin-react-compiler"],
},
}),
tailwindcss(),
{
name: "colllect-license-info",
resolveId: {
filter: { id: /^virtual:licenses-info$/g },
handler(source) {
if (source === "virtual:licenses-info") {
return {
id: `virtual:licenses-info\0`,
};
}
return null;
},
},
load: {
filter: { id: /^virtual:licenses-info\0$/g },
async handler(id) {
if (id === "virtual:licenses-info\0") {
const lisenses = await new Promise<Record<string, PnpmLicense[]>>(
(resolve, reject) => {
exec(
"pnpm licenses list --json -P --no-optional",
(error, stdout) => {
if (error) {
reject(error);
return;
}
resolve(JSON.parse(stdout));
},
);
},
);
return `export default ${JSON.stringify(
Object.fromEntries(
await Promise.all(
Object.entries(lisenses).map(
async ([license, packageInfos]) =>
[
license,
await Promise.all(
packageInfos.map(
async ({ paths, ...packageInfo }) => {
for (const path of paths.toReversed()) {
const files = await readdir(path);
const licenseFile = files.find((f) =>
f.toUpperCase().startsWith("LICENSE"),
);
if (licenseFile) {
const licenseText = await readFile(
`${path}/${licenseFile}`,
"utf-8",
);
packageInfo.licenseText = licenseText;
break;
}
}
if (!packageInfo.licenseText) {
const packageJson = await readFile(
`${paths.at(-1)}/package.json`,
"utf-8",
);
const packageData = JSON.parse(packageJson);
const repoMatch = GITHUB_REPO_URL_REGEX.exec(
packageData.repository?.url ?? "",
);
if (!repoMatch) {
console.warn(
`License file not found for package ${packageInfo.name}. Repo URL not found or invalid.`,
);
packageInfo.licenseText =
"License text not found.";
return packageInfo;
}
const { owner, repo } = repoMatch.groups;
for (const licenseUrl of [
`https://raw.githubusercontent.com/${owner}/${repo}/main/LICENSE`,
`https://raw.githubusercontent.com/${owner}/${repo}/main/LICENSE.md`,
`https://raw.githubusercontent.com/${owner}/${repo}/main/LICENSE.txt`,
`https://raw.githubusercontent.com/${owner}/${repo}/master/LICENSE`,
`https://raw.githubusercontent.com/${owner}/${repo}/master/LICENSE.md`,
`https://raw.githubusercontent.com/${owner}/${repo}/master/LICENSE.txt`,
]) {
const response = await fetch(licenseUrl);
if (response.ok) {
const licenseText = await response.text();
packageInfo.licenseText = licenseText;
break;
}
}
if (!packageInfo.licenseText) {
console.warn(
`License file not found for package ${packageInfo.name}. Checked GitHub paths.`,
);
packageInfo.licenseText =
"License text not found.";
}
}
return packageInfo;
},
),
),
] as const,
),
),
),
)};`;
}
},
},
},
],
});
interface PnpmLicense {
name: string;
versions: string[];
paths: string[];
license: string;
homepage?: string;
description?: string;
author?: string;
licenseText?: string;
}