"""Custom rollup_bundle for language service.

Overrides format to AMD and produces only umd and min, no FESM.

We do this so that we bundle all of the dependencies into the bundle
except for typescript, fs and path.

This allows editors and other tools to easily use the language service bundle
without having to provide all of the angular specific peer dependencies.
"""

load("@build_bazel_rules_nodejs//internal/rollup:rollup_bundle.bzl",
    "ROLLUP_ATTRS",
    "rollup_module_mappings_aspect",
    "write_rollup_config",
    "run_rollup",
    "run_uglify"
)
load("//packages/bazel/src:esm5.bzl", "esm5_outputs_aspect", "flatten_esm5", "esm5_root_dir")

# Note: the file is called "umd.js" and "umd.min.js" because of historical
# reasons. The format is actually amd and not umd, but we are afraid to rename
# the file because that would likely break the IDE and other integrations that
# have the path hardcoded in them.
_ROLLUP_OUTPUTS = {
    "build_umd": "%{name}.umd.js",
    "build_umd_min": "%{name}.umd.min.js",
}

def _ls_rollup_bundle(ctx):
  esm5_sources = flatten_esm5(ctx)
  rollup_config = write_rollup_config(ctx,
      root_dir = "/".join([ctx.bin_dir.path, ctx.label.package, esm5_root_dir(ctx)]),
      output_format = "amd")
  run_rollup(ctx, esm5_sources, rollup_config, ctx.outputs.build_umd)
  source_map = run_uglify(ctx, ctx.outputs.build_umd, ctx.outputs.build_umd_min)
  return DefaultInfo(files=depset([ctx.outputs.build_umd, ctx.outputs.build_umd_min, source_map]))

ls_rollup_bundle = rule(
    implementation = _ls_rollup_bundle,
    attrs = dict(ROLLUP_ATTRS, **{
        "deps": attr.label_list(aspects = [
            rollup_module_mappings_aspect,
            esm5_outputs_aspect,
        ]),
    }),
    outputs = _ROLLUP_OUTPUTS,
)