Skip to content

Commit 4bc3f5b

Browse files
feat(medusa,framework,cli,admin-bundler): Integrate admin extensions into medusa build:plugin (medusajs#10941)
**What** Calls the `plugin` script from `@medusajs/admin-bundler` as part of `medusa plugin:build`.
1 parent 2a2b387 commit 4bc3f5b

File tree

8 files changed

+93
-18
lines changed

8 files changed

+93
-18
lines changed

Diff for: .changeset/eight-nails-act.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@medusajs/admin-bundler": patch
3+
"@medusajs/cli": patch
4+
"@medusajs/framework": patch
5+
"@medusajs/medusa": patch
6+
---
7+
8+
feat(medusa,admin-bundler,cli,framework): Integrate admin extensions into plugin build

Diff for: packages/admin/admin-bundler/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"@vitejs/plugin-react": "^4.2.1",
3333
"autoprefixer": "^10.4.16",
3434
"compression": "^1.7.4",
35-
"glob": "^11.0.0",
35+
"glob": "^10.3.10",
3636
"postcss": "^8.4.32",
3737
"tailwindcss": "^3.3.6",
3838
"vite": "^5.2.11"

Diff for: packages/admin/admin-bundler/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { build } from "./lib/build"
22
export { develop } from "./lib/develop"
3+
export { plugin } from "./lib/plugin"
34
export { serve } from "./lib/serve"
45

56
export * from "./types"

Diff for: packages/admin/admin-bundler/src/lib/plugin.ts

+51-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
1+
import react from "@vitejs/plugin-react"
12
import { readFileSync } from "fs"
3+
import { rm } from "fs/promises"
24
import { glob } from "glob"
35
import path from "path"
46
import { UserConfig } from "vite"
57

6-
export async function plugin() {
8+
interface PluginOptions {
9+
root: string
10+
outDir: string
11+
}
12+
13+
export async function plugin(options: PluginOptions) {
714
const vite = await import("vite")
8-
const entries = await glob("src/admin/**/*.{ts,tsx,js,jsx}")
15+
const entries = await glob(`${options.root}/src/admin/**/*.{ts,tsx,js,jsx}`)
16+
17+
/**
18+
* If there is no entry point, we can skip the build
19+
*/
20+
if (entries.length === 0) {
21+
return
22+
}
923

1024
const entryPoints = entries.reduce((acc, entry) => {
11-
// Convert src/admin/routes/brands/page.tsx -> admin/routes/brands/page
1225
const outPath = entry
1326
.replace(/^src\//, "")
1427
.replace(/\.(ts|tsx|js|jsx)$/, "")
1528

16-
acc[outPath] = path.resolve(process.cwd(), entry)
29+
acc[outPath] = path.resolve(options.root, entry)
1730
return acc
1831
}, {} as Record<string, string>)
1932

2033
const pkg = JSON.parse(
21-
readFileSync(path.resolve(process.cwd(), "package.json"), "utf-8")
34+
readFileSync(path.resolve(options.root, "package.json"), "utf-8")
2235
)
2336
const external = new Set([
2437
...Object.keys(pkg.dependencies || {}),
@@ -30,14 +43,22 @@ export async function plugin() {
3043
"@medusajs/admin-sdk",
3144
])
3245

46+
/**
47+
* We need to ensure that the NODE_ENV is set to production,
48+
* otherwise Vite will build the dev version of React.
49+
*/
50+
const originalNodeEnv = process.env.NODE_ENV
51+
process.env.NODE_ENV = "production"
52+
3353
const pluginConfig: UserConfig = {
3454
build: {
3555
lib: {
3656
entry: entryPoints,
3757
formats: ["es"],
3858
},
59+
emptyOutDir: false,
3960
minify: false,
40-
outDir: path.resolve(process.cwd(), "dist"),
61+
outDir: path.resolve(options.root, options.outDir),
4162
rollupOptions: {
4263
external: [...external],
4364
output: {
@@ -47,11 +68,34 @@ export async function plugin() {
4768
"react/jsx-runtime": "react/jsx-runtime",
4869
},
4970
preserveModules: true,
50-
entryFileNames: `[name].js`,
71+
entryFileNames: (chunkInfo) => {
72+
return `${chunkInfo.name.replace(`${options.root}/`, "")}.js`
73+
},
5174
},
5275
},
5376
},
77+
plugins: [
78+
react(),
79+
{
80+
name: "clear-admin-plugin",
81+
buildStart: async () => {
82+
const adminDir = path.join(options.root, options.outDir, "admin")
83+
try {
84+
await rm(adminDir, { recursive: true, force: true })
85+
} catch (e) {
86+
// Directory might not exist, ignore
87+
}
88+
},
89+
},
90+
],
91+
logLevel: "silent",
92+
clearScreen: false,
5493
}
5594

5695
await vite.build(pluginConfig)
96+
97+
/**
98+
* Restore the original NODE_ENV
99+
*/
100+
process.env.NODE_ENV = originalNodeEnv
57101
}

Diff for: packages/cli/medusa-cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"express": "^4.21.0",
5757
"fs-exists-cached": "^1.0.0",
5858
"fs-extra": "^10.0.0",
59-
"glob": "^7.1.6",
59+
"glob": "^10.3.10",
6060
"hosted-git-info": "^4.0.2",
6161
"inquirer": "^8.0.0",
6262
"is-valid-path": "^0.1.1",

Diff for: packages/core/framework/src/build-tools/compiler.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import path from "path"
2-
import type tsStatic from "typescript"
1+
import type { AdminOptions, ConfigModule, Logger } from "@medusajs/types"
32
import { getConfigFile } from "@medusajs/utils"
43
import { access, constants, copyFile, rm } from "fs/promises"
5-
import type { AdminOptions, ConfigModule, Logger } from "@medusajs/types"
4+
import path from "path"
5+
import type tsStatic from "typescript"
66

77
/**
88
* The compiler exposes the opinionated APIs for compiling Medusa
@@ -486,4 +486,25 @@ export class Compiler {
486486

487487
ts.createWatchProgram(host)
488488
}
489+
490+
async buildPluginAdminExtensions(bundler: {
491+
plugin: (options: { root: string; outDir: string }) => Promise<void>
492+
}) {
493+
const tracker = this.#trackDuration()
494+
this.#logger.info("Compiling plugin admin extensions...")
495+
496+
try {
497+
await bundler.plugin({
498+
root: this.#projectRoot,
499+
outDir: this.#pluginsDistFolder,
500+
})
501+
this.#logger.info(
502+
`Plugin admin extensions build completed successfully (${tracker.getSeconds()}s)`
503+
)
504+
return true
505+
} catch (error) {
506+
this.#logger.error(`Plugin admin extensions build failed`, error)
507+
return false
508+
}
509+
}
489510
}

Diff for: packages/medusa/src/commands/plugin/build.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { logger } from "@medusajs/framework/logger"
1+
import { plugin } from "@medusajs/admin-bundler"
22
import { Compiler } from "@medusajs/framework/build-tools"
3-
3+
import { logger } from "@medusajs/framework/logger"
44
export default async function build({
55
directory,
6-
adminOnly,
76
}: {
87
directory: string
9-
adminOnly: boolean
108
}): Promise<boolean> {
119
logger.info("Starting build...")
1210
const compiler = new Compiler(directory, logger)
@@ -18,5 +16,8 @@ export default async function build({
1816
}
1917

2018
await compiler.buildPluginBackend(tsConfig)
19+
await compiler.buildPluginAdminExtensions({
20+
plugin,
21+
})
2122
return true
2223
}

Diff for: yarn.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -5252,7 +5252,7 @@ __metadata:
52525252
compression: ^1.7.4
52535253
copyfiles: ^2.4.1
52545254
express: ^4.21.0
5255-
glob: ^11.0.0
5255+
glob: ^10.3.10
52565256
postcss: ^8.4.32
52575257
tailwindcss: ^3.3.6
52585258
tsup: ^8.0.1
@@ -5486,7 +5486,7 @@ __metadata:
54865486
express: ^4.21.0
54875487
fs-exists-cached: ^1.0.0
54885488
fs-extra: ^10.0.0
5489-
glob: ^7.1.6
5489+
glob: ^10.3.10
54905490
hosted-git-info: ^4.0.2
54915491
inquirer: ^8.0.0
54925492
is-valid-path: ^0.1.1

0 commit comments

Comments
 (0)