cli: switch bundler config to use get-packages

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-01-07 11:47:12 +01:00
parent 89dc617471
commit 0555c594e4
3 changed files with 28 additions and 32 deletions
@@ -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',
},
},
],
);
@@ -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,
+9 -19
View File
@@ -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<LernaPackage[]> {
const { Project } = require('@lerna/project');
const project = new Project(cliPaths.targetDir);
return project.getPackages();
}
export async function createConfig(
paths: BundlingPaths,
options: BundlingOptions,
): Promise<webpack.Configuration> {
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);