Skip to content

Commit 354dbbf

Browse files
committed
fix(@angular/build): re-analyze exports on package.json change in library builder
When `package.json` changes in watch mode, the `exports` field is now re-analyzed to detect any added, removed, or modified entry points. New entry points are registered into watched files and trigger a re-compilation, while removed entry points are pruned from cached bundle results.
1 parent c5b8bf0 commit 354dbbf

6 files changed

Lines changed: 716 additions & 148 deletions

File tree

‎packages/angular/build/src/builders/library/builder.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
normalizeLibraryOptions,
3131
} from './options';
3232
import type { SingleBuildState } from './pipeline/build-action';
33+
import { updateWatchedEntryPoints } from './pipeline/entry-points';
3334
import type { createComponentStylesheetBundlerForLibrary } from './pipeline/stylesheet-bundler';
3435
import type { Schema as LibraryBuilderOptions } from './schema';
3536

@@ -275,6 +276,18 @@ async function* runWatchLoop(
275276
try {
276277
const packageJson = await loadPackageJson(packageJsonPath);
277278
options.packageJson = packageJson;
279+
if (packageJson.name) {
280+
options.packageName = packageJson.name;
281+
}
282+
283+
updateWatchedEntryPoints(
284+
packageJson,
285+
options,
286+
buildState,
287+
allWatchedFiles,
288+
packageJsonPath,
289+
);
290+
278291
hasPackageJsonChanges = true;
279292
} catch (error) {
280293
assertIsError(error);
@@ -290,7 +303,8 @@ async function* runWatchLoop(
290303

291304
const hasSourceChanges =
292305
!buildState.singleProgramCache ||
293-
Boolean(buildState.hasCompilationError) ||
306+
buildState.hasCompilationError ||
307+
buildState.hasEntryPointsChanges ||
294308
hasModifiedWatchedFile(changedFiles, allWatchedFiles, posixPackageJsonPath);
295309

296310
if (

‎packages/angular/build/src/builders/library/options.ts‎

Lines changed: 2 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import { normalizeAssetPatterns } from '../../utils';
1414
import { supportColor } from '../../utils/color';
1515
import { assertIsError } from '../../utils/error';
1616
import { normalizeCacheOptions } from '../../utils/normalize-cache';
17-
import { isSubDirectory, toPosixPath } from '../../utils/path';
17+
import { isSubDirectory } from '../../utils/path';
1818
import {
1919
type PostcssConfiguration,
2020
generateSearchDirectories,
2121
getTailwindConfig,
2222
loadPostcssConfiguration,
2323
} from '../../utils/postcss-configuration';
2424
import { getProjectRootPaths } from '../../utils/project-metadata';
25-
import { type NormalizedEntryPoint, getEntryPointBundleName } from './pipeline/entry-points';
25+
import { type NormalizedEntryPoint, normalizeEntryPoints } from './pipeline/entry-points';
2626
import type { Schema as LibraryBuilderOptions } from './schema';
2727

2828
export type { NormalizedEntryPoint } from './pipeline/entry-points';
@@ -217,147 +217,3 @@ export async function normalizeLibraryOptions(
217217
tailwindConfiguration,
218218
};
219219
}
220-
221-
/**
222-
* Normalizes a single entry point specification.
223-
*
224-
* @param key The entry point key from package.json exports (e.g. '.' or './testing').
225-
* @param posixKey Normalized POSIX key without trailing slashes.
226-
* @param isPrimary Whether this is the primary entry point.
227-
* @param targetPath The relative file path string from exports.
228-
* @param projectRoot The library project root directory.
229-
* @param packageName The root package name (e.g. `@my/lib`).
230-
* @returns The normalized entry point descriptor.
231-
*/
232-
function normalizeEntryPoint(
233-
key: string,
234-
posixKey: string,
235-
isPrimary: boolean,
236-
targetPath: string,
237-
projectRoot: string,
238-
packageName: string,
239-
): NormalizedEntryPoint {
240-
const name = isPrimary
241-
? '.'
242-
: posixKey[0] === '.' && posixKey[1] === '/'
243-
? posixKey.slice(2)
244-
: posixKey;
245-
246-
if (name !== '.' && (path.posix.isAbsolute(name) || name.includes('..'))) {
247-
throw new Error(
248-
`Invalid entry point key '${key}'. Entry point keys must be relative subpaths without '..' (e.g. './testing').`,
249-
);
250-
}
251-
252-
const subpath = isPrimary ? '.' : `./${name}`;
253-
const displayName = isPrimary ? packageName : `${packageName}/${name}`;
254-
const bundleName = getEntryPointBundleName(packageName, name);
255-
256-
const entryFilePath = path.resolve(projectRoot, targetPath);
257-
258-
if (!/(?<!\.d)\.(?:ts|mts)$/.test(entryFilePath)) {
259-
throw new Error(
260-
`Entry point '${key}' file path must be a TypeScript file ('.ts' or '.mts'): '${entryFilePath}'.`,
261-
);
262-
}
263-
264-
return {
265-
subpath,
266-
name,
267-
displayName,
268-
bundleName,
269-
entryFilePath,
270-
isPrimary,
271-
};
272-
}
273-
274-
/**
275-
* Normalizes all entry points from the library's `package.json` `exports` field.
276-
*
277-
* @param rawExports The `exports` field from `package.json`.
278-
* @param projectRoot The library project root directory.
279-
* @param packageJsonPath Path to `package.json` for error reporting.
280-
* @param packageName The root package name (e.g. `@my/lib`).
281-
* @returns A Map of normalized entry points keyed by name.
282-
*/
283-
function normalizeEntryPoints(
284-
rawExports: PackageJsonData['exports'],
285-
projectRoot: string,
286-
packageJsonPath: string,
287-
packageName: string,
288-
): Map<string, NormalizedEntryPoint> {
289-
if (!rawExports || (typeof rawExports !== 'string' && typeof rawExports !== 'object')) {
290-
throw new Error(
291-
`The 'package.json' at '${packageJsonPath}' must contain an 'exports' field defining the primary entry point ('.').`,
292-
);
293-
}
294-
295-
const exportsRecord = typeof rawExports === 'string' ? { '.': rawExports } : rawExports;
296-
297-
const entryPoints = new Map<string, NormalizedEntryPoint>();
298-
let hasPrimary = false;
299-
300-
for (const [key, value] of Object.entries(exportsRecord)) {
301-
let target: string | undefined;
302-
303-
if (typeof value === 'string') {
304-
target = value;
305-
} else if (
306-
typeof value === 'object' &&
307-
value !== null &&
308-
!Array.isArray(value) &&
309-
typeof (value as Record<string, unknown>)['default'] === 'string'
310-
) {
311-
target = (value as Record<string, unknown>)['default'] as string;
312-
}
313-
314-
const posixKey = toPosixPath(key).replace(/\/+$/, '');
315-
const isPrimary = posixKey === '.' || posixKey === '';
316-
317-
if (!target) {
318-
if (isPrimary) {
319-
throw new Error(
320-
`The primary entry point '.' in '${packageJsonPath}' must specify a string path ` +
321-
`or a 'default' condition pointing to a TypeScript file.`,
322-
);
323-
}
324-
325-
// Non-JS/TS conditional export (e.g., sass/style-only subpath); preserve in package.json without compiling.
326-
continue;
327-
}
328-
329-
if (!isPrimary && !/\.m?ts$/.test(target)) {
330-
// Static asset, stylesheet, or package.json export; preserve in package.json without compiling.
331-
continue;
332-
}
333-
334-
const entryPoint = normalizeEntryPoint(
335-
key,
336-
posixKey,
337-
isPrimary,
338-
target,
339-
projectRoot,
340-
packageName,
341-
);
342-
343-
if (entryPoints.has(entryPoint.name)) {
344-
throw new Error(
345-
`Duplicate entry point detected: '${key}' resolves to the same name ('${entryPoint.name}') as an existing entry point.`,
346-
);
347-
}
348-
349-
entryPoints.set(entryPoint.name, entryPoint);
350-
351-
if (entryPoint.isPrimary) {
352-
hasPrimary = true;
353-
}
354-
}
355-
356-
if (!hasPrimary) {
357-
throw new Error(
358-
`The 'exports' field in '${packageJsonPath}' must contain a primary entry point with key '.'.`,
359-
);
360-
}
361-
362-
return entryPoints;
363-
}

‎packages/angular/build/src/builders/library/pipeline/build-action.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface SingleBuildState {
3030
hasCompilationError?: boolean;
3131
hasEmittedManifests?: boolean;
3232
hasEmittedAssets?: boolean;
33+
hasEntryPointsChanges?: boolean;
3334
directoryExists: Set<string>;
3435
}
3536

@@ -86,7 +87,8 @@ export async function buildAction(actionContext: BuildActionContext): Promise<vo
8687
const shouldCompileEntryPoints =
8788
!modifiedFiles ||
8889
!buildState.singleProgramCache ||
89-
Boolean(buildState.hasCompilationError) ||
90+
buildState.hasCompilationError ||
91+
buildState.hasEntryPointsChanges ||
9092
pendingChangedEsmFiles.size > 0 ||
9193
pendingChangedDtsFiles.size > 0 ||
9294
hasModifiedWatchedFile(modifiedFiles, allWatchedFiles, posixPackageJsonPath);
@@ -100,6 +102,7 @@ export async function buildAction(actionContext: BuildActionContext): Promise<vo
100102

101103
if (shouldCompileEntryPoints) {
102104
buildState.hasCompilationError = true;
105+
buildState.hasEntryPointsChanges = false;
103106

104107
const {
105108
esmFiles,

0 commit comments

Comments
 (0)