@mercurjs/dashboard-sdk 2.0.0-canary.7 → 2.0.0-canary.71

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
@@ -30,13 +30,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
- dashboardPlugin: () => dashboardPlugin,
34
- defineConfig: () => defineConfig
33
+ mercurDashboardPlugin: () => mercurDashboardPlugin
35
34
  });
36
35
  module.exports = __toCommonJS(index_exports);
37
36
 
38
37
  // src/plugin.ts
39
38
  var import_path5 = __toESM(require("path"), 1);
39
+ var import_fs4 = __toESM(require("fs"), 1);
40
40
 
41
41
  // src/utils.ts
42
42
  function resolveExports(moduleExports) {
@@ -73,7 +73,6 @@ function normalizePath(filePath) {
73
73
 
74
74
  // src/constants.ts
75
75
  var VALID_FILE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js"];
76
- var CONFIG_NAME = "mercur.config.ts";
77
76
  var CONFIG_VIRTUAL_MODULE = "virtual:mercur/config";
78
77
  var ROUTES_VIRTUAL_MODULE = "virtual:mercur/routes";
79
78
  var COMPONENTS_VIRTUAL_MODULE = "virtual:mercur/components";
@@ -258,21 +257,26 @@ function buildRouteTree(results) {
258
257
  }
259
258
  return Array.from(routeMap.values());
260
259
  }
261
- function generateRoutes({ srcDir }) {
260
+ function generateRoutes({ srcDir, pluginDirs }) {
262
261
  const pagesDir = import_path.default.join(srcDir, "pages");
263
- const files = crawlPages(pagesDir);
264
- if (files.length === 0) {
265
- return `export const customRoutes = []`;
266
- }
267
262
  let index = 0;
268
263
  const results = [];
269
- for (const file of files) {
264
+ for (const file of crawlPages(pagesDir)) {
270
265
  const result = parseFile(file, pagesDir, index);
271
266
  if (result) {
272
267
  results.push(result);
273
268
  index++;
274
269
  }
275
270
  }
271
+ for (const pluginDir of pluginDirs) {
272
+ for (const file of crawlPages(pluginDir)) {
273
+ const result = parseFile(file, pluginDir, index);
274
+ if (result) {
275
+ results.push(result);
276
+ index++;
277
+ }
278
+ }
279
+ }
276
280
  if (results.length === 0) {
277
281
  return `export const customRoutes = []`;
278
282
  }
@@ -390,21 +394,26 @@ function parseFile2(file, pagesDir, index) {
390
394
  menuItem: generateMenuItem(config, file, pagesDir, index)
391
395
  };
392
396
  }
393
- function generateMenuItems({ srcDir }) {
397
+ function generateMenuItems({ srcDir, pluginDirs }) {
394
398
  const pagesDir = import_path2.default.join(srcDir, "pages");
395
- const files = crawlPages2(pagesDir);
396
- if (files.length === 0) {
397
- return `export default { menuItems: [] }`;
398
- }
399
399
  let index = 0;
400
400
  const results = [];
401
- for (const file of files) {
401
+ for (const file of crawlPages2(pagesDir)) {
402
402
  const result = parseFile2(file, pagesDir, index);
403
403
  if (result) {
404
404
  results.push(result);
405
405
  index++;
406
406
  }
407
407
  }
408
+ for (const pluginDir of pluginDirs) {
409
+ for (const file of crawlPages2(pluginDir)) {
410
+ const result = parseFile2(file, pluginDir, index);
411
+ if (result) {
412
+ results.push(result);
413
+ index++;
414
+ }
415
+ }
416
+ }
408
417
  if (results.length === 0) {
409
418
  return `export default { menuItems: [] }`;
410
419
  }
@@ -506,47 +515,111 @@ function loadI18nModule(mercurConfig) {
506
515
  }
507
516
 
508
517
  // src/plugin.ts
509
- function buildConfig(config, root) {
510
- const srcDir = import_path5.default.join(root, "src");
511
- return {
512
- ...config,
513
- backendUrl: config.backendUrl ?? "http://localhost:9000",
514
- root,
515
- srcDir,
516
- configPath: import_path5.default.resolve(root, CONFIG_NAME)
517
- };
518
+ function isPageFile(file) {
519
+ const basename = import_path5.default.basename(file, import_path5.default.extname(file));
520
+ return basename === "page";
521
+ }
522
+ var UI_MODULE_KEYS = [
523
+ "admin_ui",
524
+ "vendor_ui"
525
+ ];
526
+ function findNodeModulesRoot(configDir) {
527
+ let dir = configDir;
528
+ while (dir !== import_path5.default.dirname(dir)) {
529
+ const candidate = import_path5.default.join(dir, "node_modules");
530
+ if (import_fs4.default.existsSync(candidate) && import_fs4.default.statSync(candidate).isDirectory()) {
531
+ return candidate;
532
+ }
533
+ dir = import_path5.default.dirname(dir);
534
+ }
535
+ return import_path5.default.join(configDir, "node_modules");
518
536
  }
519
- async function loadMercurConfig(root) {
520
- const configPath = import_path5.default.resolve(root, CONFIG_NAME);
537
+ function resolvePluginRoot(resolve, configDir, nodeModulesRoot) {
521
538
  try {
522
- const mod = await getFileExports(configPath);
523
- const content = mod.default ?? mod;
524
- return buildConfig(content, root);
525
- } catch (error) {
526
- console.error(error);
527
- throw new Error(
528
- `[@mercurjs/dashboard-sdk] Could not find or load ${CONFIG_NAME} in ${root}`
529
- );
539
+ if (resolve.startsWith(".")) {
540
+ const resolved = import_path5.default.resolve(configDir, resolve);
541
+ if (import_fs4.default.existsSync(resolved)) {
542
+ return import_fs4.default.realpathSync(resolved);
543
+ }
544
+ return null;
545
+ }
546
+ const packagePath = import_path5.default.join(nodeModulesRoot, resolve);
547
+ if (!import_fs4.default.existsSync(packagePath)) {
548
+ return null;
549
+ }
550
+ return import_fs4.default.realpathSync(packagePath);
551
+ } catch {
552
+ return null;
530
553
  }
531
554
  }
532
- function isPageFile(file) {
533
- const basename = import_path5.default.basename(file, import_path5.default.extname(file));
534
- return basename === "page";
555
+ function resolvePluginDirs(plugins, configDir) {
556
+ const nodeModulesRoot = findNodeModulesRoot(configDir);
557
+ const dirs = [];
558
+ const appTypes = ["admin", "vendor"];
559
+ for (const plugin of plugins) {
560
+ const resolve = typeof plugin === "string" ? plugin : plugin?.resolve;
561
+ if (!resolve || typeof resolve !== "string") continue;
562
+ const pluginRoot = resolvePluginRoot(resolve, configDir, nodeModulesRoot);
563
+ if (!pluginRoot) continue;
564
+ for (const appType of appTypes) {
565
+ const extDir = import_path5.default.join(pluginRoot, ".medusa/server/src", appType);
566
+ if (import_fs4.default.existsSync(extDir) && import_fs4.default.statSync(extDir).isDirectory()) {
567
+ dirs.push(extDir);
568
+ }
569
+ }
570
+ }
571
+ return dirs;
535
572
  }
536
- function dashboardPlugin() {
573
+ async function loadMedusaConfig(medusaConfigPath, root) {
574
+ try {
575
+ const mod = await getFileExports(medusaConfigPath);
576
+ const medusaConfig = mod.default ?? mod;
577
+ const modules = medusaConfig?.modules ?? {};
578
+ const configDir = import_path5.default.dirname(medusaConfigPath);
579
+ let base;
580
+ for (const key of UI_MODULE_KEYS) {
581
+ const value = modules[key];
582
+ if (!value || typeof value !== "object" || !value.options?.appDir) continue;
583
+ const appDir = import_path5.default.resolve(configDir, value.options.appDir);
584
+ if (appDir === root) {
585
+ base = value.options.path;
586
+ break;
587
+ }
588
+ }
589
+ const plugins = medusaConfig?.plugins ?? [];
590
+ const pluginDirs = resolvePluginDirs(plugins, configDir);
591
+ return { base, pluginDirs };
592
+ } catch {
593
+ return { pluginDirs: [] };
594
+ }
595
+ }
596
+ function mercurDashboardPlugin(pluginConfig) {
537
597
  let root;
538
598
  let config;
539
599
  return {
540
600
  name: "@mercurjs/dashboard-sdk",
541
601
  async config(viteConfig) {
542
602
  root = viteConfig.root || process.cwd();
543
- config = await loadMercurConfig(root);
603
+ const medusaConfigPath = import_path5.default.resolve(root, pluginConfig.medusaConfigPath);
604
+ const { base, pluginDirs } = await loadMedusaConfig(medusaConfigPath, root);
605
+ const srcDir = import_path5.default.join(root, "src");
606
+ const backendUrl = pluginConfig.backendUrl ?? "http://localhost:9000";
607
+ config = {
608
+ ...pluginConfig,
609
+ backendUrl,
610
+ base,
611
+ root,
612
+ srcDir,
613
+ pluginDirs
614
+ };
544
615
  return {
616
+ base: config.base,
545
617
  define: {
546
- "__BACKEND_URL__": JSON.stringify(config.backendUrl)
618
+ "__BACKEND_URL__": JSON.stringify(config.backendUrl),
619
+ "__BASE__": JSON.stringify(config.base || "/")
547
620
  },
548
621
  optimizeDeps: {
549
- exclude: ["@mercurjs/vendor", "virtual:mercur/config"]
622
+ exclude: ["virtual:mercur/config", "virtual:mercur/routes", "virtual:mercur/components", "virtual:mercur/menu-items", "virtual:mercur/i18n"]
550
623
  }
551
624
  };
552
625
  },
@@ -575,11 +648,6 @@ function dashboardPlugin() {
575
648
  server.watcher.on("unlink", handlePageChange);
576
649
  },
577
650
  handleHotUpdate({ file, server }) {
578
- const configPath = import_path5.default.resolve(root, CONFIG_NAME);
579
- if (file === configPath) {
580
- server.restart();
581
- return;
582
- }
583
651
  if (isPageFile(file)) {
584
652
  const mod = server.moduleGraph.getModuleById(RESOLVED_ROUTES_MODULE);
585
653
  if (mod) {
@@ -589,13 +657,7 @@ function dashboardPlugin() {
589
657
  }
590
658
  };
591
659
  }
592
-
593
- // src/define-config.ts
594
- function defineConfig(config) {
595
- return config;
596
- }
597
660
  // Annotate the CommonJS export names for ESM import in node:
598
661
  0 && (module.exports = {
599
- dashboardPlugin,
600
- defineConfig
662
+ mercurDashboardPlugin
601
663
  });
package/dist/index.d.cts CHANGED
@@ -1,9 +1,9 @@
1
1
  import * as Vite from 'vite';
2
2
  import { ComponentType } from 'react';
3
3
 
4
- declare function dashboardPlugin(): Vite.Plugin;
5
-
6
4
  interface MercurConfig {
5
+ medusaConfigPath: string;
6
+ backendUrl?: string;
7
7
  name?: string;
8
8
  logo?: string;
9
9
  components?: {
@@ -14,13 +14,14 @@ interface MercurConfig {
14
14
  i18n?: {
15
15
  defaultLanguage: string;
16
16
  };
17
- backendUrl?: string;
17
+ enableSellerRegistration?: boolean;
18
18
  }
19
19
  interface BuiltMercurConfig extends MercurConfig {
20
20
  backendUrl: string;
21
+ base?: string;
21
22
  root: string;
22
23
  srcDir: string;
23
- configPath: string;
24
+ pluginDirs: string[];
24
25
  }
25
26
  type RouteConfig = {
26
27
  label: string;
@@ -31,6 +32,6 @@ type RouteConfig = {
31
32
  public?: boolean;
32
33
  };
33
34
 
34
- declare function defineConfig(config: MercurConfig): MercurConfig;
35
+ declare function mercurDashboardPlugin(pluginConfig: MercurConfig): Vite.Plugin;
35
36
 
36
- export { type BuiltMercurConfig, type MercurConfig, type RouteConfig, dashboardPlugin, defineConfig };
37
+ export { type BuiltMercurConfig, type MercurConfig, type RouteConfig, mercurDashboardPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mercurjs/dashboard-sdk",
3
- "version": "2.0.0-canary.7",
3
+ "version": "2.0.0-canary.71",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/mercurjs/mercur",