Skip to content

Commit

Permalink
feat: add plugin build command (medusajs#10935)
Browse files Browse the repository at this point in the history
Fixes: FRMW-2863

Adds the `plugin:build` command that is used to compile the source code of a plugin for publishing it to a package registry. The command is similar to the `build` command, except it does not copy the `package.json` and the `lock` files to the build output
  • Loading branch information
thetutlage authored Jan 13, 2025
1 parent b0cfb05 commit b0f581c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/calm-cameras-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@medusajs/medusa": patch
"@medusajs/framework": patch
"@medusajs/cli": patch
---

feat: add plugin build command
12 changes: 11 additions & 1 deletion packages/cli/medusa-cli/src/create-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,20 @@ function buildLocalCommands(cli, isLocalProject) {
})
),
})
.command({
command: "plugin:build",
desc: "Build plugin source for publishing to a package registry",
handler: handlerP(
getCommandHandler("plugin/build", (args, cmd) => {
process.env.NODE_ENV = process.env.NODE_ENV || `development`
cmd(args)
return new Promise((resolve) => {})
})
),
})
.command({
command: "plugin:develop",
desc: "Start plugin development process in watch mode. Changes will be re-published to the local packages registry",
builder: (builder) => {},
handler: handlerP(
getCommandHandler("plugin/develop", (args, cmd) => {
process.env.NODE_ENV = process.env.NODE_ENV || `development`
Expand Down
51 changes: 49 additions & 2 deletions packages/core/framework/src/build-tools/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,55 @@ export class Compiler {
}
}

// @todo
buildPluginBackend() {}
/**
* Compiles the plugin source code to JavaScript using the
* TypeScript's official compiler
*/
async buildPluginBackend(tsConfig: tsStatic.ParsedCommandLine) {
const tracker = this.#trackDuration()
const dist = ".medusa/server"
this.#logger.info("Compiling plugin source...")

/**
* Step 1: Cleanup existing build output
*/
this.#logger.info(
`Removing existing "${path.relative(this.#projectRoot, dist)}" folder`
)
await this.#clean(dist)

/**
* Step 2: Compile TypeScript source code
*/
const { emitResult, diagnostics } = await this.#emitBuildOutput(
tsConfig,
["integration-tests", "test", "unit-tests", "src/admin"],
dist
)

/**
* Exit early if no output is written to the disk
*/
if (emitResult.emitSkipped) {
this.#logger.warn("Plugin build completed without emitting any output")
return false
}

/**
* Notify about the state of build
*/
if (diagnostics.length) {
this.#logger.warn(
`Plugin build completed with errors (${tracker.getSeconds()}s)`
)
} else {
this.#logger.info(
`Plugin build completed successfully (${tracker.getSeconds()}s)`
)
}

return true
}

/**
* Compiles the backend source code of a plugin project in watch
Expand Down
22 changes: 22 additions & 0 deletions packages/medusa/src/commands/plugin/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { logger } from "@medusajs/framework/logger"
import { Compiler } from "@medusajs/framework/build-tools"

export default async function build({
directory,
adminOnly,
}: {
directory: string
adminOnly: boolean
}): Promise<boolean> {
logger.info("Starting build...")
const compiler = new Compiler(directory, logger)

const tsConfig = await compiler.loadTSConfigFile()
if (!tsConfig) {
logger.error("Unable to compile plugin")
return false
}

await compiler.buildPluginBackend(tsConfig)
return true
}

0 comments on commit b0f581c

Please sign in to comment.