Skip to content

Commit 627f048

Browse files
tboschvicb
authored andcommitted
fix(compiler): implement i18n with new compiler
This commit is from vikerman@ closes #19429
1 parent a7798f2 commit 627f048

File tree

3 files changed

+110
-26
lines changed

3 files changed

+110
-26
lines changed

packages/bazel/src/ng_module.bzl

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ def _expected_outs(ctx, label):
5050
declaration_files += [ctx.new_file(ctx.bin_dir, basename + ext) for ext in declarations]
5151
summary_files += [ctx.new_file(ctx.bin_dir, basename + ext) for ext in summaries]
5252

53+
i18n_messages_files = [ctx.new_file(ctx.bin_dir, ctx.label.name + "_ngc_messages.xmb")]
54+
5355
return struct(
5456
closure_js = closure_js_files,
5557
devmode_js = devmode_js_files,
5658
declarations = declaration_files,
5759
summaries = summary_files,
60+
i18n_messages = i18n_messages_files,
5861
)
5962

6063
def _ngc_tsconfig(ctx, files, srcs, **kwargs):
@@ -92,7 +95,69 @@ _collect_summaries_aspect = aspect(
9295
attr_aspects = ["deps"],
9396
)
9497

95-
def _compile_action(ctx, inputs, outputs, config_file_path):
98+
# Extra options passed to Node when running ngc.
99+
_EXTRA_NODE_OPTIONS_FLAGS = [
100+
# Expose the v8 garbage collection API to JS.
101+
"--node_options=--expose-gc"
102+
]
103+
104+
def ngc_compile_action(ctx, label, inputs, outputs, messages_out, config_file_path,
105+
locale=None, i18n_args=[]):
106+
mnemonic = "AngularTemplateCompile"
107+
progress_message = "Compiling Angular templates (ngc) %s" % label
108+
supports_workers = "0"
109+
if locale:
110+
mnemonic = "AngularI18NMerging"
111+
supports_workers = "0"
112+
progress_message = ("Recompiling Angular templates (ngc) %s for locale %s" %
113+
(label, locale))
114+
else:
115+
supports_workers = str(int(ctx.attr._supports_workers))
116+
117+
arguments = _EXTRA_NODE_OPTIONS_FLAGS
118+
# One at-sign makes this a params-file, enabling the worker strategy.
119+
# Two at-signs escapes the argument so it's passed through to ngc
120+
# rather than the contents getting expanded.
121+
if supports_workers == "1":
122+
arguments += ["@@" + config_file_path]
123+
else:
124+
arguments += ["-p", config_file_path]
125+
126+
arguments += i18n_args
127+
128+
ctx.action(
129+
progress_message = progress_message,
130+
mnemonic = mnemonic,
131+
inputs = inputs,
132+
outputs = outputs,
133+
arguments = arguments,
134+
executable = ctx.executable.compiler,
135+
execution_requirements = {
136+
"supports-workers": supports_workers,
137+
},
138+
)
139+
140+
if messages_out != None:
141+
ctx.action(inputs = list(inputs),
142+
outputs = messages_out,
143+
executable = ctx.executable._ng_xi18n,
144+
arguments = (_EXTRA_NODE_OPTIONS_FLAGS +
145+
[config_file_path] +
146+
[messages_out[0].short_path]),
147+
progress_message = "Extracting Angular 2 messages (ng_xi18n)",
148+
mnemonic = "Angular2MessageExtractor")
149+
150+
# Return the parameters of the compilation which will be used to replay the
151+
# ngc action for i18N.
152+
if not locale and not ctx.attr.no_i18n:
153+
return struct(
154+
label = label,
155+
tsconfig = config_file_path,
156+
inputs = inputs,
157+
outputs = outputs,
158+
)
159+
160+
def _compile_action(ctx, inputs, outputs, messages_out, config_file_path):
96161
summaries = depset()
97162
for dep in ctx.attr.deps:
98163
if hasattr(dep, "collect_summaries_aspect_result"):
@@ -108,34 +173,16 @@ def _compile_action(ctx, inputs, outputs, config_file_path):
108173
if hasattr(ctx.attr, "tsconfig") and ctx.file.tsconfig:
109174
action_inputs += [ctx.file.tsconfig]
110175

