@deot/docs-dever 1.1.2 → 1.1.3

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/index.cjs CHANGED
@@ -35,6 +35,7 @@ _vitejs_plugin_vue_jsx = __toESM(_vitejs_plugin_vue_jsx, 1);
35
35
  let node_fs = require("node:fs");
36
36
  node_fs = __toESM(node_fs, 1);
37
37
  let node_crypto = require("node:crypto");
38
+ let node_module = require("node:module");
38
39
  let node_http = require("node:http");
39
40
  node_http = __toESM(node_http, 1);
40
41
  //#region packages/dever/src/workspace.ts
@@ -324,6 +325,48 @@ var respondWorkspaceModule = async (req, res, projectRoot) => {
324
325
  return true;
325
326
  };
326
327
  //#endregion
328
+ //#region packages/dever/src/plugins/optional-dependencies.ts
329
+ var resolveOptionalDependency = (id, importer) => {
330
+ if (!importer || !node_path.isAbsolute(importer) || /^[./]|:/.test(id)) return;
331
+ const filename = importer.split("?")[0];
332
+ const name = id.startsWith("@") ? id.split("/").slice(0, 2).join("/") : id.split("/")[0];
333
+ let directory = node_path.dirname(filename);
334
+ while (true) {
335
+ const manifest = node_path.join(directory, "package.json");
336
+ if (node_fs.existsSync(manifest)) {
337
+ const pkg = JSON.parse(node_fs.readFileSync(manifest, "utf8"));
338
+ if (!pkg.peerDependencies?.[name] || !pkg.peerDependenciesMeta?.[name]?.optional) return;
339
+ try {
340
+ (0, node_module.createRequire)(filename).resolve(id);
341
+ } catch (reason) {
342
+ if (reason.code === "MODULE_NOT_FOUND") return {
343
+ id: `https://cdn.jsdelivr.net/npm/${id}/+esm`,
344
+ external: true
345
+ };
346
+ }
347
+ return;
348
+ }
349
+ const parent = node_path.dirname(directory);
350
+ if (parent === directory) return;
351
+ directory = parent;
352
+ }
353
+ };
354
+ /**
355
+ * 本地包未安装的 optional peer 由浏览器按需从 CDN 加载。
356
+ * @returns 同时用于 Vite 模块转换与依赖预打包的解析插件。
357
+ */
358
+ var createOptionalDependenciesPlugin = () => {
359
+ const resolver = {
360
+ name: "docs-optional-dependencies",
361
+ resolveId: resolveOptionalDependency
362
+ };
363
+ return {
364
+ ...resolver,
365
+ enforce: "pre",
366
+ config: () => ({ optimizeDeps: { rolldownOptions: { plugins: [resolver] } } })
367
+ };
368
+ };
369
+ //#endregion
327
370
  //#region packages/dever/src/plugins/index.ts
328
371
  var innerPathRegex = /^\/(@|__)/;
329
372
  var directResourceExtensions = /* @__PURE__ */ new Set([".md", ".json"]);
