chore: Update admin build/serve configuration (#9584)
**Breaking changes** The `outDir` has been deprecated and wont be used anymore, instead all the path are computed internally following these rules - if admin is not `disabled` and the `build` command is run without the `--admin-only` flag, then the admin output dir will be `.medusa/server/public/admin` and it will be served from that same location from the medusa instance. - if admin is not `disabled` and the `build` command is run with the `--admin-only` flag, then the admin output dir will be `.medusa/admin` with the purpose of deploying the admin separately. ⚠️ (expect to receive a warning log) - if the admin is `disabled` and the `build` command is run with the `--admin-only` flag, then fallback to rule number 2 | admin enabled | medusa build --admin-only | output dir | |---|---|---| | true | true | `.medusa/admin` ⚠️ (expect to receive a warning log) | | true | false | `.medusa/server/public/admin` | | false | true | `.medusa/admin` | | false | false | none | ```diff // medusa-config.ts { // ... admin: { - outDir: 'some/path' } } ``` cc @kasperkristensen @sradevski @olivermrbl
This commit is contained in:
@@ -4,10 +4,35 @@ import type tsStatic from "typescript"
|
||||
import { logger } from "@medusajs/framework/logger"
|
||||
import { ConfigModule } from "@medusajs/framework/types"
|
||||
import { getConfigFile } from "@medusajs/framework/utils"
|
||||
import {
|
||||
ADMIN_ONLY_OUTPUT_DIR,
|
||||
ADMIN_RELATIVE_OUTPUT_DIR,
|
||||
ADMIN_SOURCE_DIR,
|
||||
} from "../utils"
|
||||
|
||||
const ADMIN_FOLDER = "src/admin"
|
||||
const INTEGRATION_TESTS_FOLDER = "integration-tests"
|
||||
|
||||
function computeDist(
|
||||
projectRoot: string,
|
||||
tsConfig: { options: { outDir?: string } }
|
||||
): string {
|
||||
const distFolder = tsConfig.options.outDir ?? ".medusa/server"
|
||||
return path.isAbsolute(distFolder)
|
||||
? distFolder
|
||||
: path.join(projectRoot, distFolder)
|
||||
}
|
||||
|
||||
async function loadTsConfig(projectRoot: string) {
|
||||
const ts = await import("typescript")
|
||||
const tsConfig = parseTSConfig(projectRoot, ts)
|
||||
if (!tsConfig) {
|
||||
logger.error("Unable to compile backend source")
|
||||
return false
|
||||
}
|
||||
|
||||
return tsConfig!
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the file to the destination without throwing any
|
||||
* errors if the source file is missing
|
||||
@@ -98,21 +123,14 @@ function parseTSConfig(projectRoot: string, ts: typeof tsStatic) {
|
||||
/**
|
||||
* Builds the backend project using TSC
|
||||
*/
|
||||
async function buildBackend(projectRoot: string): Promise<boolean> {
|
||||
async function buildBackend(
|
||||
projectRoot: string,
|
||||
tsConfig: tsStatic.ParsedCommandLine
|
||||
): Promise<boolean> {
|
||||
const startTime = process.hrtime()
|
||||
logger.info("Compiling backend source...")
|
||||
|
||||
const ts = await import("typescript")
|
||||
const tsConfig = parseTSConfig(projectRoot, ts)
|
||||
if (!tsConfig) {
|
||||
logger.error("Unable to compile backend source")
|
||||
return false
|
||||
}
|
||||
|
||||
const distFolder = tsConfig.options.outDir ?? ".medusa/server"
|
||||
const dist = path.isAbsolute(distFolder)
|
||||
? distFolder
|
||||
: path.join(projectRoot, distFolder)
|
||||
const dist = computeDist(projectRoot, tsConfig)
|
||||
|
||||
logger.info(`Removing existing "${path.relative(projectRoot, dist)}" folder`)
|
||||
await clean(dist)
|
||||
@@ -123,11 +141,12 @@ async function buildBackend(projectRoot: string): Promise<boolean> {
|
||||
*/
|
||||
const filesToCompile = tsConfig.fileNames.filter((fileName) => {
|
||||
return (
|
||||
!fileName.includes(`${ADMIN_FOLDER}/`) &&
|
||||
!fileName.includes(`${ADMIN_SOURCE_DIR}/`) &&
|
||||
!fileName.includes(`${INTEGRATION_TESTS_FOLDER}/`)
|
||||
)
|
||||
})
|
||||
|
||||
const ts = await import("typescript")
|
||||
const program = ts.createProgram(filesToCompile, {
|
||||
...tsConfig.options,
|
||||
...{
|
||||
@@ -195,24 +214,41 @@ async function buildBackend(projectRoot: string): Promise<boolean> {
|
||||
/**
|
||||
* Builds the frontend project using the "@medusajs/admin-bundler"
|
||||
*/
|
||||
async function buildFrontend(projectRoot: string): Promise<boolean> {
|
||||
async function buildFrontend(
|
||||
projectRoot: string,
|
||||
adminOnly: boolean,
|
||||
tsConfig: tsStatic.ParsedCommandLine
|
||||
): Promise<boolean> {
|
||||
const startTime = process.hrtime()
|
||||
const configFile = await loadMedusaConfig(projectRoot)
|
||||
if (!configFile) {
|
||||
return false
|
||||
}
|
||||
|
||||
const adminSource = path.join(projectRoot, ADMIN_FOLDER)
|
||||
const dist = computeDist(projectRoot, tsConfig)
|
||||
|
||||
const adminOutputPath = adminOnly
|
||||
? path.join(projectRoot, ADMIN_ONLY_OUTPUT_DIR)
|
||||
: path.join(dist, ADMIN_RELATIVE_OUTPUT_DIR)
|
||||
|
||||
const adminSource = path.join(projectRoot, ADMIN_SOURCE_DIR)
|
||||
const adminOptions = {
|
||||
disable: false,
|
||||
sources: [adminSource],
|
||||
...configFile.configModule.admin,
|
||||
outDir: adminOutputPath,
|
||||
}
|
||||
|
||||
if (adminOptions.disable) {
|
||||
if (adminOptions.disable && !adminOnly) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (!adminOptions.disable && adminOnly) {
|
||||
logger.warn(
|
||||
`You are building using the flag --admin-only but the admin is enabled in your medusa-config, If you intend to host the dashboard separately you should disable the admin in your medusa config`
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
logger.info("Compiling frontend source...")
|
||||
const { build: buildProductionBuild } = await import(
|
||||
@@ -231,7 +267,28 @@ async function buildFrontend(projectRoot: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
export default async function ({ directory }: { directory: string }) {
|
||||
export default async function ({
|
||||
directory,
|
||||
adminOnly,
|
||||
}: {
|
||||
directory: string
|
||||
adminOnly: boolean
|
||||
}): Promise<boolean> {
|
||||
logger.info("Starting build...")
|
||||
await Promise.all([buildBackend(directory), buildFrontend(directory)])
|
||||
|
||||
const tsConfig = await loadTsConfig(directory)
|
||||
if (!tsConfig) {
|
||||
return false
|
||||
}
|
||||
|
||||
const promises: Promise<any>[] = []
|
||||
|
||||
if (!adminOnly) {
|
||||
promises.push(buildBackend(directory, tsConfig))
|
||||
}
|
||||
|
||||
promises.push(buildFrontend(directory, adminOnly, tsConfig))
|
||||
|
||||
await Promise.all(promises)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { AdminOptions, ConfigModule } from "@medusajs/framework/types"
|
||||
import { Express } from "express"
|
||||
import fs from "fs"
|
||||
import path from "path"
|
||||
import { ADMIN_RELATIVE_OUTPUT_DIR } from "../utils"
|
||||
|
||||
type Options = {
|
||||
app: Express
|
||||
@@ -10,10 +11,9 @@ type Options = {
|
||||
rootDirectory: string
|
||||
}
|
||||
|
||||
type IntializedOptions = Required<
|
||||
Pick<AdminOptions, "path" | "disable" | "outDir">
|
||||
> &
|
||||
type IntializedOptions = Required<Pick<AdminOptions, "path" | "disable">> &
|
||||
AdminOptions & {
|
||||
outDir: string
|
||||
sources?: string[]
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ export default async function adminLoader({
|
||||
disable: false,
|
||||
sources,
|
||||
...admin,
|
||||
outDir: path.join(rootDirectory, ADMIN_RELATIVE_OUTPUT_DIR),
|
||||
}
|
||||
|
||||
if (adminOptions?.disable) {
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export const ADMIN_SOURCE_DIR = "src/admin"
|
||||
export const ADMIN_RELATIVE_OUTPUT_DIR = "./public/admin"
|
||||
export const ADMIN_ONLY_OUTPUT_DIR = ".medusa/admin"
|
||||
@@ -2,3 +2,4 @@ export * from "./clean-response-data"
|
||||
export * from "./exception-formatter"
|
||||
export * from "./middlewares"
|
||||
export * from "./define-middlewares"
|
||||
export * from "./admin-consts"
|
||||
|
||||
Reference in New Issue
Block a user