feat(bazel): ng_module produces bundle index (#22176)

It creates the bundle index .d.ts and .metadata.json files.
The names are based on the ng_module target.

PR Close #22176
This commit is contained in:
Alex Eagle
2018-02-13 11:26:06 -08:00
committed by Victor Berchet
parent 6c1e7ac40e
commit 5f52ea3d06
12 changed files with 247 additions and 9 deletions

View File

@ -1 +1,31 @@
# Empty marker file, indicating this directory is a Bazel package.
package(default_visibility = ["//visibility:public"])
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")
nodejs_binary(
name = "rollup_with_build_optimizer",
data = ["@angular_devkit//packages/angular_devkit/build_optimizer:lib"],
# Since our rule extends the one in rules_nodejs, we use the same runtime
# dependency @build_bazel_rules_nodejs_rollup_deps. We don't need any
# additional npm dependencies when we run rollup or uglify.
entry_point = "build_bazel_rules_nodejs_rollup_deps/node_modules/rollup/bin/rollup",
node_modules = "@build_bazel_rules_nodejs_rollup_deps//:node_modules",
)
nodejs_binary(
name = "modify_tsconfig",
data = ["modify_tsconfig.js"],
entry_point = "angular/packages/bazel/src/modify_tsconfig.js",
)
nodejs_binary(
name = "index_bundler",
data = [
# BEGIN-INTERNAL
# Only needed when compiling within the Angular repo.
# Users will get this dependency from node_modules.
"//packages/compiler-cli",
# END-INTERNAL
],
entry_point = "@angular/compiler-cli/src/metadata/bundle_index_main.js",
)

View File

@ -12,6 +12,7 @@ load(":rules_typescript.bzl",
"compile_ts",
"DEPS_ASPECTS",
"ts_providers_dict_to_struct",
"json_marshal",
)
# Calculate the expected output of the template compiler for every source in
@ -220,6 +221,43 @@ def _ts_expected_outs(ctx, label):
_ignored = [label]
return _expected_outs(ctx)
def _write_bundle_index(ctx):
basename = "_%s.bundle_index" % ctx.label.name
tsconfig_file = ctx.actions.declare_file("%s.tsconfig.json" % basename)
metadata_file = ctx.actions.declare_file("%s.metadata.json" % basename)
tstyping_file = ctx.actions.declare_file("%s.d.ts" % basename)
tsconfig = dict(tsc_wrapped_tsconfig(ctx, ctx.files.srcs, ctx.files.srcs), **{
"angularCompilerOptions": {
"flatModuleOutFile": basename,
},
})
if ctx.attr.module_name:
tsconfig["angularCompilerOptions"]["flatModuleId"] = ctx.attr.module_name
# createBundleIndexHost in bundle_index_host.ts will throw if the "files" has more than one entry.
# We don't want to fail() here, however, because not all ng_module's will have the bundle index written.
# So we make the assumption that the index.ts file in the highest parent directory is the entry point.
index_file = None
for f in tsconfig["files"]:
if f.endswith("/index.ts"):
if not index_file or len(f) < len(index_file):
index_file = f
tsconfig["files"] = [index_file]
ctx.actions.write(tsconfig_file, json_marshal(tsconfig))
outputs = [metadata_file, tstyping_file]
ctx.action(
progress_message = "Producing metadata for bundle %s" % ctx.label.name,
executable = ctx.executable._index_bundler,
inputs = ctx.files.srcs + [tsconfig_file],
outputs = outputs,
arguments = ["-p", tsconfig_file.path],
)
return outputs
def ng_module_impl(ctx, ts_compile_actions):
"""Implementation function for the ng_module rule.
@ -247,6 +285,13 @@ def ng_module_impl(ctx, ts_compile_actions):
}
providers["ngc_messages"] = outs.i18n_messages
# Only produces the flattened "index bundle" metadata when requested by some other rule
# and only under Bazel
if hasattr(ctx.executable, "_index_bundler"):
bundle_index_metadata = _write_bundle_index(ctx)
# note, not recursive
providers["angular"]["flat_module_metadata"] = depset(bundle_index_metadata)
return providers
def _ng_module_impl(ctx):
@ -291,6 +336,10 @@ ng_module = rule(
"node_modules": attr.label(
default = Label("@//:node_modules")
),
"_index_bundler": attr.label(
executable = True,
cfg = "host",
default = Label("//packages/bazel/src:index_bundler")),
}),
outputs = COMMON_OUTPUTS,
)