-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
198 lines (184 loc) · 5.02 KB
/
vite.config.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { fileURLToPath, URL } from "node:url";
import { globSync } from "glob";
import path from "node:path";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import dts from "vite-plugin-dts";
// https://vitejs.dev/config/
const baseConfig = {
plugins: [vue()],
base: "./",
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
"~": path.resolve(__dirname),
},
},
};
// 将图标分组为块的函数
function getGroupedIconChunks() {
const iconDirectory = path.resolve(__dirname, "src/icons");
const iconFiles = globSync(`${iconDirectory}/*/*.js`);
const chunks = {};
const groupSize = 150; //根据需要调整组大小
for (let i = 0; i < iconFiles.length; i += groupSize) {
const chunkName = `icons/group${i / groupSize}`;
const group = iconFiles.slice(i, i + groupSize);
chunks[chunkName] = group;
}
return chunks;
}
const siteConfig = {
...baseConfig,
build: {
outDir: "dist",
emptyOutDir: true,
rollupOptions: {
input: {
main: path.resolve(__dirname, "index.html"),
iframe: path.resolve(__dirname, "iframe.html"),
},
output: {
manualChunks(id) {
// if (id.includes('node_modules')) {
// // 将 node_modules 中打包的库拆分成单独的 chunk
// return id.toString().split('node_modules/')[1].split('/')[0].toString();
// }
const utilsKeywords = [
"runtime",
"overlayscrollbars",
"icons-base",
"icons-guangfa",
"package",
"vue-clipboard3",
];
if (utilsKeywords.some((keyword) => id.includes(keyword))) {
return `utils`;
}
const mainKeywords = ["src/main", "vite"];
if (mainKeywords.some((keyword) => id.includes(keyword))) {
return `main`;
}
console.log("ID:", id);
if (id.includes("src/icons")) {
const groups = getGroupedIconChunks();
// console.log('Groups:', groups);
for (const [group, files] of Object.entries(groups)) {
if (files.includes(id)) {
return group;
}
}
return null; //如果没有组匹配,则默认情况
}
},
},
},
},
};
// 为了匹配多种路径模式,可以使用数组传递给 `globSync`
function getFileInput() {
const files = globSync([
// "icons-*.js",
"src/index.js",
"src/map.js",
"src/runtime/*.js",
"src/icons/*/*.js",
]);
return Object.fromEntries(
files.map((file) => {
// 判断是否是根目录的文件
const isRootFile = path.isAbsolute(file) || !file.startsWith("src/");
const relativePath = isRootFile
? path.basename(file, path.extname(file)) // 根目录文件使用文件名作为键
: path.relative(
// 相对于 `src` 文件夹生成相对路径
"src",
file.slice(0, file.length - path.extname(file).length)
);
return [
relativePath,
// 使用 `fileURLToPath` 将文件路径转换为 URL 文件路径
fileURLToPath(new URL(file, import.meta.url)),
];
})
);
}
// 获取/icons文件夹下的所有图标名称
function getIconExternals() {
// 同时匹配文件和目录
const allIcons = globSync(["src/icons/*", "src/icons/*/*.js"], {
ignore: {
ignored: (p) => /index\.js$/.test(p.name),
},
});
// 统一格式化路径
return allIcons.map((file) => {
// 如果是文件,使用 path.parse 取文件名;如果是目录,用相对路径
return file.endsWith(".js")
? `./${path.parse(file).name}`
: `./${path.relative("src", file)}`;
});
}
// 默认参数
const baseOutputConfig = {
entryFileNames: "[name].js",
chunkFileNames: "[name].js",
globals: {
vue: "Vue",
},
manualChunks: {
gsap: ["gsap"],
},
};
const packagesConfig = {
...baseConfig,
// esbuild: {
// minifySyntax: false,
// minifyWhitespace: false,
// minifyIdentifiers: false,
// },
build: {
outDir: "packages",
emptyOutDir: true,
// minify: true,
rollupOptions: {
input: getFileInput(),
external: ["vue", "./map", "../../runtime", ...getIconExternals()],
preserveEntrySignatures: "allow-extension",
output: [
{
format: "es",
dir: "packages/es",
...baseOutputConfig,
},
{
format: "cjs",
dir: "packages/cjs",
...baseOutputConfig,
},
],
},
},
publicDir: false,
plugins: [
// 使用插件生成 dts
// 处理 src 目录
dts({
entryRoot: "src",
outDir: ["packages/es", "packages/cjs"],
}),
// 处理根目录文件
dts({
include: ["icons-*.js"],
entryRoot: ".",
outDir: ".",
}),
],
};
export default defineConfig(() => {
if (process.env.BUILD === "packages") {
return packagesConfig;
} else {
return siteConfig;
}
});