Skip to content

Commit

Permalink
refactor(@angular-devkit/build-angular): always return outputWithFile…
Browse files Browse the repository at this point in the history
…s when using the application builder

This allows access to file when using the `buildApplication` API

Closes: angular#27386
  • Loading branch information
alan-agius4 committed Apr 10, 2024
1 parent ee9d4dd commit d1a09a2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import { BuilderContext, BuilderOutput } from '@angular-devkit/architect';
import { BuilderContext } from '@angular-devkit/architect';
import { existsSync } from 'node:fs';
import path from 'node:path';
import { BuildOutputFile } from '../../tools/esbuild/bundler-context';
Expand Down Expand Up @@ -37,8 +37,7 @@ const packageWatchFiles = [
'.pnp.data.json',
];

type BuildActionOutput = (ExecutionResult['outputWithFiles'] | ExecutionResult['output']) &
BuilderOutput;
export type BuildActionOutput = ExecutionResult['outputWithFiles'];

export async function* runEsBuildBuildAction(
action: (rebuildState?: RebuildState) => Promise<ExecutionResult>,
Expand Down Expand Up @@ -223,7 +222,7 @@ export async function* runEsBuildBuildAction(

async function writeAndEmitOutput(
writeToFileSystem: boolean,
{ outputFiles, output, outputWithFiles, assetFiles }: ExecutionResult,
{ outputFiles, outputWithFiles, assetFiles }: ExecutionResult,
outputOptions: NormalizedApplicationBuildOptions['outputOptions'],
writeToFileSystemFilter: ((file: BuildOutputFile) => boolean) | undefined,
): Promise<BuildActionOutput> {
Expand All @@ -234,11 +233,9 @@ async function writeAndEmitOutput(
: outputFiles;

await writeResultFiles(outputFilesToWrite, assetFiles, outputOptions);

return output;
} else {
// Requires casting due to unneeded `JsonObject` requirement. Remove once fixed.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return outputWithFiles as any;
}

// Requires casting due to unneeded `JsonObject` requirement. Remove once fixed.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return outputWithFiles as any;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
import type { Plugin } from 'esbuild';
import { BuildOutputFile, BuildOutputFileType } from '../../tools/esbuild/bundler-context';
import { BuildOutputFileType } from '../../tools/esbuild/bundler-context';
import { createJsonBuildManifest } from '../../tools/esbuild/utils';
import { colors as ansiColors } from '../../utils/color';
import { purgeStaleBuildCache } from '../../utils/purge-cache';
import { assertCompatibleAngularVersion } from '../../utils/version';
import { runEsBuildBuildAction } from './build-action';
import { BuildActionOutput, runEsBuildBuildAction } from './build-action';
import { executeBuild } from './execute-build';
import {
ApplicationBuilderExtensions,
Expand All @@ -24,6 +24,10 @@ import { Schema as ApplicationBuilderOptions } from './schema';

export { ApplicationBuilderOptions };

export interface ApplicationBuilderOutput
extends BuilderOutput,
Omit<Partial<BuildActionOutput>, 'success'> {}

export async function* buildApplicationInternal(
options: ApplicationBuilderInternalOptions,
// TODO: Integrate abort signal support into builder system
Expand Down Expand Up @@ -140,11 +144,6 @@ export async function* buildApplicationInternal(
);
}

export interface ApplicationBuilderOutput extends BuilderOutput {
outputFiles?: BuildOutputFile[];
assetFiles?: { source: string; destination: string }[];
}

/**
* Builds an application using the `application` builder with the provided
* options.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
import { BuilderContext, createBuilder } from '@angular-devkit/architect';
import type { Plugin } from 'esbuild';
import { constants as fsConstants } from 'node:fs';
import fs from 'node:fs/promises';
Expand All @@ -15,7 +15,7 @@ import { BuildOutputFile } from '../../tools/esbuild/bundler-context';
import { BuildOutputAsset } from '../../tools/esbuild/bundler-execution-result';
import { emitFilesToDisk } from '../../tools/esbuild/utils';
import { deleteOutputDir } from '../../utils';
import { buildApplicationInternal } from '../application';
import { ApplicationBuilderOutput, buildApplicationInternal } from '../application';
import { Schema as ApplicationBuilderOptions, OutputPathClass } from '../application/schema';
import { logBuilderStatusWarnings } from './builder-status-warnings';
import { Schema as BrowserBuilderOptions } from './schema';
Expand All @@ -34,12 +34,7 @@ export async function* buildEsbuildBrowser(
write?: boolean;
},
plugins?: Plugin[],
): AsyncIterable<
BuilderOutput & {
outputFiles?: BuildOutputFile[];
assetFiles?: { source: string; destination: string }[];
}
> {
): AsyncIterable<ApplicationBuilderOutput> {
// Inform user of status of builder and options
logBuilderStatusWarnings(userOptions, context);
const normalizedOptions = normalizeOptions(userOptions);
Expand Down Expand Up @@ -103,8 +98,8 @@ function normalizeOptions(
// We write the file directly from this builder to maintain webpack output compatibility
// and not output browser files into '/browser'.
async function writeResultFiles(
outputFiles: BuildOutputFile[],
assetFiles: BuildOutputAsset[] | undefined,
outputFiles: Readonly<BuildOutputFile[]>,
assetFiles: Readonly<BuildOutputAsset[]> | undefined,
outputPath: string,
) {
const directoryExists = new Set<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,15 @@ export async function* serveWithVite(
if (!result.success) {
// If server is active, send an error notification
if (result.errors?.length && server) {
const { text = '', location } = result.errors[0];
hadError = true;

server.ws.send({
type: 'error',
err: {
message: result.errors[0].text,
message: text,
stack: '',
loc: result.errors[0].location,
loc: location as never,
},
});
}
Expand Down Expand Up @@ -375,7 +377,7 @@ function handleUpdate(
function analyzeResultFiles(
normalizePath: (id: string) => string,
htmlIndexPath: string,
resultFiles: BuildOutputFile[],
resultFiles: Readonly<BuildOutputFile[]>,
generatedFiles: Map<string, OutputFileRecord>,
) {
const seen = new Set<string>(['/index.html']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ export class ExecutionResult {
this.externalMetadata = { implicitBrowser, implicitServer, explicit: explicit ?? [] };
}

get output() {
return {
success: this.errors.length === 0,
};
}

get outputWithFiles() {
get outputWithFiles(): Readonly<{
success: boolean;
outputFiles: Readonly<BuildOutputFile[]>;
assetFiles: Readonly<BuildOutputAsset[]>;
errors: Readonly<(Message | PartialMessage)[]>;
externalMetadata: Readonly<ExternalResultMetadata> | undefined;
}> {
return {
success: this.errors.length === 0,
outputFiles: this.outputFiles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export async function writeResultFiles(

const MAX_CONCURRENT_WRITES = 64;
export async function emitFilesToDisk<T = BuildOutputAsset | BuildOutputFile>(
files: T[],
files: Readonly<T[]>,
writeFileCallback: (file: T) => Promise<void>,
): Promise<void> {
// Write files in groups of MAX_CONCURRENT_WRITES to avoid too many open files
Expand Down

0 comments on commit d1a09a2

Please sign in to comment.