Skip to content

Commit e6da38a

Browse files
devversionatscott
authored andcommitted
feat(bazel): allow setting compilationMode in ng_module rule (#41418)
Adds a new attribute to the `ng_module` rule that allows users to set the Angular compiler `compilationMode` flag. An alternative would have been to just enable the option in the user-specified tsconfig. Though that is more inconvenient if a Bazel workspace wants to change the compilation mode conditionally at anaylsis phase through build settings. Related to: https://github.com/angular/components/pull/22351t PR Close #41418
1 parent aa36121 commit e6da38a

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

packages/bazel/src/ng_module.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ def _ngc_tsconfig(ctx, files, srcs, **kwargs):
339339
# Summaries are only enabled if Angular outputs are to be produced.
340340
"enableSummariesForJit": is_legacy_ngc,
341341
"enableIvy": is_ivy_enabled(ctx),
342+
"compilationMode": ctx.attr.compilation_mode,
342343
"fullTemplateTypeCheck": ctx.attr.type_check,
343344
# In Google3 we still want to use the symbol factory re-exports in order to
344345
# not break existing apps inside Google. Unlike Bazel, Google3 does not only
@@ -725,6 +726,14 @@ NG_MODULE_ATTRIBUTES = {
725726
"filter_summaries": attr.bool(default = False),
726727
"type_check": attr.bool(default = True),
727728
"inline_resources": attr.bool(default = True),
729+
"compilation_mode": attr.string(
730+
doc = """Set the compilation mode for the Angular compiler.
731+
732+
This attribute is a noop if Ivy is not enabled.
733+
""",
734+
values = ["partial", "full", ""],
735+
default = "",
736+
),
728737
"no_i18n": attr.bool(default = False),
729738
"compiler": attr.label(
730739
doc = """Sets a different ngc compiler binary to use for this library.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
load("//tools:defaults.bzl", "jasmine_node_test", "ng_module", "ts_library")
2+
3+
ts_library(
4+
name = "ng_module_ivy_test_lib",
5+
testonly = True,
6+
srcs = ["ng_module_ivy_test.ts"],
7+
tags = ["ivy-only"],
8+
)
9+
10+
# `ng_module` with `compilation_mode` explicitly set to `partial`.
11+
ng_module(
12+
name = "test_module_partial_compilation",
13+
srcs = ["test_module_partial_compilation.ts"],
14+
compilation_mode = "partial",
15+
tags = ["ivy-only"],
16+
deps = ["//packages/core"],
17+
)
18+
19+
# `ng_module` with `compilation_mode` explicitly set to `full`.
20+
ng_module(
21+
name = "test_module_full_compilation",
22+
srcs = ["test_module_full_compilation.ts"],
23+
compilation_mode = "full",
24+
tags = ["ivy-only"],
25+
deps = ["//packages/core"],
26+
)
27+
28+
# `ng_module` with no specific `compilation_mode` attribute specified.
29+
ng_module(
30+
name = "test_module_default_compilation",
31+
srcs = ["test_module_default_compilation.ts"],
32+
tags = ["ivy-only"],
33+
deps = ["//packages/core"],
34+
)
35+
36+
jasmine_node_test(
37+
name = "ng_module_ivy_test",
38+
srcs = [":ng_module_ivy_test_lib"],
39+
data = [
40+
":test_module_default_compilation",
41+
":test_module_full_compilation",
42+
":test_module_partial_compilation",
43+
],
44+
tags = ["ivy-only"],
45+
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {readFileSync} from 'fs';
10+
11+
/** Runfiles helper from bazel to resolve file name paths. */
12+
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']!);
13+
14+
describe('ng_module with ivy enabled', () => {
15+
describe('default compilation mode', () => {
16+
it('should generate definitions', () => {
17+
const outputFile = runfiles.resolveWorkspaceRelative(
18+
'packages/bazel/test/ngc-wrapped/ivy_enabled/test_module_default_compilation.js');
19+
const fileContent = readFileSync(outputFile, 'utf8');
20+
expect(fileContent).toContain(`TestComponent.ɵcmp = i0.ɵɵdefineComponent(`);
21+
});
22+
});
23+
24+
describe('full compilation mode', () => {
25+
it('should generate definitions', () => {
26+
const outputFile = runfiles.resolveWorkspaceRelative(
27+
'packages/bazel/test/ngc-wrapped/ivy_enabled/test_module_full_compilation.js');
28+
const fileContent = readFileSync(outputFile, 'utf8');
29+
expect(fileContent).toContain(`TestComponent.ɵcmp = i0.ɵɵdefineComponent(`);
30+
});
31+
});
32+
33+
describe('partial compilation mode', () => {
34+
it('should generate declarations', () => {
35+
const outputFile = runfiles.resolveWorkspaceRelative(
36+
'packages/bazel/test/ngc-wrapped/ivy_enabled/test_module_partial_compilation.js');
37+
const fileContent = readFileSync(outputFile, 'utf8');
38+
expect(fileContent).toContain(`TestComponent.ɵcmp = i0.ɵɵngDeclareComponent(`);
39+
});
40+
});
41+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Component} from '@angular/core';
10+
11+
@Component({
12+
template: 'Hello',
13+
})
14+
export class TestComponent {
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Component} from '@angular/core';
10+
11+
@Component({
12+
template: 'Hello',
13+
})
14+
export class TestComponent {
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Component} from '@angular/core';
10+
11+
@Component({
12+
template: 'Hello',
13+
})
14+
export class TestComponent {
15+
}

0 commit comments

Comments
 (0)