111-
arguments = ["--node_options=--expose-gc"]
112-
# One at-sign makes this a params-file, enabling the worker strategy.
113-
# Two at-signs escapes the argument so it's passed through to ngc
114-
# rather than the contents getting expanded.
115-
if ctx.attr._supports_workers:
116-
arguments += ["@@" + config_file_path]
117-
else:
118-
arguments += ["-p", config_file_path]
176+
return ngc_compile_action(ctx, ctx.label, action_inputs, outputs, messages_out, config_file_path)
119177

120-
ctx.action(
121-
progress_message = "Compiling Angular templates (ngc) %s" % ctx.label,
122-
mnemonic = "AngularTemplateCompile",
123-
inputs = action_inputs,
124-
outputs = outputs,
125-
arguments = arguments,
126-
executable = ctx.executable.compiler,
127-
execution_requirements = {
128-
"supports-workers": str(int(ctx.attr._supports_workers)),
129-
},
130-
)
131178

132179
def _prodmode_compile_action(ctx, inputs, outputs, config_file_path):
133180
outs = _expected_outs(ctx, ctx.label)
134-
_compile_action(ctx, inputs, outputs + outs.closure_js, config_file_path)
181+
return _compile_action(ctx, inputs, outputs + outs.closure_js, outs.i18n_messages, config_file_path)
135182

136183
def _devmode_compile_action(ctx, inputs, outputs, config_file_path):
137184
outs = _expected_outs(ctx, ctx.label)
138-
_compile_action(ctx, inputs, outputs + outs.devmode_js + outs.declarations + outs.summaries, config_file_path)
185+
_compile_action(ctx, inputs, outputs + outs.devmode_js + outs.declarations + outs.summaries, None, config_file_path)
139186

140187
def ng_module_impl(ctx, ts_compile_actions):
141188
providers = ts_compile_actions(
@@ -147,9 +194,11 @@ def ng_module_impl(ctx, ts_compile_actions):
147194
#addl_declarations = [_expected_outs(ctx)]
148195
#providers["typescript"]["declarations"] += addl_declarations
149196
#providers["typescript"]["transitive_declarations"] += addl_declarations
197+
outs = _expected_outs(ctx, ctx.label)
150198
providers["angular"] = {
151199
"summaries": _expected_outs(ctx, ctx.label).summaries
152200
}
201+
providers["ngc_messages"] = outs.i18n_messages
153202

154203
return providers
155204

@@ -167,7 +216,6 @@ NG_MODULE_ATTRIBUTES = {
167216
".html",
168217
]),
169218

170-
# TODO(alexeagle): wire up when we have i18n in bazel
171219
"no_i18n": attr.bool(default = False),
172220

173221
"compiler": attr.label(
@@ -176,6 +224,12 @@ NG_MODULE_ATTRIBUTES = {
176224
cfg = "host",
177225
),
178226

227+
"_ng_xi18n": attr.label(
228+
default = Label("//src/ngc-wrapped:xi18n"),
229+
executable = True,
230+
cfg = "host",
231+
),
232+
179233
"_supports_workers": attr.bool(default = True),
180234
}
181235

@@ -192,4 +246,4 @@ ng_module = rule(
192246
),
193247
},
194248
outputs = COMMON_OUTPUTS,
195-
)
249+
)

packages/bazel/src/ngc-wrapped/BUILD.bazel

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ licenses(["notice"]) # Apache 2.0
55

66
ts_library(
77
name = "ngc_lib",
8-
srcs = ["index.ts"],
8+
srcs = [
9+
"index.ts",
10+
"extract_i18n.ts",
11+
],
912
deps = [
1013
# BEGIN-INTERNAL
1114
# Only needed when compiling within the Angular repo.
@@ -27,4 +30,15 @@ nodejs_binary(
2730
"@build_bazel_rules_typescript//internal:worker_protocol.proto"
2831
],
2932
visibility = ["//visibility:public"],
30-
)
33+
)
34+
35+
nodejs_binary(
36+
name = "xi18n",
37+
# Entry point assumes the user is outside this WORKSPACE,
38+
# and references our rules with @angular//src/ngc-wrapped
39+
entry_point = "angular/src/ngc-wrapped/index.js/extract_i18n.js",
40+
data = [
41+
":ngc_lib",
42+
],
43+
visibility = ["//visibility:public"],
44+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. 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+
* @fileoverview Extracts i18n messages.
9+
*/
10+
11+
// Entry point
12+
if (require.main === module) {
13+
const args = process.argv.slice(2);
14+
console.error('>>> now yet implemented!');
15+
process.exitCode = 1;
16+
}

0 commit comments

Comments
 (0)