The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
fix(dashboard-sdk): resolve plugin roots via Node, and stop emitting top-level await (#1429)
Viktor Holika1b8c1648b65
diff --git a/packages/dashboard-sdk/src/menu-items.ts b/packages/dashboard-sdk/src/menu-items.tsindex e1c180176..f45e46053 100644--- a/packages/dashboard-sdk/src/menu-items.ts+++ b/packages/dashboard-sdk/src/menu-items.ts@@ -303,9 +303,10 @@ export function generateMenuItems({ srcDir, pluginExtensions }: BuiltMercurConfi}}- // Plugin extensions — dynamic import to resolve .default+ // Plugin extensions. Static rather than `await import(...)`: a top-level+ // await here would force every consuming app to raise its build target.const pluginDeclarations = pluginExtensions.map((ext, i) =>- `const __plugin${i} = (await import("${normalizePath(ext)}")).default`+ `import __plugin${i} from "${normalizePath(ext)}"`)const pluginSpreads = pluginExtensions.map((_, i) =>` ...(__plugin${i}.menuItemModule?.menuItems ?? [])`diff --git a/packages/dashboard-sdk/src/plugin.ts b/packages/dashboard-sdk/src/plugin.tsindex c77873f37..2ceb9eca4 100644--- a/packages/dashboard-sdk/src/plugin.ts+++ b/packages/dashboard-sdk/src/plugin.ts@@ -103,23 +103,45 @@ function resolvePluginRoot(configDir: string,nodeModulesRoot: string,): string | null {- try {- if (resolve.startsWith(".")) {+ if (resolve.startsWith(".")) {+ try {const resolved = path.resolve(configDir, resolve);- if (fs.existsSync(resolved)) {- return fs.realpathSync(resolved);- }+ return fs.existsSync(resolved) ? fs.realpathSync(resolved) : null;+ } catch {return null;}+ }- // Check in node_modules, following symlinks for workspace packages- const packagePath = path.join(nodeModulesRoot, resolve);- if (!fs.existsSync(packagePath)) {- return null;+ // Ask Node how it would resolve the specifier rather than guessing a single+ // directory. `findNodeModulesRoot` returns the *nearest* node_modules, which+ // in a monorepo is the app's own — holding only that app's dependencies —+ // while workspace symlinks live at the repo root. Node walks every level.+ const require = createRequire(path.join(configDir, "noop.js"));++ try {+ return fs.realpathSync(+ path.dirname(require.resolve(`${resolve}/package.json`)),+ );+ } catch {+ // `exports` may not expose package.json; fall back to the entry point+ // and walk up to the directory that owns it.+ }++ try {+ let dir = path.dirname(require.resolve(resolve));+ while (dir !== path.dirname(dir)) {+ if (fs.existsSync(path.join(dir, "package.json"))) {+ return fs.realpathSync(dir);+ }+ dir = path.dirname(dir);}+ } catch {+ // Not resolvable from the app — fall through to the directory probe.+ }- // Follow symlinks (handles workspace/linked packages)- return fs.realpathSync(packagePath);+ try {+ const packagePath = path.join(nodeModulesRoot, resolve);+ return fs.existsSync(packagePath) ? fs.realpathSync(packagePath) : null;} catch {return null;}@@ -135,7 +157,15 @@ function resolvePluginExtensions(plugins: any[], configDir: string, appType: "adif (!resolve || typeof resolve !== "string") continue;const pluginRoot = resolvePluginRoot(resolve, configDir, nodeModulesRoot);- if (!pluginRoot) continue;+ if (!pluginRoot) {+ // Silently skipping leaves the dashboard missing whatever the plugin+ // contributes, with a build that still looks clean.+ console.warn(+ `[@mercurjs/dashboard-sdk] Could not resolve plugin "${resolve}" from ${configDir}. ` ++ `Its ${appType} extensions will be missing from the build.`,+ );+ continue;+ }const extFile = path.join(pluginRoot,diff --git a/packages/dashboard-sdk/src/routes.ts b/packages/dashboard-sdk/src/routes.tsindex feb6cf1c2..70a1de079 100644--- a/packages/dashboard-sdk/src/routes.ts+++ b/packages/dashboard-sdk/src/routes.ts@@ -292,9 +292,10 @@ export function generateRoutes({ srcDir, pluginExtensions }: BuiltMercurConfig):}}- // Plugin extensions — dynamic import to resolve .default+ // Plugin extensions. Static rather than `await import(...)`: a top-level+ // await here would force every consuming app to raise its build target.const pluginDeclarations = pluginExtensions.map((ext, i) =>- `const __plugin${i} = (await import("${normalizePath(ext)}")).default`+ `import __plugin${i} from "${normalizePath(ext)}"`)const pluginSpreads = pluginExtensions.map((_, i) =>` ...(__plugin${i}.routeModule?.routes ?? [])`diff --git a/packages/dashboard-sdk/src/widgets.ts b/packages/dashboard-sdk/src/widgets.tsindex bef46105e..4ff3784e3 100644--- a/packages/dashboard-sdk/src/widgets.ts+++ b/packages/dashboard-sdk/src/widgets.ts@@ -177,7 +177,7 @@ export function generateWidgets({const pluginDeclarations = pluginExtensions.map((ext, i) =>- `const __plugin${i} = (await import("${normalizePath(ext)}")).default`+ `import __plugin${i} from "${normalizePath(ext)}"`)const pluginSpreads = pluginExtensions.map((_, i) => ` ...(__plugin${i}.widgetModule?.widgets ?? [])`