@nikala-ui/folio 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/README.md +16 -0
  2. package/dist/cli/commands/init.js +21 -2
  3. package/dist/client/app.jsx +6 -3
  4. package/dist/client/components/docs-layout-shell.d.ts +1 -0
  5. package/dist/client/components/docs-layout-shell.jsx +1 -1
  6. package/dist/client/components/docs-page-content.jsx +9 -5
  7. package/dist/client/navigation/page-navigation.js +10 -27
  8. package/dist/client/public/algolia-logo.svg +15 -0
  9. package/dist/client/public/favicon.ico +0 -0
  10. package/dist/client/public/logo.svg +11 -0
  11. package/dist/client/routing/use-docs-router.d.ts +1 -0
  12. package/dist/client/routing/use-docs-router.js +12 -3
  13. package/dist/client/style.css +139 -55
  14. package/dist/components/ui/api-table.jsx +6 -4
  15. package/dist/components/ui/code-block.jsx +6 -4
  16. package/dist/components/ui/command.jsx +9 -5
  17. package/dist/components/ui/list.jsx +3 -1
  18. package/dist/components/ui/logo.d.ts +4 -1
  19. package/dist/components/ui/logo.jsx +8 -22
  20. package/dist/components/ui/package-manager-tabs.jsx +4 -2
  21. package/dist/components/ui/pager.jsx +4 -2
  22. package/dist/components/ui/section-heading.jsx +1 -1
  23. package/dist/components/ui/sidebar.jsx +6 -3
  24. package/dist/components/ui/spinner.jsx +3 -1
  25. package/dist/components/ui/theme-toggle.jsx +14 -12
  26. package/dist/config.d.ts +1 -0
  27. package/dist/config.js +84 -29
  28. package/dist/core/content-scanner.d.ts +2 -1
  29. package/dist/core/content-scanner.js +2 -9
  30. package/dist/core/plugin/index.d.ts +2 -0
  31. package/dist/core/plugin/index.js +1 -0
  32. package/dist/core/plugin/lifecycle-error.d.ts +19 -0
  33. package/dist/core/plugin/lifecycle-error.js +28 -0
  34. package/dist/core/plugin/lifecycle-snapshot.d.ts +1 -0
  35. package/dist/core/plugin/lifecycle-snapshot.js +156 -0
  36. package/dist/core/plugin/lifecycle.d.ts +34 -0
  37. package/dist/core/plugin/lifecycle.js +170 -0
  38. package/dist/core/route-tree.d.ts +3 -2
  39. package/dist/core/route-tree.js +40 -10
  40. package/dist/index.d.ts +5 -3
  41. package/dist/index.js +3 -3
  42. package/dist/plugin.d.ts +60 -0
  43. package/dist/plugin.js +51 -0
  44. package/dist/plugins/i18n/components/site-language-switcher.d.ts +10 -0
  45. package/dist/plugins/i18n/components/site-language-switcher.jsx +39 -0
  46. package/dist/plugins/i18n/constants.d.ts +1 -0
  47. package/dist/plugins/i18n/constants.js +1 -0
  48. package/dist/plugins/i18n/index.d.ts +11 -0
  49. package/dist/plugins/i18n/index.js +89 -0
  50. package/dist/plugins/i18n/navigation.d.ts +5 -0
  51. package/dist/plugins/i18n/navigation.js +33 -0
  52. package/dist/plugins/i18n/runtime.d.ts +19 -0
  53. package/dist/plugins/i18n/runtime.jsx +65 -0
  54. package/dist/plugins/i18n/site-locale.d.ts +12 -0
  55. package/dist/plugins/i18n/site-locale.js +119 -0
  56. package/dist/server/index.d.ts +4 -1
  57. package/dist/server/index.js +69 -33
  58. package/dist/server/module-serialization.d.ts +6 -0
  59. package/dist/server/module-serialization.js +26 -0
  60. package/dist/server/plugin/components.d.ts +7 -0
  61. package/dist/server/plugin/components.js +82 -0
  62. package/dist/server/plugin/constants.d.ts +17 -0
  63. package/dist/server/plugin/constants.js +21 -0
  64. package/dist/server/{plugin.d.ts → plugin/index.d.ts} +3 -1
  65. package/dist/server/plugin/index.js +166 -0
  66. package/dist/server/plugin/lifecycle.d.ts +32 -0
  67. package/dist/server/plugin/lifecycle.js +110 -0
  68. package/dist/server/plugin/styles.d.ts +3 -0
  69. package/dist/server/plugin/styles.js +22 -0
  70. package/dist/server/plugin/virtual-modules.d.ts +11 -0
  71. package/dist/server/plugin/virtual-modules.js +88 -0
  72. package/dist/server/seo.d.ts +4 -0
  73. package/dist/server/seo.js +87 -0
  74. package/dist/themes/default/content/breadcrumbs.jsx +5 -2
  75. package/dist/themes/default/content/table-of-contents.jsx +3 -1
  76. package/dist/themes/default/layout.jsx +39 -12
  77. package/dist/themes/default/navbar.jsx +14 -7
  78. package/dist/themes/default/navigation/mobile-table-of-contents.jsx +3 -1
  79. package/dist/themes/default/navigation/sidebar-tree.d.ts +2 -0
  80. package/dist/themes/default/navigation/sidebar-tree.jsx +11 -2
  81. package/dist/themes/default/overlays/search-dialog.jsx +14 -3
  82. package/dist/themes/default/sidebar.jsx +7 -5
  83. package/dist/themes/types.d.ts +5 -0
  84. package/dist/types.d.ts +36 -0
  85. package/dist/utils/format-title.d.ts +1 -0
  86. package/dist/utils/format-title.js +11 -0
  87. package/dist/vendor/docs-src/components/ui/api-table.tsx +6 -4
  88. package/dist/vendor/docs-src/components/ui/code-block.tsx +6 -4
  89. package/dist/vendor/docs-src/components/ui/command.tsx +9 -5
  90. package/dist/vendor/docs-src/components/ui/list.tsx +4 -2
  91. package/dist/vendor/docs-src/components/ui/logo.tsx +19 -48
  92. package/dist/vendor/docs-src/components/ui/package-manager-tabs.tsx +4 -2
  93. package/dist/vendor/docs-src/components/ui/pager.tsx +4 -2
  94. package/dist/vendor/docs-src/components/ui/section-heading.tsx +5 -1
  95. package/dist/vendor/docs-src/components/ui/sidebar.tsx +7 -4
  96. package/dist/vendor/docs-src/components/ui/spinner.tsx +3 -1
  97. package/dist/vendor/docs-src/components/ui/theme-toggle.tsx +15 -13
  98. package/dist/vendor/docs-src/plugins/i18n/components/site-language-switcher.tsx +56 -0
  99. package/dist/vendor/docs-src/plugins/i18n/constants.ts +1 -0
  100. package/dist/vendor/docs-src/plugins/i18n/index.ts +110 -0
  101. package/dist/vendor/docs-src/plugins/i18n/navigation.ts +49 -0
  102. package/dist/vendor/docs-src/plugins/i18n/runtime.tsx +91 -0
  103. package/dist/vendor/docs-src/plugins/i18n/site-locale.ts +140 -0
  104. package/dist/version.d.ts +1 -1
  105. package/dist/version.js +1 -1
  106. package/package.json +11 -4
  107. package/dist/server/plugin.js +0 -521
