Skip to content

Commit

Permalink
feat(examples): show how to use ts_library(use_angular_plugin) with w…
Browse files Browse the repository at this point in the history
…orker mode
  • Loading branch information
Alex Eagle committed Apr 20, 2020
1 parent 0eda43f commit b5af99d
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 35 deletions.
6 changes: 2 additions & 4 deletions examples/angular/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ load("@npm//html-insert-assets:index.bzl", "html_insert_assets")
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")
load("//tools:angular_ts_library.bzl", "ng_ts_library")

package(default_visibility = ["//:__subpackages__"])

Expand Down Expand Up @@ -43,18 +44,15 @@ ts_library(
],
)

ts_library(
ng_ts_library(
name = "src",
srcs = [
"main.dev.ts",
"main.prod.ts",
],
tsconfig = ":tsconfig.json",
use_angular_plugin = True,
deps = [
"//src/app",
# Needed for the angular compiler plugin
"@npm//@angular/compiler-cli",
"@npm//@angular/core",
"@npm//@angular/platform-browser",
"@npm//@angular/router",
Expand Down
12 changes: 3 additions & 9 deletions examples/angular/src/app/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("//tools:angular_ts_library.bzl", "ng_ts_library")

package(default_visibility = ["//:__subpackages__"])

ts_library(
ng_ts_library(
name = "app",
srcs = glob(
include = ["*.ts"],
exclude = ["app.server.module.ts"],
),
angular_assets = ["app.component.html"],
tsconfig = "//src:tsconfig.json",
use_angular_plugin = True,
deps = [
"//src/app/hello-world",
"//src/app/home",
"//src/app/todos",
"//src/app/todos/reducers",
"//src/shared/material",
# Needed for the angular compiler plugin
"@npm//@angular/compiler-cli",
"@npm//@angular/core",
"@npm//@angular/platform-browser",
"@npm//@angular/router",
"@npm//@ngrx/store",
],
)

ts_library(
ng_ts_library(
name = "app_server",
srcs = ["app.server.module.ts"],
tsconfig = "//src:tsconfig-server",
use_angular_plugin = True,
deps = [
":app",
# Needed for the angular compiler plugin
"@npm//@angular/compiler-cli",
"@npm//@angular/core",
"@npm//@angular/platform-server",
],
Expand Down
6 changes: 2 additions & 4 deletions examples/angular/src/app/hello-world/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@io_bazel_rules_sass//:defs.bzl", "sass_binary")
load("@npm_bazel_karma//:index.bzl", "karma_web_test_suite")
load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("//tools:angular_ts_library.bzl", "ng_ts_library")

package(default_visibility = ["//:__subpackages__"])

Expand All @@ -9,7 +10,7 @@ sass_binary(
src = "hello-world.component.scss",
)

ts_library(
ng_ts_library(
name = "hello-world",
srcs = [
"hello-world.component.ts",
Expand All @@ -20,12 +21,9 @@ ts_library(
":hello-world-styles",
],
tsconfig = "//src:tsconfig.json",
use_angular_plugin = True,
deps = [
"//src/lib/shorten",
"//src/shared/material",
# Needed for the angular compiler plugin
"@npm//@angular/compiler-cli",
"@npm//@angular/core",
"@npm//@angular/forms",
"@npm//@angular/router",
Expand Down
7 changes: 2 additions & 5 deletions examples/angular/src/app/home/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("//tools:angular_ts_library.bzl", "ng_ts_library")

package(default_visibility = ["//:__subpackages__"])

ts_library(
ng_ts_library(
name = "home",
srcs = ["home.ts"],
angular_assets = ["home.html"],
tsconfig = "//src:tsconfig.json",
use_angular_plugin = True,
deps = [
# Needed for the angular compiler plugin
"@npm//@angular/compiler-cli",
"@npm//@angular/core",
"@npm//@angular/router",
],
Expand Down
7 changes: 2 additions & 5 deletions examples/angular/src/app/todos/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@io_bazel_rules_sass//:defs.bzl", "sass_binary")
load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("//tools:angular_ts_library.bzl", "ng_ts_library")

package(default_visibility = ["//:__subpackages__"])

Expand All @@ -8,7 +8,7 @@ sass_binary(
src = "todos.component.scss",
)

ts_library(
ng_ts_library(
name = "todos",
srcs = [
"todos.component.ts",
Expand All @@ -19,12 +19,9 @@ ts_library(
":todos-styles",
],
tsconfig = "//src:tsconfig.json",
use_angular_plugin = True,
deps = [
"//src/app/todos/reducers",
"//src/shared/material",
# Needed for the angular compiler plugin
"@npm//@angular/compiler-cli",
"@npm//@angular/common",
"@npm//@angular/core",
"@npm//@angular/forms",
Expand Down
7 changes: 2 additions & 5 deletions examples/angular/src/shared/material/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("//tools:angular_ts_library.bzl", "ng_ts_library")

package(default_visibility = ["//:__subpackages__"])

ts_library(
ng_ts_library(
name = "material",
srcs = glob(["*.ts"]),
tsconfig = "//src:tsconfig.json",
use_angular_plugin = True,
deps = [
# Needed for the angular compiler plugin
"@npm//@angular/compiler-cli",
"@npm//@angular/core",
"@npm//@angular/material",
],
Expand Down
15 changes: 15 additions & 0 deletions examples/angular/tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")

# Custom ts_library compiler that runs tsc_wrapped with angular/compiler-cli statically linked
# This can be used with worker mode because we don't need the linker at runtime to make
# the angular plugin loadable
# Just a clone of @npm//@bazel/typescript/bin:tsc_wrapped with added deps
nodejs_binary(
name = "tsc_wrapped_with_angular",
data = [
"@npm//@angular/compiler-cli",
"@npm//@bazel/typescript",
],
entry_point = "@npm//:node_modules/@bazel/typescript/internal/tsc_wrapped/tsc_wrapped.js",
visibility = ["//:__subpackages__"],
)
11 changes: 11 additions & 0 deletions examples/angular/tools/angular_ts_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"Shows how to enable both worker mode and use_angular_plugin to make a drop-in replacement for ng_module"

load("@npm_bazel_typescript//:index.bzl", "ts_library")

def ng_ts_library(**kwargs):
ts_library(
compiler = "//tools:tsc_wrapped_with_angular",
supports_workers = True,
use_angular_plugin = True,
**kwargs
)
14 changes: 11 additions & 3 deletions packages/typescript/src/internal/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ def _compile_action(ctx, inputs, outputs, tsconfig_file, node_opts, description
# because it causes bazel to spawn a new worker for every action
# See https://github.com/bazelbuild/rules_nodejs/issues/1803
# TODO: understand the interaction between linker and workers better
# When using plugins, we need the linker, so we disable workers for that case as well
if ctx.attr.supports_workers and not ctx.attr.use_angular_plugin:
if ctx.attr.supports_workers:
# One at-sign makes this a params-file, enabling the worker strategy.
# Two at-signs escapes the argument so it's passed through to tsc_wrapped
# rather than the contents getting expanded.
Expand Down Expand Up @@ -523,4 +522,13 @@ def ts_library_macro(tsconfig = None, **kwargs):
if not tsconfig:
tsconfig = "//:tsconfig.json"

ts_library(tsconfig = tsconfig, **kwargs)
# plugins generally require the linker
# (unless the user statically linked them into the compiler binary)
# Therefore we disable workers for them by default
if "supports_workers" in kwargs.keys():
supports_workers = kwargs.pop("supports_workers")
else:
uses_plugin = kwargs.get("use_angular_plugin", False)
supports_workers = not uses_plugin

ts_library(tsconfig = tsconfig, supports_workers = supports_workers, **kwargs)

0 comments on commit b5af99d

Please sign in to comment.