-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(dashboard,admin-sdk,admin-shared,admin-vite-plugin): Add support for UI extensions #7383
Changes from all commits
253cf97
b45856b
152525e
7cd5f16
cb2a136
d42c58e
5129878
48d486d
d4000f8
f368085
0ec95f0
9ecc98b
b4724b8
b95168f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" |
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)) | ||
} |
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[] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,17 @@ | |
"author": "Kasper Kristensen <kasper@medusajs.com>", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the timing is not the best, but I feel like we can do better with the naming. To me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree that the naming can be better, and something we need to revisit, but would hold off on it to post RC, as it requires we update the packages a couple of different places. |
||
"types": "dist/index.d.ts", | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"files": [ | ||
"dist", | ||
"package.json" | ||
], | ||
"scripts": { | ||
"build": "tsc" | ||
"build": "tsup" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "^18.3.2", | ||
"tsup": "^8.0.2", | ||
"typescript": "^5.3.3" | ||
}, | ||
"packageManager": "yarn@3.2.1" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./types" | ||
export * from "./utils" |
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 | ||
} |
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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const ROUTE_IMPORTS = ["routes/pages", "routes/links"] as const |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./constants" | ||
export * from "./types" |
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] |
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) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./constants" | ||
export * from "./utils" |
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we pull the compression settings from the config (I see we have some there)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we might want to have a separate compression config in
projectConfig.admin
that we could pass here, as I don't necessarily think the compressions options you want generally for your server === the compression you want for the admin dashboard. But I think we can leave that for later, the default compression options seems to work great for this use case, but we should definetly open them up to people if they want to modify it for whatever reason.