build: enable bundle_dts for core package (#28884)

`ng_module` will now include an `src/r3_symbol.d.ts` when compiling the core package under `ngc` togather with `dts bundling`, This is due that `ngcc` relies on this file to be present, but the `r3_symbols` file which is not part of our public api.

With this change, we can now ship an addition dts file which is flattened.

PR Close #28884
This commit is contained in:
Alan
2019-03-06 09:04:18 +01:00
committed by Kara Erickson
parent a746b5b1ea
commit 7b0e9eddd1
7 changed files with 83 additions and 54 deletions

View File

@ -114,5 +114,19 @@ api-extractor: running with
}
const [tsConfig, entryPoint, dtsBundleOut] = process.argv.slice(2);
process.exitCode = runMain(tsConfig, entryPoint, dtsBundleOut);
const entryPoints = entryPoint.split(',');
const dtsBundleOuts = dtsBundleOut.split(',');
if (entryPoints.length !== entryPoints.length) {
throw new Error(
`Entry points count (${entryPoints.length}) does not match Bundle out count (${dtsBundleOuts.length})`);
}
for (let i = 0; i < entryPoints.length; i++) {
process.exitCode = runMain(tsConfig, entryPoints[i], dtsBundleOuts[i]);
if (process.exitCode !== 0) {
break;
}
}
}

View File

