fix(config): properly pass ts-based options to plugins
This commit is contained in:
@@ -829,14 +829,16 @@ export async function regeneratePluginIndex(options: { verbose?: boolean } = {})
|
||||
return
|
||||
}
|
||||
|
||||
const plugins = fs.readdirSync(PLUGINS_CACHE_DIR).filter((name) => {
|
||||
const pluginDirs = fs.readdirSync(PLUGINS_CACHE_DIR).filter((name) => {
|
||||
const pluginPath = path.join(PLUGINS_CACHE_DIR, name)
|
||||
return fs.statSync(pluginPath).isDirectory()
|
||||
})
|
||||
|
||||
const exports: string[] = []
|
||||
// Phase 1: Collect all exports per plugin, detect conflicts
|
||||
const pluginExports = new Map<string, { named: string[]; types: string[] }>()
|
||||
const nameCount = new Map<string, number>()
|
||||
|
||||
for (const pluginName of plugins) {
|
||||
for (const pluginName of pluginDirs) {
|
||||
const pluginDir = path.join(PLUGINS_CACHE_DIR, pluginName)
|
||||
const distIndex = path.join(pluginDir, "dist", "index.d.ts")
|
||||
|
||||
@@ -849,27 +851,85 @@ export async function regeneratePluginIndex(options: { verbose?: boolean } = {})
|
||||
|
||||
const dtsContent = fs.readFileSync(distIndex, "utf-8")
|
||||
const exportedNames = parseExportsFromDts(dtsContent)
|
||||
const named = exportedNames.filter((e) => !e.startsWith("type "))
|
||||
const types = exportedNames.filter((e) => e.startsWith("type ")).map((e) => e.slice(5))
|
||||
|
||||
if (exportedNames.length > 0) {
|
||||
const namedExports = exportedNames.filter((e) => !e.startsWith("type "))
|
||||
const typeExports = exportedNames.filter((e) => e.startsWith("type ")).map((e) => e.slice(5))
|
||||
|
||||
if (namedExports.length > 0) {
|
||||
exports.push(`export { ${namedExports.join(", ")} } from "./${pluginName}"`)
|
||||
}
|
||||
if (typeExports.length > 0) {
|
||||
exports.push(`export type { ${typeExports.join(", ")} } from "./${pluginName}"`)
|
||||
if (named.length > 0 || types.length > 0) {
|
||||
pluginExports.set(pluginName, { named, types })
|
||||
for (const n of named) {
|
||||
nameCount.set(n, (nameCount.get(n) ?? 0) + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indexContent = exports.join("\n") + "\n"
|
||||
// Phase 2: Generate index with registry import, plugin map, and conditional top-level exports
|
||||
const lines: string[] = []
|
||||
|
||||
lines.push(`import { componentRegistry } from "../../quartz/components/registry"`)
|
||||
lines.push("")
|
||||
|
||||
// Type re-exports
|
||||
for (const [pluginName, { types }] of pluginExports) {
|
||||
if (types.length > 0) {
|
||||
lines.push(`export type { ${types.join(", ")} } from "./${pluginName}"`)
|
||||
}
|
||||
}
|
||||
lines.push("")
|
||||
|
||||
// Generate the plugins map with override wrappers
|
||||
lines.push(
|
||||
`export const plugins: Record<string, Record<string, (...args: unknown[]) => void>> = {`,
|
||||
)
|
||||
for (const [pluginName, { named }] of pluginExports) {
|
||||
if (named.length === 0) continue
|
||||
const escapedName = pluginName.replace(/"/g, '\\"')
|
||||
lines.push(` "${escapedName}": {`)
|
||||
for (const n of named) {
|
||||
lines.push(
|
||||
` ${n}: (...args: unknown[]) => { componentRegistry.setOptionOverrides("${escapedName}", args[0] as Record<string, unknown>); },`,
|
||||
)
|
||||
}
|
||||
lines.push(` },`)
|
||||
}
|
||||
lines.push(`}`)
|
||||
lines.push("")
|
||||
|
||||
// Top-level exports: only for non-conflicting names
|
||||
for (const [pluginName, { named }] of pluginExports) {
|
||||
if (named.length === 0) continue
|
||||
|
||||
const unique = named.filter((n) => (nameCount.get(n) ?? 0) === 1)
|
||||
const conflicting = named.filter((n) => (nameCount.get(n) ?? 0) > 1)
|
||||
|
||||
if (unique.length > 0) {
|
||||
const escapedName = pluginName.replace(/"/g, '\\"')
|
||||
for (const n of unique) {
|
||||
lines.push(`export const ${n} = plugins["${escapedName}"].${n}`)
|
||||
}
|
||||
}
|
||||
|
||||
if (conflicting.length > 0 && options.verbose) {
|
||||
for (const n of conflicting) {
|
||||
console.warn(
|
||||
styleText("yellow", `⚠`),
|
||||
`Export "${n}" conflicts across plugins — use plugins["${pluginName}"].${n} in quartz.ts`,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lines.push("")
|
||||
|
||||
const indexContent = lines.join("\n")
|
||||
const indexPath = path.join(PLUGINS_CACHE_DIR, "index.ts")
|
||||
|
||||
fs.writeFileSync(indexPath, indexContent)
|
||||
|
||||
if (options.verbose) {
|
||||
console.log(styleText("green", `✓`), `Regenerated plugin index with ${plugins.length} plugins`)
|
||||
console.log(
|
||||
styleText("green", `✓`),
|
||||
`Regenerated plugin index with ${pluginDirs.length} plugins`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user