cli: Enable cache for production build using experimental env var

Signed-off-by: Marcus Eide <eide@spotify.com>
This commit is contained in:
Marcus Eide
2022-12-13 15:17:25 +01:00
parent ff7d67eebb
commit 4b572126f1
3 changed files with 20 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli': patch
---
Add experimental environment variable to enable caching for production builds.
+3 -4
View File
@@ -42,7 +42,6 @@ export async function buildBundle(options: BuildOptions) {
isDev: false,
baseUrl: resolveBaseUrl(options.frontendConfig),
});
const compiler = webpack(config);
const isCi = yn(process.env.CI, { default: false });
@@ -64,7 +63,7 @@ export async function buildBundle(options: BuildOptions) {
);
}
const { stats } = await build(compiler, isCi).catch(error => {
const { stats } = await build(config, isCi).catch(error => {
console.log(chalk.red('Failed to compile.\n'));
throw new Error(`Failed to compile.\n${error.message || error}`);
});
@@ -90,10 +89,10 @@ export async function buildBundle(options: BuildOptions) {
);
}
async function build(compiler: webpack.Compiler, isCi: boolean) {
async function build(config: webpack.Configuration, isCi: boolean) {
const stats = await new Promise<webpack.Stats | undefined>(
(resolve, reject) => {
compiler.run((err, buildStats) => {
webpack(config, (err, buildStats) => {
if (err) {
if (err.message) {
const { errors } = formatWebpackMessages({
+12
View File
@@ -36,6 +36,8 @@ import { runPlain } from '../run';
import ESLintPlugin from 'eslint-webpack-plugin';
import pickBy from 'lodash/pickBy';
const BUILD_CACHE_ENV_VAR = 'BACKSTAGE_CLI_EXPERIMENTAL_BUILD_CACHE';
export function resolveBaseUrl(config: Config): URL {
const baseUrl = config.getString('app.baseUrl');
try {
@@ -206,6 +208,16 @@ export async function createConfig(
: {}),
},
plugins,
...(process.env[BUILD_CACHE_ENV_VAR]
? {
cache: {
type: 'filesystem',
buildDependencies: {
config: [__filename],
},
},
}
: {}),
};
}