@@ -522,6 +565,7 @@ var configureWorkspaceServer = (options, resolvedWorkspace) => (server) => {
522
565
  let realResourceRoot = realWorkspace;
523
566
  try {
524
567
  const rawPathname = getRawPathname(req.url || "/");
568
+ if (/^\/__docs\/preview(?:-config-generate)?$/.test(rawPathname) && String(req.headers.accept || "").includes("text/html")) return next();
525
569
  if (!options.preview && rawPathname.startsWith("/__docs/module/")) {
526
570
  respondWorkspaceModule(req, res, resolved.projectRoot).catch((reason) => {
527
571
  if (res.writableEnded) return;
@@ -701,15 +745,17 @@ var createHistoryPlugin = (options, resolvedWorkspace) => ({
701
745
  server.middlewares.use(async (req, res, next) => {
702
746
  const requestUrl = req.url || "/";
703
747
  const url = requestUrl.replace(/\/+/g, "/").replace(/[?#].*$/s, "") || "";
748
+ const accept = String(req.headers?.accept || "text/html");
749
+ const isPreviewPage = /^\/__docs\/preview(?:-config-generate)?$/.test(url) && accept.includes("text/html");
704
750
  const candidate = node_path.resolve(root, `.${url}`);
705
751
  const isFile = node_fs.existsSync(candidate) && node_fs.statSync(candidate).isFile();
706
- if (options.preview && url.startsWith("/__docs/")) {
752
+ if (options.preview && url.startsWith("/__docs/") && !isPreviewPage) {
707
753
  res.statusCode = 404;
708
754
  res.end("Not Found");
709
755
  return;
710
756
  }
711
- if (res.writableEnded || requestUrl.includes("html-proxy&") || innerPathRegex.test(url) || isFile) return next();
712
- if (!String(req.headers?.accept || "text/html").includes("text/html")) return next();
757
+ if (res.writableEnded || requestUrl.includes("html-proxy&") || innerPathRegex.test(url) && !isPreviewPage || isFile) return next();
758
+ if (!accept.includes("text/html")) return next();
713
759
  const indexFile = resolvedWorkspace?.entry || resolveDocsWorkspace(options.preview ? root : process.cwd(), options.preview ? "." : options.workspace).entry;
714
760
  if (!node_fs.existsSync(indexFile)) {
715
761
  res.statusCode = 404;
@@ -774,6 +820,7 @@ var createStaticCopyPlugin = (options, resolvedWorkspace) => ({
774
820
  }
775
821
  });
776
822
  var plugins_default = (options = {}, resolvedWorkspace) => (0, vite.defineConfig)({ plugins: [
823
+ createOptionalDependenciesPlugin(),
777
824
  createRuntimePlugin(options, resolvedWorkspace),
778
825
  createWorkspacePlugin(options, resolvedWorkspace),
779
826
  createHistoryPlugin(options, resolvedWorkspace),
@@ -938,7 +985,8 @@ var createPreviewRequestHandler = (options) => {
938
985
  return;
939
986
  }
940
987
  const pathname = decodeRequestPath(req.url || "/");
941
- if (pathname.startsWith("/__docs/")) {
988
+ const isPreviewPage = /^\/__docs\/preview(?:-config-generate)?$/.test(pathname) && String(req.headers.accept || "").includes("text/html");
989
+ if (pathname.startsWith("/__docs/") && !isPreviewPage) {
942
990
  res.statusCode = 404;
943
991
  res.end("Not Found");
944
992
  return;
package/dist/index.js CHANGED
@@ -7,6 +7,7 @@ import vue from "@vitejs/plugin-vue";
7
7
  import vueJsx from "@vitejs/plugin-vue-jsx";
8
8
  import * as fs$1 from "node:fs";
9
9
  import { createHash } from "node:crypto";
10
+ import { createRequire } from "node:module";
10
11
  import * as http from "node:http";
11
12
  //#region packages/dever/src/workspace.ts
12
13
  /**
@@ -295,6 +296,48 @@ var respondWorkspaceModule = async (req, res, projectRoot) => {
295
296
  return true;
296
297
  };
297
298
  //#endregion
299
+ //#region packages/dever/src/plugins/optional-dependencies.ts
300
+ var resolveOptionalDependency = (id, importer) => {
301
+ if (!importer || !path.isAbsolute(importer) || /^[./]|:/.test(id)) return;
302
+ const filename = importer.split("?")[0];
303
+ const name = id.startsWith("@") ? id.split("/").slice(0, 2).join("/") : id.split("/")[0];
304
+ let directory = path.dirname(filename);
305
+ while (true) {
306
+ const manifest = path.join(directory, "package.json");
307
+ if (fs$1.existsSync(manifest)) {
308
+ const pkg = JSON.parse(fs$1.readFileSync(manifest, "utf8"));
309
+ if (!pkg.peerDependencies?.[name] || !pkg.peerDependenciesMeta?.[name]?.optional) return;
310
+ try {
311
+ createRequire(filename).resolve(id);
312
+ } catch (reason) {
313
+ if (reason.code === "MODULE_NOT_FOUND") return {
314
+ id: `https://cdn.jsdelivr.net/npm/${id}/+esm`,
315
+ external: true
316
+ };
317
+ }
318
+ return;
319
+ }
320
+ const parent = path.dirname(directory);
321
+ if (parent === directory) return;
322
+ directory = parent;
323
+ }
324
+ };
325
+ /**
326
+ * 本地包未安装的 optional peer 由浏览器按需从 CDN 加载。
327
+ * @returns 同时用于 Vite 模块转换与依赖预打包的解析插件。
328
+ */
329
+ var createOptionalDependenciesPlugin = () => {
330
+ const resolver = {
331
+ name: "docs-optional-dependencies",
332
+ resolveId: resolveOptionalDependency
333
+ };
334
+ return {
335
+ ...resolver,
336
+ enforce: "pre",
337
+ config: () => ({ optimizeDeps: { rolldownOptions: { plugins: [resolver] } } })
338
+ };
339
+ };
340
+ //#endregion
298
341
  //#region packages/dever/src/plugins/index.ts
299
342
  var innerPathRegex = /^\/(@|__)/;
300
343
  var directResourceExtensions = /* @__PURE__ */ new Set([".md", ".json"]);
@@ -493,6 +536,7 @@ var configureWorkspaceServer = (options, resolvedWorkspace) => (server) => {
493
536
  let realResourceRoot = realWorkspace;
494
537
  try {
495
538
  const rawPathname = getRawPathname(req.url || "/");
539
+ if (/^\/__docs\/preview(?:-config-generate)?$/.test(rawPathname) && String(req.headers.accept || "").includes("text/html")) return next();
496
540
  if (!options.preview && rawPathname.startsWith("/__docs/module/")) {
497
541
  respondWorkspaceModule(req, res, resolved.projectRoot).catch((reason) => {
498
542
  if (res.writableEnded) return;
@@ -672,15 +716,17 @@ var createHistoryPlugin = (options, resolvedWorkspace) => ({
672
716
  server.middlewares.use(async (req, res, next) => {
673
717
  const requestUrl = req.url || "/";
674
718
  const url = requestUrl.replace(/\/+/g, "/").replace(/[?#].*$/s, "") || "";
719
+ const accept = String(req.headers?.accept || "text/html");
720
+ const isPreviewPage = /^\/__docs\/preview(?:-config-generate)?$/.test(url) && accept.includes("text/html");
675
721
  const candidate = path.resolve(root, `.${url}`);
676
722
  const isFile = fs$1.existsSync(candidate) && fs$1.statSync(candidate).isFile();
677
- if (options.preview && url.startsWith("/__docs/")) {
723
+ if (options.preview && url.startsWith("/__docs/") && !isPreviewPage) {
678
724
  res.statusCode = 404;
679
725
  res.end("Not Found");
680
726
  return;
681
727
  }
682
- if (res.writableEnded || requestUrl.includes("html-proxy&") || innerPathRegex.test(url) || isFile) return next();
683
- if (!String(req.headers?.accept || "text/html").includes("text/html")) return next();
728
+ if (res.writableEnded || requestUrl.includes("html-proxy&") || innerPathRegex.test(url) && !isPreviewPage || isFile) return next();
729
+ if (!accept.includes("text/html")) return next();
684
730
  const indexFile = resolvedWorkspace?.entry || resolveDocsWorkspace(options.preview ? root : process.cwd(), options.preview ? "." : options.workspace).entry;
685
731
  if (!fs$1.existsSync(indexFile)) {
686
732
  res.statusCode = 404;
@@ -745,6 +791,7 @@ var createStaticCopyPlugin = (options, resolvedWorkspace) => ({
745
791
  }
746
792
  });
747
793
  var plugins_default = (options = {}, resolvedWorkspace) => defineConfig({ plugins: [
794
+ createOptionalDependenciesPlugin(),
748
795
  createRuntimePlugin(options, resolvedWorkspace),
749
796
  createWorkspacePlugin(options, resolvedWorkspace),
750
797
  createHistoryPlugin(options, resolvedWorkspace),
@@ -909,7 +956,8 @@ var createPreviewRequestHandler = (options) => {
909
956
  return;
910
957
  }
911
958
  const pathname = decodeRequestPath(req.url || "/");
912
- if (pathname.startsWith("/__docs/")) {
959
+ const isPreviewPage = /^\/__docs\/preview(?:-config-generate)?$/.test(pathname) && String(req.headers.accept || "").includes("text/html");
960
+ if (pathname.startsWith("/__docs/") && !isPreviewPage) {
913
961
  res.statusCode = 404;
914
962
  res.end("Not Found");
915
963
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deot/docs-dever",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",