feat(medusa,framework,cli,admin-bundler): Integrate admin extensions into medusa build:plugin (#10941)

**What**
Calls the `plugin` script from `@medusajs/admin-bundler` as part of `medusa plugin:build`.
This commit is contained in:
Kasper Fabricius Kristensen
2025-01-13 21:48:55 +01:00
committed by GitHub
parent 2a2b387bc3
commit 4bc3f5b845
8 changed files with 93 additions and 18 deletions
@@ -1,5 +1,6 @@
export { build } from "./lib/build"
export { develop } from "./lib/develop"
export { plugin } from "./lib/plugin"
export { serve } from "./lib/serve"
export * from "./types"
+51 -7
View File
@@ -1,24 +1,37 @@
import react from "@vitejs/plugin-react"
import { readFileSync } from "fs"
import { rm } from "fs/promises"
import { glob } from "glob"
import path from "path"
import { UserConfig } from "vite"
export async function plugin() {
interface PluginOptions {
root: string
outDir: string
}
export async function plugin(options: PluginOptions) {
const vite = await import("vite")
const entries = await glob("src/admin/**/*.{ts,tsx,js,jsx}")
const entries = await glob(`${options.root}/src/admin/**/*.{ts,tsx,js,jsx}`)
/**
* If there is no entry point, we can skip the build
*/
if (entries.length === 0) {
return
}
const entryPoints = entries.reduce((acc, entry) => {
// Convert src/admin/routes/brands/page.tsx -> admin/routes/brands/page
const outPath = entry
.replace(/^src\//, "")
.replace(/\.(ts|tsx|js|jsx)$/, "")
acc[outPath] = path.resolve(process.cwd(), entry)
acc[outPath] = path.resolve(options.root, entry)
return acc
}, {} as Record<string, string>)
const pkg = JSON.parse(
readFileSync(path.resolve(process.cwd(), "package.json"), "utf-8")
readFileSync(path.resolve(options.root, "package.json"), "utf-8")
)
const external = new Set([
...Object.keys(pkg.dependencies || {}),
@@ -30,14 +43,22 @@ export async function plugin() {
"@medusajs/admin-sdk",
])
/**
* We need to ensure that the NODE_ENV is set to production,
* otherwise Vite will build the dev version of React.
*/
const originalNodeEnv = process.env.NODE_ENV
process.env.NODE_ENV = "production"
const pluginConfig: UserConfig = {
build: {
lib: {
entry: entryPoints,
formats: ["es"],
},
emptyOutDir: false,
minify: false,
outDir: path.resolve(process.cwd(), "dist"),
outDir: path.resolve(options.root, options.outDir),
rollupOptions: {
external: [...external],
output: {
@@ -47,11 +68,34 @@ export async function plugin() {
"react/jsx-runtime": "react/jsx-runtime",
},
preserveModules: true,
entryFileNames: `[name].js`,
entryFileNames: (chunkInfo) => {
return `${chunkInfo.name.replace(`${options.root}/`, "")}.js`
},
},
},
},
plugins: [
react(),
{
name: "clear-admin-plugin",
buildStart: async () => {
const adminDir = path.join(options.root, options.outDir, "admin")
try {
await rm(adminDir, { recursive: true, force: true })
} catch (e) {
// Directory might not exist, ignore
}
},
},
],
logLevel: "silent",
clearScreen: false,
}
await vite.build(pluginConfig)
/**
* Restore the original NODE_ENV
*/
process.env.NODE_ENV = originalNodeEnv
}