豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 04a54cd

Browse files
committed
feat(bazel/spec-bundling): support specifying strategy for handling unknown linker declarations
This is necessary to that we can use the spec bundle rule in Angular itself where Angular packages are using `0.0.0-PLACEHOLDER`.
1 parent 5df32aa commit 04a54cd

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

bazel/spec-bundling/bundle-config.bzl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
def _spec_bundle_config_file_impl(ctx):
2+
run_angular_linker = ctx.attr.run_angular_linker
3+
linker_unknown_declaration_handling = ctx.attr.linker_unknown_declaration_handling
4+
25
ctx.actions.expand_template(
36
template = ctx.file._template,
47
output = ctx.outputs.output_name,
58
substitutions = {
6-
"TMPL_RUN_LINKER": "true" if ctx.attr.run_angular_linker else "false",
9+
"TMPL_RUN_LINKER": "true" if run_angular_linker else "false",
10+
"TMPL_LINKER_UNKNOWN_DECLARATION_HANDLING": ("'%s'" % linker_unknown_declaration_handling) if linker_unknown_declaration_handling else "undefined",
711
},
812
)
913

@@ -19,6 +23,12 @@ spec_bundle_config_file = rule(
1923
mandatory = True,
2024
doc = "Name of the file where the config should be written to.",
2125
),
26+
"linker_unknown_declaration_handling": attr.string(
27+
values = ["ignore", "warn", "error"],
28+
doc = """Controls how unknown declaration versions should be handled by the Angular linker.
29+
https://github.com/angular/angular/blob/f94c6f433dba3924b79f137cfcc49d2dfd4d679c/packages/compiler-cli/linker/src/file_linker/linker_options.ts#L27-L39.
30+
""",
31+
),
2232
"_template": attr.label(
2333
allow_single_file = True,
2434
default = "//bazel/spec-bundling:esbuild.config-tmpl.mjs",

bazel/spec-bundling/esbuild.config-tmpl.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ async function fetchAndCreateLinkerEsbuildPlugin() {
1818
const {createLinkerEsbuildPlugin} = await import(
1919
'@angular/dev-infra-private/shared-scripts/angular-linker/esbuild-plugin.mjs'
2020
);
21-
return await createLinkerEsbuildPlugin(/.*/, /* ensureNoPartialDeclaration */ true);
21+
return await createLinkerEsbuildPlugin(/.*/, /* ensureNoPartialDeclaration */ true, {
22+
unknownDeclarationVersionHandling: TMPL_LINKER_UNKNOWN_DECLARATION_HANDLING,
23+
});
2224
}
2325

2426
// Based on the Bazel action and its substitutions, we run the linker for all inputs.

bazel/spec-bundling/index.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def spec_bundle(
99
platform,
1010
bootstrap = [],
1111
run_angular_linker = False,
12+
linker_unknown_declaration_handling = None,
1213
# We cannot use `ES2017` or higher as that would result in `async/await` not being downleveled.
1314
# ZoneJS needs to be able to intercept these as otherwise change detection would not work properly.
1415
target = "es2016",
@@ -29,6 +30,8 @@ def spec_bundle(
2930
ending with `init.js` are picked up.
3031
target: Target ECMAScript to use for the specs bundle.
3132
run_angular_linker: Whether the Angular linker should process the bundled code.
33+
linker_unknown_declaration_handling: Control how unknown partial declarations should be
34+
treated. This passes through to the `unknownDeclarationVersionHandling` linker plugin option.
3235
external: List of modules/packages which should not be bundled.
3336
workspace_name: Workspace name that needs to be provided for the AMD module name.
3437
"""
@@ -52,6 +55,7 @@ def spec_bundle(
5255
testonly = True,
5356
output_name = "%s_config.mjs" % name,
5457
run_angular_linker = run_angular_linker,
58+
linker_unknown_declaration_handling = linker_unknown_declaration_handling,
5559
)
5660

5761
esbuild_config(

bazel/spec-bundling/test/BUILD.bazel

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@ load("//bazel/spec-bundling:index.bzl", "spec_bundle")
55
load("//tools:defaults.bzl", "ts_library")
66

77
ts_library(
8-
name = "test_lib",
8+
name = "test_lib_apf",
99
testonly = True,
10-
srcs = glob(["**/*.spec.ts"]),
10+
srcs = ["core_apf_esm.spec.ts"],
11+
deps = [
12+
"@npm//@angular/core",
13+
],
14+
)
15+
16+
ts_library(
17+
name = "test_lib_invalid_linker_declaration",
18+
testonly = True,
19+
srcs = ["core_invalid_linker_decl.spec.ts"],
1120
deps = [
1221
"@npm//@angular/core",
1322
],
@@ -17,14 +26,22 @@ spec_bundle(
1726
name = "test_bundle",
1827
platform = "node",
1928
run_angular_linker = True,
20-
deps = [":test_lib"],
29+
deps = [":test_lib_apf"],
2130
)
2231

2332
spec_bundle(
2433
name = "test_bundle_legacy_cjs",
2534
platform = "cjs-legacy",
2635
run_angular_linker = True,
27-
deps = [":test_lib"],
36+
deps = [":test_lib_apf"],
37+
)
38+
39+
spec_bundle(
40+
name = "test_bundle_invalid_declaration_linker",
41+
linker_unknown_declaration_handling = "ignore",
42+
platform = "node",
43+
run_angular_linker = True,
44+
deps = [":test_lib_invalid_linker_declaration"],
2845
)
2946

3047
jasmine_node_test(
@@ -40,3 +57,10 @@ jasmine_node_test(
4057
":test_bundle_legacy_cjs",
4158
],
4259
)
60+
61+
jasmine_node_test(
62+
name = "test_invalid_declaration_linker",
63+
deps = [
64+
":test_bundle_invalid_declaration_linker",
65+
],
66+
)

bazel/spec-bundling/test/core_apf_esm.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('@angular/core ESM import', () => {
1313
class TestCmp {}
1414
core.ɵɵngDeclareComponent({
1515
version: '0.0.0',
16-
minVersion: '12.0.0',
16+
minVersion: '0.0.0',
1717
type: TestCmp,
1818
selector: 'test',
1919
ngImport: core,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This is an ESM import that would usually break within `jasmine_node_test` because it
2+
// consumes devmode CommonJS sources and `rules_nodejs` does not support ESM well yet.
3+
import {VERSION} from '@angular/core';
4+
import * as core from '@angular/core';
5+
6+
describe('@angular/core ESM import', () => {
7+
it('should work', () => {
8+
expect(VERSION.major).toBeGreaterThanOrEqual(13);
9+
});
10+
11+
it('should have run the linker', () => {
12+
expect(() => {
13+
class TestCmp {}
14+
core.ɵɵngDeclareComponent({
15+
version: '0.0.0',
16+
// use a high version that would cause the linking process to fail due to
17+
// an unknown version. We expect the bundling to still work though since
18+
// we set the handling to `ignore` using `linker_unknown_declaration_handling`.
19+
minVersion: '9999999999999.0.0',
20+
type: TestCmp,
21+
selector: 'test',
22+
ngImport: core,
23+
template: `<span>Test template</span>`,
24+
} as any);
25+
}).not.toThrow();
26+
});
27+
});

0 commit comments

Comments
 (0)