prev-cli 0.1.4 → 0.1.6
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 +86 -7
- package/package.json +2 -1
- package/src/theme/Layout.tsx +47 -6
package/dist/cli.js
CHANGED
|
@@ -7,6 +7,7 @@ import { parseArgs } from "util";
|
|
|
7
7
|
import { createServer as createServer2, build, preview } from "vite";
|
|
8
8
|
|
|
9
9
|
// src/vite/config.ts
|
|
10
|
+
import { createLogger } from "vite";
|
|
10
11
|
import react from "@vitejs/plugin-react-swc";
|
|
11
12
|
import tailwindcss from "@tailwindcss/vite";
|
|
12
13
|
import mdx from "@mdx-js/rollup";
|
|
@@ -269,6 +270,59 @@ function entryPlugin(rootDir) {
|
|
|
269
270
|
}
|
|
270
271
|
|
|
271
272
|
// src/vite/config.ts
|
|
273
|
+
function createFriendlyLogger() {
|
|
274
|
+
const logger = createLogger("info", { allowClearScreen: false });
|
|
275
|
+
const hiddenPatterns = [
|
|
276
|
+
/Re-optimizing dependencies/,
|
|
277
|
+
/new dependencies optimized/,
|
|
278
|
+
/optimized dependencies changed/,
|
|
279
|
+
/Dependencies bundled/,
|
|
280
|
+
/Pre-bundling dependencies/,
|
|
281
|
+
/\(client\) ✨/
|
|
282
|
+
];
|
|
283
|
+
const transformMessage = (msg) => {
|
|
284
|
+
for (const pattern of hiddenPatterns) {
|
|
285
|
+
if (pattern.test(msg))
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
if (msg.includes("hmr update")) {
|
|
289
|
+
const match = msg.match(/hmr update (.+)/);
|
|
290
|
+
if (match) {
|
|
291
|
+
return ` ↻ Updated: ${match[1]}`;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (msg.includes("page reload")) {
|
|
295
|
+
return " ↻ Page reloaded";
|
|
296
|
+
}
|
|
297
|
+
return msg;
|
|
298
|
+
};
|
|
299
|
+
return {
|
|
300
|
+
...logger,
|
|
301
|
+
info(msg, options) {
|
|
302
|
+
const transformed = transformMessage(msg);
|
|
303
|
+
if (transformed)
|
|
304
|
+
logger.info(transformed, options);
|
|
305
|
+
},
|
|
306
|
+
warn(msg, options) {
|
|
307
|
+
if (!hiddenPatterns.some((p) => p.test(msg))) {
|
|
308
|
+
logger.warn(msg, options);
|
|
309
|
+
}
|
|
310
|
+
},
|
|
311
|
+
warnOnce(msg, options) {
|
|
312
|
+
if (!hiddenPatterns.some((p) => p.test(msg))) {
|
|
313
|
+
logger.warnOnce(msg, options);
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
error(msg, options) {
|
|
317
|
+
logger.error(msg, options);
|
|
318
|
+
},
|
|
319
|
+
clearScreen() {},
|
|
320
|
+
hasErrorLogged(err) {
|
|
321
|
+
return logger.hasErrorLogged(err);
|
|
322
|
+
},
|
|
323
|
+
hasWarned: false
|
|
324
|
+
};
|
|
325
|
+
}
|
|
272
326
|
function findCliRoot2() {
|
|
273
327
|
let dir = path4.dirname(fileURLToPath2(import.meta.url));
|
|
274
328
|
for (let i = 0;i < 10; i++) {
|
|
@@ -315,6 +369,8 @@ async function createViteConfig(options) {
|
|
|
315
369
|
root: rootDir,
|
|
316
370
|
mode,
|
|
317
371
|
cacheDir,
|
|
372
|
+
customLogger: createFriendlyLogger(),
|
|
373
|
+
logLevel: mode === "production" ? "silent" : "info",
|
|
318
374
|
plugins: [
|
|
319
375
|
mdx({
|
|
320
376
|
remarkPlugins: [remarkGfm],
|
|
@@ -365,7 +421,9 @@ async function createViteConfig(options) {
|
|
|
365
421
|
strictPort: false
|
|
366
422
|
},
|
|
367
423
|
build: {
|
|
368
|
-
outDir: path4.join(rootDir, "dist")
|
|
424
|
+
outDir: path4.join(rootDir, "dist"),
|
|
425
|
+
reportCompressedSize: false,
|
|
426
|
+
chunkSizeWarningLimit: 1e4
|
|
369
427
|
}
|
|
370
428
|
};
|
|
371
429
|
}
|
|
@@ -402,6 +460,22 @@ async function findAvailablePort(minPort, maxPort) {
|
|
|
402
460
|
}
|
|
403
461
|
|
|
404
462
|
// src/vite/start.ts
|
|
463
|
+
function printWelcome(type) {
|
|
464
|
+
console.log();
|
|
465
|
+
console.log(" ✨ prev");
|
|
466
|
+
console.log();
|
|
467
|
+
if (type === "dev") {
|
|
468
|
+
console.log(" Your docs are ready! Open in your browser:");
|
|
469
|
+
} else {
|
|
470
|
+
console.log(" Previewing your production build:");
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
function printReady() {
|
|
474
|
+
console.log();
|
|
475
|
+
console.log(" Edit your .mdx files and see changes instantly.");
|
|
476
|
+
console.log(" Press Ctrl+C to stop.");
|
|
477
|
+
console.log();
|
|
478
|
+
}
|
|
405
479
|
async function startDev(rootDir, options = {}) {
|
|
406
480
|
const port = options.port ?? await getRandomPort();
|
|
407
481
|
const config = await createViteConfig({
|
|
@@ -411,20 +485,24 @@ async function startDev(rootDir, options = {}) {
|
|
|
411
485
|
});
|
|
412
486
|
const server = await createServer2(config);
|
|
413
487
|
await server.listen();
|
|
414
|
-
|
|
415
|
-
console.log(` prev dev server running at:`);
|
|
488
|
+
printWelcome("dev");
|
|
416
489
|
server.printUrls();
|
|
417
|
-
|
|
490
|
+
printReady();
|
|
418
491
|
return server;
|
|
419
492
|
}
|
|
420
493
|
async function buildSite(rootDir) {
|
|
494
|
+
console.log();
|
|
495
|
+
console.log(" ✨ prev build");
|
|
496
|
+
console.log();
|
|
497
|
+
console.log(" Building your documentation site...");
|
|
421
498
|
const config = await createViteConfig({
|
|
422
499
|
rootDir,
|
|
423
500
|
mode: "production"
|
|
424
501
|
});
|
|
425
502
|
await build(config);
|
|
426
503
|
console.log();
|
|
427
|
-
console.log(
|
|
504
|
+
console.log(" Done! Your site is ready in ./dist");
|
|
505
|
+
console.log(" You can deploy this folder anywhere.");
|
|
428
506
|
console.log();
|
|
429
507
|
}
|
|
430
508
|
async function previewSite(rootDir, options = {}) {
|
|
@@ -435,10 +513,11 @@ async function previewSite(rootDir, options = {}) {
|
|
|
435
513
|
port
|
|
436
514
|
});
|
|
437
515
|
const server = await preview(config);
|
|
438
|
-
|
|
439
|
-
console.log(` Preview server running at:`);
|
|
516
|
+
printWelcome("preview");
|
|
440
517
|
server.printUrls();
|
|
441
518
|
console.log();
|
|
519
|
+
console.log(" Press Ctrl+C to stop.");
|
|
520
|
+
console.log();
|
|
442
521
|
return server;
|
|
443
522
|
}
|
|
444
523
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prev-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Transform MDX directories into beautiful documentation websites",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@mdx-js/rollup": "^3.0.0",
|
|
44
|
+
"@terrastruct/d2": "^0.1.33",
|
|
44
45
|
"@tailwindcss/vite": "^4.0.0",
|
|
45
46
|
"@vitejs/plugin-react-swc": "^4.2.2",
|
|
46
47
|
"class-variance-authority": "^0.7.0",
|
package/src/theme/Layout.tsx
CHANGED
|
@@ -5,15 +5,16 @@ import './styles.css'
|
|
|
5
5
|
|
|
6
6
|
// Lazy-load and render mermaid diagrams
|
|
7
7
|
async function renderMermaidDiagrams() {
|
|
8
|
-
const codeBlocks = document.querySelectorAll('code.language-mermaid')
|
|
8
|
+
const codeBlocks = document.querySelectorAll('code.language-mermaid, code.hljs.language-mermaid')
|
|
9
9
|
if (codeBlocks.length === 0) return
|
|
10
10
|
|
|
11
11
|
const mermaid = await import('mermaid')
|
|
12
12
|
mermaid.default.initialize({ startOnLoad: false, theme: 'neutral' })
|
|
13
13
|
|
|
14
14
|
for (const block of codeBlocks) {
|
|
15
|
-
const pre = block.parentElement
|
|
16
|
-
if (!pre || pre.dataset.
|
|
15
|
+
const pre = block.parentElement as HTMLElement
|
|
16
|
+
if (!pre || pre.dataset.rendered) continue
|
|
17
|
+
pre.dataset.rendered = 'true'
|
|
17
18
|
|
|
18
19
|
const code = block.textContent || ''
|
|
19
20
|
const container = document.createElement('div')
|
|
@@ -22,19 +23,59 @@ async function renderMermaidDiagrams() {
|
|
|
22
23
|
try {
|
|
23
24
|
const { svg } = await mermaid.default.render(`mermaid-${Math.random().toString(36).slice(2)}`, code)
|
|
24
25
|
container.innerHTML = svg
|
|
25
|
-
|
|
26
|
+
// Hide original instead of replacing (avoids React DOM conflicts)
|
|
27
|
+
pre.style.display = 'none'
|
|
28
|
+
pre.insertAdjacentElement('afterend', container)
|
|
26
29
|
} catch (e) {
|
|
27
30
|
console.error('Mermaid render error:', e)
|
|
28
31
|
}
|
|
29
32
|
}
|
|
30
33
|
}
|
|
31
34
|
|
|
35
|
+
// Lazy-load and render D2 diagrams
|
|
36
|
+
async function renderD2Diagrams() {
|
|
37
|
+
const codeBlocks = document.querySelectorAll('code.language-d2, code.hljs.language-d2')
|
|
38
|
+
if (codeBlocks.length === 0) return
|
|
39
|
+
|
|
40
|
+
const { D2 } = await import('@terrastruct/d2')
|
|
41
|
+
const d2 = new D2()
|
|
42
|
+
|
|
43
|
+
for (const block of codeBlocks) {
|
|
44
|
+
const pre = block.parentElement as HTMLElement
|
|
45
|
+
if (!pre || pre.dataset.rendered) continue
|
|
46
|
+
pre.dataset.rendered = 'true'
|
|
47
|
+
|
|
48
|
+
const code = block.textContent || ''
|
|
49
|
+
const container = document.createElement('div')
|
|
50
|
+
container.className = 'd2-diagram'
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
const result = await d2.compile(code)
|
|
54
|
+
const svg = await d2.render(result.diagram)
|
|
55
|
+
container.innerHTML = svg
|
|
56
|
+
// Hide original instead of replacing (avoids React DOM conflicts)
|
|
57
|
+
pre.style.display = 'none'
|
|
58
|
+
pre.insertAdjacentElement('afterend', container)
|
|
59
|
+
} catch (e) {
|
|
60
|
+
console.error('D2 render error:', e)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Render all diagrams
|
|
66
|
+
async function renderDiagrams() {
|
|
67
|
+
await Promise.all([
|
|
68
|
+
renderMermaidDiagrams(),
|
|
69
|
+
renderD2Diagrams()
|
|
70
|
+
])
|
|
71
|
+
}
|
|
72
|
+
|
|
32
73
|
export function Layout() {
|
|
33
74
|
const location = useLocation()
|
|
34
75
|
|
|
35
76
|
useEffect(() => {
|
|
36
|
-
// Render
|
|
37
|
-
const timer = setTimeout(
|
|
77
|
+
// Render diagrams after content loads
|
|
78
|
+
const timer = setTimeout(renderDiagrams, 100)
|
|
38
79
|
return () => clearTimeout(timer)
|
|
39
80
|
}, [location.pathname])
|
|
40
81
|
|