-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dashboard,admin-sdk,admin-shared,admin-vite-plugin): Add support…
… for UI extensions (#7383) * intial work * update lock * add routes and fix HMR of configs * cleanup * rm imports * rm debug from plugin * address feedback * address feedback
- Loading branch information
1 parent
521c252
commit f1176a0
Showing
50 changed files
with
1,359 additions
and
1,091 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import { defineRouteConfig, defineWidgetConfig } from "@medusajs/admin-shared" | ||
|
||
export { build } from "./lib/build" | ||
export { develop } from "./lib/develop" | ||
export { serve } from "./lib/serve" | ||
|
||
export { defineRouteConfig, defineWidgetConfig } | ||
|
||
export * from "./types" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
import type { InlineConfig } from "vite" | ||
import { BundlerOptions } from "../types" | ||
import { getViteConfig } from "./config" | ||
|
||
export async function build(options: BundlerOptions) { | ||
const vite = await import("vite") | ||
|
||
const viteConfig = await getViteConfig(options) | ||
|
||
try { | ||
await vite.build( | ||
vite.mergeConfig(viteConfig, { mode: "production", logLevel: "silent" }) | ||
) | ||
} catch (error) { | ||
console.error(error) | ||
throw new Error("Failed to build admin panel") | ||
const buildConfig: InlineConfig = { | ||
mode: "production", | ||
logLevel: "error", | ||
} | ||
|
||
await vite.build(vite.mergeConfig(viteConfig, buildConfig)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { AdminOptions } from "@medusajs/types" | ||
|
||
export type BundlerOptions = Required<Pick<AdminOptions, "outDir" | "path">> & | ||
Pick<AdminOptions, "vite" | "backendUrl"> | ||
Pick<AdminOptions, "vite" | "backendUrl"> & { | ||
sources?: string[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/admin-next/admin-shared/src/extensions/config/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./types" | ||
export * from "./utils" |
12 changes: 12 additions & 0 deletions
12
packages/admin-next/admin-shared/src/extensions/config/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { ComponentType } from "react" | ||
|
||
import { InjectionZone } from "../widgets" | ||
|
||
export type WidgetConfig = { | ||
zone: InjectionZone | InjectionZone[] | ||
} | ||
|
||
export type RouteConfig = { | ||
label?: string | ||
icon?: ComponentType | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/admin-next/admin-shared/src/extensions/config/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { RouteConfig, WidgetConfig } from "./types" | ||
|
||
function createConfigHelper<TConfig extends Record<string, unknown>>( | ||
config: TConfig | ||
): TConfig { | ||
return { | ||
...config, | ||
/** | ||
* This property is required to allow the config to be exported, | ||
* while still allowing HMR to work correctly. | ||
* | ||
* It tricks Fast Refresh into thinking that the config is a React component, | ||
* which allows it to be updated without a full page reload. | ||
*/ | ||
$$typeof: Symbol.for("react.memo"), | ||
} | ||
} | ||
|
||
/** | ||
* Define a widget configuration. | ||
* | ||
* @param config The widget configuration. | ||
* @returns The widget configuration. | ||
*/ | ||
export function defineWidgetConfig(config: WidgetConfig) { | ||
return createConfigHelper(config) | ||
} | ||
|
||
/** | ||
* Define a route configuration. | ||
* | ||
* @param config The route configuration. | ||
* @returns The route configuration. | ||
*/ | ||
export function defineRouteConfig(config: RouteConfig) { | ||
return createConfigHelper(config) | ||
} |
1 change: 1 addition & 0 deletions
1
packages/admin-next/admin-shared/src/extensions/routes/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const ROUTE_IMPORTS = ["routes/pages", "routes/links"] as const |
2 changes: 2 additions & 0 deletions
2
packages/admin-next/admin-shared/src/extensions/routes/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./constants" | ||
export * from "./types" |
3 changes: 3 additions & 0 deletions
3
packages/admin-next/admin-shared/src/extensions/routes/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ROUTE_IMPORTS } from "./constants" | ||
|
||
export type RouteImport = (typeof ROUTE_IMPORTS)[number] |
40 changes: 40 additions & 0 deletions
40
packages/admin-next/admin-shared/src/extensions/virtual/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ROUTE_IMPORTS } from "../routes" | ||
import { INJECTION_ZONES } from "../widgets" | ||
import { getVirtualId, getWidgetImport, resolveVirtualId } from "./utils" | ||
|
||
const VIRTUAL_WIDGET_MODULES = INJECTION_ZONES.map((zone) => { | ||
return getVirtualId(getWidgetImport(zone)) | ||
}) | ||
|
||
const VIRTUAL_ROUTE_MODULES = ROUTE_IMPORTS.map((route) => { | ||
return getVirtualId(route) | ||
}) | ||
|
||
/** | ||
* All virtual modules that are used in the admin panel. Virtual modules are used | ||
* to inject custom widgets, routes and settings. A virtual module is imported using | ||
* a string that corresponds to the id of the virtual module. | ||
* | ||
* @example | ||
* ```ts | ||
* import ProductDetailsBefore from "virtual:medusa/widgets/product/details/before" | ||
* ``` | ||
*/ | ||
export const VIRTUAL_MODULES = [ | ||
...VIRTUAL_WIDGET_MODULES, | ||
...VIRTUAL_ROUTE_MODULES, | ||
] | ||
|
||
/** | ||
* Reolved paths to all virtual widget modules. | ||
*/ | ||
export const RESOLVED_WIDGET_MODULES = VIRTUAL_WIDGET_MODULES.map((id) => { | ||
return resolveVirtualId(id) | ||
}) | ||
|
||
/** | ||
* Reolved paths to all virtual route modules. | ||
*/ | ||
export const RESOLVED_ROUTE_MODULES = VIRTUAL_ROUTE_MODULES.map((id) => { | ||
return resolveVirtualId(id) | ||
}) |
2 changes: 2 additions & 0 deletions
2
packages/admin-next/admin-shared/src/extensions/virtual/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./constants" | ||
export * from "./utils" |
25 changes: 25 additions & 0 deletions
25
packages/admin-next/admin-shared/src/extensions/virtual/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { InjectionZone } from "../widgets" | ||
|
||
const PREFIX = "virtual:medusa/" | ||
|
||
export const getVirtualId = (name: string) => { | ||
return `${PREFIX}${name}` | ||
} | ||
|
||
export const resolveVirtualId = (id: string) => { | ||
return `\0${id}` | ||
} | ||
|
||
export const getWidgetImport = (zone: InjectionZone) => { | ||
return `widgets/${zone.replace(/\./g, "/")}` | ||
} | ||
|
||
export const getWidgetZone = (resolvedId: string): InjectionZone => { | ||
const virtualPrefix = `\0${PREFIX}widgets/` | ||
|
||
const zone = resolvedId | ||
.replace(virtualPrefix, "") | ||
.replace(/\//g, ".") as InjectionZone | ||
|
||
return zone as InjectionZone | ||
} |
Oops, something went wrong.