Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/admin-watch-patch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/medusa": patch
"@medusajs/framework": patch
---

fix(medusa, framework): rebuild plugin admin extensions in develop mode
92 changes: 92 additions & 0 deletions packages/core/framework/src/build-tools/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,28 @@ export class Compiler {
})
}

async #hasPluginAdminExtensions() {
try {
await access(path.join(this.#projectRoot, "src/admin"), constants.F_OK)
return true
} catch (error) {
if (error.code !== "ENOENT") {
throw error
}
return false
}
}

async buildPluginAdminExtensions(bundler: {
plugin: (options: { root: string; outDir: string }) => Promise<void>
}) {
if (!(await this.#hasPluginAdminExtensions())) {
this.#logger.info(
"Skipping plugin admin extensions build, since src/admin does not exist"
)
return true
}

const tracker = this.#trackDuration()
this.#logger.info("Compiling plugin admin extensions...")

Expand All @@ -564,4 +583,77 @@ export class Compiler {
return false
}
}

async developPluginAdminExtensions(
bundler: {
plugin: (options: { root: string; outDir: string }) => Promise<void>
},
onFileChange?: (
filePath: string,
action: "add" | "change" | "unlink"
) => void
) {
let isBuilding = false
let hasQueuedBuild = false
let latestQueuedFile: string | undefined
let latestQueuedAction: "add" | "change" | "unlink" | undefined

const rebuild = async (
file: string,
action: "add" | "change" | "unlink"
) => {
if (isBuilding) {
hasQueuedBuild = true
latestQueuedFile = file
latestQueuedAction = action
return
}

let currentFile = file
let currentAction = action

do {
hasQueuedBuild = false
latestQueuedFile = undefined
latestQueuedAction = undefined
isBuilding = true

this.#logger.info(
`${currentFile} updated: Rebuilding admin extensions`
)
const buildSucceeded = await this.buildPluginAdminExtensions(bundler)

isBuilding = false

if (buildSucceeded) {
onFileChange?.(currentFile, currentAction)
}

if (hasQueuedBuild && latestQueuedFile && latestQueuedAction) {
currentFile = latestQueuedFile
currentAction = latestQueuedAction
}
} while (hasQueuedBuild)
}

const watcher = chokidar.watch(["src/admin"], {
ignoreInitial: true,
cwd: this.#projectRoot,
ignored: [/node_modules/, /(^|[\\/\\])\../, ".medusa"],
})

watcher.on("add", (file) => {
void rebuild(file, "add")
})
watcher.on("change", (file) => {
void rebuild(file, "change")
})
watcher.on("unlink", (file) => {
void rebuild(file, "unlink")
})

watcher.on("ready", () => {
this.#logger.info("watching for plugin admin extension file changes")
})
}
}
4 changes: 4 additions & 0 deletions packages/medusa/src/commands/plugin/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,9 @@ export default async function developPlugin({
}

await compiler.buildPluginBackend(parsedConfig)
const adminBundler = await import("@medusajs/admin-bundler")
await compiler.buildPluginAdminExtensions(adminBundler)

await compiler.developPluginBackend(transformFile, publishChanges)
await compiler.developPluginAdminExtensions(adminBundler, publishChanges)
}
Loading