Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): several fixes to assets and files…
Browse files Browse the repository at this point in the history
… writes in browser-esbuild builder

This commit ports #26016 to the esbuilder and also fixes an issue where assets were being outputted in the wrong directory.

Closes #26021
  • Loading branch information
alan-agius4 committed Oct 12, 2023
1 parent 62aa739 commit b2678bb
Show file tree
Hide file tree
Showing 4 changed files with 445 additions and 66 deletions.
4 changes: 4 additions & 0 deletions goldens/circular-deps/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"packages/angular_devkit/build_angular/src/tools/esbuild/bundler-context.ts",
"packages/angular_devkit/build_angular/src/tools/esbuild/utils.ts"
],
[
"packages/angular_devkit/build_angular/src/tools/esbuild/bundler-execution-result.ts",
"packages/angular_devkit/build_angular/src/tools/esbuild/utils.ts"
],
[
"packages/angular_devkit/build_angular/src/tools/webpack/utils/stats.ts",
"packages/angular_devkit/build_angular/src/utils/bundle-calculator.ts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { constants as fsConstants } from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import { BuildOutputFile } from '../../tools/esbuild/bundler-context';
import { BuildOutputAsset } from '../../tools/esbuild/bundler-execution-result';
import { emitFilesToDisk } from '../../tools/esbuild/utils';
import { buildApplicationInternal } from '../application';
import { Schema as ApplicationBuilderOptions } from '../application/schema';
import { logBuilderStatusWarnings } from './builder-status-warnings';
Expand Down Expand Up @@ -74,36 +76,37 @@ function normalizeOptions(options: BrowserBuilderOptions): ApplicationBuilderOpt
// and not output browser files into '/browser'.
async function writeResultFiles(
outputFiles: BuildOutputFile[],
assetFiles: { source: string; destination: string }[] | undefined,
assetFiles: BuildOutputAsset[] | undefined,
outputPath: string,
) {
const directoryExists = new Set<string>();
await Promise.all(
outputFiles.map(async (file) => {
// Ensure output subdirectories exist
const basePath = path.dirname(file.path);
if (basePath && !directoryExists.has(basePath)) {
await fs.mkdir(path.join(outputPath, basePath), { recursive: true });
directoryExists.add(basePath);
}
// Write file contents
await fs.writeFile(path.join(outputPath, file.path), file.contents);
}),
);
const ensureDirectoryExists = async (basePath: string) => {
if (basePath && !directoryExists.has(basePath)) {
await fs.mkdir(path.join(outputPath, basePath), { recursive: true });
directoryExists.add(basePath);
}
};

// Writes the output file to disk and ensures the containing directories are present
await emitFilesToDisk(outputFiles, async (file: BuildOutputFile) => {
// Ensure output subdirectories exist
const basePath = path.dirname(file.path);
await ensureDirectoryExists(basePath);

// Write file contents
await fs.writeFile(path.join(outputPath, file.path), file.contents);
});

if (assetFiles?.length) {
await Promise.all(
assetFiles.map(async ({ source, destination }) => {
// Ensure output subdirectories exist
const basePath = path.dirname(destination);
if (basePath && !directoryExists.has(basePath)) {
await fs.mkdir(path.join(outputPath, basePath), { recursive: true });
directoryExists.add(basePath);
}
// Copy file contents
await fs.copyFile(source, path.join(outputPath), fsConstants.COPYFILE_FICLONE);
}),
);
await emitFilesToDisk(assetFiles, async ({ source, destination }) => {
const basePath = path.dirname(destination);

// Ensure output subdirectories exist
await ensureDirectoryExists(basePath);

// Copy file contents
await fs.copyFile(source, path.join(outputPath, destination), fsConstants.COPYFILE_FICLONE);
});
}
}

Expand Down
Loading

0 comments on commit b2678bb

Please sign in to comment.