prev-cli 0.22.2 → 0.22.4
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.
- package/dist/cli.js +118 -76
- package/package.json +1 -1
- package/src/theme/entry.tsx +4 -4
package/dist/cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
// src/cli.ts
|
|
4
4
|
import { parseArgs } from "util";
|
|
5
|
-
import
|
|
5
|
+
import path10 from "path";
|
|
6
6
|
import { existsSync as existsSync7, mkdirSync as mkdirSync2, writeFileSync as writeFileSync4, rmSync as rmSync3, readFileSync as readFileSync5 } from "fs";
|
|
7
7
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
8
8
|
|
|
@@ -15,7 +15,7 @@ import react from "@vitejs/plugin-react";
|
|
|
15
15
|
import mdx from "@mdx-js/rollup";
|
|
16
16
|
import remarkGfm from "remark-gfm";
|
|
17
17
|
import rehypeHighlight from "rehype-highlight";
|
|
18
|
-
import
|
|
18
|
+
import path8 from "path";
|
|
19
19
|
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
20
20
|
import { existsSync as existsSync5, readFileSync as readFileSync4 } from "fs";
|
|
21
21
|
|
|
@@ -70,6 +70,9 @@ async function ensureCacheDir(rootDir) {
|
|
|
70
70
|
return cacheDir;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
// src/vite/plugins/pages-plugin.ts
|
|
74
|
+
import path3 from "path";
|
|
75
|
+
|
|
73
76
|
// src/vite/pages.ts
|
|
74
77
|
import fg from "fast-glob";
|
|
75
78
|
import { readFile } from "fs/promises";
|
|
@@ -128,7 +131,8 @@ function isIndexFile(basename) {
|
|
|
128
131
|
return lower === "index" || lower === "readme";
|
|
129
132
|
}
|
|
130
133
|
function fileToRoute(file) {
|
|
131
|
-
const
|
|
134
|
+
const normalizedFile = file.replace(/^\./, "").replace(/\/\./g, "/");
|
|
135
|
+
const withoutExt = normalizedFile.replace(/\.mdx?$/, "");
|
|
132
136
|
const basename = path2.basename(withoutExt).toLowerCase();
|
|
133
137
|
if (basename === "index" || basename === "readme") {
|
|
134
138
|
const dir = path2.dirname(withoutExt);
|
|
@@ -253,45 +257,82 @@ function buildSidebarTree(pages) {
|
|
|
253
257
|
// src/vite/plugins/pages-plugin.ts
|
|
254
258
|
var VIRTUAL_MODULE_ID = "virtual:prev-pages";
|
|
255
259
|
var RESOLVED_VIRTUAL_MODULE_ID = "\x00" + VIRTUAL_MODULE_ID;
|
|
260
|
+
var VIRTUAL_MODULES_ID = "virtual:prev-page-modules";
|
|
261
|
+
var RESOLVED_VIRTUAL_MODULES_ID = "\x00" + VIRTUAL_MODULES_ID;
|
|
256
262
|
function pagesPlugin(rootDir, options = {}) {
|
|
257
263
|
const { include } = options;
|
|
264
|
+
let cachedPages = null;
|
|
265
|
+
async function getPages() {
|
|
266
|
+
if (!cachedPages) {
|
|
267
|
+
cachedPages = await scanPages(rootDir, { include });
|
|
268
|
+
}
|
|
269
|
+
return cachedPages;
|
|
270
|
+
}
|
|
258
271
|
return {
|
|
259
272
|
name: "prev-pages",
|
|
260
273
|
resolveId(id) {
|
|
261
274
|
if (id === VIRTUAL_MODULE_ID) {
|
|
262
275
|
return RESOLVED_VIRTUAL_MODULE_ID;
|
|
263
276
|
}
|
|
277
|
+
if (id === VIRTUAL_MODULES_ID) {
|
|
278
|
+
return RESOLVED_VIRTUAL_MODULES_ID;
|
|
279
|
+
}
|
|
264
280
|
},
|
|
265
281
|
async load(id) {
|
|
266
282
|
if (id === RESOLVED_VIRTUAL_MODULE_ID) {
|
|
267
|
-
const pages = await
|
|
283
|
+
const pages = await getPages();
|
|
268
284
|
const sidebar = buildSidebarTree(pages);
|
|
269
285
|
return `
|
|
270
286
|
export const pages = ${JSON.stringify(pages)};
|
|
271
287
|
export const sidebar = ${JSON.stringify(sidebar)};
|
|
272
288
|
`;
|
|
273
289
|
}
|
|
290
|
+
if (id === RESOLVED_VIRTUAL_MODULES_ID) {
|
|
291
|
+
const pages = await getPages();
|
|
292
|
+
const imports = pages.map((page, i) => {
|
|
293
|
+
const absolutePath = path3.join(rootDir, page.file);
|
|
294
|
+
return `import * as _page${i} from ${JSON.stringify(absolutePath)};`;
|
|
295
|
+
}).join(`
|
|
296
|
+
`);
|
|
297
|
+
const entries = pages.map((page, i) => {
|
|
298
|
+
return ` ${JSON.stringify("/" + page.file)}: _page${i}`;
|
|
299
|
+
}).join(`,
|
|
300
|
+
`);
|
|
301
|
+
return `${imports}
|
|
302
|
+
|
|
303
|
+
export const pageModules = {
|
|
304
|
+
${entries}
|
|
305
|
+
};`;
|
|
306
|
+
}
|
|
274
307
|
},
|
|
275
308
|
handleHotUpdate({ file, server }) {
|
|
276
309
|
if (file.endsWith(".mdx") || file.endsWith(".md")) {
|
|
310
|
+
cachedPages = null;
|
|
277
311
|
const mod = server.moduleGraph.getModuleById(RESOLVED_VIRTUAL_MODULE_ID);
|
|
312
|
+
const modulesMod = server.moduleGraph.getModuleById(RESOLVED_VIRTUAL_MODULES_ID);
|
|
313
|
+
const mods = [];
|
|
278
314
|
if (mod) {
|
|
279
315
|
server.moduleGraph.invalidateModule(mod);
|
|
280
|
-
|
|
316
|
+
mods.push(mod);
|
|
317
|
+
}
|
|
318
|
+
if (modulesMod) {
|
|
319
|
+
server.moduleGraph.invalidateModule(modulesMod);
|
|
320
|
+
mods.push(modulesMod);
|
|
281
321
|
}
|
|
322
|
+
return mods.length > 0 ? mods : undefined;
|
|
282
323
|
}
|
|
283
324
|
}
|
|
284
325
|
};
|
|
285
326
|
}
|
|
286
327
|
|
|
287
328
|
// src/vite/plugins/entry-plugin.ts
|
|
288
|
-
import
|
|
329
|
+
import path4 from "path";
|
|
289
330
|
import { fileURLToPath } from "url";
|
|
290
331
|
import { existsSync, readFileSync, writeFileSync, unlinkSync } from "fs";
|
|
291
332
|
function findCliRoot() {
|
|
292
|
-
let dir =
|
|
333
|
+
let dir = path4.dirname(fileURLToPath(import.meta.url));
|
|
293
334
|
for (let i = 0;i < 10; i++) {
|
|
294
|
-
const pkgPath =
|
|
335
|
+
const pkgPath = path4.join(dir, "package.json");
|
|
295
336
|
if (existsSync(pkgPath)) {
|
|
296
337
|
try {
|
|
297
338
|
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
@@ -300,15 +341,15 @@ function findCliRoot() {
|
|
|
300
341
|
}
|
|
301
342
|
} catch {}
|
|
302
343
|
}
|
|
303
|
-
const parent =
|
|
344
|
+
const parent = path4.dirname(dir);
|
|
304
345
|
if (parent === dir)
|
|
305
346
|
break;
|
|
306
347
|
dir = parent;
|
|
307
348
|
}
|
|
308
|
-
return
|
|
349
|
+
return path4.dirname(path4.dirname(fileURLToPath(import.meta.url)));
|
|
309
350
|
}
|
|
310
351
|
var cliRoot = findCliRoot();
|
|
311
|
-
var srcRoot =
|
|
352
|
+
var srcRoot = path4.join(cliRoot, "src");
|
|
312
353
|
function getHtml(entryPath, forBuild = false) {
|
|
313
354
|
const scriptSrc = forBuild ? entryPath : `/@fs${entryPath}`;
|
|
314
355
|
return `<!DOCTYPE html>
|
|
@@ -331,13 +372,13 @@ function getHtml(entryPath, forBuild = false) {
|
|
|
331
372
|
</html>`;
|
|
332
373
|
}
|
|
333
374
|
function entryPlugin(rootDir) {
|
|
334
|
-
const entryPath =
|
|
375
|
+
const entryPath = path4.join(srcRoot, "theme/entry.tsx");
|
|
335
376
|
let tempHtmlPath = null;
|
|
336
377
|
return {
|
|
337
378
|
name: "prev-entry",
|
|
338
379
|
config(config, { command }) {
|
|
339
380
|
if (command === "build" && rootDir) {
|
|
340
|
-
tempHtmlPath =
|
|
381
|
+
tempHtmlPath = path4.join(rootDir, "index.html");
|
|
341
382
|
writeFileSync(tempHtmlPath, getHtml(entryPath, true));
|
|
342
383
|
const existingInput = config.build?.rollupOptions?.input || {};
|
|
343
384
|
const inputObj = typeof existingInput === "string" ? { _original: existingInput } : Array.isArray(existingInput) ? Object.fromEntries(existingInput.map((f, i) => [`entry${i}`, f])) : existingInput;
|
|
@@ -384,10 +425,10 @@ function entryPlugin(rootDir) {
|
|
|
384
425
|
|
|
385
426
|
// src/vite/previews.ts
|
|
386
427
|
import fg2 from "fast-glob";
|
|
387
|
-
import
|
|
428
|
+
import path5 from "path";
|
|
388
429
|
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
389
430
|
async function scanPreviews(rootDir) {
|
|
390
|
-
const previewsDir =
|
|
431
|
+
const previewsDir = path5.join(rootDir, "previews");
|
|
391
432
|
if (!existsSync2(previewsDir)) {
|
|
392
433
|
return [];
|
|
393
434
|
}
|
|
@@ -397,17 +438,17 @@ async function scanPreviews(rootDir) {
|
|
|
397
438
|
});
|
|
398
439
|
const previewDirs = new Map;
|
|
399
440
|
for (const file of entryFiles) {
|
|
400
|
-
const dir =
|
|
441
|
+
const dir = path5.dirname(file);
|
|
401
442
|
if (!previewDirs.has(dir)) {
|
|
402
443
|
previewDirs.set(dir, file);
|
|
403
444
|
}
|
|
404
445
|
}
|
|
405
446
|
return Array.from(previewDirs.entries()).map(([dir, file]) => {
|
|
406
|
-
const name = dir === "." ?
|
|
447
|
+
const name = dir === "." ? path5.basename(previewsDir) : dir;
|
|
407
448
|
return {
|
|
408
449
|
name,
|
|
409
450
|
route: `/_preview/${name}`,
|
|
410
|
-
htmlPath:
|
|
451
|
+
htmlPath: path5.join(previewsDir, file)
|
|
411
452
|
};
|
|
412
453
|
});
|
|
413
454
|
}
|
|
@@ -417,8 +458,8 @@ async function scanPreviewFiles(previewDir) {
|
|
|
417
458
|
ignore: ["node_modules/**", "dist/**"]
|
|
418
459
|
});
|
|
419
460
|
return files.map((file) => {
|
|
420
|
-
const content = readFileSync2(
|
|
421
|
-
const ext =
|
|
461
|
+
const content = readFileSync2(path5.join(previewDir, file), "utf-8");
|
|
462
|
+
const ext = path5.extname(file).slice(1);
|
|
422
463
|
return {
|
|
423
464
|
path: file,
|
|
424
465
|
content,
|
|
@@ -562,7 +603,7 @@ async function buildPreviewHtml(config) {
|
|
|
562
603
|
|
|
563
604
|
// src/vite/plugins/previews-plugin.ts
|
|
564
605
|
import { existsSync as existsSync3, mkdirSync, rmSync, writeFileSync as writeFileSync2 } from "fs";
|
|
565
|
-
import
|
|
606
|
+
import path6 from "path";
|
|
566
607
|
var VIRTUAL_MODULE_ID2 = "virtual:prev-previews";
|
|
567
608
|
var RESOLVED_VIRTUAL_MODULE_ID2 = "\x00" + VIRTUAL_MODULE_ID2;
|
|
568
609
|
function previewsPlugin(rootDir) {
|
|
@@ -595,10 +636,10 @@ function previewsPlugin(rootDir) {
|
|
|
595
636
|
async closeBundle() {
|
|
596
637
|
if (!isBuild)
|
|
597
638
|
return;
|
|
598
|
-
const distDir =
|
|
599
|
-
const targetDir =
|
|
600
|
-
const previewsDir =
|
|
601
|
-
const oldPreviewsDir =
|
|
639
|
+
const distDir = path6.join(rootDir, "dist");
|
|
640
|
+
const targetDir = path6.join(distDir, "_preview");
|
|
641
|
+
const previewsDir = path6.join(rootDir, "previews");
|
|
642
|
+
const oldPreviewsDir = path6.join(distDir, "previews");
|
|
602
643
|
if (existsSync3(oldPreviewsDir)) {
|
|
603
644
|
rmSync(oldPreviewsDir, { recursive: true });
|
|
604
645
|
}
|
|
@@ -611,7 +652,7 @@ function previewsPlugin(rootDir) {
|
|
|
611
652
|
console.log(`
|
|
612
653
|
Building ${previews.length} preview(s)...`);
|
|
613
654
|
for (const preview of previews) {
|
|
614
|
-
const previewDir =
|
|
655
|
+
const previewDir = path6.join(previewsDir, preview.name);
|
|
615
656
|
try {
|
|
616
657
|
const config = await buildPreviewConfig(previewDir);
|
|
617
658
|
const result = await buildPreviewHtml(config);
|
|
@@ -619,9 +660,9 @@ function previewsPlugin(rootDir) {
|
|
|
619
660
|
console.error(` ✗ ${preview.name}: ${result.error}`);
|
|
620
661
|
continue;
|
|
621
662
|
}
|
|
622
|
-
const outputDir =
|
|
663
|
+
const outputDir = path6.join(targetDir, preview.name);
|
|
623
664
|
mkdirSync(outputDir, { recursive: true });
|
|
624
|
-
writeFileSync2(
|
|
665
|
+
writeFileSync2(path6.join(outputDir, "index.html"), result.html);
|
|
625
666
|
console.log(` ✓ ${preview.name}`);
|
|
626
667
|
} catch (err) {
|
|
627
668
|
console.error(` ✗ ${preview.name}: ${err}`);
|
|
@@ -692,11 +733,11 @@ function validateConfig(raw) {
|
|
|
692
733
|
}
|
|
693
734
|
// src/config/loader.ts
|
|
694
735
|
import { readFileSync as readFileSync3, existsSync as existsSync4, writeFileSync as writeFileSync3 } from "fs";
|
|
695
|
-
import
|
|
736
|
+
import path7 from "path";
|
|
696
737
|
import yaml from "js-yaml";
|
|
697
738
|
function findConfigFile(rootDir) {
|
|
698
|
-
const yamlPath =
|
|
699
|
-
const ymlPath =
|
|
739
|
+
const yamlPath = path7.join(rootDir, ".prev.yaml");
|
|
740
|
+
const ymlPath = path7.join(rootDir, ".prev.yml");
|
|
700
741
|
if (existsSync4(yamlPath))
|
|
701
742
|
return yamlPath;
|
|
702
743
|
if (existsSync4(ymlPath))
|
|
@@ -718,7 +759,7 @@ function loadConfig(rootDir) {
|
|
|
718
759
|
}
|
|
719
760
|
}
|
|
720
761
|
function saveConfig(rootDir, config) {
|
|
721
|
-
const configPath = findConfigFile(rootDir) ||
|
|
762
|
+
const configPath = findConfigFile(rootDir) || path7.join(rootDir, ".prev.yaml");
|
|
722
763
|
const content = yaml.dump(config, {
|
|
723
764
|
indent: 2,
|
|
724
765
|
lineWidth: -1,
|
|
@@ -787,9 +828,9 @@ function createFriendlyLogger() {
|
|
|
787
828
|
};
|
|
788
829
|
}
|
|
789
830
|
function findCliRoot2() {
|
|
790
|
-
let dir =
|
|
831
|
+
let dir = path8.dirname(fileURLToPath2(import.meta.url));
|
|
791
832
|
for (let i = 0;i < 10; i++) {
|
|
792
|
-
const pkgPath =
|
|
833
|
+
const pkgPath = path8.join(dir, "package.json");
|
|
793
834
|
if (existsSync5(pkgPath)) {
|
|
794
835
|
try {
|
|
795
836
|
const pkg = JSON.parse(readFileSync4(pkgPath, "utf-8"));
|
|
@@ -798,24 +839,24 @@ function findCliRoot2() {
|
|
|
798
839
|
}
|
|
799
840
|
} catch {}
|
|
800
841
|
}
|
|
801
|
-
const parent =
|
|
842
|
+
const parent = path8.dirname(dir);
|
|
802
843
|
if (parent === dir)
|
|
803
844
|
break;
|
|
804
845
|
dir = parent;
|
|
805
846
|
}
|
|
806
|
-
return
|
|
847
|
+
return path8.dirname(path8.dirname(fileURLToPath2(import.meta.url)));
|
|
807
848
|
}
|
|
808
849
|
function findNodeModules(cliRoot2) {
|
|
809
|
-
const localNodeModules =
|
|
810
|
-
if (existsSync5(
|
|
850
|
+
const localNodeModules = path8.join(cliRoot2, "node_modules");
|
|
851
|
+
if (existsSync5(path8.join(localNodeModules, "react"))) {
|
|
811
852
|
return localNodeModules;
|
|
812
853
|
}
|
|
813
854
|
let dir = cliRoot2;
|
|
814
855
|
for (let i = 0;i < 10; i++) {
|
|
815
|
-
const parent =
|
|
856
|
+
const parent = path8.dirname(dir);
|
|
816
857
|
if (parent === dir)
|
|
817
858
|
break;
|
|
818
|
-
if (
|
|
859
|
+
if (path8.basename(parent) === "node_modules" && existsSync5(path8.join(parent, "react"))) {
|
|
819
860
|
return parent;
|
|
820
861
|
}
|
|
821
862
|
dir = parent;
|
|
@@ -824,7 +865,7 @@ function findNodeModules(cliRoot2) {
|
|
|
824
865
|
}
|
|
825
866
|
var cliRoot2 = findCliRoot2();
|
|
826
867
|
var cliNodeModules = findNodeModules(cliRoot2);
|
|
827
|
-
var srcRoot2 =
|
|
868
|
+
var srcRoot2 = path8.join(cliRoot2, "src");
|
|
828
869
|
async function createViteConfig(options) {
|
|
829
870
|
const { rootDir, mode, port, include } = options;
|
|
830
871
|
const cacheDir = await ensureCacheDir(rootDir);
|
|
@@ -882,7 +923,7 @@ async function createViteConfig(options) {
|
|
|
882
923
|
if (urlPath.startsWith("/__") || urlPath.startsWith("/@") || urlPath.startsWith("/node_modules") || urlPath.includes(".")) {
|
|
883
924
|
return next();
|
|
884
925
|
}
|
|
885
|
-
const indexPath =
|
|
926
|
+
const indexPath = path8.join(srcRoot2, "theme/index.html");
|
|
886
927
|
if (existsSync5(indexPath)) {
|
|
887
928
|
server.transformIndexHtml(req.url, readFileSync4(indexPath, "utf-8")).then((html) => {
|
|
888
929
|
res.setHeader("Content-Type", "text/html");
|
|
@@ -900,8 +941,8 @@ async function createViteConfig(options) {
|
|
|
900
941
|
resolveId(id) {
|
|
901
942
|
if (id.startsWith("/_preview/")) {
|
|
902
943
|
const relativePath = id.slice("/_preview/".length);
|
|
903
|
-
const previewsDir =
|
|
904
|
-
const resolved =
|
|
944
|
+
const previewsDir = path8.join(rootDir, "previews");
|
|
945
|
+
const resolved = path8.resolve(previewsDir, relativePath);
|
|
905
946
|
if (resolved.startsWith(previewsDir)) {
|
|
906
947
|
return resolved;
|
|
907
948
|
}
|
|
@@ -911,7 +952,7 @@ async function createViteConfig(options) {
|
|
|
911
952
|
server.middlewares.use(async (req, res, next) => {
|
|
912
953
|
const urlPath = req.url?.split("?")[0] || "";
|
|
913
954
|
if (urlPath === "/_preview-runtime") {
|
|
914
|
-
const templatePath =
|
|
955
|
+
const templatePath = path8.join(srcRoot2, "preview-runtime/template.html");
|
|
915
956
|
if (existsSync5(templatePath)) {
|
|
916
957
|
const html = readFileSync4(templatePath, "utf-8");
|
|
917
958
|
res.setHeader("Content-Type", "text/html");
|
|
@@ -921,8 +962,8 @@ async function createViteConfig(options) {
|
|
|
921
962
|
}
|
|
922
963
|
if (urlPath.startsWith("/_preview-config/")) {
|
|
923
964
|
const previewName = decodeURIComponent(urlPath.slice("/_preview-config/".length));
|
|
924
|
-
const previewsDir =
|
|
925
|
-
const previewDir =
|
|
965
|
+
const previewsDir = path8.join(rootDir, "previews");
|
|
966
|
+
const previewDir = path8.resolve(previewsDir, previewName);
|
|
926
967
|
if (!previewDir.startsWith(previewsDir)) {
|
|
927
968
|
res.statusCode = 403;
|
|
928
969
|
res.end("Forbidden");
|
|
@@ -943,11 +984,11 @@ async function createViteConfig(options) {
|
|
|
943
984
|
}
|
|
944
985
|
}
|
|
945
986
|
if (urlPath.startsWith("/_preview/")) {
|
|
946
|
-
const isHtmlRequest = !
|
|
987
|
+
const isHtmlRequest = !path8.extname(urlPath) || urlPath.endsWith("/");
|
|
947
988
|
if (isHtmlRequest) {
|
|
948
989
|
const previewName = decodeURIComponent(urlPath.slice("/_preview/".length).replace(/\/$/, ""));
|
|
949
|
-
const previewsDir =
|
|
950
|
-
const htmlPath =
|
|
990
|
+
const previewsDir = path8.join(rootDir, "previews");
|
|
991
|
+
const htmlPath = path8.resolve(previewsDir, previewName, "index.html");
|
|
951
992
|
if (!htmlPath.startsWith(previewsDir)) {
|
|
952
993
|
return next();
|
|
953
994
|
}
|
|
@@ -974,15 +1015,15 @@ async function createViteConfig(options) {
|
|
|
974
1015
|
],
|
|
975
1016
|
resolve: {
|
|
976
1017
|
alias: {
|
|
977
|
-
"@prev/ui":
|
|
978
|
-
"@prev/theme":
|
|
979
|
-
react:
|
|
980
|
-
"react-dom":
|
|
981
|
-
"@tanstack/react-router":
|
|
982
|
-
"@mdx-js/react":
|
|
983
|
-
mermaid:
|
|
984
|
-
dayjs:
|
|
985
|
-
"@terrastruct/d2":
|
|
1018
|
+
"@prev/ui": path8.join(srcRoot2, "ui"),
|
|
1019
|
+
"@prev/theme": path8.join(srcRoot2, "theme"),
|
|
1020
|
+
react: path8.join(cliNodeModules, "react"),
|
|
1021
|
+
"react-dom": path8.join(cliNodeModules, "react-dom"),
|
|
1022
|
+
"@tanstack/react-router": path8.join(cliNodeModules, "@tanstack/react-router"),
|
|
1023
|
+
"@mdx-js/react": path8.join(cliNodeModules, "@mdx-js/react"),
|
|
1024
|
+
mermaid: path8.join(cliNodeModules, "mermaid"),
|
|
1025
|
+
dayjs: path8.join(cliNodeModules, "dayjs"),
|
|
1026
|
+
"@terrastruct/d2": path8.join(cliNodeModules, "@terrastruct/d2")
|
|
986
1027
|
},
|
|
987
1028
|
dedupe: [
|
|
988
1029
|
"react",
|
|
@@ -1007,6 +1048,7 @@ async function createViteConfig(options) {
|
|
|
1007
1048
|
"virtual:prev-config",
|
|
1008
1049
|
"virtual:prev-previews",
|
|
1009
1050
|
"virtual:prev-pages",
|
|
1051
|
+
"virtual:prev-page-modules",
|
|
1010
1052
|
"@prev/theme"
|
|
1011
1053
|
]
|
|
1012
1054
|
},
|
|
@@ -1021,8 +1063,8 @@ async function createViteConfig(options) {
|
|
|
1021
1063
|
},
|
|
1022
1064
|
warmup: {
|
|
1023
1065
|
clientFiles: [
|
|
1024
|
-
|
|
1025
|
-
|
|
1066
|
+
path8.join(srcRoot2, "theme/entry.tsx"),
|
|
1067
|
+
path8.join(srcRoot2, "theme/styles.css")
|
|
1026
1068
|
]
|
|
1027
1069
|
}
|
|
1028
1070
|
},
|
|
@@ -1031,12 +1073,12 @@ async function createViteConfig(options) {
|
|
|
1031
1073
|
strictPort: false
|
|
1032
1074
|
},
|
|
1033
1075
|
build: {
|
|
1034
|
-
outDir:
|
|
1076
|
+
outDir: path8.join(rootDir, "dist"),
|
|
1035
1077
|
reportCompressedSize: false,
|
|
1036
1078
|
chunkSizeWarningLimit: 1e4,
|
|
1037
1079
|
rollupOptions: {
|
|
1038
1080
|
input: {
|
|
1039
|
-
main:
|
|
1081
|
+
main: path8.join(srcRoot2, "theme/index.html")
|
|
1040
1082
|
}
|
|
1041
1083
|
}
|
|
1042
1084
|
}
|
|
@@ -1077,7 +1119,7 @@ async function findAvailablePort(minPort, maxPort) {
|
|
|
1077
1119
|
// src/vite/start.ts
|
|
1078
1120
|
import { exec as exec2 } from "child_process";
|
|
1079
1121
|
import { existsSync as existsSync6, rmSync as rmSync2 } from "fs";
|
|
1080
|
-
import
|
|
1122
|
+
import path9 from "path";
|
|
1081
1123
|
function printWelcome(type) {
|
|
1082
1124
|
console.log();
|
|
1083
1125
|
console.log(" ✨ prev");
|
|
@@ -1110,8 +1152,8 @@ function openBrowser(url) {
|
|
|
1110
1152
|
console.log(` ↗ Opened ${url}`);
|
|
1111
1153
|
}
|
|
1112
1154
|
function clearCache(rootDir) {
|
|
1113
|
-
const viteCacheDir =
|
|
1114
|
-
const nodeModulesVite =
|
|
1155
|
+
const viteCacheDir = path9.join(rootDir, ".vite");
|
|
1156
|
+
const nodeModulesVite = path9.join(rootDir, "node_modules", ".vite");
|
|
1115
1157
|
let cleared = 0;
|
|
1116
1158
|
if (existsSync6(viteCacheDir)) {
|
|
1117
1159
|
rmSync2(viteCacheDir, { recursive: true });
|
|
@@ -1211,15 +1253,15 @@ async function previewSite(rootDir, options = {}) {
|
|
|
1211
1253
|
import yaml2 from "js-yaml";
|
|
1212
1254
|
function getVersion() {
|
|
1213
1255
|
try {
|
|
1214
|
-
let dir =
|
|
1256
|
+
let dir = path10.dirname(fileURLToPath3(import.meta.url));
|
|
1215
1257
|
for (let i = 0;i < 5; i++) {
|
|
1216
|
-
const pkgPath =
|
|
1258
|
+
const pkgPath = path10.join(dir, "package.json");
|
|
1217
1259
|
if (existsSync7(pkgPath)) {
|
|
1218
1260
|
const pkg = JSON.parse(readFileSync5(pkgPath, "utf-8"));
|
|
1219
1261
|
if (pkg.name === "prev-cli")
|
|
1220
1262
|
return pkg.version;
|
|
1221
1263
|
}
|
|
1222
|
-
dir =
|
|
1264
|
+
dir = path10.dirname(dir);
|
|
1223
1265
|
}
|
|
1224
1266
|
} catch {}
|
|
1225
1267
|
return "unknown";
|
|
@@ -1236,7 +1278,7 @@ var { values, positionals } = parseArgs({
|
|
|
1236
1278
|
allowPositionals: true
|
|
1237
1279
|
});
|
|
1238
1280
|
var command = positionals[0] || "dev";
|
|
1239
|
-
var rootDir =
|
|
1281
|
+
var rootDir = path10.resolve(values.cwd || (command === "config" || command === "create" ? "." : positionals[1]) || ".");
|
|
1240
1282
|
function printHelp() {
|
|
1241
1283
|
console.log(`
|
|
1242
1284
|
prev - Zero-config documentation site generator
|
|
@@ -1344,8 +1386,8 @@ async function clearViteCache(rootDir2) {
|
|
|
1344
1386
|
console.log(` ✓ Removed ${prevCacheDir}`);
|
|
1345
1387
|
}
|
|
1346
1388
|
} catch {}
|
|
1347
|
-
const viteCacheDir =
|
|
1348
|
-
const nodeModulesVite =
|
|
1389
|
+
const viteCacheDir = path10.join(rootDir2, ".vite");
|
|
1390
|
+
const nodeModulesVite = path10.join(rootDir2, "node_modules", ".vite");
|
|
1349
1391
|
if (existsSync7(viteCacheDir)) {
|
|
1350
1392
|
rmSync3(viteCacheDir, { recursive: true });
|
|
1351
1393
|
cleared++;
|
|
@@ -1397,7 +1439,7 @@ function handleConfig(rootDir2, subcommand) {
|
|
|
1397
1439
|
break;
|
|
1398
1440
|
}
|
|
1399
1441
|
case "init": {
|
|
1400
|
-
const targetPath =
|
|
1442
|
+
const targetPath = path10.join(rootDir2, ".prev.yaml");
|
|
1401
1443
|
if (configPath) {
|
|
1402
1444
|
console.log(`
|
|
1403
1445
|
Config already exists: ${configPath}
|
|
@@ -1456,7 +1498,7 @@ Available subcommands: show, init, path`);
|
|
|
1456
1498
|
}
|
|
1457
1499
|
}
|
|
1458
1500
|
function createPreview(rootDir2, name) {
|
|
1459
|
-
const previewDir =
|
|
1501
|
+
const previewDir = path10.join(rootDir2, "previews", name);
|
|
1460
1502
|
if (existsSync7(previewDir)) {
|
|
1461
1503
|
console.error(`Preview "${name}" already exists at: ${previewDir}`);
|
|
1462
1504
|
process.exit(1);
|
|
@@ -1582,8 +1624,8 @@ export default function App() {
|
|
|
1582
1624
|
.dark\\:text-white { color: #fff; }
|
|
1583
1625
|
}
|
|
1584
1626
|
`;
|
|
1585
|
-
writeFileSync4(
|
|
1586
|
-
writeFileSync4(
|
|
1627
|
+
writeFileSync4(path10.join(previewDir, "App.tsx"), appTsx);
|
|
1628
|
+
writeFileSync4(path10.join(previewDir, "styles.css"), stylesCss);
|
|
1587
1629
|
console.log(`
|
|
1588
1630
|
✨ Created preview: previews/${name}/
|
|
1589
1631
|
|
package/package.json
CHANGED
package/src/theme/entry.tsx
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
} from '@tanstack/react-router'
|
|
15
15
|
import { MDXProvider } from '@mdx-js/react'
|
|
16
16
|
import { pages, sidebar } from 'virtual:prev-pages'
|
|
17
|
+
import { pageModules } from 'virtual:prev-page-modules'
|
|
17
18
|
import { previews } from 'virtual:prev-previews'
|
|
18
19
|
import { Preview } from './Preview'
|
|
19
20
|
import { useDiagrams } from './diagrams'
|
|
@@ -64,11 +65,10 @@ function convertToPageTree(items: any[]): PageTree.Root {
|
|
|
64
65
|
}
|
|
65
66
|
}
|
|
66
67
|
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
// Page modules are dynamically generated by the pages plugin
|
|
69
|
+
// This ensures all MDX files are included regardless of dot-prefixed directories
|
|
70
70
|
function getPageComponent(file: string): React.ComponentType | null {
|
|
71
|
-
const mod = pageModules[`/${file}`]
|
|
71
|
+
const mod = pageModules[`/${file}`]
|
|
72
72
|
return mod?.default || null
|
|
73
73
|
}
|
|
74
74
|
|