Files
medusa-store/packages/admin-ui/src/utils/get-custom-vite-config.ts
Kasper Fabricius Kristensen 8a7421db5b feat(admin): Improve DX for deploying admin externally (#3418)
* init deploy command

* add include flag

* add 'shortcut' flag

* add dev command, fix var replacement, change default behaviour

* cleanup params of build command

* fix defaults when using the plugin to serve admin

* add changeset

* fix globals

* update README

* throw error on no build found

---------

Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
2023-03-17 13:18:51 +01:00

77 lines
1.9 KiB
TypeScript

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 = () => {
let backend = undefined
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).`
)
}
}
const global = {}
global["__BASE__"] = JSON.stringify(globals.base ? `/${globals.base}` : "/")
global["__MEDUSA_BACKEND_URL__"] = JSON.stringify(backend ? backend : "/")
return global
}
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(process.cwd(), "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",
}
}