feat(dashboard,admin-vite-plugin): Add product zones and fix zone change effect (#7427)

**What**
- Adds injection zones to the product domain.
- Fixes an issue where changing the `zone` in a widget config to another valid widget would not trigger a HMR event.
- Fixes an issue where UI Routes would not work in production.
This commit is contained in:
Kasper Fabricius Kristensen
2024-05-23 21:13:42 +02:00
committed by GitHub
parent 8a070d5d85
commit 6ec6e2c7b6
6 changed files with 114 additions and 59 deletions

View File

@@ -424,8 +424,7 @@ function createRoutePath(file: string) {
async function generateRouteEntrypoint(
sources: Set<string>,
type: "page" | "link",
base = ""
type: "page" | "link"
) {
const files = (
await Promise.all(
@@ -464,7 +463,7 @@ async function generateRouteEntrypoint(
${type}s: [${validatedRoutes
.map((file, index) => {
return type === "page"
? `{ path: "${createRoutePath(file)}", file: "${base + file}" }`
? `{ path: "${createRoutePath(file)}", Component: RouteExt${index} }`
: `{ path: "${createRoutePath(file)}", ...routeConfig${index} }`
})
.join(", ")}],
@@ -496,7 +495,6 @@ export type MedusaVitePlugin = (config?: MedusaVitePluginOptions) => Vite.Plugin
export const medusaVitePlugin: MedusaVitePlugin = (options) => {
const _extensionGraph = new Map<string, Set<string>>()
const _sources = new Set<string>(options?.sources ?? [])
let _base = ""
let server: Vite.ViteDevServer | undefined
let watcher: Vite.FSWatcher | undefined
@@ -507,7 +505,7 @@ export const medusaVitePlugin: MedusaVitePlugin = (options) => {
return await generateWidgetEntrypoint(_sources, options.get)
}
case "route":
return await generateRouteEntrypoint(_sources, options.get, _base)
return await generateRouteEntrypoint(_sources, options.get)
default:
return null
}
@@ -580,6 +578,55 @@ export const medusaVitePlugin: MedusaVitePlugin = (options) => {
_extensionGraph.set(file, imports)
}
if (_extensionGraph.has(file)) {
const modules = _extensionGraph.get(file)
if (!modules) {
return
}
for (const moduleId of modules) {
const module = server?.moduleGraph.getModuleById(moduleId)
if (!module || !module.id) {
continue
}
const matchedInjectionZone = getWidgetZone(module.id)
/**
* If the widget is imported in a module that does not match the new
* zone value, we need to reload the module, so the widget will be removed.
*/
if (!zoneValues.includes(matchedInjectionZone)) {
modules.delete(moduleId)
await server?.reloadModule(module)
}
}
const imports = new Set<string>(modules)
/**
* If the widget is not currently being imported by the virtual module that
* matches its zone value, we need to reload the module, so the widget will be added.
*/
for (const zoneValue of zoneValues) {
const zonePath = getWidgetImport(zoneValue)
const moduleId = getVirtualId(zonePath)
const resolvedModuleId = resolveVirtualId(moduleId)
if (!modules.has(resolvedModuleId)) {
const module = server?.moduleGraph.getModuleById(resolvedModuleId)
if (module) {
imports.add(resolvedModuleId)
await server?.reloadModule(module)
}
}
}
_extensionGraph.set(file, imports)
}
}
if (event === "add") {
@@ -732,16 +779,6 @@ export const medusaVitePlugin: MedusaVitePlugin = (options) => {
return {
name: "@medusajs/admin-vite-plugin",
enforce: "pre",
configResolved(config) {
if (config.server?.middlewareMode) {
/**
* If we are in middleware mode, we need to set the base to the <base> + "@fs".
*
* This ensures that the page components are lazy-loaded correctly.
*/
_base = `${config.base}@fs`
}
},
configureServer(_server) {
server = _server
watcher = _server.watcher