@mercurjs/dashboard-sdk 2.0.0-canary.76 → 2.0.0-canary.78

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
@@ -38,9 +38,6 @@ module.exports = __toCommonJS(index_exports);
38
38
  var import_path5 = __toESM(require("path"), 1);
39
39
  var import_fs4 = __toESM(require("fs"), 1);
40
40
 
41
- // src/utils.ts
42
- var import_node_module = require("module");
43
-
44
41
  // src/babel.ts
45
42
  var import_parser = require("@babel/parser");
46
43
  var import_traverse = __toESM(require("@babel/traverse"), 1);
@@ -53,7 +50,6 @@ if (typeof import_traverse.default === "function") {
53
50
  }
54
51
 
55
52
  // src/utils.ts
56
- var import_meta = {};
57
53
  function normalizePath(filePath) {
58
54
  return filePath.replace(/\\/g, "/");
59
55
  }
@@ -73,10 +69,9 @@ function resolveExports(moduleExports) {
73
69
  }
74
70
  return moduleExports;
75
71
  }
76
- var esmRequire = typeof require !== "undefined" ? require : (0, import_node_module.createRequire)(import_meta.url);
77
- async function getFileExports(filePath) {
72
+ async function getFileExports(path6) {
78
73
  const { unregister } = await safeRegister();
79
- const module2 = esmRequire(filePath);
74
+ const module2 = require(path6);
80
75
  unregister();
81
76
  return resolveExports(module2);
82
77
  }
@@ -696,7 +691,7 @@ function resolvePluginExtensions(plugins, configDir) {
696
691
  const pluginRoot = resolvePluginRoot(resolve, configDir, nodeModulesRoot);
697
692
  if (!pluginRoot) continue;
698
693
  for (const appType of appTypes) {
699
- const extFile = import_path5.default.join(pluginRoot, ".medusa/server/src", appType, "index.js");
694
+ const extFile = import_path5.default.join(pluginRoot, ".medusa/server/src", appType, "index.mjs");
700
695
  if (import_fs4.default.existsSync(extFile)) {
701
696
  extensions.push(extFile);
702
697
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mercurjs/dashboard-sdk",
3
- "version": "2.0.0-canary.76",
3
+ "version": "2.0.0-canary.78",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/mercurjs/mercur",
@@ -12,8 +12,8 @@
12
12
  "exports": {
13
13
  ".": {
14
14
  "types": "./dist/index.d.ts",
15
- "import": "./dist/index.js",
16
- "require": "./dist/index.cjs"
15
+ "require": "./dist/index.cjs",
16
+ "default": "./dist/index.cjs"
17
17
  }
18
18
  },
19
19
  "files": [
package/dist/index.d.ts DELETED
@@ -1,37 +0,0 @@
1
- import * as Vite from 'vite';
2
- import { ComponentType } from 'react';
3
-
4
- interface MercurConfig {
5
- medusaConfigPath: string;
6
- backendUrl?: string;
7
- name?: string;
8
- logo?: string;
9
- components?: {
10
- MainSidebar?: string;
11
- SettingsSidebar?: string;
12
- TopbarActions?: string;
13
- };
14
- i18n?: {
15
- defaultLanguage: string;
16
- };
17
- enableSellerRegistration?: boolean;
18
- }
19
- interface BuiltMercurConfig extends MercurConfig {
20
- backendUrl: string;
21
- base?: string;
22
- root: string;
23
- srcDir: string;
24
- pluginExtensions: string[];
25
- }
26
- type RouteConfig = {
27
- label: string;
28
- icon?: ComponentType;
29
- rank?: number;
30
- nested?: string;
31
- translationNs?: string;
32
- public?: boolean;
33
- };
34
-
35
- declare function mercurDashboardPlugin(pluginConfig: MercurConfig): Vite.Plugin;
36
-
37
- export { type BuiltMercurConfig, type MercurConfig, type RouteConfig, mercurDashboardPlugin };
package/dist/index.js DELETED
@@ -1,779 +0,0 @@
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
-
8
- // src/plugin.ts
9
- import path5 from "path";
10
- import fs4 from "fs";
11
-
12
- // src/utils.ts
13
- import { createRequire } from "module";
14
-
15
- // src/babel.ts
16
- import { parse } from "@babel/parser";
17
- import _traverse from "@babel/traverse";
18
- import {
19
- isBooleanLiteral,
20
- isCallExpression,
21
- isFunctionDeclaration,
22
- isIdentifier,
23
- isMemberExpression,
24
- isNumericLiteral,
25
- isObjectExpression,
26
- isObjectProperty,
27
- isStringLiteral,
28
- isVariableDeclaration,
29
- isVariableDeclarator,
30
- isArrayExpression
31
- } from "@babel/types";
32
- var traverse;
33
- if (typeof _traverse === "function") {
34
- traverse = _traverse;
35
- } else {
36
- traverse = _traverse.default;
37
- }
38
-
39
- // src/utils.ts
40
- function normalizePath(filePath) {
41
- return filePath.replace(/\\/g, "/");
42
- }
43
- function getParserOptions(file) {
44
- const options = {
45
- sourceType: "module",
46
- plugins: ["jsx"]
47
- };
48
- if (file.endsWith(".ts") || file.endsWith(".tsx")) {
49
- options.plugins.push("typescript");
50
- }
51
- return options;
52
- }
53
- function resolveExports(moduleExports) {
54
- if ("default" in moduleExports && moduleExports.default && "default" in moduleExports.default) {
55
- return resolveExports(moduleExports.default);
56
- }
57
- return moduleExports;
58
- }
59
- var esmRequire = typeof __require !== "undefined" ? __require : createRequire(import.meta.url);
60
- async function getFileExports(filePath) {
61
- const { unregister } = await safeRegister();
62
- const module = esmRequire(filePath);
63
- unregister();
64
- return resolveExports(module);
65
- }
66
- var safeRegister = async () => {
67
- const { register } = await import("esbuild-register/dist/node");
68
- let res;
69
- try {
70
- res = register({
71
- format: "cjs",
72
- loader: "ts"
73
- });
74
- } catch {
75
- res = {
76
- unregister: () => {
77
- }
78
- };
79
- }
80
- return res;
81
- };
82
- function hasDefaultExport(ast) {
83
- let found = false;
84
- traverse(ast, {
85
- ExportDefaultDeclaration() {
86
- found = true;
87
- },
88
- AssignmentExpression(path6) {
89
- if (path6.node.left.type === "MemberExpression" && path6.node.left.object.type === "Identifier" && path6.node.left.object.name === "exports" && path6.node.left.property.type === "Identifier" && path6.node.left.property.name === "default") {
90
- found = true;
91
- }
92
- },
93
- ExportNamedDeclaration(path6) {
94
- const specifiers = path6.node.specifiers;
95
- if (specifiers?.some(
96
- (s) => s.type === "ExportSpecifier" && s.exported.type === "Identifier" && s.exported.name === "default"
97
- )) {
98
- found = true;
99
- }
100
- }
101
- });
102
- return found;
103
- }
104
-
105
- // src/constants.ts
106
- var VALID_FILE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js"];
107
- var CONFIG_VIRTUAL_MODULE = "virtual:mercur/config";
108
- var ROUTES_VIRTUAL_MODULE = "virtual:mercur/routes";
109
- var COMPONENTS_VIRTUAL_MODULE = "virtual:mercur/components";
110
- var MENU_ITEMS_VIRTUAL_MODULE = "virtual:mercur/menu-items";
111
- var I18N_VIRTUAL_MODULE = "virtual:mercur/i18n";
112
- var RESOLVED_CONFIG_MODULE = "\0" + CONFIG_VIRTUAL_MODULE;
113
- var RESOLVED_ROUTES_MODULE = "\0" + ROUTES_VIRTUAL_MODULE;
114
- var RESOLVED_COMPONENTS_MODULE = "\0" + COMPONENTS_VIRTUAL_MODULE;
115
- var RESOLVED_MENU_ITEMS_MODULE = "\0" + MENU_ITEMS_VIRTUAL_MODULE;
116
- var RESOLVED_I18N_MODULE = "\0" + I18N_VIRTUAL_MODULE;
117
- var VIRTUAL_MODULES = [
118
- CONFIG_VIRTUAL_MODULE,
119
- ROUTES_VIRTUAL_MODULE,
120
- COMPONENTS_VIRTUAL_MODULE,
121
- MENU_ITEMS_VIRTUAL_MODULE,
122
- I18N_VIRTUAL_MODULE
123
- ];
124
-
125
- // src/virtual-modules.ts
126
- import path4 from "path";
127
-
128
- // src/routes.ts
129
- import fs from "fs";
130
- import path from "path";
131
- function getRoute(file, pagesDir) {
132
- const importPath = normalizePath(file);
133
- const normalizedPagesDir = normalizePath(pagesDir);
134
- return importPath.replace(normalizedPagesDir, "").replace(/\[\[\*\]\]/g, "*?").replace(/\[\*\]/g, "*").replace(/\(([^\[\]\)]+)\)/g, "$1?").replace(/\[\[([^\]]+)\]\]/g, ":$1?").replace(/\[([^\]]+)\]/g, ":$1").replace(
135
- new RegExp(
136
- `/page\\.(${VALID_FILE_EXTENSIONS.map((ext) => ext.slice(1)).join("|")})$`
137
- ),
138
- ""
139
- ) || "/";
140
- }
141
- function crawlPages(dir, pattern = "page") {
142
- const files = [];
143
- if (!fs.existsSync(dir)) {
144
- return files;
145
- }
146
- const entries = fs.readdirSync(dir, { withFileTypes: true });
147
- for (const entry of entries) {
148
- const fullPath = path.join(dir, entry.name);
149
- if (entry.isDirectory()) {
150
- files.push(...crawlPages(fullPath, pattern));
151
- } else if (entry.isFile()) {
152
- const ext = path.extname(entry.name);
153
- const baseName = path.basename(entry.name, ext);
154
- if (baseName === pattern && VALID_FILE_EXTENSIONS.includes(ext)) {
155
- files.push(fullPath);
156
- }
157
- }
158
- }
159
- return files;
160
- }
161
- function hasConfigPublic(ast) {
162
- let found = false;
163
- traverse(ast, {
164
- ExportNamedDeclaration(path6) {
165
- const declaration = path6.node.declaration;
166
- if (!isVariableDeclaration(declaration)) return;
167
- for (const decl of declaration.declarations) {
168
- if (isVariableDeclarator(decl) && isIdentifier(decl.id, { name: "config" }) && decl.init?.type === "ObjectExpression") {
169
- const publicProp = decl.init.properties.find(
170
- (prop) => isObjectProperty(prop) && isIdentifier(prop.key, { name: "public" }) && isBooleanLiteral(prop.value, { value: true })
171
- );
172
- if (publicProp) {
173
- found = true;
174
- }
175
- }
176
- }
177
- }
178
- });
179
- return found;
180
- }
181
- function getNamedExports(ast) {
182
- let hasHandle = false;
183
- let hasLoader = false;
184
- traverse(ast, {
185
- ExportNamedDeclaration(path6) {
186
- const declaration = path6.node.declaration;
187
- if (declaration?.type === "VariableDeclaration") {
188
- declaration.declarations.forEach((decl) => {
189
- if (decl.id.type === "Identifier" && decl.id.name === "handle") {
190
- hasHandle = true;
191
- }
192
- if (decl.id.type === "Identifier" && decl.id.name === "loader") {
193
- hasLoader = true;
194
- }
195
- });
196
- }
197
- if (declaration?.type === "FunctionDeclaration" && declaration.id?.name === "loader") {
198
- hasLoader = true;
199
- }
200
- if (declaration?.type === "FunctionDeclaration" && declaration.id?.name === "handle") {
201
- hasHandle = true;
202
- }
203
- }
204
- });
205
- return { hasHandle, hasLoader };
206
- }
207
- function generateRouteComponentName(index) {
208
- return `RouteComponent${index}`;
209
- }
210
- function generateHandleName(index) {
211
- return `RouteHandle${index}`;
212
- }
213
- function generateLoaderName(index) {
214
- return `RouteLoader${index}`;
215
- }
216
- function generateImports(file, index, hasHandle, hasLoader) {
217
- const imports = [];
218
- const componentName = generateRouteComponentName(index);
219
- const importPath = normalizePath(file);
220
- if (!hasHandle && !hasLoader) {
221
- imports.push(`import ${componentName} from "${importPath}"`);
222
- } else {
223
- const namedImports = [
224
- hasHandle && `handle as ${generateHandleName(index)}`,
225
- hasLoader && `loader as ${generateLoaderName(index)}`
226
- ].filter(Boolean).join(", ");
227
- imports.push(`import ${componentName}, { ${namedImports} } from "${importPath}"`);
228
- }
229
- return imports;
230
- }
231
- function generateRouteObject(routePath, index, hasHandle, hasLoader, isPublic) {
232
- return {
233
- Component: generateRouteComponentName(index),
234
- path: routePath,
235
- handle: hasHandle ? generateHandleName(index) : void 0,
236
- loader: hasLoader ? generateLoaderName(index) : void 0,
237
- isPublic
238
- };
239
- }
240
- function formatRoute(route, indent = " ") {
241
- let result = `${indent}{
242
- `;
243
- result += `${indent} Component: ${route.Component},
244
- `;
245
- result += `${indent} path: "${route.path}"`;
246
- if (route.handle) {
247
- result += `,
248
- ${indent} handle: ${route.handle}`;
249
- }
250
- if (route.loader) {
251
- result += `,
252
- ${indent} loader: ${route.loader}`;
253
- }
254
- if (route.isPublic) {
255
- result += `,
256
- ${indent} isPublic: true`;
257
- }
258
- if (route.children?.length) {
259
- result += `,
260
- ${indent} children: [
261
- `;
262
- result += route.children.map((child) => formatRoute(child, indent + " ")).join(",\n");
263
- result += `
264
- ${indent} ]`;
265
- }
266
- result += `
267
- ${indent}}`;
268
- return result;
269
- }
270
- function parseFile(file, pagesDir, index) {
271
- try {
272
- const code = fs.readFileSync(file, "utf-8");
273
- const ast = parse(code, getParserOptions(file));
274
- if (!hasDefaultExport(ast)) {
275
- return null;
276
- }
277
- const { hasHandle, hasLoader } = getNamedExports(ast);
278
- const isPublic = hasConfigPublic(ast);
279
- const routePath = getRoute(file, pagesDir);
280
- const imports = generateImports(file, index, hasHandle, hasLoader);
281
- const route = generateRouteObject(routePath, index, hasHandle, hasLoader, isPublic);
282
- return {
283
- imports,
284
- route
285
- };
286
- } catch {
287
- return null;
288
- }
289
- }
290
- function buildRouteTree(results) {
291
- const routeMap = /* @__PURE__ */ new Map();
292
- const sortedResults = [...results].sort(
293
- (a, b) => a.route.path.split("/").length - b.route.path.split("/").length
294
- );
295
- for (const result of sortedResults) {
296
- const routePath = result.route.path;
297
- const isParallel = routePath.includes("/@");
298
- if (isParallel) {
299
- const parentPath = routePath.split("/@")[0];
300
- const parent = routeMap.get(parentPath);
301
- if (parent) {
302
- parent.route.children = parent.route.children ?? [];
303
- parent.route.children.push({
304
- ...result.route,
305
- path: result.route.path.replace("/@", "/")
306
- });
307
- parent.imports.push(...result.imports);
308
- } else {
309
- routeMap.set(routePath, result);
310
- }
311
- } else {
312
- routeMap.set(routePath, result);
313
- }
314
- }
315
- return Array.from(routeMap.values());
316
- }
317
- function generateRoutes({ srcDir, pluginExtensions }) {
318
- const pagesDir = path.join(srcDir, "pages");
319
- let index = 0;
320
- const results = [];
321
- for (const file of crawlPages(pagesDir)) {
322
- const result = parseFile(file, pagesDir, index);
323
- if (result) {
324
- results.push(result);
325
- index++;
326
- }
327
- }
328
- const pluginDeclarations = pluginExtensions.map(
329
- (ext, i) => `const __plugin${i} = (await import("${normalizePath(ext)}")).default`
330
- );
331
- const pluginSpreads = pluginExtensions.map(
332
- (_, i) => ` ...(__plugin${i}.routeModule?.routes ?? [])`
333
- );
334
- const routeTree = buildRouteTree(results);
335
- const appImports = routeTree.flatMap((r) => r.imports);
336
- const appRoutes = routeTree.map((r) => formatRoute(r.route));
337
- const allImports = [...appImports];
338
- const allRoutes = [...appRoutes, ...pluginSpreads];
339
- if (allImports.length === 0 && pluginDeclarations.length === 0 && allRoutes.length === 0) {
340
- return `export const customRoutes = []`;
341
- }
342
- return `${allImports.join("\n")}
343
-
344
- ${pluginDeclarations.join("\n")}
345
-
346
- export const customRoutes = [
347
- ${allRoutes.join(",\n")}
348
- ]`;
349
- }
350
-
351
- // src/menu-items.ts
352
- import fs2 from "fs";
353
- import path2 from "path";
354
- function crawlPages2(dir, pattern = "page") {
355
- const files = [];
356
- if (!fs2.existsSync(dir)) {
357
- return files;
358
- }
359
- const entries = fs2.readdirSync(dir, { withFileTypes: true });
360
- for (const entry of entries) {
361
- const fullPath = path2.join(dir, entry.name);
362
- if (entry.isDirectory()) {
363
- files.push(...crawlPages2(fullPath, pattern));
364
- } else if (entry.isFile()) {
365
- const ext = path2.extname(entry.name);
366
- const baseName = path2.basename(entry.name, ext);
367
- if (baseName === pattern && VALID_FILE_EXTENSIONS.includes(ext)) {
368
- files.push(fullPath);
369
- }
370
- }
371
- }
372
- return files;
373
- }
374
- function getRoute2(file, pagesDir) {
375
- const importPath = normalizePath(file);
376
- const normalizedPagesDir = normalizePath(pagesDir);
377
- return importPath.replace(normalizedPagesDir, "").replace(/\[\[\*\]\]/g, "*?").replace(/\[\*\]/g, "*").replace(/\(([^\[\]\)]+)\)/g, "$1?").replace(/\[\[([^\]]+)\]\]/g, ":$1?").replace(/\[([^\]]+)\]/g, ":$1").replace(
378
- new RegExp(
379
- `/page\\.(${VALID_FILE_EXTENSIONS.map((ext) => ext.slice(1)).join("|")})$`
380
- ),
381
- ""
382
- ) || "/";
383
- }
384
- function getConfigObjectProperties(path6) {
385
- if (isVariableDeclarator(path6.node)) {
386
- const decl = isIdentifier(path6.node.id, { name: "config" }) ? path6.node : null;
387
- if (!decl) return null;
388
- if (isCallExpression(decl.init) && decl.init.arguments.length > 0 && isObjectExpression(decl.init.arguments[0])) {
389
- return decl.init.arguments[0].properties;
390
- }
391
- if (isObjectExpression(decl.init)) {
392
- return decl.init.properties;
393
- }
394
- return null;
395
- }
396
- const declaration = path6.node.declaration;
397
- if (isVariableDeclaration(declaration)) {
398
- const configDecl = declaration.declarations.find(
399
- (d) => isVariableDeclarator(d) && isIdentifier(d.id, { name: "config" })
400
- );
401
- if (configDecl && isCallExpression(configDecl.init) && configDecl.init.arguments.length > 0 && isObjectExpression(configDecl.init.arguments[0])) {
402
- return configDecl.init.arguments[0].properties;
403
- }
404
- const directDecl = declaration.declarations.find(
405
- (d) => isVariableDeclarator(d) && isIdentifier(d.id, { name: "config" })
406
- );
407
- if (directDecl && isObjectExpression(directDecl.init)) {
408
- return directDecl.init.properties;
409
- }
410
- }
411
- return null;
412
- }
413
- function processConfigProperties(properties) {
414
- const hasProperty = (name) => properties.some(
415
- (prop) => isObjectProperty(prop) && isIdentifier(prop.key, { name })
416
- );
417
- const hasLabel = hasProperty("label");
418
- if (!hasLabel) {
419
- return null;
420
- }
421
- const hasIcon = hasProperty("icon");
422
- const nested = properties.find(
423
- (prop) => isObjectProperty(prop) && isIdentifier(prop.key, { name: "nested" })
424
- );
425
- let nestedValue;
426
- if (nested && isObjectProperty(nested) && isStringLiteral(nested.value)) {
427
- nestedValue = nested.value.value;
428
- }
429
- const translationNs = properties.find(
430
- (prop) => isObjectProperty(prop) && isIdentifier(prop.key, { name: "translationNs" })
431
- );
432
- let translationNsValue;
433
- if (translationNs && isObjectProperty(translationNs) && isStringLiteral(translationNs.value)) {
434
- translationNsValue = translationNs.value.value;
435
- }
436
- const rank = properties.find(
437
- (prop) => isObjectProperty(prop) && isIdentifier(prop.key, { name: "rank" })
438
- );
439
- let rankValue;
440
- if (rank && isObjectProperty(rank) && isNumericLiteral(rank.value)) {
441
- rankValue = rank.value.value;
442
- }
443
- return { label: hasLabel, icon: hasIcon, rank: rankValue, nested: nestedValue, translationNs: translationNsValue };
444
- }
445
- function getRouteConfig(file) {
446
- try {
447
- const code = fs2.readFileSync(file, "utf-8");
448
- const ast = parse(code, getParserOptions(file));
449
- let config = null;
450
- let configFound = false;
451
- traverse(ast, {
452
- VariableDeclarator(path6) {
453
- if (configFound) return;
454
- const properties = getConfigObjectProperties(path6);
455
- if (!properties) return;
456
- config = processConfigProperties(properties);
457
- if (config) configFound = true;
458
- },
459
- ExportNamedDeclaration(path6) {
460
- if (configFound) return;
461
- const properties = getConfigObjectProperties(path6);
462
- if (!properties) return;
463
- config = processConfigProperties(properties);
464
- if (config) configFound = true;
465
- }
466
- });
467
- return config;
468
- } catch {
469
- return null;
470
- }
471
- }
472
- function generateRouteConfigName(index) {
473
- return `RouteConfig${index}`;
474
- }
475
- function generateImport(file, index) {
476
- const importPath = normalizePath(file);
477
- return `import { config as ${generateRouteConfigName(index)} } from "${importPath}"`;
478
- }
479
- function generateMenuItem(config, file, pagesDir, index) {
480
- const configName = generateRouteConfigName(index);
481
- return {
482
- label: `${configName}.label`,
483
- icon: config.icon ? `${configName}.icon` : void 0,
484
- path: getRoute2(file, pagesDir),
485
- rank: config.rank,
486
- nested: config.nested,
487
- translationNs: config.translationNs ? `${configName}.translationNs` : void 0
488
- };
489
- }
490
- function formatMenuItem(menuItem) {
491
- const parts = [
492
- ` label: ${menuItem.label}`,
493
- ` icon: ${menuItem.icon || "undefined"}`,
494
- ` path: "${menuItem.path}"`,
495
- ` rank: ${menuItem.rank !== void 0 ? menuItem.rank : "undefined"}`,
496
- ` nested: ${menuItem.nested ? `"${menuItem.nested}"` : "undefined"}`,
497
- ` translationNs: ${menuItem.translationNs || "undefined"}`
498
- ];
499
- return ` {
500
- ${parts.join(",\n")}
501
- }`;
502
- }
503
- function parseFile2(file, pagesDir, index) {
504
- const config = getRouteConfig(file);
505
- if (!config) {
506
- return null;
507
- }
508
- return {
509
- import: generateImport(file, index),
510
- menuItem: generateMenuItem(config, file, pagesDir, index)
511
- };
512
- }
513
- function generateMenuItems({ srcDir, pluginExtensions }) {
514
- const pagesDir = path2.join(srcDir, "pages");
515
- let index = 0;
516
- const results = [];
517
- for (const file of crawlPages2(pagesDir)) {
518
- const result = parseFile2(file, pagesDir, index);
519
- if (result) {
520
- results.push(result);
521
- index++;
522
- }
523
- }
524
- const pluginDeclarations = pluginExtensions.map(
525
- (ext, i) => `const __plugin${i} = (await import("${normalizePath(ext)}")).default`
526
- );
527
- const pluginSpreads = pluginExtensions.map(
528
- (_, i) => ` ...(__plugin${i}.menuItemModule?.menuItems ?? [])`
529
- );
530
- const appImports = results.map((r) => r.import);
531
- const appMenuItems = results.map((r) => formatMenuItem(r.menuItem));
532
- const allImports = [...appImports];
533
- const allMenuItems = [...appMenuItems, ...pluginSpreads];
534
- if (allImports.length === 0 && pluginDeclarations.length === 0 && allMenuItems.length === 0) {
535
- return `export default { menuItems: [] }`;
536
- }
537
- return `${allImports.join("\n")}
538
-
539
- ${pluginDeclarations.join("\n")}
540
-
541
- export default {
542
- menuItems: [
543
- ${allMenuItems.join(",\n")}
544
- ]
545
- }`;
546
- }
547
-
548
- // src/i18n.ts
549
- import fs3 from "fs";
550
- import path3 from "path";
551
- function findI18nIndex(srcDir) {
552
- const i18nDir = path3.join(srcDir, "i18n");
553
- if (!fs3.existsSync(i18nDir)) {
554
- return null;
555
- }
556
- for (const ext of VALID_FILE_EXTENSIONS) {
557
- const filePath = path3.join(i18nDir, `index${ext}`);
558
- if (fs3.existsSync(filePath)) {
559
- return filePath;
560
- }
561
- }
562
- return null;
563
- }
564
- function generateI18n({ srcDir }) {
565
- const indexFile = findI18nIndex(srcDir);
566
- if (!indexFile) {
567
- return `export default {}`;
568
- }
569
- const importPath = normalizePath(indexFile);
570
- return `import i18nResources from "${importPath}"
571
- export default i18nResources`;
572
- }
573
-
574
- // src/virtual-modules.ts
575
- function isVirtualModule(id) {
576
- return VIRTUAL_MODULES.includes(id);
577
- }
578
- function resolveVirtualModule(id) {
579
- return "\0" + id;
580
- }
581
- function loadVirtualModule({
582
- cwd,
583
- id,
584
- mercurConfig
585
- }) {
586
- if (id === RESOLVED_CONFIG_MODULE) {
587
- return loadConfigModule(mercurConfig);
588
- }
589
- if (id === RESOLVED_COMPONENTS_MODULE) {
590
- return loadComponentsModule(mercurConfig, cwd);
591
- }
592
- if (id === RESOLVED_ROUTES_MODULE) {
593
- return loadRoutesModule(mercurConfig);
594
- }
595
- if (id === RESOLVED_MENU_ITEMS_MODULE) {
596
- return loadMenuItemsModule(mercurConfig);
597
- }
598
- if (id === RESOLVED_I18N_MODULE) {
599
- return loadI18nModule(mercurConfig);
600
- }
601
- return null;
602
- }
603
- function loadConfigModule(mercurConfig) {
604
- const { components, ...configWithoutComponents } = mercurConfig;
605
- return `export default ${JSON.stringify(configWithoutComponents)}`;
606
- }
607
- function loadComponentsModule(mercurConfig, cwd) {
608
- const components = mercurConfig.components ?? {};
609
- const imports = [];
610
- const exports = [];
611
- Object.entries(components).forEach(([name, componentPath]) => {
612
- const resolvedPath = path4.resolve(cwd, "src", componentPath);
613
- imports.push(`import _${name} from "${resolvedPath}"`);
614
- exports.push(`${name}: _${name}`);
615
- });
616
- return `
617
- ${imports.join("\n")}
618
-
619
- export default {
620
- ${exports.join(",\n ")}
621
- }
622
- `;
623
- }
624
- function loadRoutesModule(mercurConfig) {
625
- return generateRoutes(mercurConfig);
626
- }
627
- function loadMenuItemsModule(mercurConfig) {
628
- return generateMenuItems(mercurConfig);
629
- }
630
- function loadI18nModule(mercurConfig) {
631
- return generateI18n(mercurConfig);
632
- }
633
-
634
- // src/plugin.ts
635
- function isPageFile(file) {
636
- const basename = path5.basename(file, path5.extname(file));
637
- return basename === "page";
638
- }
639
- var UI_MODULE_KEYS = [
640
- "admin_ui",
641
- "vendor_ui"
642
- ];
643
- function findNodeModulesRoot(configDir) {
644
- let dir = configDir;
645
- while (dir !== path5.dirname(dir)) {
646
- const candidate = path5.join(dir, "node_modules");
647
- if (fs4.existsSync(candidate) && fs4.statSync(candidate).isDirectory()) {
648
- return candidate;
649
- }
650
- dir = path5.dirname(dir);
651
- }
652
- return path5.join(configDir, "node_modules");
653
- }
654
- function resolvePluginRoot(resolve, configDir, nodeModulesRoot) {
655
- try {
656
- if (resolve.startsWith(".")) {
657
- const resolved = path5.resolve(configDir, resolve);
658
- if (fs4.existsSync(resolved)) {
659
- return fs4.realpathSync(resolved);
660
- }
661
- return null;
662
- }
663
- const packagePath = path5.join(nodeModulesRoot, resolve);
664
- if (!fs4.existsSync(packagePath)) {
665
- return null;
666
- }
667
- return fs4.realpathSync(packagePath);
668
- } catch {
669
- return null;
670
- }
671
- }
672
- function resolvePluginExtensions(plugins, configDir) {
673
- const nodeModulesRoot = findNodeModulesRoot(configDir);
674
- const extensions = [];
675
- const appTypes = ["admin", "vendor"];
676
- for (const plugin of plugins) {
677
- const resolve = typeof plugin === "string" ? plugin : plugin?.resolve;
678
- if (!resolve || typeof resolve !== "string") continue;
679
- const pluginRoot = resolvePluginRoot(resolve, configDir, nodeModulesRoot);
680
- if (!pluginRoot) continue;
681
- for (const appType of appTypes) {
682
- const extFile = path5.join(pluginRoot, ".medusa/server/src", appType, "index.js");
683
- if (fs4.existsSync(extFile)) {
684
- extensions.push(extFile);
685
- }
686
- }
687
- }
688
- return extensions;
689
- }
690
- async function loadMedusaConfig(medusaConfigPath, root) {
691
- try {
692
- const mod = await getFileExports(medusaConfigPath);
693
- const medusaConfig = mod.default ?? mod;
694
- const modules = medusaConfig?.modules ?? {};
695
- const configDir = path5.dirname(medusaConfigPath);
696
- let base;
697
- for (const key of UI_MODULE_KEYS) {
698
- const value = modules[key];
699
- if (!value || typeof value !== "object" || !value.options?.appDir) continue;
700
- const appDir = path5.resolve(configDir, value.options.appDir);
701
- if (appDir === root) {
702
- base = value.options.path;
703
- break;
704
- }
705
- }
706
- const plugins = medusaConfig?.plugins ?? [];
707
- const pluginExtensions = resolvePluginExtensions(plugins, configDir);
708
- return { base, pluginExtensions };
709
- } catch {
710
- return { pluginExtensions: [] };
711
- }
712
- }
713
- function mercurDashboardPlugin(pluginConfig) {
714
- let root;
715
- let config;
716
- return {
717
- name: "@mercurjs/dashboard-sdk",
718
- async config(viteConfig) {
719
- root = viteConfig.root || process.cwd();
720
- const medusaConfigPath = path5.resolve(root, pluginConfig.medusaConfigPath);
721
- const { base, pluginExtensions } = await loadMedusaConfig(medusaConfigPath, root);
722
- const srcDir = path5.join(root, "src");
723
- const backendUrl = pluginConfig.backendUrl ?? "http://localhost:9000";
724
- config = {
725
- ...pluginConfig,
726
- backendUrl,
727
- base,
728
- root,
729
- srcDir,
730
- pluginExtensions
731
- };
732
- return {
733
- base: config.base,
734
- define: {
735
- "__BACKEND_URL__": JSON.stringify(config.backendUrl),
736
- "__BASE__": JSON.stringify(config.base || "/")
737
- },
738
- optimizeDeps: {
739
- exclude: ["virtual:mercur/config", "virtual:mercur/routes", "virtual:mercur/components", "virtual:mercur/menu-items", "virtual:mercur/i18n"]
740
- }
741
- };
742
- },
743
- configResolved(resolvedConfig) {
744
- root = resolvedConfig.root;
745
- },
746
- resolveId(id) {
747
- if (isVirtualModule(id)) {
748
- return resolveVirtualModule(id);
749
- }
750
- return null;
751
- },
752
- load(id) {
753
- return loadVirtualModule({ cwd: root, id, mercurConfig: config });
754
- },
755
- configureServer(server) {
756
- const handlePageChange = (file) => {
757
- if (!isPageFile(file)) return;
758
- const mod = server.moduleGraph.getModuleById(RESOLVED_ROUTES_MODULE);
759
- if (mod) {
760
- server.moduleGraph.invalidateModule(mod);
761
- server.ws.send({ type: "full-reload" });
762
- }
763
- };
764
- server.watcher.on("add", handlePageChange);
765
- server.watcher.on("unlink", handlePageChange);
766
- },
767
- handleHotUpdate({ file, server }) {
768
- if (isPageFile(file)) {
769
- const mod = server.moduleGraph.getModuleById(RESOLVED_ROUTES_MODULE);
770
- if (mod) {
771
- server.moduleGraph.invalidateModule(mod);
772
- }
773
- }
774
- }
775
- };
776
- }
777
- export {
778
- mercurDashboardPlugin
779
- };