From fe1a55efe4d2c21fe8bbf33893d05f96905d2805 Mon Sep 17 00:00:00 2001 From: Jonathan Roebuck Date: Wed, 13 Mar 2024 15:43:32 +0000 Subject: [PATCH] Add option to minify generated code in backend package Signed-off-by: Jonathan Roebuck --- .changeset/calm-trainers-hear.md | 5 +++++ docs/local-dev/cli-commands.md | 2 +- packages/cli/src/commands/build/buildBackend.ts | 7 +++++-- packages/cli/src/commands/build/command.ts | 1 + packages/cli/src/commands/index.ts | 2 +- packages/cli/src/lib/packager/createDistWorkspace.ts | 8 ++++++-- 6 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 .changeset/calm-trainers-hear.md diff --git a/.changeset/calm-trainers-hear.md b/.changeset/calm-trainers-hear.md new file mode 100644 index 0000000000..8941ba8cb9 --- /dev/null +++ b/.changeset/calm-trainers-hear.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Extend option to minify generated code to the `backend` package. diff --git a/docs/local-dev/cli-commands.md b/docs/local-dev/cli-commands.md index 29cc8635aa..173d616e61 100644 --- a/docs/local-dev/cli-commands.md +++ b/docs/local-dev/cli-commands.md @@ -128,7 +128,7 @@ Build a package for production deployment or publishing Options: --role Run the command with an explicit package role - --minify Minify the generated code. Does not apply to app or backend packages. + --minify Minify the generated code. Does not apply to app package (app is minified by default). --skip-build-dependencies Skip the automatic building of local dependencies. Applies to backend packages only. --stats If bundle stats are available, write them to the output directory. Applies to app packages only. --config Config files to load instead of app-config.yaml. Applies to app packages only. (default: []) diff --git a/packages/cli/src/commands/build/buildBackend.ts b/packages/cli/src/commands/build/buildBackend.ts index 5d42aa18d4..3609e40eb5 100644 --- a/packages/cli/src/commands/build/buildBackend.ts +++ b/packages/cli/src/commands/build/buildBackend.ts @@ -29,17 +29,19 @@ interface BuildBackendOptions { targetDir: string; skipBuildDependencies: boolean; configPaths?: string[]; + minify?: boolean; } export async function buildBackend(options: BuildBackendOptions) { - const { targetDir, skipBuildDependencies, configPaths } = options; + const { targetDir, skipBuildDependencies, configPaths, minify } = options; const pkg = await fs.readJson(resolvePath(targetDir, 'package.json')); // We build the target package without generating type declarations. await buildPackage({ - targetDir: options.targetDir, + targetDir, packageJson: pkg, outputs: new Set([Output.cjs]), + minify, }); const tmpDir = await fs.mkdtemp(resolvePath(os.tmpdir(), 'backstage-bundle')); @@ -51,6 +53,7 @@ export async function buildBackend(options: BuildBackendOptions) { buildExcludes: [pkg.name], parallelism: getEnvironmentParallelism(), skeleton: SKELETON_FILE, + minify, }); // We built the target backend package using the regular build process, but the result of diff --git a/packages/cli/src/commands/build/command.ts b/packages/cli/src/commands/build/command.ts index 1b3aeff2f9..f15ae27ee0 100644 --- a/packages/cli/src/commands/build/command.ts +++ b/packages/cli/src/commands/build/command.ts @@ -45,6 +45,7 @@ export async function command(opts: OptionValues): Promise { targetDir: paths.targetDir, configPaths, skipBuildDependencies: Boolean(opts.skipBuildDependencies), + minify: Boolean(opts.minify), }); } diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index fb0a55f37e..431bd6cc68 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -128,7 +128,7 @@ export function registerScriptCommand(program: Command) { .option('--role ', 'Run the command with an explicit package role') .option( '--minify', - 'Minify the generated code. Does not apply to app or backend packages.', + 'Minify the generated code. Does not apply to app package (app is minified by default).', ) .option( '--skip-build-dependencies', diff --git a/packages/cli/src/lib/packager/createDistWorkspace.ts b/packages/cli/src/lib/packager/createDistWorkspace.ts index 00b8784d6d..a35b8a83c7 100644 --- a/packages/cli/src/lib/packager/createDistWorkspace.ts +++ b/packages/cli/src/lib/packager/createDistWorkspace.ts @@ -102,6 +102,11 @@ type Options = { * command performance. */ alwaysYarnPack?: boolean; + + /** + * If set to true, the generated code will be minified. + */ + minify?: boolean; }; function prefixLogFunc(prefix: string, out: 'stdout' | 'stderr') { @@ -204,8 +209,7 @@ export async function createDistWorkspace( packageJson: pkg.packageJson, outputs: outputs, logPrefix: `${chalk.cyan(relativePath(paths.targetRoot, pkg.dir))}: `, - // No need to detect these for the backend builds, we assume no minification or types - minify: false, + minify: options.minify, }); } }