From 9296a33c4d25db8422d9c1d69a745d12c78422cd Mon Sep 17 00:00:00 2001 From: MT Lewis Date: Thu, 21 Nov 2024 15:44:58 +0000 Subject: [PATCH 1/5] yarn-plugin: remove dependency on chalk Chalk costs us about 20kb in the bundle size, and we only use it to format one logged message. To avoid this dependency, this commit hard-codes the escape codes for that single message. Signed-off-by: MT Lewis --- packages/yarn-plugin/package.json | 1 - packages/yarn-plugin/src/index.ts | 12 ++++++++---- yarn.lock | 1 - 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/yarn-plugin/package.json b/packages/yarn-plugin/package.json index 1df88d001a..2b42e826b9 100644 --- a/packages/yarn-plugin/package.json +++ b/packages/yarn-plugin/package.json @@ -34,7 +34,6 @@ "@backstage/release-manifests": "workspace:^", "@yarnpkg/core": "^4.0.3", "@yarnpkg/fslib": "^3.0.2", - "chalk": "^4.0.0", "lodash": "^4.17.21", "semver": "^7.6.0" }, diff --git a/packages/yarn-plugin/src/index.ts b/packages/yarn-plugin/src/index.ts index f548463829..13c28302e2 100644 --- a/packages/yarn-plugin/src/index.ts +++ b/packages/yarn-plugin/src/index.ts @@ -22,16 +22,20 @@ */ import { Plugin, semverUtils, YarnVersion } from '@yarnpkg/core'; -import chalk from 'chalk'; import { beforeWorkspacePacking } from './handlers/beforeWorkspacePacking'; import { BackstageResolver } from './resolver/BackstageResolver'; +// All dependencies of the yarn plugin are bundled during the build. Chalk +// triples the size of the plugin bundle when included, so we avoid the +// dependency by hard-coding the ANSI escape codes for the one bit of formatting +// we need. +const RED_BOLD = `\u001B[31;1m`; +const RESET = '\u001B[0m'; + if (!semverUtils.satisfiesWithPrereleases(YarnVersion, '^4.1.1')) { console.error(); console.error( - `${chalk.bold.red( - 'Unsupported yarn version.', - )}: The Backstage yarn plugin only works with yarn ^4.1.1. Please upgrade yarn, or remove this plugin with "yarn plugin remove @yarnpkg/plugin-backstage".`, + `${RED_BOLD}Unsupported yarn version${RESET}: The Backstage yarn plugin only works with yarn ^4.1.1. Please upgrade yarn, or remove this plugin with "yarn plugin remove @yarnpkg/plugin-backstage".`, ); console.error(); } diff --git a/yarn.lock b/yarn.lock index d272ce009c..4395a38ede 100644 --- a/yarn.lock +++ b/yarn.lock @@ -45427,7 +45427,6 @@ __metadata: "@yarnpkg/builder": ^4.0.0 "@yarnpkg/core": ^4.0.3 "@yarnpkg/fslib": ^3.0.2 - chalk: ^4.0.0 lodash: ^4.17.21 nodemon: ^3.0.1 semver: ^7.6.0 From 344c591c91671ac7b4007bf341144bdb2efe20d9 Mon Sep 17 00:00:00 2001 From: MT Lewis Date: Thu, 21 Nov 2024 15:45:33 +0000 Subject: [PATCH 2/5] yarn-plugin: use native Array.some Avoids the dependency on the lodash function. Signed-off-by: MT Lewis --- packages/yarn-plugin/src/handlers/beforeWorkspacePacking.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/yarn-plugin/src/handlers/beforeWorkspacePacking.ts b/packages/yarn-plugin/src/handlers/beforeWorkspacePacking.ts index 3221400f48..0682c83f8d 100644 --- a/packages/yarn-plugin/src/handlers/beforeWorkspacePacking.ts +++ b/packages/yarn-plugin/src/handlers/beforeWorkspacePacking.ts @@ -15,7 +15,6 @@ */ import { Descriptor, Workspace, structUtils } from '@yarnpkg/core'; -import { some } from 'lodash'; import { bindBackstageVersion, getCurrentBackstageVersion, @@ -76,9 +75,8 @@ export const beforeWorkspacePacking = async ( } if ( - some( - ['dependencies', 'devDependencies', 'optionalDependencies'], - dependencyType => some(rawManifest[dependencyType], hasBackstageVersion), + ['dependencies', 'devDependencies', 'optionalDependencies'].some( + dependencyType => rawManifest[dependencyType]?.some(hasBackstageVersion), ) ) { throw new Error( From d8bd6f5547699d9553c81f076447a3fc51d1d0ed Mon Sep 17 00:00:00 2001 From: MT Lewis Date: Thu, 21 Nov 2024 15:50:37 +0000 Subject: [PATCH 3/5] yarn-plugin: cherry-pick memoize method Signed-off-by: MT Lewis --- packages/yarn-plugin/src/util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/yarn-plugin/src/util.ts b/packages/yarn-plugin/src/util.ts index df7ed75e0f..5fd0091b2b 100644 --- a/packages/yarn-plugin/src/util.ts +++ b/packages/yarn-plugin/src/util.ts @@ -16,7 +16,7 @@ import { ppath, xfs } from '@yarnpkg/fslib'; import { valid as semverValid } from 'semver'; -import { memoize } from 'lodash'; +import memoize from 'lodash/memoize'; import { getManifestByVersion as getManifestByVersionBase } from '@backstage/release-manifests'; import { BACKSTAGE_JSON, findPaths } from '@backstage/cli-common'; import { Descriptor, structUtils } from '@yarnpkg/core'; From 124a108c36ffeb5e52e541bbd13b28ee92b73e18 Mon Sep 17 00:00:00 2001 From: MT Lewis Date: Fri, 22 Nov 2024 15:27:21 +0000 Subject: [PATCH 4/5] yarn-plugin: extra values from dependency object after switch from lodash#some to native some Signed-off-by: MT Lewis --- packages/yarn-plugin/src/handlers/beforeWorkspacePacking.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/yarn-plugin/src/handlers/beforeWorkspacePacking.ts b/packages/yarn-plugin/src/handlers/beforeWorkspacePacking.ts index 0682c83f8d..fbdea3fa34 100644 --- a/packages/yarn-plugin/src/handlers/beforeWorkspacePacking.ts +++ b/packages/yarn-plugin/src/handlers/beforeWorkspacePacking.ts @@ -76,7 +76,10 @@ export const beforeWorkspacePacking = async ( if ( ['dependencies', 'devDependencies', 'optionalDependencies'].some( - dependencyType => rawManifest[dependencyType]?.some(hasBackstageVersion), + dependencyType => + Object.values(rawManifest[dependencyType] ?? {}).some( + hasBackstageVersion, + ), ) ) { throw new Error( From 27600c2cd38902c5f4024e71f6898b587ca06b2c Mon Sep 17 00:00:00 2001 From: MT Lewis Date: Fri, 22 Nov 2024 15:28:23 +0000 Subject: [PATCH 5/5] yarn-plugin: import appropriate type to verify beforeWorkspacePacking signature I realised that by default the `Plugin` type from `@yarnpkg/core` sets the type of the `hooks` property to `any`. This meant that there wasn't any restriction on the hooks that could be passed, and introduced the risk that a typo in the `beforeWorkspacePacking` name could lead to the hook being skipped. This commit pulls in the appropriate type for Hooks to verify the name and signature of the hook. Signed-off-by: MT Lewis --- packages/yarn-plugin/package.json | 1 + packages/yarn-plugin/report.api.md | 3 ++- packages/yarn-plugin/src/index.ts | 3 ++- yarn.lock | 1 + 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/yarn-plugin/package.json b/packages/yarn-plugin/package.json index 2b42e826b9..ed66832e27 100644 --- a/packages/yarn-plugin/package.json +++ b/packages/yarn-plugin/package.json @@ -34,6 +34,7 @@ "@backstage/release-manifests": "workspace:^", "@yarnpkg/core": "^4.0.3", "@yarnpkg/fslib": "^3.0.2", + "@yarnpkg/plugin-pack": "^4.0.0", "lodash": "^4.17.21", "semver": "^7.6.0" }, diff --git a/packages/yarn-plugin/report.api.md b/packages/yarn-plugin/report.api.md index f0300ec9a1..928703ed3e 100644 --- a/packages/yarn-plugin/report.api.md +++ b/packages/yarn-plugin/report.api.md @@ -3,9 +3,10 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts +import { Hooks } from '@yarnpkg/plugin-pack'; import { Plugin as Plugin_2 } from '@yarnpkg/core'; // @public (undocumented) -const plugin: Plugin_2; +const plugin: Plugin_2; export default plugin; ``` diff --git a/packages/yarn-plugin/src/index.ts b/packages/yarn-plugin/src/index.ts index 13c28302e2..9c20e9a45c 100644 --- a/packages/yarn-plugin/src/index.ts +++ b/packages/yarn-plugin/src/index.ts @@ -22,6 +22,7 @@ */ import { Plugin, semverUtils, YarnVersion } from '@yarnpkg/core'; +import { Hooks } from '@yarnpkg/plugin-pack'; import { beforeWorkspacePacking } from './handlers/beforeWorkspacePacking'; import { BackstageResolver } from './resolver/BackstageResolver'; @@ -43,7 +44,7 @@ if (!semverUtils.satisfiesWithPrereleases(YarnVersion, '^4.1.1')) { /** * @public */ -const plugin: Plugin = { +const plugin: Plugin = { hooks: { beforeWorkspacePacking, }, diff --git a/yarn.lock b/yarn.lock index 4395a38ede..3fba088f74 100644 --- a/yarn.lock +++ b/yarn.lock @@ -45427,6 +45427,7 @@ __metadata: "@yarnpkg/builder": ^4.0.0 "@yarnpkg/core": ^4.0.3 "@yarnpkg/fslib": ^3.0.2 + "@yarnpkg/plugin-pack": ^4.0.0 lodash: ^4.17.21 nodemon: ^3.0.1 semver: ^7.6.0