-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.ts
More file actions
449 lines (406 loc) · 13.9 KB
/
vite.config.ts
File metadata and controls
449 lines (406 loc) · 13.9 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import { resolve } from 'node:path'
import { defineConfig, Plugin } from 'vite'
import type {
PluginStoreRegistry,
PluginStorePackage,
} from './scripts/utils/PluginStoreSchema'
import { readFile, readdir, stat } from 'node:fs/promises'
import { join } from 'node:path'
const REGISTRY_TEMPLATE_FILE = resolve(
import.meta.dirname,
'registry-templates',
'v1',
'registry.v1.json'
)
/**
* 读取 packages 目录下的所有插件,并生成虚拟的 registry.v1.json
* 在请求 /registry.v1.json 时,返回虚拟的 registry.v1.json
*
* 猜测可能的入口文件,生成类似 "packages/april-fool-2023/src/index.ts?_=<timestamp>" 的 entry
* 猜测可能的 css 入口,生成类似 ["packages/april-fool-2023/src/style.scss?_=<timestamp>"] 的 styles
*/
function pluginIPEPluginRegistry(options: {
template: PluginStoreRegistry
rootDir: string
packagesDir: string
sourceDir: string
entriesPattern: (string | RegExp)[]
styleEntriesPattern: (string | RegExp)[]
}): Plugin {
const VIRTUAL_MODULE_ID = '/registry.v1.json'
const INDEX_HTML_PATH = resolve(options.rootDir, 'public', 'index.html')
return {
name: 'ipe-plugin-registry-dev-helper',
async configureServer(server) {
server.middlewares.use(async (req, res, next) => {
// 处理根目录和 index.html 请求
if (req.url === '/' || req.url === '/index.html') {
try {
const html = await readFile(INDEX_HTML_PATH, 'utf-8')
res.setHeader('Content-Type', 'text/html; charset=utf-8')
res.setHeader('Cache-Control', 'no-cache')
res.end(html)
return
} catch (error) {
console.error('Failed to read index.html:', error)
res.statusCode = 404
res.end('404 Not Found')
return
}
}
// 处理注册表 JSON 请求
if (req.url === VIRTUAL_MODULE_ID) {
try {
const registry = await generateRegistry()
res.setHeader('Content-Type', 'application/json')
res.setHeader('Cache-Control', 'no-cache')
res.end(JSON.stringify(registry, null, 2))
return
} catch (error) {
console.error('Failed to generate registry:', error)
res.statusCode = 500
res.end(JSON.stringify({ error: 'Failed to generate registry' }))
return
}
}
next()
})
},
resolveId(source, importer) {
// 处理 @/ 别名,将其解析为当前插件的 src 目录
if (source.startsWith('@/')) {
// 如果没有 importer,无法确定是哪个插件
if (!importer) return null
// 检查 importer 是否在 packages 目录下
const normalizedImporter = importer.replace(/\\/g, '/')
const packagesPath = options.packagesDir.replace(/\\/g, '/')
if (normalizedImporter.includes(packagesPath)) {
// 提取插件名称
// 例如:E:/path/to/packages/foo/src/index.ts -> foo
const relativePath = normalizedImporter.substring(
normalizedImporter.indexOf(packagesPath) + packagesPath.length + 1
)
const pluginName = relativePath.split('/')[0]
if (pluginName) {
// 构建目标路径
const targetPath = source.substring(2) // 移除 '@/'
const resolvedPath = join(
options.packagesDir,
pluginName,
options.sourceDir,
targetPath
)
return resolvedPath
}
}
}
return null
},
} satisfies Plugin
/**
* 生成注册表
*/
async function generateRegistry(): Promise<PluginStoreRegistry> {
const packages = await scanPackages()
return {
...options.template,
last_modified: new Date().toISOString(),
packages,
}
}
/**
* 扫描 packages 目录下的所有插件
*/
async function scanPackages(): Promise<PluginStorePackage[]> {
const packages: PluginStorePackage[] = []
const entries = await readdir(options.packagesDir, { withFileTypes: true })
for (const entry of entries) {
if (!entry.isDirectory()) continue
if (entry.name.startsWith('_') || entry.name.startsWith('.')) continue
const packageDir = join(options.packagesDir, entry.name)
const packageJsonPath = join(packageDir, 'package.json')
try {
const packageJsonContent = await readFile(packageJsonPath, 'utf-8')
const packageJson = JSON.parse(packageJsonContent)
// 获取 $ipe 配置(参考 build.ts)
const ipeConfig = packageJson.$ipe || {}
const loaderInfo = ipeConfig.loader
const devLoaderInfo = ipeConfig.dev_loader // 开发环境专用配置
if (!loaderInfo) {
console.warn(
`Package ${entry.name} does not have loader info, skipping`
)
continue
}
const packageInfo: PluginStorePackage = {
id: entry.name,
name: ipeConfig.name || packageJson.name || entry.name,
version: packageJson.version || '0.0.0',
description:
ipeConfig.description || packageJson.description || undefined,
// 处理 author 字段:可能是字符串或对象
author:
typeof packageJson.author === 'string'
? packageJson.author
: packageJson.author?.name || undefined,
license: packageJson.license || undefined,
// 确保 categories 是数组
categories: Array.isArray(ipeConfig.categories)
? ipeConfig.categories
: [],
loader: {
kind: loaderInfo.kind,
entry: undefined,
styles: [],
main_export: loaderInfo.main_export || undefined,
},
}
const timestamp = Date.now()
// 处理入口文件
// 优先使用 dev_loader.entry,其次使用 loader.entry,最后自动检测
if (devLoaderInfo?.entry) {
// 如果配置了 dev_loader.entry,直接使用
const entryPath = join(packageDir, devLoaderInfo.entry)
const relativePath = entryPath
.replace(options.rootDir, '')
.replace(/\\/g, '/')
packageInfo.loader.entry = `${
relativePath.startsWith('/')
? relativePath.slice(1)
: relativePath
}?_=${timestamp}`
} else if (loaderInfo.entry) {
// 如果配置中指定了 entry,尝试转换为开发环境的源文件路径
const devEntry = await convertToDevPath(
packageDir,
loaderInfo.entry,
options.entriesPattern
)
if (devEntry) {
const relativePath = devEntry
.replace(options.rootDir, '')
.replace(/\\/g, '/')
packageInfo.loader.entry = `${
relativePath.startsWith('/')
? relativePath.slice(1)
: relativePath
}?_=${timestamp}`
}
} else {
// 自动检测入口文件
const detectedEntry = await detectEntry(packageDir)
if (detectedEntry) {
const relativePath = detectedEntry
.replace(options.rootDir, '')
.replace(/\\/g, '/')
packageInfo.loader.entry = `${
relativePath.startsWith('/')
? relativePath.slice(1)
: relativePath
}?_=${timestamp}`
}
}
// 处理样式文件
// 优先使用 dev_loader.styles,其次使用 loader.styles,最后自动检测
if (
devLoaderInfo?.styles &&
Array.isArray(devLoaderInfo.styles) &&
devLoaderInfo.styles.length > 0
) {
// 如果配置了 dev_loader.styles,直接使用
packageInfo.loader.styles = devLoaderInfo.styles.map(
(style: string) => {
const stylePath = join(packageDir, style)
const relativePath = stylePath
.replace(options.rootDir, '')
.replace(/\\/g, '/')
return `${
relativePath.startsWith('/')
? relativePath.slice(1)
: relativePath
}?_=${timestamp}`
}
)
} else if (
loaderInfo.styles &&
Array.isArray(loaderInfo.styles) &&
loaderInfo.styles.length > 0
) {
// 如果配置中指定了 styles,尝试转换为开发环境的源文件路径
const devStyles = await Promise.all(
loaderInfo.styles.map(async (style: string) => {
const devStyle = await convertToDevPath(
packageDir,
style,
options.styleEntriesPattern
)
if (devStyle) {
const relativePath = devStyle
.replace(options.rootDir, '')
.replace(/\\/g, '/')
return `${
relativePath.startsWith('/')
? relativePath.slice(1)
: relativePath
}?_=${timestamp}`
}
return null
})
)
packageInfo.loader.styles = devStyles.filter(
(s): s is string => s !== null
)
} else {
// 自动检测样式文件
const detectedStyles = await detectStyles(packageDir)
if (detectedStyles.length > 0) {
packageInfo.loader.styles = detectedStyles.map((stylePath) => {
const relativePath = stylePath
.replace(options.rootDir, '')
.replace(/\\/g, '/')
return `${
relativePath.startsWith('/')
? relativePath.slice(1)
: relativePath
}?_=${timestamp}`
})
}
}
packages.push(packageInfo)
} catch (error) {
console.warn(`Failed to read package ${entry.name}:`, error)
continue
}
}
return packages
}
/**
* 将构建产物路径转换为开发环境的源文件路径
* 例如:dist/index.js -> src/index.ts
*/
async function convertToDevPath(
packageDir: string,
distPath: string,
patterns: (string | RegExp)[]
): Promise<string | null> {
// 首先尝试从 src 目录中查找匹配的源文件
const sourceDir = join(packageDir, options.sourceDir)
try {
const files = await readdir(sourceDir)
// 从 dist 路径中提取基础文件名(不含扩展名)
const distFileName = distPath.split('/').pop()?.split('.')[0]
if (!distFileName) return null
// 尝试找到匹配的源文件
for (const file of files) {
const fileNameWithoutExt = file.split('.')[0]
// 检查文件名是否匹配
if (fileNameWithoutExt === distFileName) {
// 检查文件是否符合模式
for (const pattern of patterns) {
const matches =
typeof pattern === 'string'
? file === pattern
: pattern.test(file)
if (matches) {
const filePath = join(sourceDir, file)
const fileStat = await stat(filePath)
if (fileStat.isFile()) {
return filePath
}
}
}
}
}
} catch (error) {
// sourceDir 不存在或无法读取
}
// 如果在 src 中找不到,尝试使用原始路径
const originalPath = join(packageDir, distPath)
try {
const fileStat = await stat(originalPath)
if (fileStat.isFile()) {
return originalPath
}
} catch (error) {
// 文件不存在
}
return null
}
/**
* 检测入口文件
*/
async function detectEntry(packageDir: string): Promise<string | null> {
const sourceDir = join(packageDir, options.sourceDir)
try {
const files = await readdir(sourceDir)
for (const pattern of options.entriesPattern) {
for (const file of files) {
const matches =
typeof pattern === 'string' ? file === pattern : pattern.test(file)
if (matches) {
const filePath = join(sourceDir, file)
const fileStat = await stat(filePath)
if (fileStat.isFile()) {
return filePath
}
}
}
}
} catch (error) {
// sourceDir 不存在或无法读取
}
return null
}
/**
* 检测样式文件
*/
async function detectStyles(packageDir: string): Promise<string[]> {
const sourceDir = join(packageDir, options.sourceDir)
const styles: string[] = []
try {
const files = await readdir(sourceDir)
for (const pattern of options.styleEntriesPattern) {
for (const file of files) {
const matches =
typeof pattern === 'string' ? file === pattern : pattern.test(file)
if (matches) {
const filePath = join(sourceDir, file)
const fileStat = await stat(filePath)
if (fileStat.isFile()) {
styles.push(filePath)
}
}
}
}
} catch (error) {
// sourceDir 不存在或无法读取
}
return styles
}
}
export default defineConfig(async () => {
const template = JSON.parse(
await readFile(REGISTRY_TEMPLATE_FILE, 'utf-8')
) as PluginStoreRegistry
return {
plugins: [
pluginIPEPluginRegistry({
template,
rootDir: resolve(import.meta.dirname),
packagesDir: resolve(import.meta.dirname, 'packages'),
sourceDir: 'src',
entriesPattern: [/^(index|main)\.(js|ts|tsx)$/],
styleEntriesPattern: [/(index|styles?)\.(css|scss|less|styl|sass)$/],
}),
],
resolve: {
alias: {
'~~': resolve(import.meta.dirname, 'common'),
},
},
server: {
port: 1029,
cors: true,
},
}
})