Skip to content

Commit 132f546

Browse files
committed
fix(@angular/build): declare tslib in the built library package
A library compiles with importHelpers, so a bundle can carry a live 'import ... from tslib' while the library's own manifest declares nothing and the built package gets no dependencies field at all. On a hoisted node_modules it resolves anyway; on a strict layout the consumer gets ERR_MODULE_NOT_FOUND at runtime. The range comes from @angular/compiler, resolved from the workspace, which is what ng-packagr reads. A tslib the library already declares, in either dependencies or peerDependencies, is left alone. Fixes #34131
1 parent c5b8bf0 commit 132f546

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import { createRequire } from 'node:module';
910
import type { BuilderContext } from '@angular-devkit/architect';
1011
import fs from 'node:fs/promises';
1112
import path from 'node:path';
@@ -49,6 +50,8 @@ export interface PackageJsonData {
4950
export interface NormalizedLibraryOptions {
5051
workspaceRoot: string;
5152
projectRoot: string;
53+
/** The tslib range `@angular/compiler` declares, when it can be resolved from the workspace. */
54+
tslibVersion?: string;
5255
packageName: string;
5356
packageJson: PackageJsonData;
5457
outputPath: string;
@@ -193,6 +196,7 @@ export async function normalizeLibraryOptions(
193196
projectRoot,
194197
packageName,
195198
packageJson,
199+
tslibVersion: resolveAngularTslibRange(workspaceRoot),
196200
outputPath: resolvedOutputPath,
197201
deleteOutputPath,
198202
packageJsonPath,
@@ -361,3 +365,26 @@ function normalizeEntryPoints(
361365

362366
return entryPoints;
363367
}
368+
369+
/**
370+
* The tslib range `@angular/compiler` declares, or `undefined` when it cannot be resolved.
371+
*
372+
* Resolved from the workspace so a library gets the range of the Angular it is built against,
373+
* which is what ng-packagr reads too. A missing or unreadable manifest is not an error: the
374+
* output then keeps exactly the dependencies the library declared itself.
375+
*/
376+
function resolveAngularTslibRange(workspaceRoot: string): string | undefined {
377+
try {
378+
const workspaceRequire = createRequire(path.join(workspaceRoot, 'index.js'));
379+
const { dependencies, peerDependencies } = workspaceRequire(
380+
'@angular/compiler/package.json',
381+
) as {
382+
dependencies?: Record<string, string>;
383+
peerDependencies?: Record<string, string>;
384+
};
385+
386+
return peerDependencies?.['tslib'] ?? dependencies?.['tslib'];
387+
} catch {
388+
return undefined;
389+
}
390+
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ export function generatePackageManifests(
2626
options: NormalizedLibraryOptions,
2727
isWatchMode: boolean,
2828
): MemoryOutputFile[] {
29-
const { packageJson: rawPackageJson, keepLifecycleScripts, compilationMode } = options;
29+
const {
30+
packageJson: rawPackageJson,
31+
keepLifecycleScripts,
32+
compilationMode,
33+
tslibVersion,
34+
} = options;
3035

3136
const {
3237
devDependencies: _devDependencies,
@@ -73,6 +78,14 @@ export function generatePackageManifests(
7378
version: isWatchMode ? `0.0.0-watch+${Date.now()}` : version,
7479
};
7580

81+
// A library compiles with `importHelpers`, so a bundle can carry a live
82+
// `import ... from 'tslib'` while the library's own manifest declares nothing.
83+
// On a hoisted node_modules it resolves anyway; on a strict layout the consumer
84+
// gets ERR_MODULE_NOT_FOUND at runtime. Declare it, as ng-packagr does.
85+
if (tslibVersion && !rawPackageJson.dependencies?.tslib && !rawPackageJson.peerDependencies?.tslib) {
86+
distPackageJson.dependencies = { ...distPackageJson.dependencies, tslib: tslibVersion };
87+
}
88+
7689
// Retain scripts if keepLifecycleScripts is set
7790
if (keepLifecycleScripts && scripts) {
7891
distPackageJson.scripts = scripts;

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,42 @@ describe('generatePackageManifests', () => {
307307
);
308308
expect(getRootPackageJson(files2).sideEffects).toEqual(['*.css']);
309309
});
310+
311+
it('declares tslib when the library does not', () => {
312+
// A library compiles with importHelpers, so a bundle can import tslib while the
313+
// library's own manifest declares nothing. On a strict node_modules layout the
314+
// consumer then gets ERR_MODULE_NOT_FOUND. ng-packagr adds it; so does this.
315+
const options = createOptions({ tslibVersion: '^2.3.0' });
316+
const result = getRootPackageJson(generatePackageManifests(options, false));
317+
318+
expect(result.dependencies).toEqual({ tslib: '^2.3.0' });
319+
});
320+
321+
it('leaves a declared tslib alone', () => {
322+
const options = createOptions({
323+
tslibVersion: '^2.3.0',
324+
packageJson: { name: 'my-lib', dependencies: { tslib: '^2.0.0' } },
325+
});
326+
const result = getRootPackageJson(generatePackageManifests(options, false));
327+
328+
expect(result.dependencies).toEqual({ tslib: '^2.0.0' });
329+
});
330+
331+
it('does not move a tslib that is declared as a peer', () => {
332+
const options = createOptions({
333+
tslibVersion: '^2.3.0',
334+
packageJson: { name: 'my-lib', peerDependencies: { tslib: '^2.0.0' } },
335+
});
336+
const result = getRootPackageJson(generatePackageManifests(options, false));
337+
338+
expect(result.dependencies).toBeUndefined();
339+
expect(result.peerDependencies).toEqual({ tslib: '^2.0.0' });
340+
});
341+
342+
it('adds nothing when the range cannot be resolved', () => {
343+
const options = createOptions();
344+
const result = getRootPackageJson(generatePackageManifests(options, false));
345+
346+
expect(result.dependencies).toBeUndefined();
347+
});
310348
});

0 commit comments

Comments
 (0)