@ -21,6 +21,7 @@ load(
)
_FLAT_DTS_FILE_SUFFIX = ".bundle.d.ts"
_R3_SYMBOLS_DTS_FILE = "src/r3_symbols.d.ts"
def compile_strategy(ctx):
"""Detect which strategy should be used to implement ng_module.
@ -142,6 +143,25 @@ def _should_produce_dts_bundle(ctx):
# see: https://github.com/Microsoft/web-build-tools/issues/1029
return _is_legacy_ngc(ctx) and hasattr(ctx.attr, "bundle_dts") and ctx.attr.bundle_dts
def _should_produce_r3_symbols_bundle(ctx):
"""Should we produce r3_symbols bundle.
NGCC relies on having r3_symbols file. This file is located in @angular/core
And should only be included when bundling core in legacy mode.
Args:
ctx: skylark rule execution context
Returns:
true when we should produce r3_symbols dts.
"""
# iif we are compiling @angular/core with ngc we should add this addition dts bundle
# because ngcc relies on having this file.
# see: https://github.com/angular/angular/blob/84406e4d6d93b28b23efbb1701bc5ae1084da67b/packages/compiler-cli/src/ngcc/src/packages/entry_point_bundle.ts#L56
# todo: alan-agius4: remove when ngcc doesn't need this anymore
return _is_legacy_ngc(ctx) and ctx.attr.module_name == "@angular/core"
def _should_produce_flat_module_outs(ctx):
"""Should we produce flat module outputs.
@ -221,13 +241,16 @@ def _expected_outs(ctx):
if not _is_bazel():
metadata_files += [ctx.actions.declare_file(basename + ext) for ext in metadata]
dts_bundle = None
dts_bundles = None
if _should_produce_dts_bundle(ctx):
# We need to add a suffix to bundle as it might collide with the flat module dts.
# The flat module dts out contains several other exports
# https://github.com/angular/angular/blob/master/packages/compiler-cli/src/metadata/index_writer.ts#L18
# https://github.com/angular/angular/blob/84406e4d6d93b28b23efbb1701bc5ae1084da67b/packages/compiler-cli/src/metadata/index_writer.ts#L18
# the file name will be like 'core.bundle.d.ts'
dts_bundle = ctx.actions.declare_file(ctx.label.name + _FLAT_DTS_FILE_SUFFIX)
dts_bundles = [ctx.actions.declare_file(ctx.label.name + _FLAT_DTS_FILE_SUFFIX)]
if _should_produce_r3_symbols_bundle(ctx):
dts_bundles.append(ctx.actions.declare_file(_R3_SYMBOLS_DTS_FILE.replace(".d.ts", _FLAT_DTS_FILE_SUFFIX)))
# We do this just when producing a flat module index for a publishable ng_module
if _should_produce_flat_module_outs(ctx):
@ -254,7 +277,7 @@ def _expected_outs(ctx):
declarations = declaration_files,
summaries = summary_files,
metadata = metadata_files,
dts_bundle = dts_bundle,
dts_bundles = dts_bundles,
bundle_index_typings = bundle_index_typings,
i18n_messages = i18n_messages_files,
)
@ -337,7 +360,7 @@ def ngc_compile_action(
node_opts,
locale = None,
i18n_args = [],
dts_bundle_out = None):
dts_bundles_out = None):
"""Helper function to create the ngc action.
This is exposed for google3 to wire up i18n replay rules, and is not intended
@ -353,7 +376,7 @@ def ngc_compile_action(
node_opts: list of strings, extra nodejs options.
locale: i18n locale, or None
i18n_args: additional command-line arguments to ngc
dts_bundle_out: produced flattened dts file
dts_bundles_out: produced flattened dts file
Returns:
the parameters of the compilation which will be used to replay the ngc action for i18N.
@ -412,25 +435,28 @@ def ngc_compile_action(
mnemonic = "Angular2MessageExtractor",
)
if dts_bundle_out != None:
if dts_bundles_out != None:
# combine the inputs and outputs and filter .d.ts and json files
filter_inputs = [f for f in inputs + outputs if f.path.endswith(".d.ts") or f.path.endswith(".json")]
if _should_produce_flat_module_outs(ctx):
dts_entry_point = "%s.d.ts" % _flat_module_out_file(ctx)
dts_entry_points = ["%s.d.ts" % _flat_module_out_file(ctx)]
else:
dts_entry_point = ctx.attr.entry_point.replace(".ts", ".d.ts")
dts_entry_points = [ctx.attr.entry_point.replace(".ts", ".d.ts")]
if _should_produce_r3_symbols_bundle(ctx):
dts_entry_points.append(_R3_SYMBOLS_DTS_FILE)
ctx.actions.run(
progress_message = "Bundling DTS %s" % str(ctx.label),
mnemonic = "APIExtractor",
executable = ctx.executable._api_extractor,
inputs = filter_inputs,
outputs = [dts_bundle_out],
outputs = dts_bundles_out,
arguments = [
tsconfig_file.path,
"/".join([ctx.bin_dir.path, ctx.label.package, dts_entry_point]),
dts_bundle_out.path,
",".join(["/".join([ctx.bin_dir.path, ctx.label.package, f]) for f in dts_entry_points]),
",".join([f.path for f in dts_bundles_out]),
],
)
@ -454,7 +480,7 @@ def _filter_ts_inputs(all_inputs):
if f.path.endswith(".js") or f.path.endswith(".ts") or f.path.endswith(".json")
]
def _compile_action(ctx, inputs, outputs, dts_bundle_out, messages_out, tsconfig_file, node_opts):
def _compile_action(ctx, inputs, outputs, dts_bundles_out, messages_out, tsconfig_file, node_opts):
# Give the Angular compiler all the user-listed assets
file_inputs = list(ctx.files.assets)
@ -483,7 +509,7 @@ def _compile_action(ctx, inputs, outputs, dts_bundle_out, messages_out, tsconfig
],
)
return ngc_compile_action(ctx, ctx.label, action_inputs, outputs, messages_out, tsconfig_file, node_opts, None, [], dts_bundle_out)
return ngc_compile_action(ctx, ctx.label, action_inputs, outputs, messages_out, tsconfig_file, node_opts, None, [], dts_bundles_out)
def _prodmode_compile_action(ctx, inputs, outputs, tsconfig_file, node_opts):
outs = _expected_outs(ctx)
@ -492,7 +518,7 @@ def _prodmode_compile_action(ctx, inputs, outputs, tsconfig_file, node_opts):
def _devmode_compile_action(ctx, inputs, outputs, tsconfig_file, node_opts):
outs = _expected_outs(ctx)
compile_action_outputs = outputs + outs.devmode_js + outs.declarations + outs.summaries + outs.metadata
_compile_action(ctx, inputs, compile_action_outputs, outs.dts_bundle, None, tsconfig_file, node_opts)
_compile_action(ctx, inputs, compile_action_outputs, outs.dts_bundles, None, tsconfig_file, node_opts)
def _ts_expected_outs(ctx, label, srcs_files = []):
# rules_typescript expects a function with two or more arguments, but our
@ -553,8 +579,8 @@ def ng_module_impl(ctx, ts_compile_actions):
flat_module_out_file = _flat_module_out_file(ctx),
)
if outs.dts_bundle != None:
providers["dts_bundle"] = outs.dts_bundle
if outs.dts_bundles != None:
providers["dts_bundles"] = outs.dts_bundles
return providers

View File

@ -229,8 +229,8 @@ def _ng_package_impl(ctx):
# fallback to a reasonable default
flat_module_out_file = "index.js"
if hasattr(dep, "dts_bundle"):
bundled_type_definitions.append(dep.dts_bundle)
if hasattr(dep, "dts_bundles"):
bundled_type_definitions += dep.dts_bundles
elif len(type_definitions) == 0:
# Filter out all TypeScript definitions generated by NGC as well as definition files
# that do not belong to the current package. We only want to package types that belong