From 0555c594e42d1d0e734a481dfa14ec483fb0a36a Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 7 Jan 2022 11:47:12 +0100 Subject: [PATCH] cli: switch bundler config to use get-packages Signed-off-by: Patrik Oldsberg --- .../LinkedPackageResolvePlugin.test.ts | 14 +++++++--- .../lib/bundler/LinkedPackageResolvePlugin.ts | 18 ++++++------ packages/cli/src/lib/bundler/config.ts | 28 ++++++------------- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/packages/cli/src/lib/bundler/LinkedPackageResolvePlugin.test.ts b/packages/cli/src/lib/bundler/LinkedPackageResolvePlugin.test.ts index d22b9b3701..450126e6fa 100644 --- a/packages/cli/src/lib/bundler/LinkedPackageResolvePlugin.test.ts +++ b/packages/cli/src/lib/bundler/LinkedPackageResolvePlugin.test.ts @@ -26,12 +26,18 @@ describe('LinkedPackageResolvePlugin', () => { path.resolve(root, 'repo/node_modules'), [ { - name: 'a', - location: path.resolve(root, 'external-a'), + dir: path.resolve(root, 'external-a'), + packageJson: { + name: 'a', + version: '1.0.0', + }, }, { - name: '@s/b', - location: path.resolve(root, 'external-b'), + dir: path.resolve(root, 'external-b'), + packageJson: { + name: '@s/b', + version: '1.0.0', + }, }, ], ); diff --git a/packages/cli/src/lib/bundler/LinkedPackageResolvePlugin.ts b/packages/cli/src/lib/bundler/LinkedPackageResolvePlugin.ts index 91615eddee..4811afec6f 100644 --- a/packages/cli/src/lib/bundler/LinkedPackageResolvePlugin.ts +++ b/packages/cli/src/lib/bundler/LinkedPackageResolvePlugin.ts @@ -17,7 +17,7 @@ import { resolve as resolvePath } from 'path'; import { WebpackPluginInstance } from 'webpack'; import { isChildPath } from '@backstage/cli-common'; -import { LernaPackage } from './types'; +import { Package } from '@manypkg/get-packages'; // Enables proper resolution of packages when linking in external packages. // Without this the packages would depend on dependencies in the node_modules @@ -25,7 +25,7 @@ import { LernaPackage } from './types'; export class LinkedPackageResolvePlugin implements WebpackPluginInstance { constructor( private readonly targetModules: string, - private readonly packages: LernaPackage[], + private readonly packages: Package[], ) {} apply(resolver: any) { @@ -41,7 +41,7 @@ export class LinkedPackageResolvePlugin implements WebpackPluginInstance { callback: () => void, ) => { const pkg = this.packages.find( - pkge => data.path && isChildPath(pkge.location, data.path), + pkge => data.path && isChildPath(pkge.dir, data.path), ); if (!pkg) { callback(); @@ -51,14 +51,14 @@ export class LinkedPackageResolvePlugin implements WebpackPluginInstance { // pkg here is an external package. We rewrite the context of any imports to resolve // from the location of the package within the node_modules of the target root rather // than the real location of the external package. - const modulesLocation = resolvePath(this.targetModules, pkg.name); + const modulesLocation = resolvePath( + this.targetModules, + pkg.packageJson.name, + ); const newContext = data.context?.issuer ? { ...data.context, - issuer: data.context.issuer.replace( - pkg.location, - modulesLocation, - ), + issuer: data.context.issuer.replace(pkg.dir, modulesLocation), } : data.context; @@ -70,7 +70,7 @@ export class LinkedPackageResolvePlugin implements WebpackPluginInstance { { ...data, context: newContext, - path: data.path && data.path.replace(pkg.location, modulesLocation), + path: data.path && data.path.replace(pkg.dir, modulesLocation), }, `resolve ${data.request} in ${modulesLocation}`, context, diff --git a/packages/cli/src/lib/bundler/config.ts b/packages/cli/src/lib/bundler/config.ts index 7ac230bb0e..35c5be8bb7 100644 --- a/packages/cli/src/lib/bundler/config.ts +++ b/packages/cli/src/lib/bundler/config.ts @@ -24,12 +24,13 @@ import { RunScriptWebpackPlugin } from 'run-script-webpack-plugin'; import webpack, { ProvidePlugin } from 'webpack'; import nodeExternals from 'webpack-node-externals'; import { isChildPath } from '@backstage/cli-common'; +import { getPackages } from '@manypkg/get-packages'; import { optimization } from './optimization'; import { Config } from '@backstage/config'; import { BundlingPaths } from './paths'; import { transforms } from './transforms'; import { LinkedPackageResolvePlugin } from './LinkedPackageResolvePlugin'; -import { BundlingOptions, BackendBundlingOptions, LernaPackage } from './types'; +import { BundlingOptions, BackendBundlingOptions } from './types'; import { version } from '../../lib/version'; import { paths as cliPaths } from '../../lib/paths'; import { runPlain } from '../run'; @@ -74,25 +75,17 @@ async function readBuildInfo() { }; } -async function loadLernaPackages(): Promise { - const { Project } = require('@lerna/project'); - const project = new Project(cliPaths.targetDir); - return project.getPackages(); -} - export async function createConfig( paths: BundlingPaths, options: BundlingOptions, ): Promise { const { checksEnabled, isDev, frontendConfig } = options; - const packages = await loadLernaPackages(); const { plugins, loaders } = transforms(options); // Any package that is part of the monorepo but outside the monorepo root dir need // separate resolution logic. - const externalPkgs = packages.filter( - p => !isChildPath(paths.root, p.location), - ); + const { packages } = await getPackages(cliPaths.targetDir); + const externalPkgs = packages.filter(p => !isChildPath(paths.root, p.dir)); const baseUrl = frontendConfig.getString('app.baseUrl'); const validBaseUrl = new URL(baseUrl); @@ -253,14 +246,11 @@ export async function createBackendConfig( const { checksEnabled, isDev } = options; // Find all local monorepo packages and their node_modules, and mark them as external. - const packages = await await loadLernaPackages(); - const localPackageNames = packages.map((p: any) => p.name); - const moduleDirs = packages.map((p: any) => - resolvePath(p.location, 'node_modules'), - ); - const externalPkgs = packages.filter( - p => !isChildPath(paths.root, p.location), - ); // See frontend config + const { packages } = await getPackages(cliPaths.targetDir); + const localPackageNames = packages.map(p => p.packageJson.name); + const moduleDirs = packages.map(p => resolvePath(p.dir, 'node_modules')); + // See frontend config + const externalPkgs = packages.filter(p => !isChildPath(paths.root, p.dir)); const { loaders } = transforms(options);