Skip to content

Commit 7134dfe

Browse files
committed
build: refactor ng_package substitutions
This refactoring moves the substitution for `@angular/ssr` and `beasties` from a global configuration to a local one within the `@angular/ssr` package definition. This is achieved by introducing an `extra_substitutions` parameter to the `ng_package` function, allowing package-specific substitutions to be defined. This change improves modularity and removes hardcoded paths from the global substitution rules, making the build system cleaner and easier to maintain. Additionally, this fixes an issue where the generated TypeScript definition files (.d.ts) for `@angular/ssr` had incorrect relative paths for the `beasties` dependency, causing type resolution failures in consuming projects. The new local substitution corrects these paths.
1 parent bceb3e2 commit 7134dfe

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

packages/angular/ssr/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ ng_package(
4949
"@angular/ssr/node",
5050
"../../third_party/beasties",
5151
],
52+
extra_substitutions = {
53+
# Needed for ssr.d.ts file
54+
"\\./third_party/beasties": "../third_party/beasties",
55+
# Needed for the FESM file.
56+
"\\./(.+)/packages/angular/ssr/third_party/beasties": "../third_party/beasties/index.js",
57+
},
5258
nested_packages = [
5359
"//packages/angular/ssr/schematics:pkg",
5460
# Included directly as the generated types reference the types file in this location.

packages/angular/ssr/test/npm_package/package_spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ const CRITTERS_ACTUAL_LICENSE_FILE_PATH = join(
2525
);
2626

2727
describe('NPM Package Tests', () => {
28-
it('should not include the contents of third_party/beasties/index.js in the FESM bundle', async () => {
28+
it('should not include the contents of `third_party/beasties/index.js` in the FESM bundle', async () => {
2929
const fesmFilePath = join(ANGULAR_SSR_PACKAGE_PATH, 'fesm2022/ssr.mjs');
3030
const fesmContent = await readFile(fesmFilePath, 'utf-8');
3131
expect(fesmContent).toContain(`import Beasties from '../third_party/beasties/index.js'`);
3232
});
3333

34+
it('should correctly reference `third_party/beasties` in the ssr types bundle', async () => {
35+
const fesmFilePath = join(ANGULAR_SSR_PACKAGE_PATH, 'types/ssr.d.ts');
36+
const fesmContent = await readFile(fesmFilePath, 'utf-8');
37+
expect(fesmContent).toContain(`import Beasties from '../third_party/beasties'`);
38+
});
39+
3440
describe('third_party/beasties/THIRD_PARTY_LICENSES.txt', () => {
3541
it('should exist', () => {
3642
expect(existsSync(CRITTERS_ACTUAL_LICENSE_FILE_PATH)).toBe(true);

tools/defaults.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ def copy_to_bin(**kwargs):
4545
def js_binary(**kwargs):
4646
_js_binary(**kwargs)
4747

48-
def ng_package(deps = [], **kwargs):
48+
def ng_package(deps = [], extra_substitutions = {}, **kwargs):
49+
nostamp_subs = dict(substitutions["nostamp"], **extra_substitutions)
50+
stamp_subs = dict(substitutions["stamp"], **extra_substitutions)
51+
4952
_ng_package(
5053
deps = deps,
5154
license = "//:LICENSE",
5255
substitutions = select({
53-
"//:stamp": substitutions["stamp"],
54-
"//conditions:default": substitutions["nostamp"],
56+
"//:stamp": stamp_subs,
57+
"//conditions:default": nostamp_subs,
5558
}),
5659
**kwargs
5760
)

tools/substitutions.bzl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ _stamp_substitutions = {
2424
"0.0.0-NG-PACKAGR-PEER-DEP": NG_PACKAGR_PEER_DEP,
2525
"0.0.0-ANGULAR-FW-VERSION": ANGULAR_FW_VERSION,
2626
"0.0.0-ANGULAR-FW-PEER-DEP": ANGULAR_FW_PEER_DEP,
27-
# The below is needed for @angular/ssr FESM file.
28-
"\\./(.+)/packages/angular/ssr/third_party/beasties": "../third_party/beasties/index.js",
2927
}
3028

3129
_no_stamp_substitutions = dict(_stamp_substitutions, **{

0 commit comments

Comments
 (0)