load("@angular//:index.bzl", "ng_module")
load("@build_bazel_rules_typescript//:defs.bzl", "ts_devserver")

package(default_visibility = ["//visibility:public"])

ng_module(
    name = "<%= sourceDir %>",
    srcs = glob(["main.ts"]),
    deps = [
        "//apps/<%= fullName %>/src/app",
        "@rxjs",
    ],
)

# Needed because the devserver only loads static files that appear under this
# package.
genrule(
    name = "zone.js",
    srcs = ["//:node_modules/zone.js/dist/zone.min.js"],
    outs = ["zone.min.js"],
    cmd = "cp $< $@",
)

STATIC_FILES = [
    "favicon.ico",
    "index.html",
    "styles.css",
    ":zone.js",
]

ts_devserver(
    name = "devserver",
    data = ["//apps/<%= fullName %>/src/assets"],
    entry_module = "<%= workspaceName %>/apps/<%= fullName %>/src/main",
    scripts = ["//:angular_bundles"],
    serving_path = "/bundle.min.js",
    static_files = STATIC_FILES,
    deps = [":<%= sourceDir %>"],
)

load("@build_bazel_rules_nodejs//:defs.bzl", "rollup_bundle", "nodejs_binary")

rollup_bundle(
    name = "bundle",
    entry_point = "<%= workspaceName %>/apps/<%= fullName %>/src/main",
    deps = [":<%= sourceDir %>"],
)

nodejs_binary(
    name = "prodserver",
    args = ["./apps/<%= fullName %>/src"],
    data = STATIC_FILES + [
        ":bundle",
        "//apps/<%= fullName %>/src/assets",
    ],
    entry_point = "http-server/bin/http-server",
)
