@vizejs/vite-plugin-musea 0.290.0 → 0.302.0

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.mjs CHANGED
@@ -7,7 +7,8 @@ import fs from "node:fs";
7
7
  import path from "node:path";
8
8
  import { fileURLToPath } from "node:url";
9
9
  import crypto from "node:crypto";
10
- import { VIZE_CONFIG_FILE_ENV, loadConfig, vizeConfigStore } from "@vizejs/vite-plugin";
10
+ import { VIZE_CONFIG_FILE_ENV, loadConfig } from "@vizejs/vite-plugin";
11
+ import { getResolvedVizeConfigRegistration } from "@vizejs/vite-plugin/internal/config-bridge";
11
12
  import * as vite from "vite";
12
13
  //#region src/with-defaults.ts
13
14
  /**
@@ -3491,13 +3492,82 @@ function publicBasePathFromViteBase(viteBase, basePath) {
3491
3492
  return viteBase && viteBase !== "/" && viteBase !== "./" ? joinUrlPath(viteBase, basePath) : basePath;
3492
3493
  }
3493
3494
  //#endregion
3495
+ //#region src/static-gallery-shell.ts
3496
+ const moduleDir = path.dirname(fileURLToPath(import.meta.url));
3497
+ function joinFileName(...parts) {
3498
+ return parts.filter(Boolean).join("/");
3499
+ }
3500
+ async function emitGalleryShell(emitFile, staticRoot, ctx, payload, galleryDistDir = resolveGalleryDistDir()) {
3501
+ if (!galleryDistDir) {
3502
+ const html = injectStaticGlobals(generateStaticFallbackGalleryHtml(ctx.basePath), ctx, payload);
3503
+ emitFile({
3504
+ type: "asset",
3505
+ fileName: joinFileName(staticRoot, "index.html"),
3506
+ source: html
3507
+ });
3508
+ return;
3509
+ }
3510
+ for (const filePath of await collectFiles(galleryDistDir)) {
3511
+ const relative = path.relative(galleryDistDir, filePath).split(path.sep).join("/");
3512
+ const target = joinFileName(staticRoot, relative);
3513
+ const content = await fs.promises.readFile(filePath);
3514
+ if (relative === "index.html") emitFile({
3515
+ type: "asset",
3516
+ fileName: target,
3517
+ source: injectStaticGlobals(rewriteGalleryBase(content.toString("utf-8"), ctx.basePath), ctx, payload)
3518
+ });
3519
+ else emitFile({
3520
+ type: "asset",
3521
+ fileName: target,
3522
+ source: rewriteGalleryTextAssetBase(content, relative, ctx.basePath)
3523
+ });
3524
+ }
3525
+ }
3526
+ function injectStaticGlobals(html, ctx, payload) {
3527
+ const script = `<script>${generateGalleryGlobalsScript({
3528
+ basePath: ctx.basePath,
3529
+ staticPreviews: payload.previews,
3530
+ themeConfig: ctx.themeConfig,
3531
+ tokenPreviewConfig: ctx.tokenPreviewConfig
3532
+ })}<\/script>`;
3533
+ return html.includes("</head>") ? html.replace("</head>", `${script}</head>`) : `${script}${html}`;
3534
+ }
3535
+ function generateStaticFallbackGalleryHtml(basePath) {
3536
+ return `<!DOCTYPE html>
3537
+ <html lang="en">
3538
+ <head>
3539
+ <meta charset="UTF-8">
3540
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
3541
+ <title>Musea - Component Gallery</title>
3542
+ <style>html,body{min-height:100%;margin:0}body{font-family:system-ui,sans-serif}</style>
3543
+ </head>
3544
+ <body>${generateGalleryBody(basePath)}
3545
+
3546
+ <script type="module">${generateGalleryScript(basePath)}
3547
+ <\/script>
3548
+ </body>
3549
+ </html>`;
3550
+ }
3551
+ function resolveGalleryDistDir() {
3552
+ return [path.join(moduleDir, "gallery"), path.resolve(moduleDir, "../dist/gallery")].find((candidate) => fs.existsSync(path.join(candidate, "index.html"))) ?? null;
3553
+ }
3554
+ async function collectFiles(dir) {
3555
+ const entries = await fs.promises.readdir(dir, { withFileTypes: true });
3556
+ const files = [];
3557
+ for (const entry of entries) {
3558
+ const filePath = path.join(dir, entry.name);
3559
+ if (entry.isDirectory()) files.push(...await collectFiles(filePath));
3560
+ else files.push(filePath);
3561
+ }
3562
+ return files;
3563
+ }
3564
+ //#endregion
3494
3565
  //#region src/static-export.ts
3495
3566
  const MUSEA_STATIC_BUILD_ENV = "VIZE_MUSEA_STATIC_BUILD";
3496
3567
  const VIRTUAL_STATIC_RUNTIME = "virtual:musea-static-runtime";
3497
3568
  const RESOLVED_STATIC_RUNTIME = "\0musea-static-runtime";
3498
3569
  const STATIC_RUNTIME_INPUT_NAME = "musea-static-runtime";
3499
3570
  const STATIC_USER_INPUT_NAME = "musea-static-entry";
3500
- const moduleDir = path.dirname(fileURLToPath(import.meta.url));
3501
3571
  function isMuseaStaticBuild() {
3502
3572
  return process.env[MUSEA_STATIC_BUILD_ENV] === "1";
3503
3573
  }
@@ -3578,49 +3648,6 @@ function findRuntimeFileName(bundle) {
3578
3648
  for (const item of Object.values(bundle)) if (item.type === "chunk" && item.name === "musea-static-runtime") return item.fileName;
3579
3649
  return null;
3580
3650
  }
3581
- async function emitGalleryShell(emitFile, staticRoot, ctx, payload) {
3582
- const galleryDir = resolveGalleryDistDir();
3583
- if (!galleryDir) {
3584
- const html = injectStaticGlobals(generateStaticFallbackGalleryHtml(ctx.basePath), ctx, payload);
3585
- emitFile({
3586
- type: "asset",
3587
- fileName: joinFileName(staticRoot, "index.html"),
3588
- source: html
3589
- });
3590
- return;
3591
- }
3592
- for (const filePath of await collectFiles(galleryDir)) {
3593
- const relative = path.relative(galleryDir, filePath).split(path.sep).join("/");
3594
- const target = joinFileName(staticRoot, relative);
3595
- const content = await fs.promises.readFile(filePath);
3596
- if (relative === "index.html") emitFile({
3597
- type: "asset",
3598
- fileName: target,
3599
- source: rewriteGalleryBase(injectStaticGlobals(content.toString("utf-8"), ctx, payload), ctx.basePath)
3600
- });
3601
- else emitFile({
3602
- type: "asset",
3603
- fileName: target,
3604
- source: rewriteGalleryTextAssetBase(content, relative, ctx.basePath)
3605
- });
3606
- }
3607
- }
3608
- function generateStaticFallbackGalleryHtml(basePath) {
3609
- return `<!DOCTYPE html>
3610
- <html lang="en">
3611
- <head>
3612
- <meta charset="UTF-8">
3613
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
3614
- <title>Musea - Component Gallery</title>
3615
- <style>html,body{min-height:100%;margin:0}body{font-family:system-ui,sans-serif}</style>
3616
- </head>
3617
- <body>${generateGalleryBody(basePath)}
3618
-
3619
- <script type="module">${generateGalleryScript(basePath)}
3620
- <\/script>
3621
- </body>
3622
- </html>`;
3623
- }
3624
3651
  function emitPreviewHtml(emitFile, staticRoot, runtimeFileName, basePath, artFiles) {
3625
3652
  const previewDir = joinFileName(staticRoot, "preview");
3626
3653
  const runtimeUrl = relativeUrl(previewDir, runtimeFileName);
@@ -3664,15 +3691,6 @@ function generateStaticPreviewHtml(art, variantName, previewId, basePath, runtim
3664
3691
  </body>
3665
3692
  </html>`;
3666
3693
  }
3667
- function injectStaticGlobals(html, ctx, payload) {
3668
- const script = `<script>${generateGalleryGlobalsScript({
3669
- basePath: ctx.basePath,
3670
- staticPreviews: payload.previews,
3671
- themeConfig: ctx.themeConfig,
3672
- tokenPreviewConfig: ctx.tokenPreviewConfig
3673
- })}<\/script>`;
3674
- return html.includes("</head>") ? html.replace("</head>", `${script}</head>`) : `${script}${html}`;
3675
- }
3676
3694
  function emitRootRedirect(emitFile, staticRoot, basePath) {
3677
3695
  if (!staticRoot) return;
3678
3696
  const target = joinUrlPath(basePath, "");
@@ -3693,38 +3711,26 @@ async function emitAxeVendor(emitFile, staticRoot) {
3693
3711
  });
3694
3712
  } catch {}
3695
3713
  }
3696
- function resolveGalleryDistDir() {
3697
- return [path.join(moduleDir, "gallery"), path.resolve(moduleDir, "../dist/gallery")].find((candidate) => fs.existsSync(path.join(candidate, "index.html"))) ?? null;
3698
- }
3699
- async function collectFiles(dir) {
3700
- const entries = await fs.promises.readdir(dir, { withFileTypes: true });
3701
- const files = [];
3702
- for (const entry of entries) {
3703
- const filePath = path.join(dir, entry.name);
3704
- if (entry.isDirectory()) files.push(...await collectFiles(filePath));
3705
- else files.push(filePath);
3706
- }
3707
- return files;
3708
- }
3709
3714
  function staticRootFromBasePath(basePath) {
3710
3715
  return basePath.replace(/^\/+|\/+$/g, "");
3711
3716
  }
3712
- function joinFileName(...parts) {
3713
- return parts.filter(Boolean).join("/");
3714
- }
3715
3717
  function relativeUrl(fromDir, toFile) {
3716
3718
  const relative = path.posix.relative(fromDir || ".", toFile);
3717
3719
  return relative.startsWith(".") ? relative : `./${relative}`;
3718
3720
  }
3719
3721
  //#endregion
3720
3722
  //#region src/plugin/config.ts
3721
- async function resolveMuseaSharedConfig(resolvedConfig) {
3722
- const sharedConfig = vizeConfigStore.get(resolvedConfig.root);
3723
- if (sharedConfig) return sharedConfig;
3723
+ async function resolveMuseaSharedConfig(resolvedConfig, loadConfigFile = loadConfig) {
3724
+ let registration = getResolvedVizeConfigRegistration(resolvedConfig);
3725
+ if (!registration.registered) {
3726
+ await Promise.resolve();
3727
+ registration = getResolvedVizeConfigRegistration(resolvedConfig);
3728
+ }
3729
+ if (registration.registered) return await registration.config;
3724
3730
  const configFile = process.env[VIZE_CONFIG_FILE_ENV];
3725
3731
  if (!configFile) return null;
3726
3732
  try {
3727
- return await loadConfig(resolvedConfig.root, {
3733
+ return await loadConfigFile(resolvedConfig.root, {
3728
3734
  configFile,
3729
3735
  env: {
3730
3736
  mode: resolvedConfig.mode,