diff --git a/.changeset/calm-cameras-unite.md b/.changeset/calm-cameras-unite.md new file mode 100644 index 0000000000000..da9483e0e6842 --- /dev/null +++ b/.changeset/calm-cameras-unite.md @@ -0,0 +1,7 @@ +--- +"@medusajs/medusa": patch +"@medusajs/framework": patch +"@medusajs/cli": patch +--- + +feat: add plugin build command diff --git a/packages/cli/medusa-cli/src/create-cli.ts b/packages/cli/medusa-cli/src/create-cli.ts index e52ff2cecc1b5..5329a1d561081 100644 --- a/packages/cli/medusa-cli/src/create-cli.ts +++ b/packages/cli/medusa-cli/src/create-cli.ts @@ -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` diff --git a/packages/core/framework/src/build-tools/compiler.ts b/packages/core/framework/src/build-tools/compiler.ts index 91606077c4e32..d2036a28c54a4 100644 --- a/packages/core/framework/src/build-tools/compiler.ts +++ b/packages/core/framework/src/build-tools/compiler.ts @@ -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 diff --git a/packages/medusa/src/commands/plugin/build.ts b/packages/medusa/src/commands/plugin/build.ts new file mode 100644 index 0000000000000..dc72625f4682e --- /dev/null +++ b/packages/medusa/src/commands/plugin/build.ts @@ -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 { + 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 +}