@mercurjs/dashboard-sdk 2.0.0-canary.74 → 2.0.0-canary.75

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 (3) hide show
  1. package/dist/index.cjs +51 -112
  2. package/dist/index.js +58 -112
  3. package/package.json +3 -2
package/dist/index.cjs CHANGED
@@ -63,6 +63,34 @@ function getParserOptions(file) {
63
63
  }
64
64
  return options;
65
65
  }
66
+ function resolveExports(moduleExports) {
67
+ if ("default" in moduleExports && moduleExports.default && "default" in moduleExports.default) {
68
+ return resolveExports(moduleExports.default);
69
+ }
70
+ return moduleExports;
71
+ }
72
+ async function getFileExports(path6) {
73
+ const { unregister } = await safeRegister();
74
+ const module2 = require(path6);
75
+ unregister();
76
+ return resolveExports(module2);
77
+ }
78
+ var safeRegister = async () => {
79
+ const { register } = await import("esbuild-register/dist/node");
80
+ let res;
81
+ try {
82
+ res = register({
83
+ format: "cjs",
84
+ loader: "ts"
85
+ });
86
+ } catch {
87
+ res = {
88
+ unregister: () => {
89
+ }
90
+ };
91
+ }
92
+ return res;
93
+ };
66
94
  function hasDefaultExport(ast) {
67
95
  let found = false;
68
96
  traverse(ast, {
@@ -309,11 +337,8 @@ function generateRoutes({ srcDir, pluginExtensions }) {
309
337
  index++;
310
338
  }
311
339
  }
312
- const pluginImports = pluginExtensions.map(
313
- (ext, i) => `import * as __pluginRaw${i} from "${normalizePath(ext)}"`
314
- );
315
- const pluginUnwraps = pluginExtensions.map(
316
- (_, i) => `const __plugin${i} = __pluginRaw${i}.default ?? __pluginRaw${i}`
340
+ const pluginDeclarations = pluginExtensions.map(
341
+ (ext, i) => `const __plugin${i} = (await import("${normalizePath(ext)}")).default`
317
342
  );
318
343
  const pluginSpreads = pluginExtensions.map(
319
344
  (_, i) => ` ...(__plugin${i}.routeModule?.routes ?? [])`
@@ -321,13 +346,15 @@ function generateRoutes({ srcDir, pluginExtensions }) {
321
346
  const routeTree = buildRouteTree(results);
322
347
  const appImports = routeTree.flatMap((r) => r.imports);
323
348
  const appRoutes = routeTree.map((r) => formatRoute(r.route));
324
- const allImports = [...appImports, ...pluginImports, ...pluginUnwraps];
349
+ const allImports = [...appImports];
325
350
  const allRoutes = [...appRoutes, ...pluginSpreads];
326
- if (allImports.length === 0 && allRoutes.length === 0) {
351
+ if (allImports.length === 0 && pluginDeclarations.length === 0 && allRoutes.length === 0) {
327
352
  return `export const customRoutes = []`;
328
353
  }
329
354
  return `${allImports.join("\n")}
330
355
 
356
+ ${pluginDeclarations.join("\n")}
357
+
331
358
  export const customRoutes = [
332
359
  ${allRoutes.join(",\n")}
333
360
  ]`;
@@ -506,24 +533,23 @@ function generateMenuItems({ srcDir, pluginExtensions }) {
506
533
  index++;
507
534
  }
508
535
  }
509
- const pluginImports = pluginExtensions.map(
510
- (ext, i) => `import * as __pluginRaw${i} from "${normalizePath(ext)}"`
511
- );
512
- const pluginUnwraps = pluginExtensions.map(
513
- (_, i) => `const __plugin${i} = __pluginRaw${i}.default ?? __pluginRaw${i}`
536
+ const pluginDeclarations = pluginExtensions.map(
537
+ (ext, i) => `const __plugin${i} = (await import("${normalizePath(ext)}")).default`
514
538
  );
515
539
  const pluginSpreads = pluginExtensions.map(
516
540
  (_, i) => ` ...(__plugin${i}.menuItemModule?.menuItems ?? [])`
517
541
  );
518
542
  const appImports = results.map((r) => r.import);
519
543
  const appMenuItems = results.map((r) => formatMenuItem(r.menuItem));
520
- const allImports = [...appImports, ...pluginImports, ...pluginUnwraps];
544
+ const allImports = [...appImports];
521
545
  const allMenuItems = [...appMenuItems, ...pluginSpreads];
522
- if (allImports.length === 0 && allMenuItems.length === 0) {
546
+ if (allImports.length === 0 && pluginDeclarations.length === 0 && allMenuItems.length === 0) {
523
547
  return `export default { menuItems: [] }`;
524
548
  }
525
549
  return `${allImports.join("\n")}
526
550
 
551
+ ${pluginDeclarations.join("\n")}
552
+
527
553
  export default {
528
554
  menuItems: [
529
555
  ${allMenuItems.join(",\n")}
@@ -673,109 +699,23 @@ function resolvePluginExtensions(plugins, configDir) {
673
699
  }
674
700
  return extensions;
675
701
  }
676
- function extractStringFromNode(node, configDir) {
677
- if ((0, import_types.isStringLiteral)(node)) {
678
- return node.value;
679
- }
680
- if ((0, import_types.isCallExpression)(node) && (0, import_types.isMemberExpression)(node.callee) && (0, import_types.isIdentifier)(node.callee.object, { name: "path" }) && (0, import_types.isIdentifier)(node.callee.property) && (node.callee.property.name === "join" || node.callee.property.name === "resolve")) {
681
- const args = node.arguments;
682
- if (args.length >= 2 && (0, import_types.isIdentifier)(args[0], { name: "__dirname" })) {
683
- const parts = args.slice(1).filter((a) => (0, import_types.isStringLiteral)(a)).map((a) => a.value);
684
- if (parts.length > 0) {
685
- return import_path5.default.resolve(configDir, ...parts);
686
- }
687
- }
688
- }
689
- return null;
690
- }
691
- function extractObjectProperties(node) {
692
- if ((0, import_types.isObjectExpression)(node)) {
693
- return node.properties;
694
- }
695
- return null;
696
- }
697
702
  async function loadMedusaConfig(medusaConfigPath, root) {
698
703
  try {
699
- const code = import_fs4.default.readFileSync(medusaConfigPath, "utf-8");
700
- const ast = (0, import_parser.parse)(code, getParserOptions(medusaConfigPath));
704
+ const mod = await getFileExports(medusaConfigPath);
705
+ const medusaConfig = mod.default ?? mod;
706
+ const modules = medusaConfig?.modules ?? {};
701
707
  const configDir = import_path5.default.dirname(medusaConfigPath);
702
- const result = { properties: null };
703
- traverse(ast, {
704
- // Handle: export default defineConfig({ ... })
705
- ExportDefaultDeclaration(nodePath) {
706
- const decl = nodePath.node.declaration;
707
- if ((0, import_types.isCallExpression)(decl) && decl.arguments.length > 0) {
708
- result.properties = extractObjectProperties(decl.arguments[0]);
709
- }
710
- },
711
- // Handle: module.exports = defineConfig({ ... }) or module.exports = { ... }
712
- AssignmentExpression(nodePath) {
713
- const left = nodePath.node.left;
714
- if ((0, import_types.isMemberExpression)(left) && (0, import_types.isIdentifier)(left.object, { name: "module" }) && (0, import_types.isIdentifier)(left.property, { name: "exports" })) {
715
- const right = nodePath.node.right;
716
- if ((0, import_types.isCallExpression)(right) && right.arguments.length > 0) {
717
- result.properties = extractObjectProperties(right.arguments[0]);
718
- } else if ((0, import_types.isObjectExpression)(right)) {
719
- result.properties = right.properties;
720
- }
721
- }
722
- }
723
- });
724
- const configObjectProperties = result.properties;
725
- if (!configObjectProperties) {
726
- return { pluginExtensions: [] };
727
- }
728
708
  let base;
729
- const modulesProp = configObjectProperties.find(
730
- (p) => (0, import_types.isObjectProperty)(p) && (0, import_types.isIdentifier)(p.key, { name: "modules" })
731
- );
732
- if (modulesProp && (0, import_types.isObjectProperty)(modulesProp) && (0, import_types.isObjectExpression)(modulesProp.value)) {
733
- for (const key of UI_MODULE_KEYS) {
734
- const uiProp = modulesProp.value.properties.find(
735
- (p) => (0, import_types.isObjectProperty)(p) && (0, import_types.isIdentifier)(p.key, { name: key })
736
- );
737
- if (!uiProp || !(0, import_types.isObjectProperty)(uiProp) || !(0, import_types.isObjectExpression)(uiProp.value)) continue;
738
- const optionsProp = uiProp.value.properties.find(
739
- (p) => (0, import_types.isObjectProperty)(p) && (0, import_types.isIdentifier)(p.key, { name: "options" })
740
- );
741
- if (!optionsProp || !(0, import_types.isObjectProperty)(optionsProp) || !(0, import_types.isObjectExpression)(optionsProp.value)) continue;
742
- const appDirProp = optionsProp.value.properties.find(
743
- (p) => (0, import_types.isObjectProperty)(p) && (0, import_types.isIdentifier)(p.key, { name: "appDir" })
744
- );
745
- if (!appDirProp || !(0, import_types.isObjectProperty)(appDirProp)) continue;
746
- const appDir = extractStringFromNode(appDirProp.value, configDir);
747
- if (!appDir) continue;
748
- const resolvedAppDir = import_path5.default.isAbsolute(appDir) ? appDir : import_path5.default.resolve(configDir, appDir);
749
- if (resolvedAppDir === root) {
750
- const pathProp = optionsProp.value.properties.find(
751
- (p) => (0, import_types.isObjectProperty)(p) && (0, import_types.isIdentifier)(p.key, { name: "path" })
752
- );
753
- if (pathProp && (0, import_types.isObjectProperty)(pathProp) && (0, import_types.isStringLiteral)(pathProp.value)) {
754
- base = pathProp.value.value;
755
- }
756
- break;
757
- }
758
- }
759
- }
760
- const plugins = [];
761
- const pluginsProp = configObjectProperties.find(
762
- (p) => (0, import_types.isObjectProperty)(p) && (0, import_types.isIdentifier)(p.key, { name: "plugins" })
763
- );
764
- if (pluginsProp && (0, import_types.isObjectProperty)(pluginsProp) && (0, import_types.isArrayExpression)(pluginsProp.value)) {
765
- for (const element of pluginsProp.value.elements) {
766
- if (!element) continue;
767
- if ((0, import_types.isObjectExpression)(element)) {
768
- const resolveProp = element.properties.find(
769
- (p) => (0, import_types.isObjectProperty)(p) && (0, import_types.isIdentifier)(p.key, { name: "resolve" })
770
- );
771
- if (resolveProp && (0, import_types.isObjectProperty)(resolveProp) && (0, import_types.isStringLiteral)(resolveProp.value)) {
772
- plugins.push({ resolve: resolveProp.value.value });
773
- }
774
- } else if ((0, import_types.isStringLiteral)(element)) {
775
- plugins.push({ resolve: element.value });
776
- }
709
+ for (const key of UI_MODULE_KEYS) {
710
+ const value = modules[key];
711
+ if (!value || typeof value !== "object" || !value.options?.appDir) continue;
712
+ const appDir = import_path5.default.resolve(configDir, value.options.appDir);
713
+ if (appDir === root) {
714
+ base = value.options.path;
715
+ break;
777
716
  }
778
717
  }
718
+ const plugins = medusaConfig?.plugins ?? [];
779
719
  const pluginExtensions = resolvePluginExtensions(plugins, configDir);
780
720
  return { base, pluginExtensions };
781
721
  } catch {
@@ -808,7 +748,6 @@ function mercurDashboardPlugin(pluginConfig) {
808
748
  "__BASE__": JSON.stringify(config.base || "/")
809
749
  },
810
750
  optimizeDeps: {
811
- include: pluginExtensions,
812
751
  exclude: ["virtual:mercur/config", "virtual:mercur/routes", "virtual:mercur/components", "virtual:mercur/menu-items", "virtual:mercur/i18n"]
813
752
  }
814
753
  };
package/dist/index.js CHANGED
@@ -1,3 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
1
8
  // src/plugin.ts
2
9
  import path5 from "path";
3
10
  import fs4 from "fs";
@@ -40,6 +47,34 @@ function getParserOptions(file) {
40
47
  }
41
48
  return options;
42
49
  }
50
+ function resolveExports(moduleExports) {
51
+ if ("default" in moduleExports && moduleExports.default && "default" in moduleExports.default) {
52
+ return resolveExports(moduleExports.default);
53
+ }
54
+ return moduleExports;
55
+ }
56
+ async function getFileExports(path6) {
57
+ const { unregister } = await safeRegister();
58
+ const module = __require(path6);
59
+ unregister();
60
+ return resolveExports(module);
61
+ }
62
+ var safeRegister = async () => {
63
+ const { register } = await import("esbuild-register/dist/node");
64
+ let res;
65
+ try {
66
+ res = register({
67
+ format: "cjs",
68
+ loader: "ts"
69
+ });
70
+ } catch {
71
+ res = {
72
+ unregister: () => {
73
+ }
74
+ };
75
+ }
76
+ return res;
77
+ };
43
78
  function hasDefaultExport(ast) {
44
79
  let found = false;
45
80
  traverse(ast, {
@@ -286,11 +321,8 @@ function generateRoutes({ srcDir, pluginExtensions }) {
286
321
  index++;
287
322
  }
288
323
  }
289
- const pluginImports = pluginExtensions.map(
290
- (ext, i) => `import * as __pluginRaw${i} from "${normalizePath(ext)}"`
291
- );
292
- const pluginUnwraps = pluginExtensions.map(
293
- (_, i) => `const __plugin${i} = __pluginRaw${i}.default ?? __pluginRaw${i}`
324
+ const pluginDeclarations = pluginExtensions.map(
325
+ (ext, i) => `const __plugin${i} = (await import("${normalizePath(ext)}")).default`
294
326
  );
295
327
  const pluginSpreads = pluginExtensions.map(
296
328
  (_, i) => ` ...(__plugin${i}.routeModule?.routes ?? [])`
@@ -298,13 +330,15 @@ function generateRoutes({ srcDir, pluginExtensions }) {
298
330
  const routeTree = buildRouteTree(results);
299
331
  const appImports = routeTree.flatMap((r) => r.imports);
300
332
  const appRoutes = routeTree.map((r) => formatRoute(r.route));
301
- const allImports = [...appImports, ...pluginImports, ...pluginUnwraps];
333
+ const allImports = [...appImports];
302
334
  const allRoutes = [...appRoutes, ...pluginSpreads];
303
- if (allImports.length === 0 && allRoutes.length === 0) {
335
+ if (allImports.length === 0 && pluginDeclarations.length === 0 && allRoutes.length === 0) {
304
336
  return `export const customRoutes = []`;
305
337
  }
306
338
  return `${allImports.join("\n")}
307
339
 
340
+ ${pluginDeclarations.join("\n")}
341
+
308
342
  export const customRoutes = [
309
343
  ${allRoutes.join(",\n")}
310
344
  ]`;
@@ -483,24 +517,23 @@ function generateMenuItems({ srcDir, pluginExtensions }) {
483
517
  index++;
484
518
  }
485
519
  }
486
- const pluginImports = pluginExtensions.map(
487
- (ext, i) => `import * as __pluginRaw${i} from "${normalizePath(ext)}"`
488
- );
489
- const pluginUnwraps = pluginExtensions.map(
490
- (_, i) => `const __plugin${i} = __pluginRaw${i}.default ?? __pluginRaw${i}`
520
+ const pluginDeclarations = pluginExtensions.map(
521
+ (ext, i) => `const __plugin${i} = (await import("${normalizePath(ext)}")).default`
491
522
  );
492
523
  const pluginSpreads = pluginExtensions.map(
493
524
  (_, i) => ` ...(__plugin${i}.menuItemModule?.menuItems ?? [])`
494
525
  );
495
526
  const appImports = results.map((r) => r.import);
496
527
  const appMenuItems = results.map((r) => formatMenuItem(r.menuItem));
497
- const allImports = [...appImports, ...pluginImports, ...pluginUnwraps];
528
+ const allImports = [...appImports];
498
529
  const allMenuItems = [...appMenuItems, ...pluginSpreads];
499
- if (allImports.length === 0 && allMenuItems.length === 0) {
530
+ if (allImports.length === 0 && pluginDeclarations.length === 0 && allMenuItems.length === 0) {
500
531
  return `export default { menuItems: [] }`;
501
532
  }
502
533
  return `${allImports.join("\n")}
503
534
 
535
+ ${pluginDeclarations.join("\n")}
536
+
504
537
  export default {
505
538
  menuItems: [
506
539
  ${allMenuItems.join(",\n")}
@@ -650,109 +683,23 @@ function resolvePluginExtensions(plugins, configDir) {
650
683
  }
651
684
  return extensions;
652
685
  }
653
- function extractStringFromNode(node, configDir) {
654
- if (isStringLiteral(node)) {
655
- return node.value;
656
- }
657
- if (isCallExpression(node) && isMemberExpression(node.callee) && isIdentifier(node.callee.object, { name: "path" }) && isIdentifier(node.callee.property) && (node.callee.property.name === "join" || node.callee.property.name === "resolve")) {
658
- const args = node.arguments;
659
- if (args.length >= 2 && isIdentifier(args[0], { name: "__dirname" })) {
660
- const parts = args.slice(1).filter((a) => isStringLiteral(a)).map((a) => a.value);
661
- if (parts.length > 0) {
662
- return path5.resolve(configDir, ...parts);
663
- }
664
- }
665
- }
666
- return null;
667
- }
668
- function extractObjectProperties(node) {
669
- if (isObjectExpression(node)) {
670
- return node.properties;
671
- }
672
- return null;
673
- }
674
686
  async function loadMedusaConfig(medusaConfigPath, root) {
675
687
  try {
676
- const code = fs4.readFileSync(medusaConfigPath, "utf-8");
677
- const ast = parse(code, getParserOptions(medusaConfigPath));
688
+ const mod = await getFileExports(medusaConfigPath);
689
+ const medusaConfig = mod.default ?? mod;
690
+ const modules = medusaConfig?.modules ?? {};
678
691
  const configDir = path5.dirname(medusaConfigPath);
679
- const result = { properties: null };
680
- traverse(ast, {
681
- // Handle: export default defineConfig({ ... })
682
- ExportDefaultDeclaration(nodePath) {
683
- const decl = nodePath.node.declaration;
684
- if (isCallExpression(decl) && decl.arguments.length > 0) {
685
- result.properties = extractObjectProperties(decl.arguments[0]);
686
- }
687
- },
688
- // Handle: module.exports = defineConfig({ ... }) or module.exports = { ... }
689
- AssignmentExpression(nodePath) {
690
- const left = nodePath.node.left;
691
- if (isMemberExpression(left) && isIdentifier(left.object, { name: "module" }) && isIdentifier(left.property, { name: "exports" })) {
692
- const right = nodePath.node.right;
693
- if (isCallExpression(right) && right.arguments.length > 0) {
694
- result.properties = extractObjectProperties(right.arguments[0]);
695
- } else if (isObjectExpression(right)) {
696
- result.properties = right.properties;
697
- }
698
- }
699
- }
700
- });
701
- const configObjectProperties = result.properties;
702
- if (!configObjectProperties) {
703
- return { pluginExtensions: [] };
704
- }
705
692
  let base;
706
- const modulesProp = configObjectProperties.find(
707
- (p) => isObjectProperty(p) && isIdentifier(p.key, { name: "modules" })
708
- );
709
- if (modulesProp && isObjectProperty(modulesProp) && isObjectExpression(modulesProp.value)) {
710
- for (const key of UI_MODULE_KEYS) {
711
- const uiProp = modulesProp.value.properties.find(
712
- (p) => isObjectProperty(p) && isIdentifier(p.key, { name: key })
713
- );
714
- if (!uiProp || !isObjectProperty(uiProp) || !isObjectExpression(uiProp.value)) continue;
715
- const optionsProp = uiProp.value.properties.find(
716
- (p) => isObjectProperty(p) && isIdentifier(p.key, { name: "options" })
717
- );
718
- if (!optionsProp || !isObjectProperty(optionsProp) || !isObjectExpression(optionsProp.value)) continue;
719
- const appDirProp = optionsProp.value.properties.find(
720
- (p) => isObjectProperty(p) && isIdentifier(p.key, { name: "appDir" })
721
- );
722
- if (!appDirProp || !isObjectProperty(appDirProp)) continue;
723
- const appDir = extractStringFromNode(appDirProp.value, configDir);
724
- if (!appDir) continue;
725
- const resolvedAppDir = path5.isAbsolute(appDir) ? appDir : path5.resolve(configDir, appDir);
726
- if (resolvedAppDir === root) {
727
- const pathProp = optionsProp.value.properties.find(
728
- (p) => isObjectProperty(p) && isIdentifier(p.key, { name: "path" })
729
- );
730
- if (pathProp && isObjectProperty(pathProp) && isStringLiteral(pathProp.value)) {
731
- base = pathProp.value.value;
732
- }
733
- break;
734
- }
735
- }
736
- }
737
- const plugins = [];
738
- const pluginsProp = configObjectProperties.find(
739
- (p) => isObjectProperty(p) && isIdentifier(p.key, { name: "plugins" })
740
- );
741
- if (pluginsProp && isObjectProperty(pluginsProp) && isArrayExpression(pluginsProp.value)) {
742
- for (const element of pluginsProp.value.elements) {
743
- if (!element) continue;
744
- if (isObjectExpression(element)) {
745
- const resolveProp = element.properties.find(
746
- (p) => isObjectProperty(p) && isIdentifier(p.key, { name: "resolve" })
747
- );
748
- if (resolveProp && isObjectProperty(resolveProp) && isStringLiteral(resolveProp.value)) {
749
- plugins.push({ resolve: resolveProp.value.value });
750
- }
751
- } else if (isStringLiteral(element)) {
752
- plugins.push({ resolve: element.value });
753
- }
693
+ for (const key of UI_MODULE_KEYS) {
694
+ const value = modules[key];
695
+ if (!value || typeof value !== "object" || !value.options?.appDir) continue;
696
+ const appDir = path5.resolve(configDir, value.options.appDir);
697
+ if (appDir === root) {
698
+ base = value.options.path;
699
+ break;
754
700
  }
755
701
  }
702
+ const plugins = medusaConfig?.plugins ?? [];
756
703
  const pluginExtensions = resolvePluginExtensions(plugins, configDir);
757
704
  return { base, pluginExtensions };
758
705
  } catch {
@@ -785,7 +732,6 @@ function mercurDashboardPlugin(pluginConfig) {
785
732
  "__BASE__": JSON.stringify(config.base || "/")
786
733
  },
787
734
  optimizeDeps: {
788
- include: pluginExtensions,
789
735
  exclude: ["virtual:mercur/config", "virtual:mercur/routes", "virtual:mercur/components", "virtual:mercur/menu-items", "virtual:mercur/i18n"]
790
736
  }
791
737
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mercurjs/dashboard-sdk",
3
- "version": "2.0.0-canary.74",
3
+ "version": "2.0.0-canary.75",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/mercurjs/mercur",
@@ -27,7 +27,8 @@
27
27
  "dependencies": {
28
28
  "@babel/parser": "7.25.6",
29
29
  "@babel/traverse": "7.25.6",
30
- "@babel/types": "7.25.6"
30
+ "@babel/types": "7.25.6",
31
+ "esbuild-register": "^3.6.0"
31
32
  },
32
33
  "devDependencies": {
33
34
  "@types/node": "^22.0.0",