Skip to content

Commit

Permalink
Allow multiple tsconfig inputs to a ts_library
Browse files Browse the repository at this point in the history
Fixes bazel-contrib#41

Closes bazel-contrib#42

PiperOrigin-RevId: 169476478
  • Loading branch information
alexeagle committed Sep 21, 2017
1 parent 1b3239a commit 2537646
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
Users should not load files under "/internal"
"""
load("//internal:build_defs.bzl", "ts_library")
load("//internal:ts_config.bzl", "ts_config")
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

load("//:defs.bzl", "ts_config", "ts_library")

# Because our tsconfig.json has an extends property, we must also tell the
# ts_library to include the extended tsconfig file in compilations.
# The ts_library rule will generate its own tsconfig which extends from the
# src file.
ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = ["tsconfig-base.json"],
)

ts_library(
name = "tsconfig_extends",
srcs = [
"uses_promise.ts",
],
tsconfig = ":tsconfig",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"lib": ["es2015.promise", "es5"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig-base"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function hiLater(): Promise<string> {
return Promise.resolve('hi');
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
load(":common/compilation.bzl", "COMMON_ATTRIBUTES", "compile_ts", "ts_providers_dict_to_struct")
load(":executables.bzl", "get_tsc")
load(":common/tsconfig.bzl", "create_tsconfig")
load(":ts_config.bzl", "TsConfig")

def _compile_action(ctx, inputs, outputs, config_file_path):
externs_files = []
Expand All @@ -39,6 +40,8 @@ def _compile_action(ctx, inputs, outputs, config_file_path):
if f.path.endswith(".ts") or f.path.endswith(".json")]
if ctx.file.tsconfig:
action_inputs += [ctx.file.tsconfig]
if TsConfig in ctx.attr.tsconfig:
action_inputs += ctx.attr.tsconfig[TsConfig].deps

# 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
Expand Down Expand Up @@ -103,7 +106,7 @@ def tsc_wrapped_tsconfig(ctx,
# this gives extra error-checking.
if ctx.file.tsconfig:
workspace_path = config["compilerOptions"]["rootDir"]
config["extends"] = "/".join([workspace_path, ctx.file.tsconfig.path[:-5]])
config["extends"] = "/".join([workspace_path, ctx.file.tsconfig.path[:-len(".json")]])

return config

Expand Down Expand Up @@ -141,14 +144,14 @@ ts_library = rule(
# be portable across internal/external, so we need this attribute
# internally as well.
"tsconfig":
attr.label(allow_files = True, single_file=True),
attr.label(allow_files = True, single_file = True),
"compiler":
attr.label(
default=get_tsc(),
single_file=False,
allow_files=True,
executable=True,
cfg="host",),
cfg="host"),
"supports_workers": attr.bool(default = True),
# @// is special syntax for the "main" repository
# The default assumes the user specified a target "node_modules" in their
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""The ts_config rule allows users to express tsconfig.json file groups.
"""

TsConfig = provider()

def _ts_config_impl(ctx):
files = depset()
files += [ctx.file.src]
return [DefaultInfo(files = files), TsConfig(deps = ctx.files.deps)]

ts_config = rule(
implementation = _ts_config_impl,
attrs = {
"src": attr.label(allow_files = True, single_file = True, mandatory = True),
"deps": attr.label_list(allow_files = True, mandatory = True),
},
)

0 comments on commit 2537646

Please sign in to comment.