Skip to content

Commit 9a36d79

Browse files
committed
refactor: reduce graph filtering
1 parent 63d9334 commit 9a36d79

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/lib/ng-package/entry-point/write-package.transform.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { glob } from 'tinyglobby';
44
import { AssetPattern } from '../../../ng-package.schema';
55
import { BuildGraph } from '../../graph/build-graph';
66
import { Node } from '../../graph/node';
7+
import { isInProgress } from '../../graph/select';
78
import { transformFromPromise } from '../../graph/transform';
89
import { colors } from '../../utils/color';
910
import { copyFile, exists, readFile, rmdir, stat, writeFile } from '../../utils/fs';
1011
import * as log from '../../utils/log';
1112
import { ensureUnixPath } from '../../utils/path';
12-
import { EntryPointNode, PackageNode, fileUrl, isEntryPoint, isEntryPointInProgress, isPackage } from '../nodes';
13+
import { EntryPointNode, PackageNode, fileUrl, isEntryPoint, isPackage } from '../nodes';
1314
import { NgPackagrOptions } from '../options.di';
1415
import { NgPackage } from '../package';
1516
import { NgEntryPoint } from './entry-point';
@@ -19,7 +20,8 @@ type CompilationMode = 'partial' | 'full' | undefined;
1920
export const writePackageTransform = (options: NgPackagrOptions) =>
2021
transformFromPromise(async graph => {
2122
const spinner = ora({ hideCursor: false, discardStdin: false });
22-
const entryPoint: EntryPointNode = graph.find(isEntryPointInProgress());
23+
const entryPoints = graph.filter(isEntryPoint);
24+
const entryPoint = entryPoints.find(isInProgress);
2325
const ngEntryPoint: NgEntryPoint = entryPoint.data.entryPoint;
2426
const ngPackageNode: PackageNode = graph.find(isPackage);
2527
const ngPackage = ngPackageNode.data;
@@ -28,7 +30,7 @@ export const writePackageTransform = (options: NgPackagrOptions) =>
2830
if (!ngEntryPoint.isSecondaryEntryPoint) {
2931
spinner.start('Copying assets');
3032
try {
31-
await copyAssets(graph, entryPoint, ngPackageNode);
33+
await copyAssets(graph, entryPoint, ngPackageNode, entryPoints);
3234
} catch (error) {
3335
spinner.fail();
3436
throw error;
@@ -59,7 +61,7 @@ export const writePackageTransform = (options: NgPackagrOptions) =>
5961
{
6062
module: relativeUnixFromDestPath(destinationFiles.fesm2022),
6163
typings: relativeUnixFromDestPath(destinationFiles.declarationsBundled),
62-
exports: generatePackageExports(ngEntryPoint, graph),
64+
exports: generatePackageExports(ngEntryPoint, entryPoints),
6365
// webpack v4+ specific flag to enable advanced optimizations and code splitting
6466
sideEffects: ngEntryPoint.packageJson.sideEffects ?? false,
6567
},
@@ -107,21 +109,20 @@ async function copyAssets(
107109
graph: BuildGraph,
108110
entryPointNode: EntryPointNode,
109111
ngPackageNode: PackageNode,
112+
entryPoints: EntryPointNode[],
110113
): Promise<void> {
111114
const ngPackage = ngPackageNode.data;
112-
113115
const globsForceIgnored: string[] = ['.gitkeep', '**/.DS_Store', '**/Thumbs.db', `${ngPackage.dest}/**`];
114116
const defaultAssets: AssetEntry[] = [
115117
{ glob: 'LICENSE', input: '/', output: '/' },
116-
...graph.filter(isEntryPoint).map(({ data }) => {
118+
...entryPoints.map(({ data }) => {
117119
const subpath = data.entryPoint.destinationFiles.directory || '/';
118120

119121
return { glob: 'README.md', input: subpath, output: subpath };
120122
}),
121123
];
122124

123125
const assets: AssetEntry[] = [];
124-
125126
for (const assetPath of [...ngPackage.assets, ...defaultAssets]) {
126127
let asset: AssetEntry;
127128
if (typeof assetPath === 'object') {
@@ -332,7 +333,10 @@ type ConditionalExport = { types?: string; default?: string };
332333
* Generates the `package.json` package exports following APF v13.
333334
* This is supposed to match with: https://github.com/angular/angular/blob/e0667efa6eada64d1fb8b143840689090fc82e52/packages/bazel/src/ng_package/packager.ts#L415.
334335
*/
335-
function generatePackageExports({ destinationPath, packageJson }: NgEntryPoint, graph: BuildGraph): PackageExports {
336+
function generatePackageExports(
337+
{ destinationPath, packageJson }: NgEntryPoint,
338+
entryPoints: EntryPointNode[],
339+
): PackageExports {
336340
const exports: PackageExports = packageJson.exports ? JSON.parse(JSON.stringify(packageJson.exports)) : {};
337341

338342
const insertMappingOrError = (subpath: string, mapping: ConditionalExport) => {
@@ -361,7 +365,6 @@ function generatePackageExports({ destinationPath, packageJson }: NgEntryPoint,
361365

362366
insertMappingOrError('./package.json', { default: './package.json' });
363367

364-
const entryPoints = graph.filter(isEntryPoint);
365368
for (const entryPoint of entryPoints) {
366369
const { destinationFiles, isSecondaryEntryPoint } = entryPoint.data.entryPoint;
367370
const subpath = isSecondaryEntryPoint ? `./${destinationFiles.directory}` : '.';

0 commit comments

Comments
 (0)