prev-cli 0.1.5 → 0.1.7
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 +94 -9
- package/package.json +1 -1
- package/src/theme/Layout.tsx +23 -18
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],
|
|
@@ -331,7 +387,10 @@ async function createViteConfig(options) {
|
|
|
331
387
|
"@prev/theme": path4.join(srcRoot2, "theme"),
|
|
332
388
|
react: path4.join(cliNodeModules, "react"),
|
|
333
389
|
"react-dom": path4.join(cliNodeModules, "react-dom"),
|
|
334
|
-
"react-router-dom": path4.join(cliNodeModules, "react-router-dom")
|
|
390
|
+
"react-router-dom": path4.join(cliNodeModules, "react-router-dom"),
|
|
391
|
+
mermaid: path4.join(cliNodeModules, "mermaid"),
|
|
392
|
+
dayjs: path4.join(cliNodeModules, "dayjs"),
|
|
393
|
+
"@terrastruct/d2": path4.join(cliNodeModules, "@terrastruct/d2")
|
|
335
394
|
}
|
|
336
395
|
},
|
|
337
396
|
optimizeDeps: {
|
|
@@ -342,7 +401,10 @@ async function createViteConfig(options) {
|
|
|
342
401
|
"react-dom/client",
|
|
343
402
|
"react-router-dom",
|
|
344
403
|
"react/jsx-runtime",
|
|
345
|
-
"react/jsx-dev-runtime"
|
|
404
|
+
"react/jsx-dev-runtime",
|
|
405
|
+
"mermaid",
|
|
406
|
+
"dayjs",
|
|
407
|
+
"@terrastruct/d2"
|
|
346
408
|
],
|
|
347
409
|
exclude: [
|
|
348
410
|
"clsx",
|
|
@@ -365,7 +427,9 @@ async function createViteConfig(options) {
|
|
|
365
427
|
strictPort: false
|
|
366
428
|
},
|
|
367
429
|
build: {
|
|
368
|
-
outDir: path4.join(rootDir, "dist")
|
|
430
|
+
outDir: path4.join(rootDir, "dist"),
|
|
431
|
+
reportCompressedSize: false,
|
|
432
|
+
chunkSizeWarningLimit: 1e4
|
|
369
433
|
}
|
|
370
434
|
};
|
|
371
435
|
}
|
|
@@ -402,6 +466,22 @@ async function findAvailablePort(minPort, maxPort) {
|
|
|
402
466
|
}
|
|
403
467
|
|
|
404
468
|
// src/vite/start.ts
|
|
469
|
+
function printWelcome(type) {
|
|
470
|
+
console.log();
|
|
471
|
+
console.log(" ✨ prev");
|
|
472
|
+
console.log();
|
|
473
|
+
if (type === "dev") {
|
|
474
|
+
console.log(" Your docs are ready! Open in your browser:");
|
|
475
|
+
} else {
|
|
476
|
+
console.log(" Previewing your production build:");
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
function printReady() {
|
|
480
|
+
console.log();
|
|
481
|
+
console.log(" Edit your .mdx files and see changes instantly.");
|
|
482
|
+
console.log(" Press Ctrl+C to stop.");
|
|
483
|
+
console.log();
|
|
484
|
+
}
|
|
405
485
|
async function startDev(rootDir, options = {}) {
|
|
406
486
|
const port = options.port ?? await getRandomPort();
|
|
407
487
|
const config = await createViteConfig({
|
|
@@ -411,20 +491,24 @@ async function startDev(rootDir, options = {}) {
|
|
|
411
491
|
});
|
|
412
492
|
const server = await createServer2(config);
|
|
413
493
|
await server.listen();
|
|
414
|
-
|
|
415
|
-
console.log(` prev dev server running at:`);
|
|
494
|
+
printWelcome("dev");
|
|
416
495
|
server.printUrls();
|
|
417
|
-
|
|
496
|
+
printReady();
|
|
418
497
|
return server;
|
|
419
498
|
}
|
|
420
499
|
async function buildSite(rootDir) {
|
|
500
|
+
console.log();
|
|
501
|
+
console.log(" ✨ prev build");
|
|
502
|
+
console.log();
|
|
503
|
+
console.log(" Building your documentation site...");
|
|
421
504
|
const config = await createViteConfig({
|
|
422
505
|
rootDir,
|
|
423
506
|
mode: "production"
|
|
424
507
|
});
|
|
425
508
|
await build(config);
|
|
426
509
|
console.log();
|
|
427
|
-
console.log(
|
|
510
|
+
console.log(" Done! Your site is ready in ./dist");
|
|
511
|
+
console.log(" You can deploy this folder anywhere.");
|
|
428
512
|
console.log();
|
|
429
513
|
}
|
|
430
514
|
async function previewSite(rootDir, options = {}) {
|
|
@@ -435,10 +519,11 @@ async function previewSite(rootDir, options = {}) {
|
|
|
435
519
|
port
|
|
436
520
|
});
|
|
437
521
|
const server = await preview(config);
|
|
438
|
-
|
|
439
|
-
console.log(` Preview server running at:`);
|
|
522
|
+
printWelcome("preview");
|
|
440
523
|
server.printUrls();
|
|
441
524
|
console.log();
|
|
525
|
+
console.log(" Press Ctrl+C to stop.");
|
|
526
|
+
console.log();
|
|
442
527
|
return server;
|
|
443
528
|
}
|
|
444
529
|
|
package/package.json
CHANGED
package/src/theme/Layout.tsx
CHANGED
|
@@ -37,28 +37,33 @@ async function renderD2Diagrams() {
|
|
|
37
37
|
const codeBlocks = document.querySelectorAll('code.language-d2, code.hljs.language-d2')
|
|
38
38
|
if (codeBlocks.length === 0) return
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
try {
|
|
41
|
+
const d2Module = await import('@terrastruct/d2')
|
|
42
|
+
const { D2 } = d2Module
|
|
43
|
+
const d2 = new D2()
|
|
42
44
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
for (const block of codeBlocks) {
|
|
46
|
+
const pre = block.parentElement as HTMLElement
|
|
47
|
+
if (!pre || pre.dataset.rendered) continue
|
|
48
|
+
pre.dataset.rendered = 'true'
|
|
47
49
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
const code = block.textContent || ''
|
|
51
|
+
const container = document.createElement('div')
|
|
52
|
+
container.className = 'd2-diagram'
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
54
|
+
try {
|
|
55
|
+
const result = await d2.compile(code)
|
|
56
|
+
const svg = await d2.render(result.diagram, result.renderOptions)
|
|
57
|
+
container.innerHTML = svg
|
|
58
|
+
// Hide original instead of replacing (avoids React DOM conflicts)
|
|
59
|
+
pre.style.display = 'none'
|
|
60
|
+
pre.insertAdjacentElement('afterend', container)
|
|
61
|
+
} catch (e) {
|
|
62
|
+
console.error('D2 render error:', e)
|
|
63
|
+
}
|
|
61
64
|
}
|
|
65
|
+
} catch (e) {
|
|
66
|
+
console.error('D2 library load error:', e)
|
|
62
67
|
}
|
|
63
68
|
}
|
|
64
69
|
|