2018-01-09 15:24:00 -08:00
"""Re-export of some bazel rules with repository-wide defaults."""
2018-08-07 16:15:45 -07:00
2020-02-04 11:45:40 -08:00
load ( "@bazel_tools//tools/build_defs/pkg:pkg.bzl" , "pkg_tar" )
2019-12-28 18:14:36 -08:00
load ( "@build_bazel_rules_nodejs//:index.bzl" , _nodejs_binary = "nodejs_binary" , _pkg_npm = "pkg_npm" )
2019-02-22 13:22:11 -08:00
load ( "@npm_bazel_jasmine//:index.bzl" , _jasmine_node_test = "jasmine_node_test" )
2019-11-13 09:08:38 -08:00
load ( "@npm_bazel_karma//:index.bzl" , _karma_web_test = "karma_web_test" , _karma_web_test_suite = "karma_web_test_suite" )
2020-02-04 11:45:40 -08:00
load ( "@npm_bazel_rollup//:index.bzl" , _rollup_bundle = "rollup_bundle" )
load ( "@npm_bazel_terser//:index.bzl" , "terser_minified" )
2019-11-25 13:46:16 -08:00
load ( "@npm_bazel_typescript//:index.bzl" , _ts_devserver = "ts_devserver" , _ts_library = "ts_library" )
2019-11-01 11:17:10 -07:00
load ( "@npm_bazel_protractor//:index.bzl" , _protractor_web_test_suite = "protractor_web_test_suite" )
2020-02-04 11:45:40 -08:00
load ( "@npm//typescript:index.bzl" , "tsc" )
2018-02-13 11:26:06 -08:00
load ( "//packages/bazel:index.bzl" , _ng_module = "ng_module" , _ng_package = "ng_package" )
2019-10-29 14:21:30 -07:00
load ( "//tools/ng_rollup_bundle:ng_rollup_bundle.bzl" , _ng_rollup_bundle = "ng_rollup_bundle" )
2019-10-24 17:40:17 -07:00
load ( "//tools:ng_benchmark.bzl" , _ng_benchmark = "ng_benchmark" )
2018-01-09 15:24:00 -08:00
2019-02-13 09:16:27 -08:00
_DEFAULT_TSCONFIG_TEST = "//packages:tsconfig-test"
2019-03-09 13:24:20 +01:00
_INTERNAL_NG_MODULE_API_EXTRACTOR = "//packages/bazel/src/api-extractor:api_extractor"
2018-10-16 13:20:00 -07:00
_INTERNAL_NG_MODULE_COMPILER = "//packages/bazel/src/ngc-wrapped"
_INTERNAL_NG_MODULE_XI18N = "//packages/bazel/src/ngc-wrapped:xi18n"
2019-10-29 14:21:30 -07:00
_INTERNAL_NG_PACKAGE_PACKAGER = "//packages/bazel/src/ng_package:packager"
_INTERNAL_NG_PACKAGE_DEFALUT_TERSER_CONFIG_FILE = "//packages/bazel/src/ng_package:terser_config.default.json"
_INTERNAL_NG_PACKAGE_DEFAULT_ROLLUP_CONFIG_TMPL = "//packages/bazel/src/ng_package:rollup.config.js"
_INTERNAL_NG_PACKAGE_DEFAULT_ROLLUP = "//packages/bazel/src/ng_package:rollup_for_ng_package"
2018-01-09 15:24:00 -08:00
2018-03-13 11:00:53 -07:00
# Packages which are versioned together on npm
ANGULAR_SCOPED_PACKAGES = [ "@angular/ %s " % p for p in [
2018-08-07 16:15:45 -07:00
# core should be the first package because it's the main package in the group
# this is significant for Angular CLI and "ng update" specifically, @angular/core
# is considered the identifier of the group by these tools.
"core" ,
"bazel" ,
"common" ,
"compiler" ,
"compiler-cli" ,
"animations" ,
"elements" ,
"platform-browser" ,
"platform-browser-dynamic" ,
"forms" ,
2019-03-27 10:50:39 -07:00
# Current plan for Angular v8 is to not include @angular/http in ng update
# "http",
2018-08-07 16:15:45 -07:00
"platform-server" ,
"platform-webworker" ,
"platform-webworker-dynamic" ,
"upgrade" ,
"router" ,
"language-service" ,
2019-11-11 07:52:46 +01:00
"localize" ,
2018-08-07 16:15:45 -07:00
"service-worker" ,
2018-03-13 11:00:53 -07:00
]]
PKG_GROUP_REPLACEMENTS = {
" \" NG_UPDATE_PACKAGE_GROUP \" " : """[
%s
2018-08-07 16:15:45 -07:00
]""" % ", \n " . join ([ " \" %s \" " % s for s in ANGULAR_SCOPED_PACKAGES ]),
2018-03-13 11:00:53 -07:00
}
2019-01-10 11:55:20 -08:00
def _default_module_name ( testonly ):
""" Provide better defaults for package names.
e.g. rather than angular/packages/core/testing we want @angular/core/testing
TODO(alexeagle): we ought to supply a default module name for every library in the repo.
But we short-circuit below in cases that are currently not working.
"""
pkg = native . package_name ()
if testonly :
# Some tests currently rely on the long-form package names
return None
if pkg . startswith ( "packages/bazel" ):
# Avoid infinite recursion in the ViewEngine compiler. Error looks like:
# Compiling Angular templates (ngc) //packages/bazel/test/ngc-wrapped/empty:empty failed (Exit 1)
# : RangeError: Maximum call stack size exceeded
# at normalizeString (path.js:57:25)
# at Object.normalize (path.js:1132:12)
# at Object.join (path.js:1167:18)
# at resolveModule (execroot/angular/bazel-out/host/bin/packages/bazel/src/ngc-wrapped/ngc-wrapped.runfiles/angular/packages/compiler-cli/src/metadata/bundler.js:582:50)
# at MetadataBundler.exportAll (execroot/angular/bazel-out/host/bin/packages/bazel/src/ngc-wrapped/ngc-wrapped.runfiles/angular/packages/compiler-cli/src/metadata/bundler.js:119:42)
# at MetadataBundler.exportAll (execroot/angular/bazel-out/host/bin/packages/bazel/src/ngc-wrapped/ngc-wrapped.runfiles/angular/packages/compiler-cli/src/metadata/bundler.js:121:52)
return None
if pkg . startswith ( "packages/" ):
return "@angular/" + pkg [ len ( "packages/" ):]
return None
2019-11-25 13:46:16 -08:00
def ts_devserver ( ** kwargs ):
"""Default values for ts_devserver"""
serving_path = kwargs . pop ( "serving_path" , "/app_bundle.js" )
_ts_devserver (
serving_path = serving_path ,
** kwargs
)
2019-12-28 18:14:36 -08:00
def ts_library ( name , tsconfig = None , testonly = False , deps = [], module_name = None , ** kwargs ):
2018-10-04 13:14:14 -07:00
"""Default values for ts_library"""
2019-02-20 09:54:42 -08:00
deps = deps + [ "@npm//tslib" ]
2018-10-04 13:14:14 -07:00
if testonly :
# Match the types[] in //packages:tsconfig-test.json
2019-02-20 09:54:42 -08:00
deps . append ( "@npm//@types/jasmine" )
deps . append ( "@npm//@types/node" )
2019-06-21 21:52:00 -07:00
deps . append ( "@npm//@types/events" )
2019-04-02 11:42:58 -07:00
if not tsconfig and testonly :
tsconfig = _DEFAULT_TSCONFIG_TEST
2018-10-15 23:24:22 -07:00
2019-01-10 11:55:20 -08:00
if not module_name :
module_name = _default_module_name ( testonly )
2018-10-04 13:14:14 -07:00
_ts_library (
2019-12-28 18:14:36 -08:00
name = name ,
2018-10-04 13:14:14 -07:00
tsconfig = tsconfig ,
testonly = testonly ,
deps = deps ,
2019-01-10 11:55:20 -08:00
module_name = module_name ,
2018-10-04 13:14:14 -07:00
** kwargs
)
2018-01-09 15:24:00 -08:00
2019-12-28 18:14:36 -08:00
# Select the es5 .js output of the ts_library for use in downstream boostrap targets
# with `output_group = "es5_sources"`. This exposes an internal detail of ts_library
# that is not ideal.
# TODO(gregmagolan): clean this up by using tsc() in these cases rather than ts_library
native . filegroup (
name = " %s _es5" % name ,
srcs = [ ": %s " % name ],
testonly = testonly ,
output_group = "es5_sources" ,
)
2019-03-06 11:08:42 +01:00
def ng_module ( name , tsconfig = None , entry_point = None , testonly = False , deps = [], module_name = None , bundle_dts = True , ** kwargs ):
2018-10-04 13:14:14 -07:00
"""Default values for ng_module"""
2019-02-20 09:54:42 -08:00
deps = deps + [ "@npm//tslib" ]
2018-10-04 13:14:14 -07:00
if testonly :
# Match the types[] in //packages:tsconfig-test.json
2019-02-20 09:54:42 -08:00
deps . append ( "@npm//@types/jasmine" )
deps . append ( "@npm//@types/node" )
2019-06-21 21:52:00 -07:00
deps . append ( "@npm//@types/events" )
2019-04-02 11:42:58 -07:00
if not tsconfig and testonly :
tsconfig = _DEFAULT_TSCONFIG_TEST
2019-01-10 11:55:20 -08:00
if not module_name :
module_name = _default_module_name ( testonly )
2018-08-07 16:15:45 -07:00
if not entry_point :
entry_point = "public_api.ts"
2018-10-04 13:14:14 -07:00
_ng_module (
name = name ,
flat_module_out_file = name ,
tsconfig = tsconfig ,
entry_point = entry_point ,
testonly = testonly ,
2019-03-06 11:08:42 +01:00
bundle_dts = bundle_dts ,
2018-10-04 13:14:14 -07:00
deps = deps ,
2018-10-16 13:20:00 -07:00
compiler = _INTERNAL_NG_MODULE_COMPILER ,
2019-03-09 13:24:20 +01:00
api_extractor = _INTERNAL_NG_MODULE_API_EXTRACTOR ,
2018-10-16 13:20:00 -07:00
ng_xi18n = _INTERNAL_NG_MODULE_XI18N ,
2019-01-10 11:55:20 -08:00
module_name = module_name ,
2018-10-04 13:14:14 -07:00
** kwargs
)
2018-05-21 15:48:00 -07:00
2018-10-04 13:14:14 -07:00
def ng_package ( name , readme_md = None , license_banner = None , deps = [], ** kwargs ):
"""Default values for ng_package"""
2018-08-07 16:15:45 -07:00
if not readme_md :
readme_md = "//packages:README.md"
if not license_banner :
license_banner = "//packages:license-banner.txt"
2018-10-04 13:14:14 -07:00
deps = deps + [
2019-02-20 09:54:42 -08:00
"@npm//tslib" ,
2018-10-04 13:14:14 -07:00
]
2020-02-04 11:45:40 -08:00
visibility = kwargs . pop ( "visibility" , None )
2018-02-13 11:26:06 -08:00
2018-08-07 16:15:45 -07:00
_ng_package (
name = name ,
2018-10-04 13:14:14 -07:00
deps = deps ,
2018-08-07 16:15:45 -07:00
readme_md = readme_md ,
license_banner = license_banner ,
2019-12-28 16:37:59 -08:00
substitutions = PKG_GROUP_REPLACEMENTS ,
2019-10-29 14:21:30 -07:00
ng_packager = _INTERNAL_NG_PACKAGE_PACKAGER ,
terser_config_file = _INTERNAL_NG_PACKAGE_DEFALUT_TERSER_CONFIG_FILE ,
rollup_config_tmpl = _INTERNAL_NG_PACKAGE_DEFAULT_ROLLUP_CONFIG_TMPL ,
rollup = _INTERNAL_NG_PACKAGE_DEFAULT_ROLLUP ,
2020-02-04 11:45:40 -08:00
visibility = visibility ,
2018-08-07 16:15:45 -07:00
** kwargs
)
2018-03-13 11:00:53 -07:00
2020-02-04 11:45:40 -08:00
pkg_tar (
name = name + "_archive" ,
srcs = [ ": %s " % name ],
extension = "tar.gz" ,
strip_prefix = "./ %s " % name ,
# should not be built unless it is a dependency of another rule
tags = [ "manual" ],
visibility = visibility ,
)
2019-12-28 16:37:59 -08:00
def pkg_npm ( name , substitutions = {}, ** kwargs ):
2020-02-04 11:45:40 -08:00
"""Default values for pkg_npm"""
visibility = kwargs . pop ( "visibility" , None )
2019-12-28 16:37:59 -08:00
_pkg_npm (
2018-08-07 16:15:45 -07:00
name = name ,
2019-12-28 16:37:59 -08:00
substitutions = dict ( substitutions , ** PKG_GROUP_REPLACEMENTS ),
2020-02-04 11:45:40 -08:00
visibility = visibility ,
2018-08-07 16:15:45 -07:00
** kwargs
)
2018-03-15 18:33:52 -07:00
2020-02-04 11:45:40 -08:00
pkg_tar (
name = name + "_archive" ,
srcs = [ ": %s " % name ],
extension = "tar.gz" ,
strip_prefix = "./ %s " % name ,
# should not be built unless it is a dependency of another rule
tags = [ "manual" ],
visibility = visibility ,
)
2020-02-23 17:35:46 -08:00
def karma_web_test_suite ( name , ** kwargs ):
"""Default values for karma_web_test_suite"""
# Default value for bootstrap
bootstrap = kwargs . pop ( "bootstrap" , [
"//:web_test_bootstrap_scripts" ,
])
# Add common deps
deps = kwargs . pop ( "deps" , []) + [
2019-02-20 09:54:42 -08:00
"@npm//karma-browserstack-launcher" ,
2019-12-03 14:25:57 -08:00
"@npm//karma-sauce-launcher" ,
2019-06-18 21:26:53 -07:00
"@npm//:node_modules/tslib/tslib.js" ,
2018-12-17 22:09:39 -08:00
"//tools/rxjs:rxjs_umd_modules" ,
2019-10-06 12:06:53 +09:00
"//packages/zone.js:npm_package" ,
2020-02-23 17:35:46 -08:00
]
2018-12-17 22:09:39 -08:00
2020-02-23 17:35:46 -08:00
# Add common runtime deps
runtime_deps = kwargs . pop ( "runtime_deps" , []) + [
"//tools/testing:browser" ,
]
data = kwargs . pop ( "data" , [])
tags = kwargs . pop ( "tags" , [])
_karma_web_test_suite (
name = name ,
runtime_deps = runtime_deps ,
bootstrap = bootstrap ,
deps = deps ,
browsers = [ "//tools/browsers:chromium" ],
data = data ,
tags = tags ,
** kwargs
)
# Add a saucelabs target for these karma tests
2018-12-17 22:09:39 -08:00
_karma_web_test (
2020-02-23 17:35:46 -08:00
name = "saucelabs_ %s " % name ,
# Default timeout is moderate (5min). This causes the test to be terminated while
# Saucelabs browsers keep running. Ultimately resulting in failing tests and browsers
# unnecessarily being acquired. Our specified Saucelabs idle timeout is 10min, so we use
# Bazel's long timeout (15min). This ensures that Karma can shut down properly.
timeout = "long" ,
runtime_deps = runtime_deps ,
2018-12-17 22:09:39 -08:00
bootstrap = bootstrap ,
config_file = "//:karma-js.conf.js" ,
2020-02-23 17:35:46 -08:00
deps = deps ,
2018-12-17 22:09:39 -08:00
data = data + [
"//:browser-providers.conf.js" ,
"//tools:jasmine-seed-generator.js" ,
],
2020-02-23 17:35:46 -08:00
karma = "//tools/saucelabs:karma-saucelabs" ,
tags = tags + [
"exclusive" ,
"manual" ,
"no-remote-exec" ,
"saucelabs" ,
],
2018-12-17 22:09:39 -08:00
configuration_env_vars = [ "KARMA_WEB_TEST_MODE" ],
** kwargs
)
2019-11-01 11:17:10 -07:00
def protractor_web_test_suite ( ** kwargs ):
"""Default values for protractor_web_test_suite"""
_protractor_web_test_suite (
browsers = [ "//tools/browsers:chromium" ],
2018-12-17 22:09:39 -08:00
** kwargs
)
2019-10-24 17:40:17 -07:00
def ng_benchmark ( ** kwargs ):
"""Default values for ng_benchmark"""
_ng_benchmark ( ** kwargs )
2018-10-22 18:02:42 -07:00
def nodejs_binary ( data = [], ** kwargs ):
2018-10-04 13:14:14 -07:00
"""Default values for nodejs_binary"""
_nodejs_binary (
2019-11-22 12:11:48 -08:00
configuration_env_vars = [ "angular_ivy_enabled" ],
2019-02-20 09:54:42 -08:00
data = data + [ "@npm//source-map-support" ],
2018-10-04 13:14:14 -07:00
** kwargs
)
2018-09-11 18:11:32 -07:00
2020-01-07 23:56:49 -08:00
def jasmine_node_test ( bootstrap = [], ** kwargs ):
2020-01-09 12:36:43 -08:00
"""Default values for jasmine_node_test
Args:
bootstrap: A list of labels of scripts to run before the entry_point.
The labels can either be individual files or a filegroup that contain a single
file.
The label is automatically added to the deps of jasmine_node_test.
If the label ends in `_es5` which by convention selects the es5 outputs
of a ts_library rule, then corresponding ts_library target sans `_es5`
is also added to the deps of jasmine_node_test.
For example with,
jasmine_node_test(
name = "test",
bootstrap = ["//tools/testing:node_es5"],
deps = [":test_lib"],
)
the `//tools/testing:node` target will automatically get added to deps
by this macro. This removes the need for duplicate deps on the
target and makes the usage of this rule less verbose."""
2020-01-07 23:56:49 -08:00
# Very common dependencies for tests
deps = kwargs . pop ( "deps" , []) + [
2019-02-20 09:54:42 -08:00
"@npm//chokidar" ,
"@npm//domino" ,
2019-03-21 12:11:29 -04:00
"@npm//jasmine-core" ,
2019-02-20 09:54:42 -08:00
"@npm//reflect-metadata" ,
"@npm//source-map-support" ,
"@npm//tslib" ,
"@npm//xhr2" ,
2018-10-04 13:14:14 -07:00
]
2020-01-07 23:56:49 -08:00
configuration_env_vars = kwargs . pop ( "configuration_env_vars" , []) + [
"angular_ivy_enabled" ,
]
2020-01-12 14:14:01 -08:00
templated_args = kwargs . pop ( "templated_args" , [])
2020-01-07 23:56:49 -08:00
for label in bootstrap :
deps += [ label ]
templated_args += [ "--node_options=--require=$(rlocation $(location %s ))" % label ]
2020-01-08 11:09:36 -08:00
if label . endswith ( "_es5" ):
2020-01-09 12:36:43 -08:00
# If this label is a filegroup derived from a ts_library then automatically
2020-01-08 11:09:36 -08:00
# add the ts_library target (which is the label sans `_es5`) to deps so we pull
2020-01-09 12:36:43 -08:00
# in all of its transitive deps. This removes the need for duplicate deps on the
# target and makes the usage of this rule less verbose.
2020-01-08 11:09:36 -08:00
deps += [ label [: - 4 ]]
2020-01-07 23:56:49 -08:00
2018-10-04 13:14:14 -07:00
_jasmine_node_test (
deps = deps ,
2020-01-07 23:56:49 -08:00
configuration_env_vars = configuration_env_vars ,
templated_args = templated_args ,
2018-10-04 13:14:14 -07:00
** kwargs
)
2018-09-11 18:11:32 -07:00
2018-10-04 13:14:14 -07:00
def ng_rollup_bundle ( deps = [], ** kwargs ):
"""Default values for ng_rollup_bundle"""
deps = deps + [
2019-02-20 09:54:42 -08:00
"@npm//tslib" ,
"@npm//reflect-metadata" ,
2018-10-04 13:14:14 -07:00
]
_ng_rollup_bundle (
deps = deps ,
** kwargs
)
2019-08-15 08:35:33 -07:00
2019-12-22 10:27:41 +09:00
def rollup_bundle ( name , testonly = False , sourcemap = "true" , ** kwargs ):
2019-10-27 16:28:03 -07:00
"""A drop in replacement for the rules nodejs [legacy rollup_bundle].
Runs [rollup_bundle], [terser_minified] and [babel] for downleveling to es5
to produce a number of output bundles.
es2015 iife : "% {name} .es2015.js"
es2015 iife minified : "% {name} .min.es2015.js"
es2015 iife minified (debug) : "% {name} .min_debug.es2015.js"
es5 iife : "% {name} .js"
es5 iife minified : "% {name} .min.js"
es5 iife minified (debug) : "% {name} .min_debug.js"
es5 umd : "% {name} .es5umd.js"
es5 umd minified : "% {name} .min.es5umd.js"
es2015 umd : "% {name} .umd.js"
es2015 umd minified : "% {name} .min.umd.js"
".js.map" files are also produced for each bundle.
[legacy rollup_bundle]: https://github.com/bazelbuild/rules_nodejs/blob/0.38.3/internal/rollup/rollup_bundle.bzl
[rollup_bundle]: https://bazelbuild.github.io/rules_nodejs/Rollup.html
[terser_minified]: https://bazelbuild.github.io/rules_nodejs/Terser.html
[babel]: https://babeljs.io/
"""
# Common arguments for all terser_minified targets
common_terser_args = {
"args" : [ "--comments" ],
"sourcemap" : False ,
}
# es2015
2019-12-22 10:27:41 +09:00
_rollup_bundle ( name = name + ".es2015" , testonly = testonly , format = "iife" , sourcemap = sourcemap , ** kwargs )
2019-10-27 16:28:03 -07:00
terser_minified ( name = name + ".min.es2015" , testonly = testonly , src = name + ".es2015" , ** common_terser_args )
native . filegroup ( name = name + ".min.es2015.js" , testonly = testonly , srcs = [ name + ".min.es2015" ])
terser_minified ( name = name + ".min_debug.es2015" , testonly = testonly , src = name + ".es2015" , ** common_terser_args )
native . filegroup ( name = name + ".min_debug.es2015.js" , testonly = testonly , srcs = [ name + ".min_debug.es2015" ])
# es5
tsc (
name = name ,
testonly = testonly ,
outs = [
name + ".js" ,
],
args = [
2020-01-08 02:08:00 -08:00
"$(execpath : %s .es2015.js)" % name ,
2019-10-27 16:28:03 -07:00
"--types" ,
"--skipLibCheck" ,
"--target" ,
"es5" ,
"--lib" ,
"es2015,dom" ,
"--allowJS" ,
"--outFile" ,
2020-01-08 02:08:00 -08:00
"$(execpath : %s .js)" % name ,
2019-10-27 16:28:03 -07:00
],
data = [
name + ".es2015.js" ,
],
2019-08-15 08:35:33 -07:00
)
2019-10-27 16:28:03 -07:00
terser_minified ( name = name + ".min" , testonly = testonly , src = name + "" , ** common_terser_args )
native . filegroup ( name = name + ".min.js" , testonly = testonly , srcs = [ name + ".min" ])
terser_minified ( name = name + ".min_debug" , testonly = testonly , src = name + "" , debug = True , ** common_terser_args )
native . filegroup ( name = name + ".min_debug.js" , testonly = testonly , srcs = [ name + ".min_debug" ])
# umd
2019-12-22 10:27:41 +09:00
_rollup_bundle ( name = name + ".umd" , testonly = testonly , format = "umd" , sourcemap = sourcemap , ** kwargs )
2019-10-27 16:28:03 -07:00
terser_minified ( name = name + ".min.umd" , testonly = testonly , src = name + ".umd" , ** common_terser_args )
native . filegroup ( name = name + ".min.umd.js" , testonly = testonly , srcs = [ name + ".min.umd" ])
tsc (
name = name + ".es5umd" ,
testonly = testonly ,
outs = [
name + ".es5umd.js" ,
],
args = [
2020-01-08 02:08:00 -08:00
"$(execpath : %s .umd.js)" % name ,
2019-10-27 16:28:03 -07:00
"--types" ,
"--skipLibCheck" ,
"--target" ,
"es5" ,
"--lib" ,
"es2015,dom" ,
"--allowJS" ,
"--outFile" ,
2020-01-08 02:08:00 -08:00
"$(execpath : %s .es5umd.js)" % name ,
2019-10-27 16:28:03 -07:00
],
data = [
name + ".umd.js" ,
],
)
terser_minified ( name = name + ".min.es5umd" , testonly = testonly , src = name + ".es5umd" , ** common_terser_args )
native . filegroup ( name = name + ".min.es5umd.js" , testonly = testonly , srcs = [ name + ".min.es5umd" ])