prev-cli 0.11.0 → 0.11.1

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 (2) hide show
  1. package/dist/cli.js +32 -15
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -348,7 +348,7 @@ function entryPlugin(rootDir) {
348
348
  const html = getHtml(entryPath, false);
349
349
  server.middlewares.use(async (req, res, next) => {
350
350
  const url = req.url || "/";
351
- if (url === "/" || !url.includes(".") && !url.startsWith("/@")) {
351
+ if (url === "/" || !url.includes(".") && !url.startsWith("/@") && !url.startsWith("/_preview/")) {
352
352
  try {
353
353
  const transformed = await server.transformIndexHtml(url, html);
354
354
  res.setHeader("Content-Type", "text/html");
@@ -549,25 +549,42 @@ async function createViteConfig(options) {
549
549
  previewsPlugin(rootDir),
550
550
  {
551
551
  name: "prev-preview-server",
552
+ resolveId(id) {
553
+ if (id.startsWith("/_preview/")) {
554
+ const relativePath = id.slice("/_preview/".length);
555
+ const previewsDir = path6.join(rootDir, "previews");
556
+ const resolved = path6.resolve(previewsDir, relativePath);
557
+ if (resolved.startsWith(previewsDir)) {
558
+ return resolved;
559
+ }
560
+ }
561
+ },
552
562
  configureServer(server) {
553
563
  server.middlewares.use(async (req, res, next) => {
554
564
  if (req.url?.startsWith("/_preview/")) {
555
- const previewName = decodeURIComponent(req.url.slice("/_preview/".length).split("?")[0]);
556
- const previewsDir = path6.join(rootDir, "previews");
557
- const htmlPath = path6.resolve(previewsDir, previewName, "index.html");
558
- if (!htmlPath.startsWith(previewsDir)) {
559
- return next();
560
- }
561
- if (existsSync4(htmlPath)) {
562
- try {
563
- const html = await server.transformIndexHtml(req.url, readFileSync2(htmlPath, "utf-8"));
564
- res.setHeader("Content-Type", "text/html");
565
- res.end(html);
566
- return;
567
- } catch (err) {
568
- console.error("Error serving preview:", err);
565
+ const urlPath = req.url.split("?")[0];
566
+ const isHtmlRequest = !path6.extname(urlPath) || urlPath.endsWith("/");
567
+ if (isHtmlRequest) {
568
+ const previewName = decodeURIComponent(urlPath.slice("/_preview/".length).replace(/\/$/, ""));
569
+ const previewsDir = path6.join(rootDir, "previews");
570
+ const htmlPath = path6.resolve(previewsDir, previewName, "index.html");
571
+ if (!htmlPath.startsWith(previewsDir)) {
569
572
  return next();
570
573
  }
574
+ if (existsSync4(htmlPath)) {
575
+ try {
576
+ let html = readFileSync2(htmlPath, "utf-8");
577
+ const previewBase = `/_preview/${previewName}/`;
578
+ html = html.replace(/(src|href)=["']\.\/([^"']+)["']/g, `$1="${previewBase}$2"`);
579
+ const transformed = await server.transformIndexHtml(req.url, html);
580
+ res.setHeader("Content-Type", "text/html");
581
+ res.end(transformed);
582
+ return;
583
+ } catch (err) {
584
+ console.error("Error serving preview:", err);
585
+ return next();
586
+ }
587
+ }
571
588
  }
572
589
  }
573
590
  next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prev-cli",
3
- "version": "0.11.0",
3
+ "version": "0.11.1",
4
4
  "description": "Transform MDX directories into beautiful documentation websites",
5
5
  "type": "module",
6
6
  "license": "MIT",