clean-jsdoc-theme 5.0.0-alpha.2 → 5.0.0-alpha.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/publish.mjs CHANGED
@@ -6,7 +6,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
6
6
  });
7
7
 
8
8
  // src/publish.ts
9
- import { readFile, readdir } from "fs/promises";
9
+ import { mkdir as mkdir2, readFile, readdir, writeFile as writeFile2 } from "fs/promises";
10
10
  import { readFileSync } from "fs";
11
11
  import { createHash } from "crypto";
12
12
  import { basename, dirname as dirname2, extname, join as joinPath, resolve as resolvePath } from "path";
@@ -98,7 +98,7 @@ async function loadDep(name, requiredExports) {
98
98
  }
99
99
  return mod;
100
100
  }
101
- var loadSetu = () => loadDep("@clean-jsdoc-theme/setu", ["generateSite"]);
101
+ var loadSetu = () => loadDep("@clean-jsdoc-theme/setu", ["generateSite", "stampSite"]);
102
102
  var loadDwar = () => loadDep("@clean-jsdoc-theme/dwar", ["render", "runPagefindAgainstDir"]);
103
103
  var loadUtils = () => loadDep("@clean-jsdoc-theme/utils", [
104
104
  "validateThemeOpts",
@@ -106,7 +106,8 @@ var loadUtils = () => loadDep("@clean-jsdoc-theme/utils", [
106
106
  "formatDiagnostics",
107
107
  "formatBuildReport",
108
108
  "normalizeBasePath",
109
- "withBase"
109
+ "withBase",
110
+ "toExtractManifest"
110
111
  ]);
111
112
  var loadOra = async () => {
112
113
  try {
@@ -168,7 +169,16 @@ var defaultTheme = {
168
169
  },
169
170
  basePath: "/"
170
171
  };
171
- function resolveTheme(opts, siteName, fonts, basePath) {
172
+ function resolveFontSet(fonts, locale) {
173
+ const base = defaultTheme.tokens.fonts;
174
+ const override = locale && fonts.locales?.[locale] || {};
175
+ return {
176
+ heading: override.heading ?? fonts.heading ?? base.heading,
177
+ body: override.body ?? fonts.body ?? base.body,
178
+ mono: override.mono ?? fonts.mono ?? base.mono
179
+ };
180
+ }
181
+ function resolveTheme(opts, siteName, fonts, basePath, locale) {
172
182
  const aiPrompt = typeof opts.aiPrompt === "string" && opts.aiPrompt.trim() ? opts.aiPrompt.trim() : void 0;
173
183
  const copyPage = normalizeCopyPage(opts.copyPage);
174
184
  const pageNav = normalizePageNav(opts.pageNav);
@@ -184,11 +194,7 @@ function resolveTheme(opts, siteName, fonts, basePath) {
184
194
  ...defaultTheme.tokens,
185
195
  colors: { ...defaultTheme.tokens.colors, ...colors ?? {} },
186
196
  darkColors: { ...defaultTheme.tokens.darkColors, ...darkColors ?? {} },
187
- fonts: {
188
- heading: fonts.heading ?? defaultTheme.tokens.fonts.heading,
189
- body: fonts.body ?? defaultTheme.tokens.fonts.body,
190
- mono: fonts.mono ?? defaultTheme.tokens.fonts.mono
191
- },
197
+ fonts: resolveFontSet(fonts, locale),
192
198
  ...siteName ? { siteName } : {}
193
199
  }
194
200
  };
@@ -346,9 +352,7 @@ function sourceLinkToCommentEnabled(opts) {
346
352
  }
347
353
  function hasMarkdownPlugin(plugins) {
348
354
  if (!Array.isArray(plugins)) return false;
349
- return plugins.some(
350
- (p) => typeof p === "string" && /(^|[\\/])markdown(\.js)?$/i.test(p.trim())
351
- );
355
+ return plugins.some((p) => typeof p === "string" && /(^|[\\/])markdown(\.js)?$/i.test(p.trim()));
352
356
  }
353
357
  function assertMarkdownPlugin() {
354
358
  let conf;
@@ -597,6 +601,17 @@ async function resolveDocImages(docs, docsDir) {
597
601
  }
598
602
  return { docs: out, files, inlineSvgs };
599
603
  }
604
+ function overlayDocs(base, locale) {
605
+ const byPath = new Map(base.docs.map((d) => [d.path, d]));
606
+ for (const d of locale.docs) byPath.set(d.path, d);
607
+ const filesByPath = /* @__PURE__ */ new Map();
608
+ for (const f of [...base.files, ...locale.files]) filesByPath.set(f.path, f);
609
+ return {
610
+ docs: [...byPath.values()].sort((a, b) => a.path.localeCompare(b.path)),
611
+ files: [...filesByPath.values()],
612
+ inlineSvgs: { ...base.inlineSvgs, ...locale.inlineSvgs }
613
+ };
614
+ }
600
615
  function normalizeDocGroups(raw) {
601
616
  return normalizeSectionOrder(raw);
602
617
  }
@@ -605,22 +620,64 @@ function formatElapsed(ms) {
605
620
  }
606
621
  function createBuildProgress(ora) {
607
622
  async function stage(label, fn) {
608
- if (!ora) return await fn();
623
+ const noop = { setText: () => {
624
+ } };
625
+ if (!ora) return await fn(noop);
609
626
  const spinner = ora(label).start();
610
627
  const begin = Date.now();
628
+ let current = label;
629
+ const refresh = () => {
630
+ const secs = Math.floor((Date.now() - begin) / 1e3);
631
+ spinner.text = secs > 0 ? `${current} (${secs}s)` : current;
632
+ };
633
+ const heartbeat = setInterval(refresh, 1e3);
634
+ if (typeof heartbeat.unref === "function") heartbeat.unref();
635
+ const handle = {
636
+ setText(text) {
637
+ current = text;
638
+ refresh();
639
+ }
640
+ };
611
641
  try {
612
- const result = await fn();
642
+ const result = await fn(handle);
643
+ clearInterval(heartbeat);
613
644
  spinner.succeed(`${label} (${formatElapsed(Date.now() - begin)})`);
614
645
  return result;
615
646
  } catch (err) {
647
+ clearInterval(heartbeat);
616
648
  spinner.fail(label);
617
649
  throw err;
618
650
  }
619
651
  }
620
652
  return { stage };
621
653
  }
654
+ function readBuildSpec() {
655
+ const path = process.env.CLEAN_JSDOC_THEME_BUILD?.trim();
656
+ if (!path) return null;
657
+ const spec = JSON.parse(readFileSync(path, "utf8"));
658
+ if (typeof spec.version === "number" && spec.version !== 1) {
659
+ throw new Error(
660
+ `clean-jsdoc-theme: build spec version ${spec.version} unsupported (expected 1) \u2014 update clean-jsdoc-theme and aadesh together.`
661
+ );
662
+ }
663
+ if (typeof spec.destination !== "string" || typeof spec.basePath !== "string") {
664
+ throw new Error(`clean-jsdoc-theme: malformed build spec at "${path}".`);
665
+ }
666
+ return {
667
+ locale: typeof spec.locale === "string" ? spec.locale : "",
668
+ defaultLocale: typeof spec.defaultLocale === "string" ? spec.defaultLocale : "en",
669
+ apiMessages: spec.apiMessages ?? {},
670
+ chromeMessages: spec.chromeMessages ?? {},
671
+ destination: spec.destination,
672
+ basePath: spec.basePath,
673
+ siteBasePath: typeof spec.siteBasePath === "string" ? spec.siteBasePath : "/",
674
+ locales: Array.isArray(spec.locales) ? spec.locales : [],
675
+ ...typeof spec.docsDir === "string" && spec.docsDir ? { docsDir: spec.docsDir } : {}
676
+ };
677
+ }
622
678
  async function publish(data, opts, tutorials) {
623
- const destination = opts.destination;
679
+ const buildSpec = readBuildSpec();
680
+ const destination = buildSpec?.destination ?? opts.destination;
624
681
  if (!destination || typeof destination !== "string") {
625
682
  throw new Error(
626
683
  'clean-jsdoc-theme publish: opts.destination is required (set "opts.destination" in your jsdoc.json).'
@@ -635,7 +692,7 @@ async function publish(data, opts, tutorials) {
635
692
  const ora = opts.progress === false ? null : await loadOra();
636
693
  const progress = createBuildProgress(ora);
637
694
  const [
638
- { generateSite },
695
+ { generateSite, stampSite },
639
696
  { render, runPagefindAgainstDir },
640
697
  {
641
698
  validateThemeOpts,
@@ -643,13 +700,23 @@ async function publish(data, opts, tutorials) {
643
700
  formatDiagnostics,
644
701
  formatBuildReport,
645
702
  normalizeBasePath,
646
- withBase
703
+ withBase,
704
+ toExtractManifest
647
705
  }
648
- ] = await progress.stage(
649
- "Loading renderer",
650
- () => Promise.all([loadSetu(), loadDwar(), loadUtils()])
651
- );
652
- const basePath = normalizeBasePath(opts.basePath);
706
+ // Loaded sequentially (not Promise.all) so the spinner can step its label
707
+ // through each module — the evaluation of these large ESM bundles is the
708
+ // single longest, most event-loop-blocking stage, so visibly naming the
709
+ // module in flight is what stops it from reading as a hang.
710
+ ] = await progress.stage("Loading renderer", async ({ setText }) => {
711
+ setText("Loading renderer (setu)");
712
+ const setu = await loadSetu();
713
+ setText("Loading renderer (dwar)");
714
+ const dwar = await loadDwar();
715
+ setText("Loading renderer (utils)");
716
+ const utils = await loadUtils();
717
+ return [setu, dwar, utils];
718
+ });
719
+ const basePath = normalizeBasePath(buildSpec?.basePath ?? opts.basePath);
653
720
  const hrefForServed = (servedPath) => withBase(basePath, "/" + servedPath);
654
721
  const fontResolver = createGoogleFontResolver();
655
722
  const { value, diagnostics } = await progress.stage(
@@ -680,12 +747,23 @@ async function publish(data, opts, tutorials) {
680
747
  "Reading sources & docs",
681
748
  async () => {
682
749
  const [srcs, rawDocs] = await Promise.all([sourcesPromise, docsPromise]);
683
- const resolved = docsDir ? await resolveDocImages(rawDocs, docsDir) : { docs: rawDocs, files: [], inlineSvgs: {} };
750
+ const base = docsDir ? await resolveDocImages(rawDocs, docsDir) : { docs: rawDocs, files: [], inlineSvgs: {} };
751
+ if (!buildSpec?.docsDir) {
752
+ return {
753
+ sources: srcs,
754
+ docs: base.docs,
755
+ docImageFiles: base.files,
756
+ inlineSvgs: base.inlineSvgs
757
+ };
758
+ }
759
+ const localeRaw = await collectDocs(buildSpec.docsDir);
760
+ const locale = await resolveDocImages(localeRaw, buildSpec.docsDir);
761
+ const merged = overlayDocs(base, locale);
684
762
  return {
685
763
  sources: srcs,
686
- docs: resolved.docs,
687
- docImageFiles: resolved.files,
688
- inlineSvgs: resolved.inlineSvgs
764
+ docs: merged.docs,
765
+ docImageFiles: merged.files,
766
+ inlineSvgs: merged.inlineSvgs
689
767
  };
690
768
  }
691
769
  );
@@ -694,7 +772,7 @@ async function publish(data, opts, tutorials) {
694
772
  const sectionOrder = normalizeSectionOrder(opts.sectionOrder);
695
773
  const menu = normalizeMenu(opts.menu);
696
774
  const clubSidebarItems = opts.clubSidebarItems === true;
697
- const manifest = await progress.stage("Generating pages", () => generateSite(data, {
775
+ const siteOptions = {
698
776
  ...pkg ? { pkg } : {},
699
777
  ...readme ? { readme } : {},
700
778
  ...tutorialTree.length > 0 ? { tutorials: tutorialTree } : {},
@@ -706,7 +784,23 @@ async function publish(data, opts, tutorials) {
706
784
  ...sectionOrder ? { sectionOrder } : {},
707
785
  ...menu ? { menu } : {},
708
786
  ...clubSidebarItems ? { clubSidebarItems } : {}
709
- }));
787
+ };
788
+ const manifest = await progress.stage(
789
+ "Generating pages",
790
+ () => buildSpec ? stampSite(data, buildSpec.apiMessages, siteOptions) : generateSite(data, siteOptions)
791
+ );
792
+ const extractPath = process.env.CLEAN_JSDOC_THEME_EXTRACT?.trim();
793
+ if (extractPath) {
794
+ const target = resolvePath(extractPath);
795
+ await progress.stage("Writing extract manifest", async () => {
796
+ await mkdir2(dirname2(target), { recursive: true });
797
+ await writeFile2(target, JSON.stringify(toExtractManifest(manifest), null, 2) + "\n", "utf8");
798
+ });
799
+ console.log(
800
+ `clean-jsdoc-theme: extract mode \u2014 wrote ${manifest.slots?.length ?? 0} slot(s) to ${target}`
801
+ );
802
+ return;
803
+ }
710
804
  const { siteName, files: logoFiles } = await prepareSiteName(value.siteName, hrefForServed);
711
805
  const notGoogle = new Set(
712
806
  diagnostics.list.filter((d) => d.code === "fonts/not-google" && d.path).map((d) => d.path)
@@ -714,23 +808,44 @@ async function publish(data, opts, tutorials) {
714
808
  const fonts = { ...value.fonts };
715
809
  if (notGoogle.has("fonts.heading")) delete fonts.heading;
716
810
  if (notGoogle.has("fonts.body")) delete fonts.body;
811
+ if (fonts.locales) {
812
+ const cleaned = {};
813
+ for (const [loc, set] of Object.entries(fonts.locales)) {
814
+ const s = { ...set };
815
+ if (notGoogle.has(`fonts.${loc}:heading`)) delete s.heading;
816
+ if (notGoogle.has(`fonts.${loc}:body`)) delete s.body;
817
+ cleaned[loc] = s;
818
+ }
819
+ fonts.locales = cleaned;
820
+ }
717
821
  const customAssets = await resolveCustomAssets(opts, hrefForServed);
718
822
  const absoluteDestination = resolvePath(destination);
719
823
  const islandCacheDir = resolvePath(process.cwd(), "node_modules", ".cache", "clean-jsdoc-theme");
720
824
  const result = await progress.stage(
721
825
  "Rendering site",
722
826
  () => render(manifest, {
723
- theme: { ...resolveTheme(opts, siteName, fonts, basePath), ...customAssets.theme },
827
+ theme: {
828
+ ...resolveTheme(opts, siteName, fonts, basePath, buildSpec?.locale),
829
+ ...customAssets.theme
830
+ },
724
831
  destination: absoluteDestination,
725
832
  islandCacheDir,
726
- inlineSvgs
833
+ inlineSvgs,
834
+ // Build mode: render chrome in the locale (SSR provider + island seeding)
835
+ // and, with >1 locale, mount the language switcher.
836
+ ...buildSpec ? {
837
+ locale: {
838
+ code: buildSpec.locale,
839
+ defaultLocale: buildSpec.defaultLocale,
840
+ messages: buildSpec.chromeMessages,
841
+ siteBasePath: buildSpec.siteBasePath,
842
+ locales: buildSpec.locales.map((l) => ({ code: l.code, label: l.name ?? l.code }))
843
+ }
844
+ } : {}
727
845
  })
728
846
  );
729
847
  const outputFiles = [...result.files, ...logoFiles, ...customAssets.files, ...docImageFiles];
730
- await progress.stage(
731
- "Writing files",
732
- () => writeOutputFiles(absoluteDestination, outputFiles)
733
- );
848
+ await progress.stage("Writing files", () => writeOutputFiles(absoluteDestination, outputFiles));
734
849
  const zlib = await import("zlib");
735
850
  const gzipSizer = (b) => zlib.gzipSync(b).length;
736
851
  console.log(
@@ -756,9 +871,7 @@ async function publish(data, opts, tutorials) {
756
871
  () => runPagefindAgainstDir(absoluteDestination)
757
872
  );
758
873
  } catch (err) {
759
- console.warn(
760
- `clean-jsdoc-theme: pagefind step skipped (optional) \u2014 ${err.message}`
761
- );
874
+ console.warn(`clean-jsdoc-theme: pagefind step skipped (optional) \u2014 ${err.message}`);
762
875
  }
763
876
  }
764
877
  export {
@@ -772,6 +885,7 @@ export {
772
885
  normalizePageNav,
773
886
  normalizeSectionOrder,
774
887
  outputSourceFilesEnabled,
888
+ overlayDocs,
775
889
  publish,
776
890
  resolveDocImages,
777
891
  sourceLinkToCommentEnabled