feat(admin,admin-ui,medusa): Add Medusa Admin plugin (#3334)
This commit is contained in:
committed by
GitHub
parent
d6b1ad1ccd
commit
40de54b010
5
packages/admin-ui/src/utils/format-base.ts
Normal file
5
packages/admin-ui/src/utils/format-base.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { Base } from "../types"
|
||||
|
||||
export const formatBase = <T extends string>(base: T): Base<T> => {
|
||||
return `/${base}/`
|
||||
}
|
||||
76
packages/admin-ui/src/utils/get-custom-vite-config.ts
Normal file
76
packages/admin-ui/src/utils/get-custom-vite-config.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import react from "@vitejs/plugin-react"
|
||||
import { resolve } from "path"
|
||||
import { BuildOptions, InlineConfig } from "vite"
|
||||
import { AdminBuildConfig } from "../types"
|
||||
import { formatBase } from "./format-base"
|
||||
|
||||
export const getCustomViteConfig = (config: AdminBuildConfig): InlineConfig => {
|
||||
const { globals = {}, build = {} } = config
|
||||
|
||||
const uiPath = resolve(__dirname, "..", "..", "ui")
|
||||
|
||||
const globalReplacements = () => {
|
||||
const base = globals.base || "app"
|
||||
|
||||
let backend = "/"
|
||||
|
||||
if (globals.backend) {
|
||||
try {
|
||||
// Test if the backend is a valid URL
|
||||
new URL(globals.backend)
|
||||
backend = globals.backend
|
||||
} catch (_e) {
|
||||
throw new Error(
|
||||
`The provided backend URL is not valid: ${globals.backend}. Please provide a valid URL (e.g. https://my-medusa-server.com).`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
__BASE__: JSON.stringify(`/${base}`),
|
||||
__MEDUSA_BACKEND_URL__: JSON.stringify(backend),
|
||||
}
|
||||
}
|
||||
|
||||
const buildConfig = (): BuildOptions => {
|
||||
const { outDir } = build
|
||||
|
||||
let destDir: string
|
||||
|
||||
if (!outDir) {
|
||||
/**
|
||||
* Default build directory is at the root of the `@medusajs/admin-ui` package.
|
||||
*/
|
||||
destDir = resolve(__dirname, "..", "..", "build")
|
||||
} else {
|
||||
/**
|
||||
* If a custom build directory is specified, it is resolved relative to the
|
||||
* current working directory.
|
||||
*/
|
||||
destDir = resolve(process.cwd(), outDir)
|
||||
}
|
||||
|
||||
return {
|
||||
outDir: destDir,
|
||||
emptyOutDir: true,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
plugins: [react()],
|
||||
root: uiPath,
|
||||
mode: "production",
|
||||
base: formatBase(globals.base),
|
||||
define: globalReplacements(),
|
||||
build: buildConfig(),
|
||||
resolve: {
|
||||
alias: {
|
||||
"@tanstack/react-query": resolve(
|
||||
require.resolve("@tanstack/react-query")
|
||||
),
|
||||
},
|
||||
},
|
||||
clearScreen: false,
|
||||
logLevel: "error",
|
||||
}
|
||||
}
|
||||
2
packages/admin-ui/src/utils/index.ts
Normal file
2
packages/admin-ui/src/utils/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./format-base"
|
||||
export * from "./get-custom-vite-config"
|
||||
Reference in New Issue
Block a user