Abstract detection logic in seperate file

Co-authored-by: Vincenzo Scamporlino <vinzscam@users.noreply.github.com>
Signed-off-by: Philipp Hugenroth <philipph@spotify.com>
This commit is contained in:
Philipp Hugenroth
2023-07-19 15:27:13 +02:00
committed by Patrik Oldsberg
parent 3c4629526d
commit 0af208f85f
4 changed files with 88 additions and 61 deletions
+7
View File
@@ -27,6 +27,7 @@ import { createConfig, resolveBaseUrl } from './config';
import { BuildOptions } from './types';
import { resolveBundlingPaths } from './paths';
import chalk from 'chalk';
import { writeDetectedPluginsModule } from './discover';
// TODO(Rugvip): Limits from CRA, we might want to tweak these though.
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
@@ -49,6 +50,12 @@ export async function buildBundle(options: BuildOptions) {
getFrontendAppConfigs: () => options.frontendAppConfigs,
});
await writeDetectedPluginsModule({
config: options.fullConfig,
entry: options.entry,
targetDir: options.targetDir,
});
const isCi = yn(process.env.CI, { default: false });
const previousFileSizes = await measureFileSizesBeforeBuild(paths.targetDist);
+2 -61
View File
@@ -26,23 +26,18 @@ import { isChildPath } from '@backstage/cli-common';
import { getPackages } from '@manypkg/get-packages';
import { optimization } from './optimization';
import { Config } from '@backstage/config';
import {
BundlingPaths,
BundlingPathsOptions,
resolveBundlingPaths,
} from './paths';
import { BundlingPaths } from './paths';
import { transforms } from './transforms';
import { LinkedPackageResolvePlugin } from './LinkedPackageResolvePlugin';
import { BundlingOptions, BackendBundlingOptions } from './types';
import { version } from '../../lib/version';
import { paths as cliPaths } from '../../lib/paths';
import { BackstagePackage, BackstagePackageJson } from '@backstage/cli-node';
import { BackstagePackage } from '@backstage/cli-node';
import { runPlain } from '../run';
import ESLintPlugin from 'eslint-webpack-plugin';
import pickBy from 'lodash/pickBy';
import yn from 'yn';
import { readEntryPoints } from '../entryPoints';
import path from 'path';
const BUILD_CACHE_ENV_VAR = 'BACKSTAGE_CLI_EXPERIMENTAL_BUILD_CACHE';
@@ -132,24 +127,6 @@ export async function createConfig(
}),
);
const detectedPlugins = await detectPlugins({
config: options.fullConfig,
entry: options.entry,
targetDir: options.targetDir,
});
const requirePackageScript = detectedPlugins
?.map(pkg => `{name: '${pkg}', module: require('${pkg}')}`)
.join(',');
await fs.writeFile(
path.join(
cliPaths.targetRoot,
'node_modules',
'backstage-autodetected-plugins.js',
),
`module.exports = { modules: [${requirePackageScript}] };`,
);
const buildInfo = await readBuildInfo();
plugins.push(
new webpack.DefinePlugin({
@@ -388,39 +365,3 @@ function nodeExternalsWithResolve(
return externals(context, request, callback);
};
}
export async function detectPlugins({
config,
entry,
targetDir,
}: { config: Config } & BundlingPathsOptions) {
const paths = resolveBundlingPaths({ entry, targetDir });
const pkg: BackstagePackageJson = await fs.readJson(paths.targetPackageJson);
// TODO: proper
// Assumption for config string based on https://github.com/backstage/backstage/issues/18372 ^
const packageDetectionMode =
config.getOptional('app.experimental.packages') || 'all';
const allowedPackages =
packageDetectionMode === 'all'
? Object.keys(pkg.dependencies ?? {})
: config.getStringArray('app.experimental.packages');
return allowedPackages
.map(depName => {
const depPackageJson: BackstagePackageJson = require(require.resolve(
`${depName}/package.json`,
{ paths: [paths.targetPath] },
));
if (
['frontend-plugin', 'frontend-plugin-module'].includes(
depPackageJson.backstage?.role || '',
)
) {
return depName;
}
return undefined;
})
.filter((d): d is string => !!d);
}
+72
View File
@@ -0,0 +1,72 @@
/*
* Copyright 2020 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { BackstagePackageJson } from '@backstage/cli-node';
import { Config } from '@backstage/config';
import fs from 'fs-extra';
import path from 'path';
import { BundlingPathsOptions, resolveBundlingPaths } from './paths';
import { paths as cliPaths } from '../../lib/paths';
type Options = { config: Config } & BundlingPathsOptions;
async function detectPlugins({ config, entry, targetDir }: Options) {
const paths = resolveBundlingPaths({ entry, targetDir });
const pkg: BackstagePackageJson = await fs.readJson(paths.targetPackageJson);
// TODO: proper
// Assumption for config string based on https://github.com/backstage/backstage/issues/18372 ^
const packageDetectionMode =
config.getOptional('app.experimental.packages') || 'all';
const allowedPackages =
packageDetectionMode === 'all'
? Object.keys(pkg.dependencies ?? {})
: config.getStringArray('app.experimental.packages');
return allowedPackages
.map(depName => {
const depPackageJson: BackstagePackageJson = require(require.resolve(
`${depName}/package.json`,
{ paths: [paths.targetPath] },
));
if (
['frontend-plugin', 'frontend-plugin-module'].includes(
depPackageJson.backstage?.role || '',
)
) {
return depName;
}
return undefined;
})
.filter((d): d is string => !!d);
}
export async function writeDetectedPluginsModule(options: Options) {
const requirePackageScript = (await detectPlugins(options))
?.map(pkg => `{name: '${pkg}', module: require('${pkg}')}`)
.join(',');
await fs.writeFile(
path.join(
cliPaths.targetRoot,
'node_modules',
'backstage-autodetected-plugins.js',
),
`module.exports = { modules: [${requirePackageScript}] };`,
);
}
+7
View File
@@ -34,6 +34,7 @@ import {
includedFilter,
} from '../../commands/versions/lint';
import { BackstagePackageJson } from '@backstage/cli-node';
import { writeDetectedPluginsModule } from './discover';
export async function serveBundle(options: ServeOptions) {
const paths = resolveBundlingPaths(options);
@@ -171,6 +172,12 @@ export async function serveBundle(options: ServeOptions) {
},
});
await writeDetectedPluginsModule({
config: options.fullConfig,
entry: options.entry,
targetDir: options.targetDir,
});
const compiler = webpack(config);
server = new WebpackDevServer(