Skip to content

Commit

Permalink
feat(typescript): allow alternative transpilers (#3134)
Browse files Browse the repository at this point in the history
* feat(typescript): allow alternative transpilers

Fixes #3133

* fixup! feat(typescript): allow alternative transpilers
  • Loading branch information
alexeagle authored Jan 4, 2022
1 parent b2c2dc4 commit dc0dc8f
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 4 deletions.
28 changes: 27 additions & 1 deletion docs/TypeScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ Defaults to `False`
<pre>
ts_project(<a href="#ts_project-name">name</a>, <a href="#ts_project-tsconfig">tsconfig</a>, <a href="#ts_project-srcs">srcs</a>, <a href="#ts_project-args">args</a>, <a href="#ts_project-deps">deps</a>, <a href="#ts_project-extends">extends</a>, <a href="#ts_project-allow_js">allow_js</a>, <a href="#ts_project-declaration">declaration</a>, <a href="#ts_project-source_map">source_map</a>,
<a href="#ts_project-declaration_map">declaration_map</a>, <a href="#ts_project-resolve_json_module">resolve_json_module</a>, <a href="#ts_project-preserve_jsx">preserve_jsx</a>, <a href="#ts_project-composite">composite</a>, <a href="#ts_project-incremental">incremental</a>,
<a href="#ts_project-emit_declaration_only">emit_declaration_only</a>, <a href="#ts_project-ts_build_info_file">ts_build_info_file</a>, <a href="#ts_project-tsc">tsc</a>, <a href="#ts_project-typescript_package">typescript_package</a>,
<a href="#ts_project-emit_declaration_only">emit_declaration_only</a>, <a href="#ts_project-transpiler">transpiler</a>, <a href="#ts_project-ts_build_info_file">ts_build_info_file</a>, <a href="#ts_project-tsc">tsc</a>, <a href="#ts_project-typescript_package">typescript_package</a>,
<a href="#ts_project-typescript_require_path">typescript_require_path</a>, <a href="#ts_project-validate">validate</a>, <a href="#ts_project-supports_workers">supports_workers</a>, <a href="#ts_project-declaration_dir">declaration_dir</a>, <a href="#ts_project-out_dir">out_dir</a>, <a href="#ts_project-root_dir">root_dir</a>,
<a href="#ts_project-link_workspace_root">link_workspace_root</a>, <a href="#ts_project-kwargs">kwargs</a>)
</pre>
Expand Down Expand Up @@ -733,6 +733,32 @@ Instructs Bazel *not* to expect `.js` or `.js.map` outputs for `.ts` sources.
Defaults to `False`
<h4 id="ts_project-transpiler">transpiler</h4>
What tool to run that produces the JavaScript outputs.
By default, this is the string `tsc`. With that value, `ts_project` expects `.js` outputs
to be written in the same action that does the type-checking to produce `.d.ts` outputs.
This is the simplest configuration, however `tsc` is slower than alternatives.
It also means developers must wait for the type-checking in the developer loop.
In theory, Persistent Workers (via the `supports_workers` attribute) remedies the
slow compilation time, however it adds additional complexity because the worker process
can only see one set of dependencies, and so it cannot be shared between different
`ts_project` rules. That attribute is documented as experimental, and may never graduate
to a better support contract.
Instead of the string `tsc`, this attribute also accepts a rule or macro with this signature:
`name, srcs, js_outs, map_outs, **kwargs`
where the `**kwargs` attribute propagates the tags, visibility, and testonly attributes from `ts_project`.
If you need to pass additional attributes to the transpiler rule, you can use a
[partial](https://github.com/bazelbuild/bazel-skylib/blob/main/lib/partial.bzl)
to bind those arguments at the "make site", then pass that partial to this attribute where it
will be called with the remaining arguments.
See the packages/typescript/test/ts_project/swc directory for an example.
Defaults to `"tsc"`
<h4 id="ts_project-ts_build_info_file">ts_build_info_file</h4>
the user-specified value of `tsBuildInfoFile` from the tsconfig.
Expand Down
65 changes: 62 additions & 3 deletions packages/typescript/internal/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo", "ExternalNpmPackageInfo", "declaration_info", "js_module_info", "run_node")
load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module_mappings_aspect")
load("@build_bazel_rules_nodejs//internal/node:node.bzl", "nodejs_binary")
load("@build_bazel_rules_nodejs//third_party/github.com/bazelbuild/bazel-skylib:lib/partial.bzl", "partial")
load(":ts_config.bzl", "TsConfigInfo", "write_tsconfig")

_ValidOptionsInfo = provider()
Expand Down Expand Up @@ -179,6 +180,10 @@ def _ts_project_impl(ctx):
else:
json_outs = []

# If there are no js_outs, that implies we are producing declarations only.
# We must avoid tsc writing any JS files in this case, as it was only run for typings.
arguments.add("--emitDeclarationOnly")

outputs = json_outs + ctx.outputs.js_outs + ctx.outputs.map_outs + ctx.outputs.typings_outs + ctx.outputs.typing_maps_outs
if ctx.outputs.buildinfo_out:
arguments.add_all([
Expand Down Expand Up @@ -367,6 +372,7 @@ def ts_project_macro(
composite = False,
incremental = False,
emit_declaration_only = False,
transpiler = "tsc",
ts_build_info_file = None,
tsc = None,
typescript_package = _DEFAULT_TYPESCRIPT_PACKAGE,
Expand Down Expand Up @@ -540,6 +546,28 @@ def ts_project_macro(
args: List of strings of additional command-line arguments to pass to tsc.
transpiler: What tool to run that produces the JavaScript outputs.
By default, this is the string `tsc`. With that value, `ts_project` expects `.js` outputs
to be written in the same action that does the type-checking to produce `.d.ts` outputs.
This is the simplest configuration, however `tsc` is slower than alternatives.
It also means developers must wait for the type-checking in the developer loop.
In theory, Persistent Workers (via the `supports_workers` attribute) remedies the
slow compilation time, however it adds additional complexity because the worker process
can only see one set of dependencies, and so it cannot be shared between different
`ts_project` rules. That attribute is documented as experimental, and may never graduate
to a better support contract.
Instead of the string `tsc`, this attribute also accepts a rule or macro with this signature:
`name, srcs, js_outs, map_outs, **kwargs`
where the `**kwargs` attribute propagates the tags, visibility, and testonly attributes from `ts_project`.
If you need to pass additional attributes to the transpiler rule, you can use a
[partial](https://github.com/bazelbuild/bazel-skylib/blob/main/lib/partial.bzl)
to bind those arguments at the "make site", then pass that partial to this attribute where it
will be called with the remaining arguments.
See the packages/typescript/test/ts_project/swc directory for an example.
tsc: Label of the TypeScript compiler binary to run.
For example, `tsc = "@my_deps//typescript/bin:tsc"`
Expand Down Expand Up @@ -756,7 +784,38 @@ def ts_project_macro(
if declaration_map:
typing_maps_outs.extend(_out_paths(srcs, typings_out_dir, root_dir, allow_js, {"*": ".d.ts.map"}))

if not len(js_outs) and not len(typings_outs):
tsc_js_outs = []
tsc_map_outs = []
if transpiler == "tsc":
tsc_js_outs = js_outs
tsc_map_outs = map_outs
else:
transpiler_kwargs = {
"tags": kwargs.get("tags", []),
"visibility": kwargs.get("visibility", None),
"testonly": kwargs.get("testonly", None),
}
if type(transpiler) == "function" or type(transpiler) == "rule":
transpiler(
name = name + "_transpile",
srcs = srcs,
js_outs = js_outs,
map_outs = map_outs,
**transpiler_kwargs
)
elif partial.is_instance(transpiler):
partial.call(
transpiler,
name = name + "_transpile",
srcs = srcs,
js_outs = js_outs,
map_outs = map_outs,
**transpiler_kwargs
)
else:
fail("transpiler attribute should be a rule/macro, a skylib partial, or the string 'tsc'. Got " + type(transpiler))

if not len(tsc_js_outs) and not len(typings_outs):
fail("""ts_project target "//{}:{}" is configured to produce no outputs.
Note that ts_project must know the srcs in advance in order to predeclare the outputs.
Expand All @@ -773,8 +832,8 @@ Check the srcs attribute to see that some .ts files are present (or .js files wi
declaration_dir = declaration_dir,
out_dir = out_dir,
root_dir = root_dir,
js_outs = js_outs,
map_outs = map_outs,
js_outs = tsc_js_outs,
map_outs = tsc_map_outs,
typings_outs = typings_outs,
typing_maps_outs = typing_maps_outs,
buildinfo_out = tsbuildinfo_path if composite or incremental else None,
Expand Down
40 changes: 40 additions & 0 deletions packages/typescript/test/ts_project/swc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//packages/typescript:index.bzl", "ts_project")
load(":swc.bzl", "swc", "swc_macro")

write_file(
name = "gen_ts",
out = "big.ts",
content = [
"export const a{0}: number = {0}".format(x)
for x in range(1000)
],
)

ts_project(
name = "transpile_with_swc",
srcs = ["big.ts"],
transpiler = swc_macro,
tsconfig = {
"compilerOptions": {
"declaration": True,
"sourceMap": True,
},
},
)

ts_project(
name = "transpile_with_configurable_swc",
srcs = ["big.ts"],
out_dir = "configurable",
transpiler = swc(
args = ["--env-name=test"],
swcrc = "//:.swcrc",
),
tsconfig = {
"compilerOptions": {
"declaration": True,
"sourceMap": True,
},
},
)
32 changes: 32 additions & 0 deletions packages/typescript/test/ts_project/swc/swc.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"Fixture to demonstrate a custom transpiler for ts_project"

load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@bazel_skylib//lib:partial.bzl", "partial")

_DUMMY_SOURCEMAP = """{"version":3,"sources":["%s"],"mappings":"AAAO,KAAK,CAAC","file":"in.js","sourcesContent":["fake"]}"""

def swc_macro(name, srcs, js_outs, map_outs, **kwargs):
"""Mock swc transpiler macro.
In real usage you would wrap a rule like
https://github.com/aspect-build/rules_swc/blob/main/docs/swc.md
"""

for i, s in enumerate(srcs):
copy_file(
name = "_{}_{}_js".format(name, s),
src = s,
out = js_outs[i],
)

write_file(
name = "_{}_{}_map".format(name, s),
out = map_outs[i],
content = [_DUMMY_SOURCEMAP % s],
)

# In Bazel 5, we could use a lambda to build a higher-order function
# but for Bazel 4 and below, we need partials.
def swc(args = [], swcrc = None):
return partial.make(swc_macro, args = args, swcrc = swcrc)
176 changes: 176 additions & 0 deletions third_party/github.com/bazelbuild/bazel-skylib/lib/partial.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Copyright 2018 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.

"""Starlark module for working with partial function objects.
Partial function objects allow some parameters are bound before the call.
Similar to https://docs.python.org/3/library/functools.html#functools.partial.
"""

# create instance singletons to avoid unnecessary allocations
_a_dict_type = type({})
_a_tuple_type = type(())
_a_struct_type = type(struct())

def _call(partial, *args, **kwargs):
"""Calls a partial created using `make`.
Args:
partial: The partial to be called.
*args: Additional positional arguments to be appended to the ones given to
make.
**kwargs: Additional keyword arguments to augment and override the ones
given to make.
Returns:
Whatever the function in the partial returns.
"""
function_args = partial.args + args
function_kwargs = dict(partial.kwargs)
function_kwargs.update(kwargs)
return partial.function(*function_args, **function_kwargs)

def _make(func, *args, **kwargs):
"""Creates a partial that can be called using `call`.
A partial can have args assigned to it at the make site, and can have args
passed to it at the call sites.
A partial 'function' can be defined with positional args and kwargs:
# function with no args
```
def function1():
...
```
# function with 2 args
```
def function2(arg1, arg2):
...
```
# function with 2 args and keyword args
```
def function3(arg1, arg2, x, y):
...
```
The positional args passed to the function are the args passed into make
followed by any additional positional args given to call. The below example
illustrates a function with two positional arguments where one is supplied by
make and the other by call:
# function demonstrating 1 arg at make site, and 1 arg at call site
```
def _foo(make_arg1, func_arg1):
print(make_arg1 + " " + func_arg1 + "!")
```
For example:
```
hi_func = partial.make(_foo, "Hello")
bye_func = partial.make(_foo, "Goodbye")
partial.call(hi_func, "Jennifer")
partial.call(hi_func, "Dave")
partial.call(bye_func, "Jennifer")
partial.call(bye_func, "Dave")
```
prints:
```
"Hello, Jennifer!"
"Hello, Dave!"
"Goodbye, Jennifer!"
"Goodbye, Dave!"
```
The keyword args given to the function are the kwargs passed into make
unioned with the keyword args given to call. In case of a conflict, the
keyword args given to call take precedence. This allows you to set a default
value for keyword arguments and override it at the call site.
Example with a make site arg, a call site arg, a make site kwarg and a
call site kwarg:
```
def _foo(make_arg1, call_arg1, make_location, call_location):
print(make_arg1 + " is from " + make_location + " and " +
call_arg1 + " is from " + call_location + "!")
func = partial.make(_foo, "Ben", make_location="Hollywood")
partial.call(func, "Jennifer", call_location="Denver")
```
Prints "Ben is from Hollywood and Jennifer is from Denver!".
```
partial.call(func, "Jennifer", make_location="LA", call_location="Denver")
```
Prints "Ben is from LA and Jennifer is from Denver!".
Note that keyword args may not overlap with positional args, regardless of
whether they are given during the make or call step. For instance, you can't
do:
```
def foo(x):
pass
func = partial.make(foo, 1)
partial.call(func, x=2)
```
Args:
func: The function to be called.
*args: Positional arguments to be passed to function.
**kwargs: Keyword arguments to be passed to function. Note that these can
be overridden at the call sites.
Returns:
A new `partial` that can be called using `call`
"""
return struct(function = func, args = args, kwargs = kwargs)

def _is_instance(v):
"""Returns True if v is a partial created using `make`.
Args:
v: The value to check.
Returns:
True if v was created by `make`, False otherwise.
"""

# Note that in bazel 3.7.0 and earlier, type(v.function) is the same
# as the type of a function even if v.function is a rule. But we
# cannot rely on this in later bazels due to breaking change
# https://github.com/bazelbuild/bazel/commit/e379ece1908aafc852f9227175dd3283312b4b82
#
# Since this check is heuristic anyway, we simply check for the
# presence of a "function" attribute without checking its type.
return type(v) == _a_struct_type and \
hasattr(v, "function") and \
hasattr(v, "args") and type(v.args) == _a_tuple_type and \
hasattr(v, "kwargs") and type(v.kwargs) == _a_dict_type

partial = struct(
make = _make,
call = _call,
is_instance = _is_instance,
)

0 comments on commit dc0dc8f

Please sign in to comment.