@@ -1,521 +0,0 @@
1
- import path from "node:path";
2
- import { fileURLToPath } from "node:url";
3
- import { createRequire } from "node:module";
4
- import fs from "fs-extra";
5
- import { scanContent, scanContentDirectories } from "../core/content-scanner.js";
6
- import { buildConfiguredSidebarTree, buildSidebarTree } from "../core/route-tree.js";
7
- import { compileMdx } from "../mdx/compiler.js";
8
- import { loadConfig } from "../config.js";
9
- import { resolveDefaultThemeMode } from "../theme-mode.js";
10
- const VIRTUAL_CONFIG_ID = "virtual:folio-config";
11
- const RESOLVED_CONFIG_ID = "\0" + VIRTUAL_CONFIG_ID;
12
- const VIRTUAL_TREE_ID = "virtual:folio-tree";
13
- const RESOLVED_TREE_ID = "\0" + VIRTUAL_TREE_ID;
14
- const VIRTUAL_ROUTES_ID = "virtual:folio-routes";
15
- const RESOLVED_ROUTES_ID = "\0" + VIRTUAL_ROUTES_ID;
16
- const VIRTUAL_SOURCES_ID = "virtual:folio-sources";
17
- const RESOLVED_SOURCES_ID = "\0" + VIRTUAL_SOURCES_ID;
18
- const VIRTUAL_COMPONENTS_ID = "virtual:folio-components";
19
- const RESOLVED_COMPONENTS_ID = "\0" + VIRTUAL_COMPONENTS_ID;
20
- const VIRTUAL_ICONS_ID = "virtual:folio-icons";
21
- const RESOLVED_ICONS_ID = "\0" + VIRTUAL_ICONS_ID;
22
- const VIRTUAL_THEME_ID = "virtual:folio-theme";
23
- const RESOLVED_THEME_ID = "\0" + VIRTUAL_THEME_ID;
24
- const VIRTUAL_SHIKI_ID = "virtual:folio-shiki-stub";
25
- const RESOLVED_SHIKI_ID = "\0" + VIRTUAL_SHIKI_ID;
26
- const CONFIG_FILENAMES = new Set([
27
- "docs.config.ts",
28
- "docs.config.js",
29
- "docs.config.mjs",
30
- "nikala.docs.config.ts",
31
- "nikala.docs.config.js",
32
- "nikala.docs.config.mjs",
33
- // Backward compatibility for older docs projects.
34
- "nikala.config.ts",
35
- "nikala.config.js",
36
- ]);
37
- function findConfigFile(rootDir) {
38
- return [...CONFIG_FILENAMES]
39
- .map((filename) => path.join(rootDir, filename))
40
- .find((file) => fs.existsSync(file));
41
- }
42
- function toLucideComponentName(name) {
43
- return name
44
- .trim()
45
- .split(/[-_\s]+/)
46
- .filter(Boolean)
47
- .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
48
- .join("");
49
- }
50
- function escapeHtmlAttribute(value) {
51
- return value
52
- .replace(/&/g, "&")
53
- .replace(/"/g, """)
54
- .replace(/</g, "&lt;")
55
- .replace(/>/g, "&gt;");
56
- }
57
- function getTailwindSourceDirectives(rootDir, docsDir) {
58
- const sources = [];
59
- const bundledDocsSource = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../vendor/docs-src");
60
- if (fs.existsSync(bundledDocsSource))
61
- sources.push(bundledDocsSource);
62
- sources.push(path.resolve(rootDir, "src"));
63
- const themesDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../themes");
64
- if (fs.existsSync(themesDir))
65
- sources.push(themesDir);
66
- if (docsDir && fs.existsSync(docsDir))
67
- sources.push(docsDir);
68
- return sources
69
- .map((src) => `@source "${src}";`)
70
- .join("\n");
71
- }
72
- function getComponentSourceDir(rootDir) {
73
- const localSource = path.resolve(rootDir, "src/components/ui");
74
- if (fs.existsSync(localSource))
75
- return localSource;
76
- const moduleDir = path.dirname(fileURLToPath(import.meta.url));
77
- const candidates = [
78
- path.resolve(moduleDir, "../vendor/docs-src/components/ui"),
79
- path.resolve(moduleDir, "../../src/components/ui"),
80
- path.resolve(moduleDir, "../components/ui"),
81
- ];
82
- const source = candidates.find((candidate) => fs.existsSync(candidate));
83
- if (source)
84
- return source;
85
- throw new Error("Folio local component sources are missing");
86
- }
87
- function collectComponentExports(directory) {
88
- if (!fs.existsSync(directory))
89
- return [];
90
- return fs.readdirSync(directory, { withFileTypes: true })
91
- .filter((entry) => entry.isFile() && /\.(?:tsx?|jsx?)$/.test(entry.name) && !/^index\./.test(entry.name))
92
- .flatMap((entry) => {
93
- const file = path.join(directory, entry.name);
94
- const source = fs.readFileSync(file, "utf8");
95
- const names = new Set();
96
- for (const match of source.matchAll(/export\s+(?:const|function|class)\s+([A-Z][A-Za-z0-9_$]*)/g)) {
97
- names.add(match[1]);
98
- }
99
- for (const match of source.matchAll(/export\s*\{([^}]+)\}/g)) {
100
- for (const name of match[1].split(",")) {
101
- const exported = name.trim().split(/\s+as\s+/).at(-1);
102
- if (exported && /^[A-Z][A-Za-z0-9_$]*$/.test(exported))
103
- names.add(exported);
104
- }
105
- }
106
- return [...names].sort().map((name) => ({ name, file }));
107
- })
108
- .sort((a, b) => a.name.localeCompare(b.name));
109
- }
110
- function collectSourceFiles(directory) {
111
- if (!fs.existsSync(directory))
112
- return [];
113
- return fs.readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
114
- const file = path.join(directory, entry.name);
115
- if (entry.isDirectory())
116
- return collectSourceFiles(file);
117
- return entry.isFile() && /\.(?:tsx?|jsx?)$/.test(entry.name) ? [file] : [];
118
- });
119
- }
120
- function resolveComponentFile(directory, importPath) {
121
- const base = path.join(directory, importPath);
122
- const candidates = [
123
- base,
124
- ...[".ts", ".tsx", ".js", ".jsx"].map((extension) => base + extension),
125
- ...["index.ts", "index.tsx", "index.js", "index.jsx"].map((entry) => path.join(base, entry)),
126
- ];
127
- return candidates.find((file) => fs.existsSync(file));
128
- }
129
- function collectStaticComponentFiles(componentDir, themeDirs) {
130
- const staticFiles = new Set();
131
- const pending = themeDirs.flatMap((directory) => collectSourceFiles(directory));
132
- while (pending.length) {
133
- const file = pending.pop();
134
- if (!fs.existsSync(file))
135
- continue;
136
- const source = fs.readFileSync(file, "utf8");
137
- for (const match of source.matchAll(/from\s+["']@\/components\/ui\/([^"']+)["']/g)) {
138
- const importedFile = resolveComponentFile(componentDir, match[1]);
139
- if (importedFile && !staticFiles.has(importedFile)) {
140
- staticFiles.add(importedFile);
141
- pending.push(importedFile);
142
- }
143
- }
144
- for (const match of source.matchAll(/from\s+["'](\.\.?\/[^"']+)["']/g)) {
145
- const importedFile = resolveComponentFile(path.dirname(file), match[1]);
146
- if (importedFile && importedFile.startsWith(`${path.resolve(componentDir)}${path.sep}`) && !staticFiles.has(importedFile)) {
147
- staticFiles.add(importedFile);
148
- pending.push(importedFile);
149
- }
150
- }
151
- }
152
- return staticFiles;
153
- }
154
- export function nikalaDocsPlugin(options = {}) {
155
- // Vite's root is the docs engine client directory. It is not the user's
156
- // project root, so content/config paths must always resolve from configRoot.
157
- let rootDir = path.resolve(options.configRoot || process.cwd());
158
- let docsDir = options.docsDir ? path.resolve(rootDir, options.docsDir) : path.resolve(rootDir, "docs");
159
- let resolvedConfig = options.config || { title: "Folio" };
160
- let cachedPages = [];
161
- let isSsrBuild = false;
162
- return {
163
- name: "vite-plugin-folio",
164
- enforce: "pre",
165
- async configResolved(viteConfig) {
166
- isSsrBuild = Boolean(viteConfig.build.ssr);
167
- // Keep Vite's internal root separate from the consuming project's root.
168
- // Otherwise a relative contentDir could resolve under packages/docs.
169
- rootDir = path.resolve(options.configRoot || process.cwd());
170
- if (!options.docsDir) {
171
- // The config is authoritative for new projects; keep legacy auto-detection.
172
- const fs = await import("fs-extra");
173
- if (!options.config) {
174
- resolvedConfig = await loadConfig(options.configRoot || rootDir);
175
- }
176
- if (resolvedConfig.contentDir) {
177
- docsDir = path.resolve(rootDir, resolvedConfig.contentDir);
178
- }
179
- else if (await fs.pathExists(path.resolve(rootDir, "docs"))) {
180
- docsDir = path.resolve(rootDir, "docs");
181
- }
182
- else if (await fs.pathExists(path.resolve(rootDir, "content"))) {
183
- docsDir = path.resolve(rootDir, "content");
184
- }
185
- else {
186
- docsDir = rootDir;
187
- }
188
- }
189
- else {
190
- docsDir = path.resolve(rootDir, options.docsDir);
191
- }
192
- },
193
- transformIndexHtml(html) {
194
- const favicon = resolvedConfig.favicon || "/favicon.ico";
195
- const defaultTheme = resolveDefaultThemeMode(resolvedConfig);
196
- return html.replace(/<link rel="icon" href="[^"]*"\s*\/>/i, `<link rel="icon" href="${escapeHtmlAttribute(favicon)}" />`).replaceAll("__NIKALA_DEFAULT_THEME__", defaultTheme);
197
- },
198
- resolveId(id, importer) {
199
- if (id === "shiki" || id.startsWith("shiki/")) {
200
- if (importer && (importer.includes("/mdx/") || importer.includes("\\mdx\\"))) {
201
- return null;
202
- }
203
- return isSsrBuild ? RESOLVED_SHIKI_ID : null;
204
- }
205
- if (id === VIRTUAL_CONFIG_ID)
206
- return RESOLVED_CONFIG_ID;
207
- if (id === VIRTUAL_TREE_ID)
208
- return RESOLVED_TREE_ID;
209
- if (id === VIRTUAL_ROUTES_ID)
210
- return RESOLVED_ROUTES_ID;
211
- if (id === VIRTUAL_SOURCES_ID)
212
- return RESOLVED_SOURCES_ID;
213
- if (id === VIRTUAL_COMPONENTS_ID)
214
- return RESOLVED_COMPONENTS_ID;
215
- if (id === VIRTUAL_ICONS_ID || id === RESOLVED_ICONS_ID)
216
- return RESOLVED_ICONS_ID;
217
- if (id === VIRTUAL_THEME_ID)
218
- return RESOLVED_THEME_ID;
219
- return null;
220
- },
221
- async load(id) {
222
- if (id === RESOLVED_SHIKI_ID) {
223
- return `
224
- export const createHighlighter = async () => null;
225
- export const bundledLanguages = {};
226
- export const bundledThemes = {};
227
- export default { createHighlighter, bundledLanguages, bundledThemes };
228
- `;
229
- }
230
- if (id === RESOLVED_CONFIG_ID) {
231
- const configFile = findConfigFile(rootDir);
232
- if (!configFile)
233
- return `export default ${JSON.stringify(resolvedConfig)};`;
234
- return `
235
- import userConfig from ${JSON.stringify(configFile)};
236
- const defaults = ${JSON.stringify(resolvedConfig)};
237
- const config = {
238
- ...defaults,
239
- ...userConfig,
240
- navigation: {
241
- ...defaults.navigation,
242
- ...userConfig.navigation,
243
- sidebar: { ...defaults.navigation?.sidebar, ...userConfig.navigation?.sidebar },
244
- },
245
- theme: { ...defaults.theme, ...userConfig.theme },
246
- shiki: {
247
- ...defaults.shiki,
248
- ...userConfig.shiki,
249
- themes: { ...defaults.shiki?.themes, ...userConfig.shiki?.themes },
250
- },
251
- search: { ...defaults.search, ...userConfig.search },
252
- };
253
- export default config;
254
- `;
255
- }
256
- if (id === RESOLVED_TREE_ID) {
257
- cachedPages = await scanContent(docsDir);
258
- const directories = await scanContentDirectories(docsDir);
259
- const configuredSidebar = resolvedConfig.navigation?.sidebar?.nav ?? resolvedConfig.sidebar ?? "auto";
260
- const tree = configuredSidebar !== "auto"
261
- ? buildConfiguredSidebarTree(configuredSidebar, cachedPages)
262
- : buildSidebarTree(cachedPages, directories);
263
- return `
264
- export const pages = ${JSON.stringify(cachedPages)};
265
- export const tree = ${JSON.stringify(tree)};
266
- export default { pages, tree };
267
- `;
268
- }
269
- if (id === RESOLVED_ROUTES_ID) {
270
- cachedPages = await scanContent(docsDir);
271
- const routeEntries = cachedPages.map((page) => ` ${JSON.stringify(page.url)}: () => import(${JSON.stringify(page.filePath)})`);
272
- return `
273
- export const routes = {
274
- ${routeEntries.join(",\n")}
275
- };
276
- export default routes;
277
- `;
278
- }
279
- if (id === RESOLVED_SOURCES_ID) {
280
- cachedPages = await scanContent(docsDir);
281
- const sourceEntries = cachedPages.map((page) => ` ${JSON.stringify(page.url)}: () => import(${JSON.stringify(`${page.filePath}?raw`)})`);
282
- return `
283
- export const sources = {
284
- ${sourceEntries.join(",\n")}
285
- };
286
- export default sources;
287
- `;
288
- }
289
- if (id === RESOLVED_COMPONENTS_ID) {
290
- const componentDir = getComponentSourceDir(rootDir);
291
- const themeDirs = [
292
- path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../themes/default"),
293
- path.resolve(rootDir, "src/themes/default"),
294
- ];
295
- if (resolvedConfig.theme?.path) {
296
- const configuredTheme = path.resolve(rootDir, resolvedConfig.theme.path);
297
- themeDirs.push(fs.existsSync(configuredTheme) && fs.statSync(configuredTheme).isDirectory()
298
- ? configuredTheme
299
- : path.dirname(configuredTheme));
300
- }
301
- const staticFiles = collectStaticComponentFiles(componentDir, themeDirs);
302
- const modules = new Map();
303
- const exports = collectComponentExports(getComponentSourceDir(rootDir));
304
- for (const { file } of exports) {
305
- const existing = modules.get(file);
306
- if (!existing)
307
- modules.set(file, { index: modules.size, file, isStatic: staticFiles.has(file) });
308
- }
309
- const moduleEntries = [...modules.values()];
310
- const imports = moduleEntries.map(({ index, file }) => modules.get(file).isStatic
311
- ? `import * as componentModule${index} from ${JSON.stringify(file)};`
312
- : `const componentModule${index} = () => import(${JSON.stringify(file)});`);
313
- const entries = exports.map(({ name, file }) => {
314
- const module = modules.get(file);
315
- return module.isStatic
316
- ? ` ${JSON.stringify(name)}: componentModule${module.index}[${JSON.stringify(name)}]`
317
- : ` ${JSON.stringify(name)}: lazy(() => componentModule${module.index}().then((module) => ({ default: module[${JSON.stringify(name)}] })))`;
318
- });
319
- return `
320
- import { lazy } from "solid-js";
321
- ${imports.join("\n")}
322
- const components = {
323
- ${entries.join(",\n")}
324
- };
325
- export default components;
326
- `;
327
- }
328
- if (id === RESOLVED_ICONS_ID) {
329
- const pages = await scanContent(docsDir);
330
- const names = [...new Set(pages.map((page) => page.frontmatter.icon).filter((name) => typeof name === "string" && name.trim().length > 0))];
331
- const require = createRequire(path.join(rootDir, "package.json"));
332
- const entries = [];
333
- for (const name of names) {
334
- const kebabName = name
335
- .trim()
336
- .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
337
- .replace(/[\s_]+/g, "-")
338
- .toLowerCase();
339
- try {
340
- require.resolve(`lucide-solid/icons/${kebabName}`);
341
- entries.push({ name, specifier: `lucide-solid/icons/${kebabName}` });
342
- }
343
- catch {
344
- console.warn(`[folio] Unknown Lucide icon "${name}"; skipping it.`);
345
- }
346
- }
347
- const imports = entries.map(({ specifier }, index) => `import icon${index} from ${JSON.stringify(specifier)};`);
348
- const iconEntries = entries.map(({ name }, index) => ` ${JSON.stringify(toLucideComponentName(name))}: icon${index}`);
349
- return `
350
- ${imports.join("\n")}
351
- export const icons = {
352
- ${iconEntries.join(",\n")}
353
- };
354
- `;
355
- }
356
- if (id === RESOLVED_THEME_ID) {
357
- const configuredPath = resolvedConfig.theme?.path;
358
- if (!configuredPath) {
359
- const defaultEntry = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../themes/default/index.js");
360
- return `import defaultTheme from ${JSON.stringify(defaultEntry)}; export const theme = defaultTheme; export default theme;`;
361
- }
362
- const requestedPath = path.resolve(options.configRoot || rootDir, configuredPath);
363
- const candidates = [
364
- requestedPath,
365
- path.join(requestedPath, "index.ts"),
366
- path.join(requestedPath, "index.tsx"),
367
- path.join(requestedPath, "index.js"),
368
- path.join(requestedPath, "index.jsx"),
369
- ];
370
- const themeEntry = candidates.find((candidate) => fs.existsSync(candidate));
371
- if (!themeEntry)
372
- throw new Error(`[folio] Theme path does not exist: ${requestedPath}`);
373
- return `import configuredTheme from ${JSON.stringify(themeEntry)}; export const theme = configuredTheme.default || configuredTheme; export default theme;`;
374
- }
375
- return null;
376
- },
377
- async transform(code, id, transformOptions) {
378
- const cssId = id.split("?", 1)[0];
379
- const packagedCss = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../client/style.css");
380
- const baseCss = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../client/base.css");
381
- const configuredCss = resolvedConfig.css
382
- ? path.resolve(rootDir, resolvedConfig.css)
383
- : packagedCss;
384
- if (cssId === packagedCss || cssId === configuredCss) {
385
- if (cssId === packagedCss && configuredCss !== packagedCss) {
386
- const sourceDirectives = getTailwindSourceDirectives(rootDir, docsDir);
387
- return {
388
- code: sourceDirectives + "\n@import " + JSON.stringify(configuredCss.replace(/\\/g, "/")) + ";\n@import " + JSON.stringify(baseCss.replace(/\\/g, "/")) + ";",
389
- map: null,
390
- };
391
- }
392
- const sourceDirectives = getTailwindSourceDirectives(rootDir, docsDir);
393
- return {
394
- code: `@import "tailwindcss";\n${sourceDirectives}\n${code.replace('@import "tailwindcss";', "")}`,
395
- map: null,
396
- };
397
- }
398
- // Compile Markdown / MDX files to SolidJS JSX
399
- if (/\.(md|mdx)$/.test(id)) {
400
- const isSsr = Boolean(transformOptions?.ssr);
401
- const result = await compileMdx(code, {
402
- filePath: id,
403
- development: !isSsr && process.env.NODE_ENV !== "production",
404
- shiki: resolvedConfig.shiki,
405
- });
406
- return {
407
- code: result.code,
408
- map: null,
409
- };
410
- }
411
- return null;
412
- },
413
- configureServer(server) {
414
- // Vite's root is the packaged client directory, so the consuming
415
- // project's config/content files are outside its default watch scope.
416
- // Register them explicitly for dev reloads.
417
- const configFiles = [...CONFIG_FILENAMES]
418
- .map((filename) => path.join(rootDir, filename))
419
- .filter((file) => fs.existsSync(file));
420
- if (configFiles.length)
421
- server.watcher.add(configFiles);
422
- if (docsDir && fs.existsSync(docsDir))
423
- server.watcher.add(docsDir);
424
- const componentsDir = getComponentSourceDir(rootDir);
425
- if (componentsDir && fs.existsSync(componentsDir))
426
- server.watcher.add(componentsDir);
427
- const configuredCss = resolvedConfig.css
428
- ? path.resolve(rootDir, resolvedConfig.css)
429
- : undefined;
430
- if (configuredCss && fs.existsSync(configuredCss))
431
- server.watcher.add(configuredCss);
432
- let reloadTimer;
433
- const invalidateVirtualModules = (includeConfig = false, includeComponents = false) => {
434
- const modTree = server.moduleGraph.getModuleById(RESOLVED_TREE_ID);
435
- const modRoutes = server.moduleGraph.getModuleById(RESOLVED_ROUTES_ID);
436
- if (modTree)
437
- server.moduleGraph.invalidateModule(modTree);
438
- if (modRoutes)
439
- server.moduleGraph.invalidateModule(modRoutes);
440
- if (includeComponents) {
441
- const modComponents = server.moduleGraph.getModuleById(RESOLVED_COMPONENTS_ID);
442
- if (modComponents)
443
- server.moduleGraph.invalidateModule(modComponents);
444
- }
445
- if (includeConfig) {
446
- const modConfig = server.moduleGraph.getModuleById(RESOLVED_CONFIG_ID);
447
- const modTheme = server.moduleGraph.getModuleById(RESOLVED_THEME_ID);
448
- if (modConfig)
449
- server.moduleGraph.invalidateModule(modConfig);
450
- if (modTheme)
451
- server.moduleGraph.invalidateModule(modTheme);
452
- }
453
- if (reloadTimer)
454
- clearTimeout(reloadTimer);
455
- reloadTimer = setTimeout(() => {
456
- reloadTimer = undefined;
457
- server.ws.send({ type: "full-reload" });
458
- }, 75);
459
- };
460
- // Watch content directory for file additions or removals
461
- server.watcher.on("add", (file) => {
462
- if (/\.(md|mdx)$/.test(file)) {
463
- invalidateVirtualModules();
464
- return;
465
- }
466
- if (componentsDir && path.resolve(file).startsWith(`${path.resolve(componentsDir)}${path.sep}`)) {
467
- invalidateVirtualModules(false, true);
468
- }
469
- });
470
- server.watcher.on("unlink", (file) => {
471
- if (/\.(md|mdx)$/.test(file)) {
472
- invalidateVirtualModules();
473
- return;
474
- }
475
- if (componentsDir && path.resolve(file).startsWith(`${path.resolve(componentsDir)}${path.sep}`)) {
476
- invalidateVirtualModules(false, true);
477
- }
478
- });
479
- server.watcher.on("change", async (file) => {
480
- if (/\.(md|mdx)$/.test(file)) {
481
- invalidateVirtualModules();
482
- return;
483
- }
484
- if (componentsDir && path.resolve(file).startsWith(`${path.resolve(componentsDir)}${path.sep}`)) {
485
- invalidateVirtualModules(false, true);
486
- return;
487
- }
488
- if (CONFIG_FILENAMES.has(path.basename(file))) {
489
- if (!options.config) {
490
- resolvedConfig = await loadConfig(rootDir);
491
- if (resolvedConfig.contentDir) {
492
- docsDir = path.resolve(rootDir, resolvedConfig.contentDir);
493
- }
494
- }
495
- invalidateVirtualModules(true);
496
- return;
497
- }
498
- if (configuredCss && path.resolve(file) === configuredCss) {
499
- const cssModule = server.moduleGraph.getModuleById(configuredCss);
500
- if (cssModule)
501
- server.moduleGraph.invalidateModule(cssModule);
502
- invalidateVirtualModules();
503
- }
504
- });
505
- },
506
- handleHotUpdate({ file, server }) {
507
- const configuredCss = resolvedConfig.css
508
- ? path.resolve(rootDir, resolvedConfig.css)
509
- : undefined;
510
- if (configuredCss && path.resolve(file) === configuredCss) {
511
- const cssModules = server.moduleGraph.getModulesByFile(configuredCss);
512
- for (const module of cssModules || []) {
513
- server.moduleGraph.invalidateModule(module);
514
- }
515
- server.ws.send({ type: "full-reload" });
516
- return [];
517
- }
518
- return undefined;
519
- },
520
- };
521
- }