blob: c7c13c7730459cd413bf8b0b77c5338ef733aac4 [file] [log] [blame]
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_web")
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@io_bazel_rules_sass//:defs.bzl", "sass_binary")
load("@npm//@babel/cli:index.bzl", "babel")
load("@npm//history-server:index.bzl", "history_server")
load("@npm//html-insert-assets:index.bzl", "html_insert_assets")
load("@npm_angular_bazel//:index.bzl", "ng_module")
load("@npm_bazel_rollup//:index.bzl", "rollup_bundle")
load("@npm_bazel_terser//:index.bzl", "terser_minified")
load("@npm_bazel_typescript//:index.bzl", "ts_config", "ts_devserver", "ts_library")
package(default_visibility = ["//:__subpackages__"])
ts_config(
name = "tsconfig-test",
src = "tsconfig-test.json",
deps = [":tsconfig.json"],
)
# Run the sass compiler to output "styles.css"
# TODO(alexeagle): demonstrate the sass_library rule too
sass_binary(
name = "styles",
src = "styles.scss",
)
ts_library(
name = "initialize_testbed",
testonly = 1,
srcs = [
"initialize_testbed.ts",
],
deps = [
"@npm//@angular/core",
"@npm//@angular/platform-browser-dynamic",
"@npm//@types",
],
)
ng_module(
name = "src",
srcs = [
"main.dev.ts",
"main.prod.ts",
],
tsconfig = ":tsconfig.json",
deps = [
"//src/app",
"@npm//@angular/core",
"@npm//@angular/platform-browser",
"@npm//@angular/router",
"@npm//@ngrx/store",
],
)
filegroup(
name = "rxjs_umd_modules",
srcs = [
":rxjs_shims.js",
"@npm//:node_modules/rxjs/bundles/rxjs.umd.js",
],
)
# Files that we serve in both development and production
_ASSETS = [
# This label references an output of the "styles" sass_binary above.
":styles.css",
# Directly reference a file that came from @angular/material npm package
"@npm//:node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
# We load zone.js outside the bundle. That's because it's a "pollyfill"
# which speculates that such features might be available in a browser.
# Also it's tricky to configure dead code elimination to understand that
# zone.js is used, given that we don't have any import statement that
# imports from it.
"@npm//:node_modules/zone.js/dist/zone.min.js",
]
html_insert_assets(
name = "inject_scripts_for_dev",
outs = ["index.html"],
args = [
"--html=$(execpath //src:example/index.html)",
"--out=$@",
"--roots=. $(RULEDIR)",
"--assets",
] + ["$(execpath %s)" % s for s in _ASSETS] + [
# This file doesn't exist during the build, but will be served by ts_devserver
"_/ts_scripts.js",
],
data = ["//src:example/index.html"] + _ASSETS,
)
# This devserver is written in Go and is super-fast.
# It doesn't run any bundler or code splitter. Instead, it concatenates
# named UMD and named AMD JavaScript code on-the-fly in-memory.
# This scales really well for massive codebases.
ts_devserver(
name = "devserver",
# Serve src/example/index.html at /index.html
additional_root_paths = ["src/example"],
# Run the program from the development version of the main
entry_module = "examples_angular/src/main.dev",
# These scripts will be included in the JS bundle after require.js
# They should have only named UMD modules, or require.js will throw.
scripts = [
"@npm//:node_modules/tslib/tslib.js",
":rxjs_umd_modules",
# We are manaully adding the bazel generated named-UMD date-fns bundle here as
# named-UMD bundles for non-APF npm packages are not yet automatically added.
# This file is generated by the npm_umd_bundle @npm//date-fns:date-fns__umd
# rule that is setup by yarn_install.
"@npm//date-fns:date-fns.umd.js",
],
# Serve these files in addition to the JavaScript bundle
static_files = _ASSETS + [
":inject_scripts_for_dev",
"//src/assets",
],
# Tell Bazel to build the sources first
deps = ["//src"],
)
rollup_bundle(
name = "bundle-es2015",
config_file = "rollup.config.js",
entry_points = {
":main.prod.ts": "index",
},
output_dir = True,
deps = [
"//src",
"@npm//rollup-plugin-commonjs",
"@npm//rollup-plugin-node-resolve",
],
)
babel(
name = "bundle-es5",
args = [
"$(execpath :bundle-es2015)",
"--no-babelrc",
"--source-maps",
"--presets=@babel/preset-env",
"--out-dir",
"$(@D)",
],
data = [
":bundle-es2015",
"@npm//@babel/preset-env",
],
output_dir = True,
)
terser_minified(
name = "bundle-es2015.min",
src = ":bundle-es2015",
)
terser_minified(
name = "bundle-es5.min",
src = ":bundle-es5",
)
html_insert_assets(
name = "inject_scripts_for_prod",
# we can't output "src/example/index.html" since that collides with the devmode file.
# pkg_web rule will re-root paths that start with _{name} by default
# so we output "_prodapp/src/example/index.html" so that it is mapped to
# `example/index.html` in the web package.
outs = ["_prodapp/src/example/index.html"],
args = [
"--html=$(execpath //src:example/index.prod.html)",
"--out=$@",
"--roots=. $(RULEDIR)",
"--assets",
] + ["$(execpath %s)" % s for s in _ASSETS],
data = ["//src:example/index.prod.html"] + _ASSETS,
)
pkg_web(
name = "prodapp",
srcs = _ASSETS + [
":bundle-es2015.min",
":bundle-es5.min",
":inject_scripts_for_prod",
"//src/assets",
# Include polyfills that will be requested by old browsers
"@npm//:node_modules/systemjs/dist/system.js",
"@npm//:node_modules/core-js/client/core.min.js",
"index.html",
],
# In production mode we serve some polyfills with script tags that have hard-coded paths in the index.html
# so we must serve them at that path, by stripping a prefix
additional_root_paths = [
"npm/node_modules/core-js/client",
"npm/node_modules/systemjs/dist",
],
)
history_server(
name = "prodserver",
data = [":prodapp"],
# '-a src/prodapp' will ask history-server to scan for all apps under the
# given folder this will result in the following auto-configuration:
# /example => src/prodapp/example
# / => src/prodapp
templated_args = [
"-a",
"src/prodapp",
],
)
nodejs_image(
name = "nodejs_image",
data = [":prodapp"],
entry_point = "@npm//:node_modules/history-server/modules/cli.js",
node_modules = "@npm//:node_modules",
# Actions created by this rule are I/O-bound,
# so there is no benefit to running them remotely
tags = ["local"],
templated_args = ["src/prodapp"],
)
container_image(
name = "image",
base = ":nodejs_image",
# Actions created by this rule are I/O-bound,
# so there is no benefit to running them remotely
tags = ["local"],
workdir = "/app/src/nodejs_image.binary.runfiles/examples_angular",
)